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.math.BigDecimal; |
21 | |
import java.math.BigInteger; |
22 | |
import java.util.ArrayList; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.List; |
25 | |
import java.util.NoSuchElementException; |
26 | |
import java.util.Properties; |
27 | |
|
28 | |
import org.apache.commons.collections.Predicate; |
29 | |
import org.apache.commons.collections.iterators.FilterIterator; |
30 | |
import org.apache.commons.configuration.event.EventSource; |
31 | |
import org.apache.commons.lang.BooleanUtils; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | 4494 | public abstract class AbstractConfiguration extends EventSource implements Configuration |
64 | |
{ |
65 | |
|
66 | |
public static final int EVENT_ADD_PROPERTY = 1; |
67 | |
|
68 | |
|
69 | |
public static final int EVENT_CLEAR_PROPERTY = 2; |
70 | |
|
71 | |
|
72 | |
public static final int EVENT_SET_PROPERTY = 3; |
73 | |
|
74 | |
|
75 | |
public static final int EVENT_CLEAR = 4; |
76 | |
|
77 | |
|
78 | |
protected static final String START_TOKEN = "${"; |
79 | |
|
80 | |
|
81 | |
protected static final String END_TOKEN = "}"; |
82 | |
|
83 | |
|
84 | 54 | private static char defaultListDelimiter = ','; |
85 | |
|
86 | |
|
87 | 2247 | private char listDelimiter = defaultListDelimiter; |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
private boolean delimiterParsingDisabled; |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
private boolean throwExceptionOnMissing; |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
public static void setDefaultListDelimiter(char delimiter) |
110 | |
{ |
111 | 2 | AbstractConfiguration.defaultListDelimiter = delimiter; |
112 | 2 | } |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public static void setDelimiter(char delimiter) |
122 | |
{ |
123 | 0 | setDefaultListDelimiter(delimiter); |
124 | 0 | } |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
public static char getDefaultListDelimiter() |
132 | |
{ |
133 | 50 | return AbstractConfiguration.defaultListDelimiter; |
134 | |
} |
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
public static char getDelimiter() |
143 | |
{ |
144 | 0 | return getDefaultListDelimiter(); |
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
public void setListDelimiter(char listDelimiter) |
157 | |
{ |
158 | 194 | this.listDelimiter = listDelimiter; |
159 | 194 | } |
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
public char getListDelimiter() |
168 | |
{ |
169 | 46862 | return listDelimiter; |
170 | |
} |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
public boolean isDelimiterParsingDisabled() |
178 | |
{ |
179 | 45973 | return delimiterParsingDisabled; |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
public void setDelimiterParsingDisabled(boolean delimiterParsingDisabled) |
194 | |
{ |
195 | 197 | this.delimiterParsingDisabled = delimiterParsingDisabled; |
196 | 197 | } |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
public void setThrowExceptionOnMissing(boolean throwExceptionOnMissing) |
210 | |
{ |
211 | 683 | this.throwExceptionOnMissing = throwExceptionOnMissing; |
212 | 683 | } |
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
public boolean isThrowExceptionOnMissing() |
220 | |
{ |
221 | 662 | return throwExceptionOnMissing; |
222 | |
} |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
public void addProperty(String key, Object value) |
228 | |
{ |
229 | 36265 | fireEvent(EVENT_ADD_PROPERTY, key, value, true); |
230 | |
|
231 | 36265 | if (!isDelimiterParsingDisabled()) |
232 | |
{ |
233 | 36213 | Iterator it = PropertyConverter.toIterator(value, getListDelimiter()); |
234 | 112164 | while (it.hasNext()) |
235 | |
{ |
236 | 39739 | addPropertyDirect(key, it.next()); |
237 | |
} |
238 | |
} |
239 | |
else |
240 | |
{ |
241 | 52 | addPropertyDirect(key, value); |
242 | |
} |
243 | |
|
244 | 36264 | fireEvent(EVENT_ADD_PROPERTY, key, value, false); |
245 | 36264 | } |
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
protected abstract void addPropertyDirect(String key, Object value); |
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
protected String interpolate(String base) |
264 | |
{ |
265 | 682 | Object result = interpolate((Object) base); |
266 | 680 | return (result == null) ? null : result.toString(); |
267 | |
} |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
protected Object interpolate(Object value) |
277 | |
{ |
278 | 2093 | return PropertyConverter.interpolate(value, this); |
279 | |
} |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
protected String interpolateHelper(String base, List priorVariables) |
298 | |
{ |
299 | 0 | return base; |
300 | |
} |
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
public Configuration subset(String prefix) |
306 | |
{ |
307 | 40 | return new SubsetConfiguration(this, prefix, "."); |
308 | |
} |
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
public abstract boolean isEmpty(); |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
public abstract boolean containsKey(String key); |
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
public void setProperty(String key, Object value) |
324 | |
{ |
325 | 781 | fireEvent(EVENT_SET_PROPERTY, key, value, true); |
326 | 781 | setDetailEvents(false); |
327 | |
try |
328 | |
{ |
329 | 781 | clearProperty(key); |
330 | 781 | addProperty(key, value); |
331 | 781 | } |
332 | |
finally |
333 | |
{ |
334 | 0 | setDetailEvents(true); |
335 | |
} |
336 | 781 | fireEvent(EVENT_SET_PROPERTY, key, value, false); |
337 | 781 | } |
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
public void clearProperty(String key) |
347 | |
{ |
348 | 816 | fireEvent(EVENT_CLEAR_PROPERTY, key, null, true); |
349 | 816 | clearPropertyDirect(key); |
350 | 816 | fireEvent(EVENT_CLEAR_PROPERTY, key, null, false); |
351 | 816 | } |
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
protected void clearPropertyDirect(String key) |
362 | |
{ |
363 | |
|
364 | 0 | } |
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | |
public void clear() |
370 | |
{ |
371 | 15 | fireEvent(EVENT_CLEAR, null, null, true); |
372 | 15 | setDetailEvents(false); |
373 | |
try |
374 | |
{ |
375 | 15 | Iterator it = getKeys(); |
376 | 213 | while (it.hasNext()) |
377 | |
{ |
378 | 183 | String key = (String) it.next(); |
379 | 183 | it.remove(); |
380 | |
|
381 | 183 | if (containsKey(key)) |
382 | |
{ |
383 | |
|
384 | 180 | clearProperty(key); |
385 | |
} |
386 | |
} |
387 | 15 | } |
388 | |
finally |
389 | |
{ |
390 | 0 | setDetailEvents(true); |
391 | |
} |
392 | 15 | fireEvent(EVENT_CLEAR, null, null, false); |
393 | 15 | } |
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
public abstract Iterator getKeys(); |
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
public Iterator getKeys(final String prefix) |
404 | |
{ |
405 | 37 | return new FilterIterator(getKeys(), new Predicate() |
406 | |
{ |
407 | 37 | public boolean evaluate(Object obj) |
408 | |
{ |
409 | 426 | String key = (String) obj; |
410 | 426 | return key.startsWith(prefix + ".") || key.equals(prefix); |
411 | |
} |
412 | |
}); |
413 | |
} |
414 | |
|
415 | |
|
416 | |
|
417 | |
|
418 | |
public Properties getProperties(String key) |
419 | |
{ |
420 | 4 | return getProperties(key, null); |
421 | |
} |
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | |
|
427 | |
|
428 | |
|
429 | |
|
430 | |
|
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
public Properties getProperties(String key, Properties defaults) |
439 | |
{ |
440 | |
|
441 | |
|
442 | |
|
443 | 4 | String[] tokens = getStringArray(key); |
444 | |
|
445 | |
|
446 | |
|
447 | |
|
448 | 4 | Properties props = defaults == null ? new Properties() : new Properties(defaults); |
449 | 10 | for (int i = 0; i < tokens.length; i++) |
450 | |
{ |
451 | 8 | String token = tokens[i]; |
452 | 8 | int equalSign = token.indexOf('='); |
453 | 8 | if (equalSign > 0) |
454 | |
{ |
455 | 6 | String pkey = token.substring(0, equalSign).trim(); |
456 | 6 | String pvalue = token.substring(equalSign + 1).trim(); |
457 | 6 | props.put(pkey, pvalue); |
458 | |
} |
459 | 2 | else if (tokens.length == 1 && "".equals(token)) |
460 | |
{ |
461 | |
|
462 | |
|
463 | 2 | break; |
464 | |
} |
465 | |
else |
466 | |
{ |
467 | 0 | throw new IllegalArgumentException('\'' + token + "' does not contain an equals sign"); |
468 | |
} |
469 | |
} |
470 | 4 | return props; |
471 | |
} |
472 | |
|
473 | |
|
474 | |
|
475 | |
|
476 | |
|
477 | |
public boolean getBoolean(String key) |
478 | |
{ |
479 | 58 | Boolean b = getBoolean(key, null); |
480 | 56 | if (b != null) |
481 | |
{ |
482 | 54 | return b.booleanValue(); |
483 | |
} |
484 | |
else |
485 | |
{ |
486 | 2 | throw new NoSuchElementException('\'' + key + "' doesn't map to an existing object"); |
487 | |
} |
488 | |
} |
489 | |
|
490 | |
|
491 | |
|
492 | |
|
493 | |
|
494 | |
public boolean getBoolean(String key, boolean defaultValue) |
495 | |
{ |
496 | 11 | return getBoolean(key, BooleanUtils.toBooleanObject(defaultValue)).booleanValue(); |
497 | |
} |
498 | |
|
499 | |
|
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | |
|
508 | |
|
509 | |
|
510 | |
|
511 | |
public Boolean getBoolean(String key, Boolean defaultValue) |
512 | |
{ |
513 | 100 | Object value = resolveContainerStore(key); |
514 | |
|
515 | 100 | if (value == null) |
516 | |
{ |
517 | 27 | return defaultValue; |
518 | |
} |
519 | |
else |
520 | |
{ |
521 | |
try |
522 | |
{ |
523 | 73 | return PropertyConverter.toBoolean(interpolate(value)); |
524 | |
} |
525 | |
catch (ConversionException e) |
526 | |
{ |
527 | 3 | throw new ConversionException('\'' + key + "' doesn't map to a Boolean object", e); |
528 | |
} |
529 | |
} |
530 | |
} |
531 | |
|
532 | |
|
533 | |
|
534 | |
|
535 | |
public byte getByte(String key) |
536 | |
{ |
537 | 15 | Byte b = getByte(key, null); |
538 | 13 | if (b != null) |
539 | |
{ |
540 | 11 | return b.byteValue(); |
541 | |
} |
542 | |
else |
543 | |
{ |
544 | 2 | throw new NoSuchElementException('\'' + key + " doesn't map to an existing object"); |
545 | |
} |
546 | |
} |
547 | |
|
548 | |
|
549 | |
|
550 | |
|
551 | |
public byte getByte(String key, byte defaultValue) |
552 | |
{ |
553 | 4 | return getByte(key, new Byte(defaultValue)).byteValue(); |
554 | |
} |
555 | |
|
556 | |
|
557 | |
|
558 | |
|
559 | |
public Byte getByte(String key, Byte defaultValue) |
560 | |
{ |
561 | 22 | Object value = resolveContainerStore(key); |
562 | |
|
563 | 22 | if (value == null) |
564 | |
{ |
565 | 4 | return defaultValue; |
566 | |
} |
567 | |
else |
568 | |
{ |
569 | |
try |
570 | |
{ |
571 | 18 | return PropertyConverter.toByte(interpolate(value)); |
572 | |
} |
573 | |
catch (ConversionException e) |
574 | |
{ |
575 | 2 | throw new ConversionException('\'' + key + "' doesn't map to a Byte object", e); |
576 | |
} |
577 | |
} |
578 | |
} |
579 | |
|
580 | |
|
581 | |
|
582 | |
|
583 | |
public double getDouble(String key) |
584 | |
{ |
585 | 15 | Double d = getDouble(key, null); |
586 | 13 | if (d != null) |
587 | |
{ |
588 | 11 | return d.doubleValue(); |
589 | |
} |
590 | |
else |
591 | |
{ |
592 | 2 | throw new NoSuchElementException('\'' + key + "' doesn't map to an existing object"); |
593 | |
} |
594 | |
} |
595 | |
|
596 | |
|
597 | |
|
598 | |
|
599 | |
public double getDouble(String key, double defaultValue) |
600 | |
{ |
601 | 11 | return getDouble(key, new Double(defaultValue)).doubleValue(); |
602 | |
} |
603 | |
|
604 | |
|
605 | |
|
606 | |
|
607 | |
public Double getDouble(String key, Double defaultValue) |
608 | |
{ |
609 | 29 | Object value = resolveContainerStore(key); |
610 | |
|
611 | 29 | if (value == null) |
612 | |
{ |
613 | 11 | return defaultValue; |
614 | |
} |
615 | |
else |
616 | |
{ |
617 | |
try |
618 | |
{ |
619 | 18 | return PropertyConverter.toDouble(interpolate(value)); |
620 | |
} |
621 | |
catch (ConversionException e) |
622 | |
{ |
623 | 2 | throw new ConversionException('\'' + key + "' doesn't map to a Double object", e); |
624 | |
} |
625 | |
} |
626 | |
} |
627 | |
|
628 | |
|
629 | |
|
630 | |
|
631 | |
public float getFloat(String key) |
632 | |
{ |
633 | 14 | Float f = getFloat(key, null); |
634 | 12 | if (f != null) |
635 | |
{ |
636 | 10 | return f.floatValue(); |
637 | |
} |
638 | |
else |
639 | |
{ |
640 | 2 | throw new NoSuchElementException('\'' + key + "' doesn't map to an existing object"); |
641 | |
} |
642 | |
} |
643 | |
|
644 | |
|
645 | |
|
646 | |
|
647 | |
public float getFloat(String key, float defaultValue) |
648 | |
{ |
649 | 7 | return getFloat(key, new Float(defaultValue)).floatValue(); |
650 | |
} |
651 | |
|
652 | |
|
653 | |
|
654 | |
|
655 | |
public Float getFloat(String key, Float defaultValue) |
656 | |
{ |
657 | 24 | Object value = resolveContainerStore(key); |
658 | |
|
659 | 24 | if (value == null) |
660 | |
{ |
661 | 7 | return defaultValue; |
662 | |
} |
663 | |
else |
664 | |
{ |
665 | |
try |
666 | |
{ |
667 | 17 | return PropertyConverter.toFloat(interpolate(value)); |
668 | |
} |
669 | |
catch (ConversionException e) |
670 | |
{ |
671 | 2 | throw new ConversionException('\'' + key + "' doesn't map to a Float object", e); |
672 | |
} |
673 | |
} |
674 | |
} |
675 | |
|
676 | |
|
677 | |
|
678 | |
|
679 | |
public int getInt(String key) |
680 | |
{ |
681 | 39 | Integer i = getInteger(key, null); |
682 | 39 | if (i != null) |
683 | |
{ |
684 | 39 | return i.intValue(); |
685 | |
} |
686 | |
else |
687 | |
{ |
688 | 0 | throw new NoSuchElementException('\'' + key + "' doesn't map to an existing object"); |
689 | |
} |
690 | |
} |
691 | |
|
692 | |
|
693 | |
|
694 | |
|
695 | |
public int getInt(String key, int defaultValue) |
696 | |
{ |
697 | 3 | Integer i = getInteger(key, null); |
698 | |
|
699 | 3 | if (i == null) |
700 | |
{ |
701 | 3 | return defaultValue; |
702 | |
} |
703 | |
|
704 | 0 | return i.intValue(); |
705 | |
} |
706 | |
|
707 | |
|
708 | |
|
709 | |
|
710 | |
public Integer getInteger(String key, Integer defaultValue) |
711 | |
{ |
712 | 43 | Object value = resolveContainerStore(key); |
713 | |
|
714 | 43 | if (value == null) |
715 | |
{ |
716 | 3 | return defaultValue; |
717 | |
} |
718 | |
else |
719 | |
{ |
720 | |
try |
721 | |
{ |
722 | 40 | return PropertyConverter.toInteger(interpolate(value)); |
723 | |
} |
724 | |
catch (ConversionException e) |
725 | |
{ |
726 | 0 | throw new ConversionException('\'' + key + "' doesn't map to an Integer object", e); |
727 | |
} |
728 | |
} |
729 | |
} |
730 | |
|
731 | |
|
732 | |
|
733 | |
|
734 | |
public long getLong(String key) |
735 | |
{ |
736 | 15 | Long l = getLong(key, null); |
737 | 13 | if (l != null) |
738 | |
{ |
739 | 11 | return l.longValue(); |
740 | |
} |
741 | |
else |
742 | |
{ |
743 | 2 | throw new NoSuchElementException('\'' + key + "' doesn't map to an existing object"); |
744 | |
} |
745 | |
} |
746 | |
|
747 | |
|
748 | |
|
749 | |
|
750 | |
public long getLong(String key, long defaultValue) |
751 | |
{ |
752 | 7 | return getLong(key, new Long(defaultValue)).longValue(); |
753 | |
} |
754 | |
|
755 | |
|
756 | |
|
757 | |
|
758 | |
public Long getLong(String key, Long defaultValue) |
759 | |
{ |
760 | 26 | Object value = resolveContainerStore(key); |
761 | |
|
762 | 26 | if (value == null) |
763 | |
{ |
764 | 7 | return defaultValue; |
765 | |
} |
766 | |
else |
767 | |
{ |
768 | |
try |
769 | |
{ |
770 | 19 | return PropertyConverter.toLong(interpolate(value)); |
771 | |
} |
772 | |
catch (ConversionException e) |
773 | |
{ |
774 | 2 | throw new ConversionException('\'' + key + "' doesn't map to a Long object", e); |
775 | |
} |
776 | |
} |
777 | |
} |
778 | |
|
779 | |
|
780 | |
|
781 | |
|
782 | |
public short getShort(String key) |
783 | |
{ |
784 | 17 | Short s = getShort(key, null); |
785 | 15 | if (s != null) |
786 | |
{ |
787 | 13 | return s.shortValue(); |
788 | |
} |
789 | |
else |
790 | |
{ |
791 | 2 | throw new NoSuchElementException('\'' + key + "' doesn't map to an existing object"); |
792 | |
} |
793 | |
} |
794 | |
|
795 | |
|
796 | |
|
797 | |
|
798 | |
public short getShort(String key, short defaultValue) |
799 | |
{ |
800 | 7 | return getShort(key, new Short(defaultValue)).shortValue(); |
801 | |
} |
802 | |
|
803 | |
|
804 | |
|
805 | |
|
806 | |
public Short getShort(String key, Short defaultValue) |
807 | |
{ |
808 | 31 | Object value = resolveContainerStore(key); |
809 | |
|
810 | 31 | if (value == null) |
811 | |
{ |
812 | 9 | return defaultValue; |
813 | |
} |
814 | |
else |
815 | |
{ |
816 | |
try |
817 | |
{ |
818 | 22 | return PropertyConverter.toShort(interpolate(value)); |
819 | |
} |
820 | |
catch (ConversionException e) |
821 | |
{ |
822 | 2 | throw new ConversionException('\'' + key + "' doesn't map to a Short object", e); |
823 | |
} |
824 | |
} |
825 | |
} |
826 | |
|
827 | |
|
828 | |
|
829 | |
|
830 | |
public BigDecimal getBigDecimal(String key) |
831 | |
{ |
832 | 7 | BigDecimal number = getBigDecimal(key, null); |
833 | 5 | if (number != null) |
834 | |
{ |
835 | 3 | return number; |
836 | |
} |
837 | 2 | else if (isThrowExceptionOnMissing()) |
838 | |
{ |
839 | 1 | throw new NoSuchElementException('\'' + key + "' doesn't map to an existing object"); |
840 | |
} |
841 | |
else |
842 | |
{ |
843 | 1 | return null; |
844 | |
} |
845 | |
} |
846 | |
|
847 | |
|
848 | |
|
849 | |
|
850 | |
public BigDecimal getBigDecimal(String key, BigDecimal defaultValue) |
851 | |
{ |
852 | 12 | Object value = resolveContainerStore(key); |
853 | |
|
854 | 12 | if (value == null) |
855 | |
{ |
856 | 4 | return defaultValue; |
857 | |
} |
858 | |
else |
859 | |
{ |
860 | |
try |
861 | |
{ |
862 | 8 | return PropertyConverter.toBigDecimal(interpolate(value)); |
863 | |
} |
864 | |
catch (ConversionException e) |
865 | |
{ |
866 | 2 | throw new ConversionException('\'' + key + "' doesn't map to a BigDecimal object", e); |
867 | |
} |
868 | |
} |
869 | |
} |
870 | |
|
871 | |
|
872 | |
|
873 | |
|
874 | |
public BigInteger getBigInteger(String key) |
875 | |
{ |
876 | 8 | BigInteger number = getBigInteger(key, null); |
877 | 6 | if (number != null) |
878 | |
{ |
879 | 4 | return number; |
880 | |
} |
881 | 2 | else if (isThrowExceptionOnMissing()) |
882 | |
{ |
883 | 1 | throw new NoSuchElementException('\'' + key + "' doesn't map to an existing object"); |
884 | |
} |
885 | |
else |
886 | |
{ |
887 | 1 | return null; |
888 | |
} |
889 | |
} |
890 | |
|
891 | |
|
892 | |
|
893 | |
|
894 | |
public BigInteger getBigInteger(String key, BigInteger defaultValue) |
895 | |
{ |
896 | 13 | Object value = resolveContainerStore(key); |
897 | |
|
898 | 13 | if (value == null) |
899 | |
{ |
900 | 4 | return defaultValue; |
901 | |
} |
902 | |
else |
903 | |
{ |
904 | |
try |
905 | |
{ |
906 | 9 | return PropertyConverter.toBigInteger(interpolate(value)); |
907 | |
} |
908 | |
catch (ConversionException e) |
909 | |
{ |
910 | 2 | throw new ConversionException('\'' + key + "' doesn't map to a BigDecimal object", e); |
911 | |
} |
912 | |
} |
913 | |
} |
914 | |
|
915 | |
|
916 | |
|
917 | |
|
918 | |
public String getString(String key) |
919 | |
{ |
920 | 511 | String s = getString(key, null); |
921 | 509 | if (s != null) |
922 | |
{ |
923 | 309 | return s; |
924 | |
} |
925 | 200 | else if (isThrowExceptionOnMissing()) |
926 | |
{ |
927 | 7 | throw new NoSuchElementException('\'' + key + "' doesn't map to an existing object"); |
928 | |
} |
929 | |
else |
930 | |
{ |
931 | 193 | return null; |
932 | |
} |
933 | |
} |
934 | |
|
935 | |
|
936 | |
|
937 | |
|
938 | |
public String getString(String key, String defaultValue) |
939 | |
{ |
940 | 587 | Object value = resolveContainerStore(key); |
941 | |
|
942 | 587 | if (value instanceof String) |
943 | |
{ |
944 | 379 | return interpolate((String) value); |
945 | |
} |
946 | 208 | else if (value == null) |
947 | |
{ |
948 | 208 | return interpolate(defaultValue); |
949 | |
} |
950 | |
else |
951 | |
{ |
952 | 0 | throw new ConversionException('\'' + key + "' doesn't map to a String object"); |
953 | |
} |
954 | |
} |
955 | |
|
956 | |
|
957 | |
|
958 | |
|
959 | |
public String[] getStringArray(String key) |
960 | |
{ |
961 | 18 | Object value = getProperty(key); |
962 | |
|
963 | |
String[] array; |
964 | |
|
965 | 18 | if (value instanceof String) |
966 | |
{ |
967 | 10 | array = new String[1]; |
968 | |
|
969 | 10 | array[0] = interpolate((String) value); |
970 | |
} |
971 | 8 | else if (value instanceof List) |
972 | |
{ |
973 | 6 | List list = (List) value; |
974 | 6 | array = new String[list.size()]; |
975 | |
|
976 | 22 | for (int i = 0; i < array.length; i++) |
977 | |
{ |
978 | 16 | array[i] = interpolate((String) list.get(i)); |
979 | |
} |
980 | |
} |
981 | 2 | else if (value == null) |
982 | |
{ |
983 | 2 | array = new String[0]; |
984 | |
} |
985 | |
else |
986 | |
{ |
987 | 0 | throw new ConversionException('\'' + key + "' doesn't map to a String/List object"); |
988 | |
} |
989 | 18 | return array; |
990 | |
} |
991 | |
|
992 | |
|
993 | |
|
994 | |
|
995 | |
public List getList(String key) |
996 | |
{ |
997 | 247 | return getList(key, new ArrayList()); |
998 | |
} |
999 | |
|
1000 | |
|
1001 | |
|
1002 | |
|
1003 | |
public List getList(String key, List defaultValue) |
1004 | |
{ |
1005 | 207 | Object value = getProperty(key); |
1006 | |
List list; |
1007 | |
|
1008 | 207 | if (value instanceof String) |
1009 | |
{ |
1010 | 49 | list = new ArrayList(1); |
1011 | 49 | list.add(interpolate((String) value)); |
1012 | |
} |
1013 | 158 | else if (value instanceof List) |
1014 | |
{ |
1015 | 106 | list = new ArrayList(); |
1016 | 106 | List l = (List) value; |
1017 | |
|
1018 | |
|
1019 | 106 | Iterator it = l.iterator(); |
1020 | 581 | while (it.hasNext()) |
1021 | |
{ |
1022 | 369 | list.add(interpolate(it.next())); |
1023 | |
} |
1024 | |
|
1025 | |
} |
1026 | 52 | else if (value == null) |
1027 | |
{ |
1028 | 51 | list = defaultValue; |
1029 | |
} |
1030 | |
else |
1031 | |
{ |
1032 | 1 | throw new ConversionException('\'' + key + "' doesn't map to a List object: " + value + ", a " |
1033 | |
+ value.getClass().getName()); |
1034 | |
} |
1035 | 206 | return list; |
1036 | |
} |
1037 | |
|
1038 | |
|
1039 | |
|
1040 | |
|
1041 | |
|
1042 | |
|
1043 | |
|
1044 | |
|
1045 | |
|
1046 | |
protected Object resolveContainerStore(String key) |
1047 | |
{ |
1048 | 1030 | Object value = getProperty(key); |
1049 | 1030 | if (value != null) |
1050 | |
{ |
1051 | 733 | if (value instanceof List) |
1052 | |
{ |
1053 | 11 | List list = (List) value; |
1054 | 11 | value = list.isEmpty() ? null : list.get(0); |
1055 | |
} |
1056 | 722 | else if (value instanceof Object[]) |
1057 | |
{ |
1058 | 1 | Object[] array = (Object[]) value; |
1059 | 1 | value = array.length == 0 ? null : array[0]; |
1060 | |
} |
1061 | 721 | else if (value instanceof boolean[]) |
1062 | |
{ |
1063 | 1 | boolean[] array = (boolean[]) value; |
1064 | 1 | value = array.length == 0 ? null : array[0] ? Boolean.TRUE : Boolean.FALSE; |
1065 | |
} |
1066 | 720 | else if (value instanceof byte[]) |
1067 | |
{ |
1068 | 1 | byte[] array = (byte[]) value; |
1069 | 1 | value = array.length == 0 ? null : new Byte(array[0]); |
1070 | |
} |
1071 | 719 | else if (value instanceof short[]) |
1072 | |
{ |
1073 | 1 | short[] array = (short[]) value; |
1074 | 1 | value = array.length == 0 ? null : new Short(array[0]); |
1075 | |
} |
1076 | 718 | else if (value instanceof int[]) |
1077 | |
{ |
1078 | 1 | int[] array = (int[]) value; |
1079 | 1 | value = array.length == 0 ? null : new Integer(array[0]); |
1080 | |
} |
1081 | 717 | else if (value instanceof long[]) |
1082 | |
{ |
1083 | 1 | long[] array = (long[]) value; |
1084 | 1 | value = array.length == 0 ? null : new Long(array[0]); |
1085 | |
} |
1086 | 716 | else if (value instanceof float[]) |
1087 | |
{ |
1088 | 1 | float[] array = (float[]) value; |
1089 | 1 | value = array.length == 0 ? null : new Float(array[0]); |
1090 | |
} |
1091 | 715 | else if (value instanceof double[]) |
1092 | |
{ |
1093 | 1 | double[] array = (double[]) value; |
1094 | 1 | value = array.length == 0 ? null : new Double(array[0]); |
1095 | |
} |
1096 | |
} |
1097 | |
|
1098 | 1030 | return value; |
1099 | |
} |
1100 | |
|
1101 | |
} |