1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.configuration;
19
20 import java.util.Iterator;
21
22 import org.apache.commons.configuration.interpol.ConfigurationInterpolator;
23
24
25
26
27
28
29
30
31
32
33
34
35 public class SubsetConfiguration extends AbstractConfiguration
36 {
37
38 protected Configuration parent;
39
40
41 protected String prefix;
42
43
44 protected String delimiter;
45
46
47
48
49
50
51
52 public SubsetConfiguration(Configuration parent, String prefix)
53 {
54 this.parent = parent;
55 this.prefix = prefix;
56 }
57
58
59
60
61
62
63
64
65 public SubsetConfiguration(Configuration parent, String prefix, String delimiter)
66 {
67 this.parent = parent;
68 this.prefix = prefix;
69 this.delimiter = delimiter;
70 }
71
72
73
74
75
76
77
78
79 protected String getParentKey(String key)
80 {
81 if ("".equals(key) || key == null)
82 {
83 return prefix;
84 }
85 else
86 {
87 return delimiter == null ? prefix + key : prefix + delimiter + key;
88 }
89 }
90
91
92
93
94
95
96
97
98 protected String getChildKey(String key)
99 {
100 if (!key.startsWith(prefix))
101 {
102 throw new IllegalArgumentException("The parent key '" + key + "' is not in the subset.");
103 }
104 else
105 {
106 String modifiedKey = null;
107 if (key.length() == prefix.length())
108 {
109 modifiedKey = "";
110 }
111 else
112 {
113 int i = prefix.length() + (delimiter != null ? delimiter.length() : 0);
114 modifiedKey = key.substring(i);
115 }
116
117 return modifiedKey;
118 }
119 }
120
121
122
123
124
125
126 public Configuration getParent()
127 {
128 return parent;
129 }
130
131
132
133
134
135
136 public String getPrefix()
137 {
138 return prefix;
139 }
140
141
142
143
144
145
146 public void setPrefix(String prefix)
147 {
148 this.prefix = prefix;
149 }
150
151 @Override
152 public Configuration subset(String prefix)
153 {
154 return parent.subset(getParentKey(prefix));
155 }
156
157 public boolean isEmpty()
158 {
159 return !getKeys().hasNext();
160 }
161
162 public boolean containsKey(String key)
163 {
164 return parent.containsKey(getParentKey(key));
165 }
166
167 @Override
168 public void addPropertyDirect(String key, Object value)
169 {
170 parent.addProperty(getParentKey(key), value);
171 }
172
173 @Override
174 protected void clearPropertyDirect(String key)
175 {
176 parent.clearProperty(getParentKey(key));
177 }
178
179 public Object getProperty(String key)
180 {
181 return parent.getProperty(getParentKey(key));
182 }
183
184 @Override
185 public Iterator<String> getKeys(String prefix)
186 {
187 return new SubsetIterator(parent.getKeys(getParentKey(prefix)));
188 }
189
190 public Iterator<String> getKeys()
191 {
192 return new SubsetIterator(parent.getKeys(prefix));
193 }
194
195 @Override
196 protected Object interpolate(Object base)
197 {
198 if (delimiter == null && "".equals(prefix))
199 {
200 return super.interpolate(base);
201 }
202 else
203 {
204 SubsetConfiguration config = new SubsetConfiguration(parent, "");
205 ConfigurationInterpolator interpolator = config.getInterpolator();
206 getInterpolator().registerLocalLookups(interpolator);
207 if (parent instanceof AbstractConfiguration)
208 {
209 interpolator.setParentInterpolator(((AbstractConfiguration) parent).getInterpolator());
210 }
211 return config.interpolate(base);
212 }
213 }
214
215 @Override
216 protected String interpolate(String base)
217 {
218 return super.interpolate(base);
219 }
220
221
222
223
224
225
226 @Override
227 public void setThrowExceptionOnMissing(boolean throwExceptionOnMissing)
228 {
229 if (parent instanceof AbstractConfiguration)
230 {
231 ((AbstractConfiguration) parent).setThrowExceptionOnMissing(throwExceptionOnMissing);
232 }
233 else
234 {
235 super.setThrowExceptionOnMissing(throwExceptionOnMissing);
236 }
237 }
238
239
240
241
242
243
244 @Override
245 public boolean isThrowExceptionOnMissing()
246 {
247 if (parent instanceof AbstractConfiguration)
248 {
249 return ((AbstractConfiguration) parent).isThrowExceptionOnMissing();
250 }
251 else
252 {
253 return super.isThrowExceptionOnMissing();
254 }
255 }
256
257
258
259
260
261
262
263
264 @Override
265 public char getListDelimiter()
266 {
267 return (parent instanceof AbstractConfiguration) ? ((AbstractConfiguration) parent)
268 .getListDelimiter()
269 : super.getListDelimiter();
270 }
271
272
273
274
275
276
277
278
279 @Override
280 public void setListDelimiter(char delim)
281 {
282 if (parent instanceof AbstractConfiguration)
283 {
284 ((AbstractConfiguration) parent).setListDelimiter(delim);
285 }
286 else
287 {
288 super.setListDelimiter(delim);
289 }
290 }
291
292
293
294
295
296
297
298
299
300
301 @Override
302 public boolean isDelimiterParsingDisabled()
303 {
304 return (parent instanceof AbstractConfiguration) ? ((AbstractConfiguration) parent)
305 .isDelimiterParsingDisabled()
306 : super.isDelimiterParsingDisabled();
307 }
308
309
310
311
312
313
314
315
316
317 @Override
318 public void setDelimiterParsingDisabled(boolean delimiterParsingDisabled)
319 {
320 if (parent instanceof AbstractConfiguration)
321 {
322 ((AbstractConfiguration) parent)
323 .setDelimiterParsingDisabled(delimiterParsingDisabled);
324 }
325 else
326 {
327 super.setDelimiterParsingDisabled(delimiterParsingDisabled);
328 }
329 }
330
331
332
333
334
335
336
337
338 private class SubsetIterator implements Iterator<String>
339 {
340
341 private final Iterator<String> parentIterator;
342
343
344
345
346
347
348
349 public SubsetIterator(Iterator<String> it)
350 {
351 parentIterator = it;
352 }
353
354
355
356
357
358
359
360 public boolean hasNext()
361 {
362 return parentIterator.hasNext();
363 }
364
365
366
367
368
369
370
371
372 public String next()
373 {
374 return getChildKey(parentIterator.next());
375 }
376
377
378
379
380
381 public void remove()
382 {
383 parentIterator.remove();
384 }
385 }
386 }