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