@@ -54,9 +54,15 @@ public class ConfigurationParser {
54
54
*/
55
55
public static final String OPTIONALLY_PREFIX = "optionally" ;
56
56
57
- private ConfigurationHandler handler ;
57
+ protected static final String FROM_SEPARATOR = " from " ;
58
58
59
- private Properties systemProperties ;
59
+ protected static final String USING_SEPARATOR = " using " ;
60
+
61
+ protected static final String DEFAULT_SEPARATOR = " default " ;
62
+
63
+ private final ConfigurationHandler handler ;
64
+
65
+ private final Properties systemProperties ;
60
66
61
67
public ConfigurationParser (ConfigurationHandler handler , Properties systemProperties ) {
62
68
this .handler = handler ;
@@ -74,188 +80,219 @@ public ConfigurationParser(ConfigurationHandler handler, Properties systemProper
74
80
*/
75
81
public void parse (InputStream is )
76
82
throws IOException , ConfigurationException , DuplicateRealmException , NoSuchRealmException {
77
- BufferedReader reader = new BufferedReader (new InputStreamReader (is , StandardCharsets .UTF_8 ));
83
+ try (
84
+ BufferedReader reader = new BufferedReader (
85
+ new InputStreamReader (is , StandardCharsets .UTF_8 )
86
+ )
87
+ ) {
78
88
79
- String line ;
89
+ String line ;
80
90
81
- int lineNo = 0 ;
91
+ int lineNo = 0 ;
82
92
83
- boolean mainSet = false ;
93
+ boolean mainSet = false ;
84
94
85
- String curRealm = null ;
95
+ String curRealm = null ;
86
96
87
- while (true ) {
88
- line = reader .readLine ();
89
-
90
- if (line == null ) {
91
- break ;
92
- }
93
-
94
- ++lineNo ;
95
- line = line .trim ();
96
-
97
- if (canIgnore (line )) {
98
- continue ;
99
- }
97
+ while (true ) {
98
+ line = reader .readLine ();
100
99
101
- if (line .startsWith (MAIN_PREFIX )) {
102
- if (mainSet ) {
103
- throw new ConfigurationException ("Duplicate main configuration" , lineNo , line );
100
+ if (line == null ) {
101
+ break ;
104
102
}
105
103
106
- String conf = line .substring (MAIN_PREFIX .length ()).trim ();
104
+ ++lineNo ;
105
+ line = line .trim ();
107
106
108
- int fromLoc = conf .indexOf ("from" );
109
-
110
- if (fromLoc < 0 ) {
111
- throw new ConfigurationException ("Missing from clause" , lineNo , line );
107
+ if (canIgnore (line )) {
108
+ continue ;
112
109
}
113
110
114
- String mainClassName = filter (conf .substring (0 , fromLoc ).trim ());
111
+ char lineFirstChar = line .charAt (0 );
112
+ switch (lineFirstChar ) {
113
+ case 'm' : {
114
+ if (line .startsWith (MAIN_PREFIX )) {
115
+ if (mainSet ) {
116
+ throw new ConfigurationException ("Duplicate main configuration" , lineNo , line );
117
+ }
115
118
116
- String mainRealmName = filter ( conf . substring ( fromLoc + 4 ). trim ());
119
+ int fromLoc = line . indexOf ( FROM_SEPARATOR , MAIN_PREFIX . length ());
117
120
118
- this .handler .setAppMain (mainClassName , mainRealmName );
121
+ if (fromLoc < 0 ) {
122
+ throw new ConfigurationException ("Missing from clause" , lineNo , line );
123
+ }
119
124
120
- mainSet = true ;
121
- } else if (line .startsWith (SET_PREFIX )) {
122
- String conf = line .substring (SET_PREFIX .length ()).trim ();
125
+ String mainClassName = filter (line .substring (MAIN_PREFIX .length (), fromLoc ).trim ());
123
126
124
- int usingLoc = conf . indexOf ( " using" ) + 1 ;
127
+ String mainRealmName = filter ( line . substring ( fromLoc + FROM_SEPARATOR . length ()). trim ()) ;
125
128
126
- String property = null ;
129
+ this . handler . setAppMain ( mainClassName , mainRealmName ) ;
127
130
128
- String propertiesFileName = null ;
131
+ mainSet = true ;
129
132
130
- if (usingLoc > 0 ) {
131
- property = conf .substring (0 , usingLoc ).trim ();
133
+ break ;
134
+ }
135
+ throw new ConfigurationException ("Unhandled configuration" , lineNo , line );
136
+ }
137
+ case 's' : {
138
+ if (line .startsWith (SET_PREFIX )) {
139
+ String conf = line .substring (SET_PREFIX .length ()).trim ();
132
140
133
- propertiesFileName = filter ( conf . substring ( usingLoc + 5 ). trim () );
141
+ int usingLoc = conf . indexOf ( USING_SEPARATOR );
134
142
135
- conf = propertiesFileName ;
136
- }
143
+ String property = null ;
137
144
138
- String defaultValue = null ;
145
+ String propertiesFileName = null ;
139
146
140
- int defaultLoc = conf .indexOf (" default" ) + 1 ;
147
+ if (usingLoc >= 0 ) {
148
+ property = conf .substring (0 , usingLoc ).trim ();
141
149
142
- if (defaultLoc > 0 ) {
143
- defaultValue = filter (conf .substring (defaultLoc + 7 ).trim ());
150
+ propertiesFileName = filter (conf .substring (usingLoc + USING_SEPARATOR .length ()).trim ());
144
151
145
- if (property == null ) {
146
- property = conf .substring (0 , defaultLoc ).trim ();
147
- } else {
148
- propertiesFileName = conf .substring (0 , defaultLoc ).trim ();
149
- }
150
- }
152
+ conf = propertiesFileName ;
153
+ }
151
154
152
- String value = systemProperties . getProperty ( property ) ;
155
+ String defaultValue = null ;
153
156
154
- if (value != null ) {
155
- continue ;
156
- }
157
+ int defaultLoc = conf .indexOf (DEFAULT_SEPARATOR );
157
158
158
- if (propertiesFileName != null ) {
159
- File propertiesFile = new File ( propertiesFileName );
159
+ if (defaultLoc >= 0 ) {
160
+ defaultValue = filter ( conf . substring ( defaultLoc + DEFAULT_SEPARATOR . length ()). trim () );
160
161
161
- if (propertiesFile .exists ()) {
162
- Properties properties = new Properties ();
162
+ if (property == null ) {
163
+ property = conf .substring (0 , defaultLoc ).trim ();
164
+ } else {
165
+ propertiesFileName = conf .substring (0 , defaultLoc ).trim ();
166
+ }
167
+ }
163
168
164
- try {
165
- properties .load (Files .newInputStream (Paths .get (propertiesFileName )));
169
+ String value = systemProperties .getProperty (property );
166
170
167
- value = properties .getProperty (property );
168
- } catch (Exception e ) {
169
- // do nothing
170
- }
171
- }
172
- }
171
+ if (value != null ) {
172
+ continue ;
173
+ }
173
174
174
- if (value == null && defaultValue != null ) {
175
- value = defaultValue ;
176
- }
175
+ if (propertiesFileName != null ) {
176
+ File propertiesFile = new File (propertiesFileName );
177
177
178
- if (value != null ) {
179
- value = filter (value );
180
- systemProperties .setProperty (property , value );
181
- }
182
- } else if (line .startsWith ("[" )) {
183
- int rbrack = line .indexOf ("]" );
178
+ if (propertiesFile .exists ()) {
179
+ Properties properties = new Properties ();
184
180
185
- if (rbrack < 0 ) {
186
- throw new ConfigurationException ("Invalid realm specifier" , lineNo , line );
187
- }
181
+ try (InputStream inputStream = Files .newInputStream (Paths .get (propertiesFileName ))){
182
+ properties .load (inputStream );
183
+ value = properties .getProperty (property );
184
+ } catch (Exception e ) {
185
+ // do nothing
186
+ }
187
+ }
188
+ }
188
189
189
- String realmName = line .substring (1 , rbrack );
190
+ if (value == null && defaultValue != null ) {
191
+ value = defaultValue ;
192
+ }
190
193
191
- handler .addRealm (realmName );
194
+ if (value != null ) {
195
+ value = filter (value );
196
+ systemProperties .setProperty (property , value );
197
+ }
192
198
193
- curRealm = realmName ;
194
- } else if (line .startsWith (IMPORT_PREFIX )) {
195
- if (curRealm == null ) {
196
- throw new ConfigurationException ("Unhandled import" , lineNo , line );
197
- }
199
+ break ;
200
+ }
201
+ throw new ConfigurationException ("Unhandled configuration" , lineNo , line );
202
+ }
203
+ case '[' : {
204
+ int rbrack = line .indexOf ("]" );
198
205
199
- String conf = line .substring (IMPORT_PREFIX .length ()).trim ();
206
+ if (rbrack < 0 ) {
207
+ throw new ConfigurationException ("Invalid realm specifier" , lineNo , line );
208
+ }
200
209
201
- int fromLoc = conf . indexOf ( "from" );
210
+ String realmName = line . substring ( 1 , rbrack );
202
211
203
- if (fromLoc < 0 ) {
204
- throw new ConfigurationException ("Missing from clause" , lineNo , line );
205
- }
212
+ handler .addRealm (realmName );
206
213
207
- String importSpec = conf . substring ( 0 , fromLoc ). trim () ;
214
+ curRealm = realmName ;
208
215
209
- String relamName = conf .substring (fromLoc + 4 ).trim ();
216
+ break ;
217
+ }
218
+ case 'i' : {
219
+ if (line .startsWith (IMPORT_PREFIX )) {
220
+ if (curRealm == null ) {
221
+ throw new ConfigurationException ("Unhandled import" , lineNo , line );
222
+ }
223
+ int fromLoc = line .indexOf (FROM_SEPARATOR , IMPORT_PREFIX .length ());
210
224
211
- handler .addImportFrom (relamName , importSpec );
225
+ if (fromLoc < 0 ) {
226
+ throw new ConfigurationException ("Missing from clause" , lineNo , line );
227
+ }
212
228
213
- } else if (line .startsWith (LOAD_PREFIX )) {
214
- String constituent = line .substring (LOAD_PREFIX .length ()).trim ();
229
+ String importSpec = line .substring (IMPORT_PREFIX .length (), fromLoc ).trim ();
215
230
216
- constituent = filter ( constituent );
231
+ String relamName = line . substring ( fromLoc + FROM_SEPARATOR . length ()). trim ( );
217
232
218
- if (constituent .contains ("*" )) {
219
- loadGlob (constituent , false /*not optionally*/ );
220
- } else {
221
- File file = new File (constituent );
233
+ handler .addImportFrom (relamName , importSpec );
222
234
223
- if (file .exists ()) {
224
- handler .addLoadFile (file );
225
- } else {
226
- try {
227
- handler .addLoadURL (new URL (constituent ));
228
- } catch (MalformedURLException e ) {
229
- throw new FileNotFoundException (constituent );
235
+ break ;
230
236
}
237
+ throw new ConfigurationException ("Unhandled configuration" , lineNo , line );
231
238
}
232
- }
233
- } else if (line .startsWith (OPTIONALLY_PREFIX )) {
234
- String constituent = line .substring (OPTIONALLY_PREFIX .length ()).trim ();
235
-
236
- constituent = filter (constituent );
237
-
238
- if (constituent .contains ("*" )) {
239
- loadGlob (constituent , true /*optionally*/ );
240
- } else {
241
- File file = new File (constituent );
242
-
243
- if (file .exists ()) {
244
- handler .addLoadFile (file );
245
- } else {
246
- try {
247
- handler .addLoadURL (new URL (constituent ));
248
- } catch (MalformedURLException e ) {
249
- // swallow
239
+ case 'l' : {
240
+ if (line .startsWith (LOAD_PREFIX )) {
241
+ String constituent = line .substring (LOAD_PREFIX .length ()).trim ();
242
+
243
+ constituent = filter (constituent );
244
+
245
+ if (constituent .contains ("*" )) {
246
+ loadGlob (constituent , false /*not optionally*/ );
247
+ } else {
248
+ File file = new File (constituent );
249
+
250
+ if (file .exists ()) {
251
+ handler .addLoadFile (file );
252
+ } else {
253
+ try {
254
+ handler .addLoadURL (new URL (constituent ));
255
+ } catch (MalformedURLException e ) {
256
+ throw new FileNotFoundException (constituent );
257
+ }
258
+ }
259
+ }
260
+
261
+ break ;
250
262
}
263
+ throw new ConfigurationException ("Unhandled configuration" , lineNo , line );
251
264
}
265
+ case 'o' : {
266
+ if (line .startsWith (OPTIONALLY_PREFIX )) {
267
+ String constituent = line .substring (OPTIONALLY_PREFIX .length ()).trim ();
268
+
269
+ constituent = filter (constituent );
270
+
271
+ if (constituent .contains ("*" )) {
272
+ loadGlob (constituent , true /*optionally*/ );
273
+ } else {
274
+ File file = new File (constituent );
275
+
276
+ if (file .exists ()) {
277
+ handler .addLoadFile (file );
278
+ } else {
279
+ try {
280
+ handler .addLoadURL (new URL (constituent ));
281
+ } catch (MalformedURLException e ) {
282
+ // swallow
283
+ }
284
+ }
285
+ }
286
+
287
+ break ;
288
+ }
289
+ throw new ConfigurationException ("Unhandled configuration" , lineNo , line );
290
+ }
291
+ default :
292
+ throw new ConfigurationException ("Unhandled configuration" , lineNo , line );
252
293
}
253
- } else {
254
- throw new ConfigurationException ("Unhandled configuration" , lineNo , line );
255
294
}
256
295
}
257
-
258
- reader .close ();
259
296
}
260
297
261
298
/**
@@ -373,6 +410,6 @@ protected String filter(String text) throws ConfigurationException {
373
410
* otherwise <code>false</code>.
374
411
*/
375
412
private boolean canIgnore (String line ) {
376
- return (line .length () == 0 || line .startsWith ("#" ));
413
+ return (line .isEmpty () || line .startsWith ("#" ));
377
414
}
378
415
}
0 commit comments