1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.commons.configuration; |
18 |
|
|
19 |
|
import java.awt.Color; |
20 |
|
import java.math.BigDecimal; |
21 |
|
import java.math.BigInteger; |
22 |
|
import java.net.MalformedURLException; |
23 |
|
import java.net.URL; |
24 |
|
import java.text.ParseException; |
25 |
|
import java.text.SimpleDateFormat; |
26 |
|
import java.util.ArrayList; |
27 |
|
import java.util.Calendar; |
28 |
|
import java.util.Collection; |
29 |
|
import java.util.Date; |
30 |
|
import java.util.Iterator; |
31 |
|
import java.util.List; |
32 |
|
import java.util.Locale; |
33 |
|
|
34 |
|
import org.apache.commons.collections.IteratorUtils; |
35 |
|
import org.apache.commons.collections.iterators.IteratorChain; |
36 |
|
import org.apache.commons.collections.iterators.SingletonIterator; |
37 |
|
import org.apache.commons.lang.BooleanUtils; |
38 |
|
import org.apache.commons.lang.StringUtils; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
0 |
public final class PropertyConverter |
48 |
|
{ |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
public static Boolean toBoolean(Object value) throws ConversionException |
56 |
|
{ |
57 |
1630 |
if (value instanceof Boolean) |
58 |
|
{ |
59 |
627 |
return (Boolean) value; |
60 |
|
} |
61 |
1003 |
else if (value instanceof String) |
62 |
|
{ |
63 |
957 |
Boolean b = BooleanUtils.toBooleanObject((String) value); |
64 |
957 |
if (b == null) |
65 |
|
{ |
66 |
92 |
throw new ConversionException("The value " + value + " can't be converted to a Boolean object"); |
67 |
|
} |
68 |
865 |
return b; |
69 |
|
} |
70 |
|
else |
71 |
|
{ |
72 |
46 |
throw new ConversionException("The value " + value + " can't be converted to a Boolean object"); |
73 |
|
} |
74 |
|
} |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
public static Byte toByte(Object value) throws ConversionException |
83 |
|
{ |
84 |
1064 |
if (value instanceof Byte) |
85 |
|
{ |
86 |
420 |
return (Byte) value; |
87 |
|
} |
88 |
644 |
else if (value instanceof String) |
89 |
|
{ |
90 |
|
try |
91 |
|
{ |
92 |
598 |
String string = (String) value; |
93 |
598 |
if (string.startsWith("0x")) |
94 |
|
{ |
95 |
23 |
return new Byte((byte) Integer.parseInt(string.substring(2), 16)); |
96 |
|
} |
97 |
|
else |
98 |
|
{ |
99 |
575 |
return new Byte(string); |
100 |
|
} |
101 |
|
} |
102 |
92 |
catch (NumberFormatException e) |
103 |
|
{ |
104 |
92 |
throw new ConversionException("The value " + value + " can't be converted to a Byte object", e); |
105 |
|
} |
106 |
|
} |
107 |
|
else |
108 |
|
{ |
109 |
46 |
throw new ConversionException("The value " + value + " can't be converted to a Byte object"); |
110 |
|
} |
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
public static Short toShort(Object value) throws ConversionException |
120 |
|
{ |
121 |
1179 |
if (value instanceof Short) |
122 |
|
{ |
123 |
420 |
return (Short) value; |
124 |
|
} |
125 |
759 |
else if (value instanceof String) |
126 |
|
{ |
127 |
|
try |
128 |
|
{ |
129 |
713 |
String string = (String) value; |
130 |
713 |
if (string.startsWith("0x")) |
131 |
|
{ |
132 |
23 |
return new Short((short) Integer.parseInt(string.substring(2), 16)); |
133 |
|
} |
134 |
|
else |
135 |
|
{ |
136 |
690 |
return new Short(string); |
137 |
|
} |
138 |
|
|
139 |
|
} |
140 |
92 |
catch (NumberFormatException e) |
141 |
|
{ |
142 |
92 |
throw new ConversionException("The value " + value + " can't be converted to a Short object", e); |
143 |
|
} |
144 |
|
} |
145 |
|
else |
146 |
|
{ |
147 |
46 |
throw new ConversionException("The value " + value + " can't be converted to a Short object"); |
148 |
|
} |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
public static Integer toInteger(Object value) throws ConversionException |
158 |
|
{ |
159 |
1133 |
if (value instanceof Integer) |
160 |
|
{ |
161 |
466 |
return (Integer) value; |
162 |
|
} |
163 |
667 |
else if (value instanceof String) |
164 |
|
{ |
165 |
|
try |
166 |
|
{ |
167 |
621 |
String string = (String) value; |
168 |
621 |
if (string.startsWith("0x")) |
169 |
|
{ |
170 |
23 |
return new Integer((int) Long.parseLong(string.substring(2), 16)); |
171 |
|
} |
172 |
|
else |
173 |
|
{ |
174 |
598 |
return new Integer(string); |
175 |
|
} |
176 |
|
} |
177 |
46 |
catch (NumberFormatException e) |
178 |
|
{ |
179 |
46 |
throw new ConversionException("The value " + value + " can't be converted to an Integer object", e); |
180 |
|
} |
181 |
|
} |
182 |
|
else |
183 |
|
{ |
184 |
46 |
throw new ConversionException("The value " + value + " can't be converted to an Integer object"); |
185 |
|
} |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
public static Long toLong(Object value) throws ConversionException |
195 |
|
{ |
196 |
1064 |
if (value instanceof Long) |
197 |
|
{ |
198 |
420 |
return (Long) value; |
199 |
|
} |
200 |
644 |
else if (value instanceof String) |
201 |
|
{ |
202 |
|
try |
203 |
|
{ |
204 |
598 |
String string = (String) value; |
205 |
598 |
if (string.startsWith("0x")) |
206 |
|
{ |
207 |
23 |
return new Long(class="keyword">new BigInteger(string.substring(2), 16).longValue()); |
208 |
|
} |
209 |
|
else |
210 |
|
{ |
211 |
575 |
return new Long(string); |
212 |
|
} |
213 |
|
} |
214 |
92 |
catch (NumberFormatException e) |
215 |
|
{ |
216 |
92 |
throw new ConversionException("The value " + value + " can't be converted to a Long object", e); |
217 |
|
} |
218 |
|
} |
219 |
|
else |
220 |
|
{ |
221 |
46 |
throw new ConversionException("The value " + value + " can't be converted to a Long object"); |
222 |
|
} |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
public static Float toFloat(Object value) throws ConversionException |
232 |
|
{ |
233 |
1041 |
if (value instanceof Float) |
234 |
|
{ |
235 |
420 |
return (Float) value; |
236 |
|
} |
237 |
621 |
else if (value instanceof String) |
238 |
|
{ |
239 |
|
try |
240 |
|
{ |
241 |
575 |
return new Float((String) value); |
242 |
|
} |
243 |
92 |
catch (NumberFormatException e) |
244 |
|
{ |
245 |
92 |
throw new ConversionException("The value " + value + " can't be converted to a Float object", e); |
246 |
|
} |
247 |
|
} |
248 |
|
else |
249 |
|
{ |
250 |
46 |
throw new ConversionException("The value " + value + " can't be converted to a Float object"); |
251 |
|
} |
252 |
|
} |
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
public static Double toDouble(Object value) throws ConversionException |
261 |
|
{ |
262 |
1041 |
if (value instanceof Double) |
263 |
|
{ |
264 |
420 |
return (Double) value; |
265 |
|
} |
266 |
621 |
else if (value instanceof String) |
267 |
|
{ |
268 |
|
try |
269 |
|
{ |
270 |
575 |
return new Double((String) value); |
271 |
|
} |
272 |
92 |
catch (NumberFormatException e) |
273 |
|
{ |
274 |
92 |
throw new ConversionException("The value " + value + " can't be converted to a Double object", e); |
275 |
|
} |
276 |
|
} |
277 |
|
else |
278 |
|
{ |
279 |
46 |
throw new ConversionException("The value " + value + " can't be converted to a Double object"); |
280 |
|
} |
281 |
|
} |
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
|
287 |
|
|
288 |
|
|
289 |
|
public static BigInteger toBigInteger(Object value) throws ConversionException |
290 |
|
{ |
291 |
805 |
if (value instanceof BigInteger) |
292 |
|
{ |
293 |
322 |
return (BigInteger) value; |
294 |
|
} |
295 |
483 |
else if (value instanceof String) |
296 |
|
{ |
297 |
|
try |
298 |
|
{ |
299 |
437 |
String string = (String) value; |
300 |
437 |
if (string.startsWith("0x")) |
301 |
|
{ |
302 |
23 |
return new BigInteger(string.substring(2), 16); |
303 |
|
} |
304 |
|
else |
305 |
|
{ |
306 |
414 |
return new BigInteger(string); |
307 |
|
} |
308 |
|
} |
309 |
92 |
catch (NumberFormatException e) |
310 |
|
{ |
311 |
92 |
throw new ConversionException("The value " + value + " can't be converted to a BigInteger object", e); |
312 |
|
} |
313 |
|
} |
314 |
|
else |
315 |
|
{ |
316 |
46 |
throw new ConversionException("The value " + value + " can't be converted to a BigInteger object"); |
317 |
|
} |
318 |
|
} |
319 |
|
|
320 |
|
|
321 |
|
|
322 |
|
|
323 |
|
|
324 |
|
|
325 |
|
|
326 |
|
public static BigDecimal toBigDecimal(Object value) throws ConversionException |
327 |
|
{ |
328 |
782 |
if (value instanceof BigDecimal) |
329 |
|
{ |
330 |
322 |
return (BigDecimal) value; |
331 |
|
} |
332 |
460 |
else if (value instanceof String) |
333 |
|
{ |
334 |
|
try |
335 |
|
{ |
336 |
414 |
return new BigDecimal((String) value); |
337 |
|
} |
338 |
92 |
catch (NumberFormatException e) |
339 |
|
{ |
340 |
92 |
throw new ConversionException("The value " + value + " can't be converted to a BigDecimal object", e); |
341 |
|
} |
342 |
|
} |
343 |
|
else |
344 |
|
{ |
345 |
46 |
throw new ConversionException("The value " + value + " can't be converted to a BigDecimal object"); |
346 |
|
} |
347 |
|
} |
348 |
|
|
349 |
|
|
350 |
|
|
351 |
|
|
352 |
|
|
353 |
|
|
354 |
|
|
355 |
|
public static URL toURL(Object value) throws ConversionException |
356 |
|
{ |
357 |
689 |
if (value instanceof URL) |
358 |
|
{ |
359 |
344 |
return (URL) value; |
360 |
|
} |
361 |
345 |
else if (value instanceof String) |
362 |
|
{ |
363 |
|
try |
364 |
|
{ |
365 |
299 |
return new URL((String) value); |
366 |
|
} |
367 |
46 |
catch (MalformedURLException e) |
368 |
|
{ |
369 |
46 |
throw new ConversionException("The value " + value + " can't be converted to an URL", e); |
370 |
|
} |
371 |
|
} |
372 |
|
else |
373 |
|
{ |
374 |
46 |
throw new ConversionException("The value " + value + " can't be converted to an URL"); |
375 |
|
} |
376 |
|
} |
377 |
|
|
378 |
|
|
379 |
|
|
380 |
|
|
381 |
|
|
382 |
|
|
383 |
|
|
384 |
|
public static Locale toLocale(Object value) throws ConversionException |
385 |
|
{ |
386 |
805 |
if (value instanceof Locale) |
387 |
|
{ |
388 |
322 |
return (Locale) value; |
389 |
|
} |
390 |
483 |
else if (value instanceof String) |
391 |
|
{ |
392 |
437 |
List elements = split((String) value, '_'); |
393 |
437 |
int size = elements.size(); |
394 |
|
|
395 |
437 |
if (size >= 1 && (((String) elements.get(0)).length() == 2 || ((String) elements.get(0)).length() == 0)) |
396 |
|
{ |
397 |
391 |
String language = (String) elements.get(0); |
398 |
391 |
String country = (String) ((size >= 2) ? elements.get(1) : ""); |
399 |
391 |
String variant = (String) ((size >= 3) ? elements.get(2) : ""); |
400 |
|
|
401 |
391 |
return new Locale(language, country, variant); |
402 |
|
} |
403 |
|
else |
404 |
|
{ |
405 |
46 |
throw new ConversionException("The value " + value + " can't be converted to a Locale"); |
406 |
|
} |
407 |
|
} |
408 |
|
else |
409 |
|
{ |
410 |
46 |
throw new ConversionException("The value " + value + " can't be converted to a Locale"); |
411 |
|
} |
412 |
|
} |
413 |
|
|
414 |
|
|
415 |
|
|
416 |
|
|
417 |
|
|
418 |
|
|
419 |
|
|
420 |
|
|
421 |
|
|
422 |
|
|
423 |
|
public static List split(String s, char delimiter) |
424 |
|
{ |
425 |
42899 |
if (s == null) |
426 |
|
{ |
427 |
135 |
return new ArrayList(); |
428 |
|
} |
429 |
|
|
430 |
42764 |
List list = new ArrayList(); |
431 |
|
|
432 |
42764 |
StringBuffer token = new StringBuffer(); |
433 |
42764 |
int begin = 0; |
434 |
42764 |
int end = 0; |
435 |
109765 |
while (begin <= s.length()) |
436 |
|
{ |
437 |
|
|
438 |
67001 |
int index = s.indexOf(delimiter, end); |
439 |
|
|
440 |
|
|
441 |
67001 |
end = (index != -1) ? index : s.length(); |
442 |
|
|
443 |
|
|
444 |
67001 |
String chunk = s.substring(begin , end); |
445 |
|
|
446 |
67001 |
if (chunk.endsWith("\\") && end != s.length()) |
447 |
|
{ |
448 |
3156 |
token.append(chunk.substring(0, chunk.length() - 1)); |
449 |
3156 |
token.append(delimiter); |
450 |
|
} |
451 |
|
else |
452 |
|
{ |
453 |
|
|
454 |
63845 |
token.append(chunk); |
455 |
|
|
456 |
|
|
457 |
63845 |
list.add(token.toString().trim()); |
458 |
|
|
459 |
|
|
460 |
63845 |
token = new StringBuffer(); |
461 |
|
} |
462 |
|
|
463 |
|
|
464 |
67001 |
end = end + 1; |
465 |
67001 |
begin = end; |
466 |
|
} |
467 |
|
|
468 |
42764 |
return list; |
469 |
|
} |
470 |
|
|
471 |
|
|
472 |
|
|
473 |
|
|
474 |
|
|
475 |
|
|
476 |
|
|
477 |
|
|
478 |
|
|
479 |
|
|
480 |
|
|
481 |
|
|
482 |
|
|
483 |
|
|
484 |
|
public static Color toColor(Object value) throws ConversionException |
485 |
|
{ |
486 |
713 |
if (value instanceof Color) |
487 |
|
{ |
488 |
322 |
return (Color) value; |
489 |
|
} |
490 |
391 |
else if (value instanceof String && !StringUtils.isBlank((String) value) && ((String) value).length() >= 6) |
491 |
|
{ |
492 |
|
try |
493 |
|
{ |
494 |
345 |
String color = ((String) value).trim(); |
495 |
|
|
496 |
|
|
497 |
345 |
if (color.startsWith("#")) |
498 |
|
{ |
499 |
46 |
color = color.substring(1); |
500 |
|
} |
501 |
|
|
502 |
345 |
int red = Integer.parseInt(color.substring(0, 2), 16); |
503 |
299 |
int green = Integer.parseInt(color.substring(2, 4), 16); |
504 |
299 |
int blue = Integer.parseInt(color.substring(4, 6), 16); |
505 |
299 |
int alpha = 255; |
506 |
|
|
507 |
|
|
508 |
299 |
if (color.length() >= 8) |
509 |
|
{ |
510 |
23 |
alpha = Integer.parseInt(color.substring(6, 8), 16); |
511 |
|
} |
512 |
|
|
513 |
299 |
return new Color(red, green, blue, alpha); |
514 |
|
} |
515 |
46 |
catch (Exception e) |
516 |
|
{ |
517 |
46 |
throw new ConversionException("The value " + value + " can't be converted to a Color", e); |
518 |
|
} |
519 |
|
} |
520 |
|
else |
521 |
|
{ |
522 |
46 |
throw new ConversionException("The value " + value + " can't be converted to a Color"); |
523 |
|
} |
524 |
|
} |
525 |
|
|
526 |
|
|
527 |
|
|
528 |
|
|
529 |
|
|
530 |
|
|
531 |
|
|
532 |
|
|
533 |
|
public static Date toDate(Object value, String format) throws ConversionException |
534 |
|
{ |
535 |
805 |
if (value instanceof Date) |
536 |
|
{ |
537 |
345 |
return (Date) value; |
538 |
|
} |
539 |
460 |
else if (value instanceof Calendar) |
540 |
|
{ |
541 |
115 |
return ((Calendar) value).getTime(); |
542 |
|
} |
543 |
345 |
else if (value instanceof String) |
544 |
|
{ |
545 |
|
try |
546 |
|
{ |
547 |
299 |
return new SimpleDateFormat(format).parse((String) value); |
548 |
|
} |
549 |
46 |
catch (ParseException e) |
550 |
|
{ |
551 |
46 |
throw new ConversionException("The value " + value + " can't be converted to a Date", e); |
552 |
|
} |
553 |
|
} |
554 |
|
else |
555 |
|
{ |
556 |
46 |
throw new ConversionException("The value " + value + " can't be converted to a Date"); |
557 |
|
} |
558 |
|
} |
559 |
|
|
560 |
|
|
561 |
|
|
562 |
|
|
563 |
|
|
564 |
|
|
565 |
|
|
566 |
|
|
567 |
|
public static Calendar toCalendar(Object value, String format) throws ConversionException |
568 |
|
{ |
569 |
805 |
if (value instanceof Calendar) |
570 |
|
{ |
571 |
230 |
return (Calendar) value; |
572 |
|
} |
573 |
575 |
else if (value instanceof Date) |
574 |
|
{ |
575 |
230 |
Calendar calendar = Calendar.getInstance(); |
576 |
230 |
calendar.setTime((Date) value); |
577 |
230 |
return calendar; |
578 |
|
} |
579 |
345 |
else if (value instanceof String) |
580 |
|
{ |
581 |
|
try |
582 |
|
{ |
583 |
299 |
Calendar calendar = Calendar.getInstance(); |
584 |
299 |
calendar.setTime(new SimpleDateFormat(format).parse((String) value)); |
585 |
253 |
return calendar; |
586 |
|
} |
587 |
46 |
catch (ParseException e) |
588 |
|
{ |
589 |
46 |
throw new ConversionException("The value " + value + " can't be converted to a Calendar", e); |
590 |
|
} |
591 |
|
} |
592 |
|
else |
593 |
|
{ |
594 |
46 |
throw new ConversionException("The value " + value + " can't be converted to a Calendar"); |
595 |
|
} |
596 |
|
} |
597 |
|
|
598 |
|
|
599 |
|
|
600 |
|
|
601 |
|
|
602 |
|
|
603 |
|
|
604 |
|
|
605 |
|
|
606 |
|
|
607 |
|
|
608 |
|
|
609 |
|
|
610 |
|
|
611 |
|
|
612 |
|
public static Iterator toIterator(Object value, char delimiter) |
613 |
|
{ |
614 |
571025 |
if (value == null) |
615 |
|
{ |
616 |
69 |
return IteratorUtils.emptyIterator(); |
617 |
|
} |
618 |
570956 |
if (value instanceof String) |
619 |
|
{ |
620 |
279845 |
String s = (String) value; |
621 |
279845 |
if (s.indexOf(delimiter) > 0) |
622 |
|
{ |
623 |
20444 |
return split((String) value, delimiter).iterator(); |
624 |
|
} |
625 |
|
else |
626 |
|
{ |
627 |
259401 |
return new SingletonIterator(value); |
628 |
|
} |
629 |
|
} |
630 |
291111 |
else if (value instanceof Collection) |
631 |
|
{ |
632 |
11983 |
return toIterator(((Collection) value).iterator(), delimiter); |
633 |
|
} |
634 |
279128 |
else if (value.getClass().isArray()) |
635 |
|
{ |
636 |
18101 |
return toIterator(IteratorUtils.arrayIterator(value), delimiter); |
637 |
|
} |
638 |
261027 |
else if (value instanceof Iterator) |
639 |
|
{ |
640 |
30084 |
Iterator iterator = (Iterator) value; |
641 |
30084 |
IteratorChain chain = new IteratorChain(); |
642 |
93472 |
while (iterator.hasNext()) |
643 |
|
{ |
644 |
63388 |
chain.addIterator(toIterator(iterator.next(), delimiter)); |
645 |
|
} |
646 |
30084 |
return chain; |
647 |
|
} |
648 |
|
else |
649 |
|
{ |
650 |
230943 |
return new SingletonIterator(value); |
651 |
|
} |
652 |
|
} |
653 |
|
|
654 |
|
} |