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.awt.Color; |
21 | |
import java.lang.reflect.Constructor; |
22 | |
import java.lang.reflect.InvocationTargetException; |
23 | |
import java.math.BigDecimal; |
24 | |
import java.math.BigInteger; |
25 | |
import java.net.MalformedURLException; |
26 | |
import java.net.URL; |
27 | |
import java.text.ParseException; |
28 | |
import java.text.SimpleDateFormat; |
29 | |
import java.util.ArrayList; |
30 | |
import java.util.Calendar; |
31 | |
import java.util.Collection; |
32 | |
import java.util.Date; |
33 | |
import java.util.Iterator; |
34 | |
import java.util.List; |
35 | |
import java.util.Locale; |
36 | |
|
37 | |
import org.apache.commons.collections.IteratorUtils; |
38 | |
import org.apache.commons.collections.iterators.IteratorChain; |
39 | |
import org.apache.commons.collections.iterators.SingletonIterator; |
40 | |
import org.apache.commons.lang.BooleanUtils; |
41 | |
import org.apache.commons.lang.StringUtils; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 108 | public final class PropertyConverter |
51 | |
{ |
52 | |
|
53 | |
static final String LIST_ESCAPE = "\\"; |
54 | |
|
55 | |
|
56 | |
private static final String HEX_PREFIX = "0x"; |
57 | |
|
58 | |
|
59 | |
private static final int HEX_RADIX = 16; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | 53 | private static final Class[] CONSTR_ARGS = {String.class}; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
private PropertyConverter() |
70 | 0 | { |
71 | |
|
72 | 0 | } |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public static Boolean toBoolean(Object value) throws ConversionException |
88 | |
{ |
89 | 117 | if (value instanceof Boolean) |
90 | |
{ |
91 | 42 | return (Boolean) value; |
92 | |
} |
93 | 75 | else if (value instanceof String) |
94 | |
{ |
95 | 73 | Boolean b = BooleanUtils.toBooleanObject((String) value); |
96 | 73 | if (b == null) |
97 | |
{ |
98 | 5 | throw new ConversionException("The value " + value + " can't be converted to a Boolean object"); |
99 | |
} |
100 | 68 | return b; |
101 | |
} |
102 | |
else |
103 | |
{ |
104 | 2 | throw new ConversionException("The value " + value + " can't be converted to a Boolean object"); |
105 | |
} |
106 | |
} |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public static Byte toByte(Object value) throws ConversionException |
116 | |
{ |
117 | 54 | Number n = toNumber(value, Byte.class); |
118 | 48 | if (n instanceof Byte) |
119 | |
{ |
120 | 46 | return (Byte) n; |
121 | |
} |
122 | |
else |
123 | |
{ |
124 | 2 | return new Byte(n.byteValue()); |
125 | |
} |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public static Short toShort(Object value) throws ConversionException |
136 | |
{ |
137 | 58 | Number n = toNumber(value, Short.class); |
138 | 52 | if (n instanceof Short) |
139 | |
{ |
140 | 51 | return (Short) n; |
141 | |
} |
142 | |
else |
143 | |
{ |
144 | 1 | return new Short(n.shortValue()); |
145 | |
} |
146 | |
} |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
public static Integer toInteger(Object value) throws ConversionException |
156 | |
{ |
157 | 76 | Number n = toNumber(value, Integer.class); |
158 | 72 | if (n instanceof Integer) |
159 | |
{ |
160 | 71 | return (Integer) n; |
161 | |
} |
162 | |
else |
163 | |
{ |
164 | 1 | return new Integer(n.intValue()); |
165 | |
} |
166 | |
} |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
public static Long toLong(Object value) throws ConversionException |
176 | |
{ |
177 | 55 | Number n = toNumber(value, Long.class); |
178 | 49 | if (n instanceof Long) |
179 | |
{ |
180 | 46 | return (Long) n; |
181 | |
} |
182 | |
else |
183 | |
{ |
184 | 3 | return new Long(n.longValue()); |
185 | |
} |
186 | |
} |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
public static Float toFloat(Object value) throws ConversionException |
196 | |
{ |
197 | 53 | Number n = toNumber(value, Float.class); |
198 | 47 | if (n instanceof Float) |
199 | |
{ |
200 | 46 | return (Float) n; |
201 | |
} |
202 | |
else |
203 | |
{ |
204 | 1 | return new Float(n.floatValue()); |
205 | |
} |
206 | |
} |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
public static Double toDouble(Object value) throws ConversionException |
216 | |
{ |
217 | 54 | Number n = toNumber(value, Double.class); |
218 | 48 | if (n instanceof Double) |
219 | |
{ |
220 | 47 | return (Double) n; |
221 | |
} |
222 | |
else |
223 | |
{ |
224 | 1 | return new Double(n.doubleValue()); |
225 | |
} |
226 | |
} |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
public static BigInteger toBigInteger(Object value) throws ConversionException |
236 | |
{ |
237 | 41 | Number n = toNumber(value, BigInteger.class); |
238 | 35 | if (n instanceof BigInteger) |
239 | |
{ |
240 | 34 | return (BigInteger) n; |
241 | |
} |
242 | |
else |
243 | |
{ |
244 | 1 | return BigInteger.valueOf(n.longValue()); |
245 | |
} |
246 | |
} |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
public static BigDecimal toBigDecimal(Object value) throws ConversionException |
256 | |
{ |
257 | 40 | Number n = toNumber(value, BigDecimal.class); |
258 | 34 | if (n instanceof BigDecimal) |
259 | |
{ |
260 | 33 | return (BigDecimal) n; |
261 | |
} |
262 | |
else |
263 | |
{ |
264 | 1 | return new BigDecimal(n.doubleValue()); |
265 | |
} |
266 | |
} |
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
static Number toNumber(Object value, Class targetClass) |
281 | |
throws ConversionException |
282 | |
{ |
283 | 439 | if (value instanceof Number) |
284 | |
{ |
285 | 157 | return (Number) value; |
286 | |
} |
287 | |
else |
288 | |
{ |
289 | 282 | String str = value.toString(); |
290 | 282 | if (str.startsWith(HEX_PREFIX)) |
291 | |
{ |
292 | |
try |
293 | |
{ |
294 | 7 | return new BigInteger(str.substring(HEX_PREFIX.length()), |
295 | |
HEX_RADIX); |
296 | |
} |
297 | |
catch (NumberFormatException nex) |
298 | |
{ |
299 | 1 | throw new ConversionException("Could not convert " + str |
300 | |
+ " to " + targetClass.getName() |
301 | |
+ "! Invalid hex number.", nex); |
302 | |
} |
303 | |
} |
304 | |
|
305 | |
try |
306 | |
{ |
307 | 275 | Constructor constr = targetClass.getConstructor(CONSTR_ARGS); |
308 | 274 | return (Number) constr.newInstance(new Object[]{str}); |
309 | |
} |
310 | |
catch (InvocationTargetException itex) |
311 | |
{ |
312 | 47 | throw new ConversionException("Could not convert " + str |
313 | |
+ " to " + targetClass.getName(), itex |
314 | |
.getTargetException()); |
315 | |
} |
316 | |
catch (Exception ex) |
317 | |
{ |
318 | |
|
319 | 1 | throw new ConversionException( |
320 | |
"Conversion error when trying to convert " + str |
321 | |
+ " to " + targetClass.getName(), ex); |
322 | |
} |
323 | |
} |
324 | |
} |
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
|
333 | |
public static URL toURL(Object value) throws ConversionException |
334 | |
{ |
335 | 35 | if (value instanceof URL) |
336 | |
{ |
337 | 15 | return (URL) value; |
338 | |
} |
339 | 20 | else if (value instanceof String) |
340 | |
{ |
341 | |
try |
342 | |
{ |
343 | 18 | return new URL((String) value); |
344 | |
} |
345 | |
catch (MalformedURLException e) |
346 | |
{ |
347 | 2 | throw new ConversionException("The value " + value + " can't be converted to an URL", e); |
348 | |
} |
349 | |
} |
350 | |
else |
351 | |
{ |
352 | 2 | throw new ConversionException("The value " + value + " can't be converted to an URL"); |
353 | |
} |
354 | |
} |
355 | |
|
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
public static Locale toLocale(Object value) throws ConversionException |
364 | |
{ |
365 | 40 | if (value instanceof Locale) |
366 | |
{ |
367 | 14 | return (Locale) value; |
368 | |
} |
369 | 26 | else if (value instanceof String) |
370 | |
{ |
371 | 24 | List elements = split((String) value, '_'); |
372 | 24 | int size = elements.size(); |
373 | |
|
374 | 24 | if (size >= 1 && (((String) elements.get(0)).length() == 2 || ((String) elements.get(0)).length() == 0)) |
375 | |
{ |
376 | 22 | String language = (String) elements.get(0); |
377 | 22 | String country = (String) ((size >= 2) ? elements.get(1) : ""); |
378 | 22 | String variant = (String) ((size >= 3) ? elements.get(2) : ""); |
379 | |
|
380 | 22 | return new Locale(language, country, variant); |
381 | |
} |
382 | |
else |
383 | |
{ |
384 | 2 | throw new ConversionException("The value " + value + " can't be converted to a Locale"); |
385 | |
} |
386 | |
} |
387 | |
else |
388 | |
{ |
389 | 2 | throw new ConversionException("The value " + value + " can't be converted to a Locale"); |
390 | |
} |
391 | |
} |
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
public static List split(String s, char delimiter) |
404 | |
{ |
405 | 10079 | if (s == null) |
406 | |
{ |
407 | 9 | return new ArrayList(); |
408 | |
} |
409 | |
|
410 | 10070 | List list = new ArrayList(); |
411 | |
|
412 | 10070 | StringBuffer token = new StringBuffer(); |
413 | 10070 | int begin = 0; |
414 | 10070 | int end = 0; |
415 | 33813 | while (begin <= s.length()) |
416 | |
{ |
417 | |
|
418 | 13673 | int index = s.indexOf(delimiter, end); |
419 | |
|
420 | |
|
421 | 13673 | end = (index != -1) ? index : s.length(); |
422 | |
|
423 | |
|
424 | 13673 | String chunk = s.substring(begin , end); |
425 | |
|
426 | 13673 | if (chunk.endsWith(LIST_ESCAPE) && end != s.length()) |
427 | |
{ |
428 | 936 | token.append(chunk.substring(0, chunk.length() - 1)); |
429 | 936 | token.append(delimiter); |
430 | |
} |
431 | |
else |
432 | |
{ |
433 | |
|
434 | 12737 | token.append(chunk); |
435 | |
|
436 | |
|
437 | 12737 | list.add(token.toString().trim()); |
438 | |
|
439 | |
|
440 | 12737 | token = new StringBuffer(); |
441 | |
} |
442 | |
|
443 | |
|
444 | 13673 | end = end + 1; |
445 | 13673 | begin = end; |
446 | |
} |
447 | |
|
448 | 10070 | return list; |
449 | |
} |
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | |
|
456 | |
|
457 | |
|
458 | |
|
459 | |
|
460 | |
|
461 | |
|
462 | |
public static String escapeDelimiters(String s, char delimiter) |
463 | |
{ |
464 | 105 | return StringUtils.replace(s, String.valueOf(delimiter), LIST_ESCAPE |
465 | |
+ delimiter); |
466 | |
} |
467 | |
|
468 | |
|
469 | |
|
470 | |
|
471 | |
|
472 | |
|
473 | |
|
474 | |
|
475 | |
|
476 | |
|
477 | |
|
478 | |
|
479 | |
|
480 | |
|
481 | |
|
482 | |
public static Color toColor(Object value) throws ConversionException |
483 | |
{ |
484 | 36 | if (value instanceof Color) |
485 | |
{ |
486 | 14 | return (Color) value; |
487 | |
} |
488 | 22 | else if (value instanceof String && !StringUtils.isBlank((String) value)) |
489 | |
{ |
490 | 20 | String color = ((String) value).trim(); |
491 | |
|
492 | 20 | int[] components = new int[3]; |
493 | |
|
494 | |
|
495 | 20 | int minlength = components.length * 2; |
496 | 20 | if (color.length() < minlength) |
497 | |
{ |
498 | 0 | throw new ConversionException("The value " + value + " can't be converted to a Color"); |
499 | |
} |
500 | |
|
501 | |
|
502 | 20 | if (color.startsWith("#")) |
503 | |
{ |
504 | 2 | color = color.substring(1); |
505 | |
} |
506 | |
|
507 | |
try |
508 | |
{ |
509 | |
|
510 | 74 | for (int i = 0; i < components.length; i++) |
511 | |
{ |
512 | 56 | components[i] = Integer.parseInt(color.substring(2 * i, 2 * i + 2), HEX_RADIX); |
513 | |
} |
514 | |
|
515 | |
|
516 | |
int alpha; |
517 | 18 | if (color.length() >= minlength + 2) |
518 | |
{ |
519 | 1 | alpha = Integer.parseInt(color.substring(minlength, minlength + 2), HEX_RADIX); |
520 | |
} |
521 | |
else |
522 | |
{ |
523 | 17 | alpha = Color.black.getAlpha(); |
524 | |
} |
525 | |
|
526 | 18 | return new Color(components[0], components[1], components[2], alpha); |
527 | |
} |
528 | |
catch (Exception e) |
529 | |
{ |
530 | 2 | throw new ConversionException("The value " + value + " can't be converted to a Color", e); |
531 | |
} |
532 | |
} |
533 | |
else |
534 | |
{ |
535 | 2 | throw new ConversionException("The value " + value + " can't be converted to a Color"); |
536 | |
} |
537 | |
} |
538 | |
|
539 | |
|
540 | |
|
541 | |
|
542 | |
|
543 | |
|
544 | |
|
545 | |
|
546 | |
|
547 | |
public static Date toDate(Object value, String format) throws ConversionException |
548 | |
{ |
549 | 42 | if (value instanceof Date) |
550 | |
{ |
551 | 15 | return (Date) value; |
552 | |
} |
553 | 27 | else if (value instanceof Calendar) |
554 | |
{ |
555 | 5 | return ((Calendar) value).getTime(); |
556 | |
} |
557 | 22 | else if (value instanceof String) |
558 | |
{ |
559 | |
try |
560 | |
{ |
561 | 20 | return new SimpleDateFormat(format).parse((String) value); |
562 | |
} |
563 | |
catch (ParseException e) |
564 | |
{ |
565 | 2 | throw new ConversionException("The value " + value + " can't be converted to a Date", e); |
566 | |
} |
567 | |
} |
568 | |
else |
569 | |
{ |
570 | 2 | throw new ConversionException("The value " + value + " can't be converted to a Date"); |
571 | |
} |
572 | |
} |
573 | |
|
574 | |
|
575 | |
|
576 | |
|
577 | |
|
578 | |
|
579 | |
|
580 | |
|
581 | |
|
582 | |
public static Calendar toCalendar(Object value, String format) throws ConversionException |
583 | |
{ |
584 | 40 | if (value instanceof Calendar) |
585 | |
{ |
586 | 10 | return (Calendar) value; |
587 | |
} |
588 | 30 | else if (value instanceof Date) |
589 | |
{ |
590 | 10 | Calendar calendar = Calendar.getInstance(); |
591 | 10 | calendar.setTime((Date) value); |
592 | 10 | return calendar; |
593 | |
} |
594 | 20 | else if (value instanceof String) |
595 | |
{ |
596 | |
try |
597 | |
{ |
598 | 18 | Calendar calendar = Calendar.getInstance(); |
599 | 18 | calendar.setTime(new SimpleDateFormat(format).parse((String) value)); |
600 | 16 | return calendar; |
601 | |
} |
602 | |
catch (ParseException e) |
603 | |
{ |
604 | 2 | throw new ConversionException("The value " + value + " can't be converted to a Calendar", e); |
605 | |
} |
606 | |
} |
607 | |
else |
608 | |
{ |
609 | 2 | throw new ConversionException("The value " + value + " can't be converted to a Calendar"); |
610 | |
} |
611 | |
} |
612 | |
|
613 | |
|
614 | |
|
615 | |
|
616 | |
|
617 | |
|
618 | |
|
619 | |
|
620 | |
|
621 | |
|
622 | |
|
623 | |
|
624 | |
|
625 | |
|
626 | |
|
627 | |
|
628 | |
public static Iterator toIterator(Object value, char delimiter) |
629 | |
{ |
630 | 42069 | if (value == null) |
631 | |
{ |
632 | 3 | return IteratorUtils.emptyIterator(); |
633 | |
} |
634 | 42066 | if (value instanceof String) |
635 | |
{ |
636 | 28736 | String s = (String) value; |
637 | 28736 | if (s.indexOf(delimiter) > 0) |
638 | |
{ |
639 | 2057 | return split((String) value, delimiter).iterator(); |
640 | |
} |
641 | |
else |
642 | |
{ |
643 | 26679 | return new SingletonIterator(value); |
644 | |
} |
645 | |
} |
646 | 13330 | else if (value instanceof Collection) |
647 | |
{ |
648 | 698 | return toIterator(((Collection) value).iterator(), delimiter); |
649 | |
} |
650 | 12632 | else if (value.getClass().isArray()) |
651 | |
{ |
652 | 810 | return toIterator(IteratorUtils.arrayIterator(value), delimiter); |
653 | |
} |
654 | 11822 | else if (value instanceof Iterator) |
655 | |
{ |
656 | 1508 | Iterator iterator = (Iterator) value; |
657 | 1508 | IteratorChain chain = new IteratorChain(); |
658 | 6234 | while (iterator.hasNext()) |
659 | |
{ |
660 | 3218 | chain.addIterator(toIterator(iterator.next(), delimiter)); |
661 | |
} |
662 | 1508 | return chain; |
663 | |
} |
664 | |
else |
665 | |
{ |
666 | 10314 | return new SingletonIterator(value); |
667 | |
} |
668 | |
} |
669 | |
|
670 | |
|
671 | |
|
672 | |
|
673 | |
|
674 | |
|
675 | |
|
676 | |
|
677 | |
|
678 | |
|
679 | |
|
680 | |
public static Object interpolate(Object value, AbstractConfiguration config) |
681 | |
{ |
682 | 2139 | if (value instanceof String) |
683 | |
{ |
684 | 1424 | return interpolateHelper((String) value, null, config); |
685 | |
} |
686 | |
else |
687 | |
{ |
688 | 715 | return value; |
689 | |
} |
690 | |
} |
691 | |
|
692 | |
|
693 | |
|
694 | |
|
695 | |
|
696 | |
|
697 | |
|
698 | |
|
699 | |
|
700 | |
|
701 | |
|
702 | |
|
703 | |
|
704 | |
|
705 | |
private static String interpolateHelper(String base, List priorVariables, |
706 | |
AbstractConfiguration config) |
707 | |
{ |
708 | 1529 | if (base == null) |
709 | |
{ |
710 | 0 | return null; |
711 | |
} |
712 | |
|
713 | |
|
714 | |
|
715 | 1529 | if (priorVariables == null) |
716 | |
{ |
717 | 1424 | priorVariables = new ArrayList(); |
718 | 1424 | priorVariables.add(base); |
719 | |
} |
720 | |
|
721 | 1529 | int begin = -1; |
722 | 1529 | int end = -1; |
723 | 1529 | int prec = 0 - AbstractConfiguration.END_TOKEN.length(); |
724 | 1529 | StringBuffer result = new StringBuffer(); |
725 | |
|
726 | |
|
727 | 1529 | while (((begin = base.indexOf(AbstractConfiguration.START_TOKEN, prec |
728 | |
+ AbstractConfiguration.END_TOKEN.length())) > -1) |
729 | 1637 | && ((end = base.indexOf(AbstractConfiguration.END_TOKEN, begin)) > -1)) |
730 | |
{ |
731 | 117 | result.append(base.substring(prec |
732 | |
+ AbstractConfiguration.END_TOKEN.length(), begin)); |
733 | 117 | String variable = base.substring(begin |
734 | |
+ AbstractConfiguration.START_TOKEN.length(), end); |
735 | |
|
736 | |
|
737 | 117 | if (priorVariables.contains(variable)) |
738 | |
{ |
739 | 3 | String initialBase = priorVariables.remove(0).toString(); |
740 | 3 | priorVariables.add(variable); |
741 | 3 | StringBuffer priorVariableSb = new StringBuffer(); |
742 | |
|
743 | |
|
744 | |
|
745 | 15 | for (Iterator it = priorVariables.iterator(); it.hasNext();) |
746 | |
{ |
747 | 9 | priorVariableSb.append(it.next()); |
748 | 9 | if (it.hasNext()) |
749 | |
{ |
750 | 6 | priorVariableSb.append("->"); |
751 | |
} |
752 | |
} |
753 | |
|
754 | 3 | throw new IllegalStateException( |
755 | |
"infinite loop in property interpolation of " |
756 | |
+ initialBase + ": " |
757 | |
+ priorVariableSb.toString()); |
758 | |
} |
759 | |
|
760 | |
else |
761 | |
{ |
762 | 114 | priorVariables.add(variable); |
763 | |
} |
764 | |
|
765 | 114 | Object value = config.resolveContainerStore(variable); |
766 | 114 | if (value != null) |
767 | |
{ |
768 | 105 | result.append(interpolateHelper(value.toString(), |
769 | |
priorVariables, config)); |
770 | |
|
771 | |
|
772 | |
|
773 | |
|
774 | |
|
775 | 99 | priorVariables.remove(priorVariables.size() - 1); |
776 | |
} |
777 | |
else |
778 | |
{ |
779 | |
|
780 | 9 | result.append(AbstractConfiguration.START_TOKEN); |
781 | 9 | result.append(variable); |
782 | 9 | result.append(AbstractConfiguration.END_TOKEN); |
783 | |
} |
784 | |
|
785 | 108 | prec = end; |
786 | |
} |
787 | 1520 | result.append(base.substring(prec |
788 | |
+ AbstractConfiguration.END_TOKEN.length(), base.length())); |
789 | 1520 | return result.toString(); |
790 | |
} |
791 | |
} |