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