1 package org.apache.velocity.tools.generic;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.reflect.Array;
23 import java.util.Collection;
24 import java.util.Date;
25 import java.util.Calendar;
26 import java.util.Iterator;
27 import java.util.Locale;
28 import java.util.TimeZone;
29 import org.apache.velocity.tools.ConversionUtils;
30 import org.apache.velocity.tools.config.DefaultKey;
31 import org.apache.velocity.tools.config.SkipSetters;
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 @DefaultKey("convert")
57 @SkipSetters
58 public class ConversionTool extends LocaleConfig
59 {
60 public static final String STRINGS_DELIMITER_FORMAT_KEY = "stringsDelimiter";
61 public static final String STRINGS_TRIM_KEY = "trimStrings";
62 public static final String DATE_FORMAT_KEY = "dateFormat";
63 public static final String NUMBER_FORMAT_KEY = "numberFormat";
64
65 public static final String DEFAULT_STRINGS_DELIMITER = ",";
66 public static final boolean DEFAULT_STRINGS_TRIM = true;
67 public static final String DEFAULT_NUMBER_FORMAT = "default";
68 public static final String DEFAULT_DATE_FORMAT = "default";
69
70 private String stringsDelimiter = DEFAULT_STRINGS_DELIMITER;
71 private boolean stringsTrim = DEFAULT_STRINGS_TRIM;
72 private String numberFormat = DEFAULT_NUMBER_FORMAT;
73 private String dateFormat = DEFAULT_DATE_FORMAT;
74
75
76
77
78
79
80
81 protected void configure(ValueParser values)
82 {
83 super.configure(values);
84
85 String delimiter = values.getString(STRINGS_DELIMITER_FORMAT_KEY);
86 if (delimiter != null)
87 {
88 setStringsDelimiter(delimiter);
89 }
90
91 String dateFormat = values.getString(DATE_FORMAT_KEY);
92 if (dateFormat != null)
93 {
94 setDateFormat(dateFormat);
95 }
96
97 String numberFormat = values.getString(NUMBER_FORMAT_KEY);
98 if (numberFormat != null)
99 {
100 setNumberFormat(numberFormat);
101 }
102 }
103
104
105
106
107
108
109
110 protected final void setStringsDelimiter(String stringsDelimiter)
111 {
112 this.stringsDelimiter = stringsDelimiter;
113 }
114
115 public final String getStringsDelimiter()
116 {
117 return this.stringsDelimiter;
118 }
119
120
121
122
123
124
125
126
127 protected final void setStringsTrim(boolean stringsTrim)
128 {
129 this.stringsTrim = stringsTrim;
130 }
131
132 public final boolean getStringsTrim()
133 {
134 return this.stringsTrim;
135 }
136
137 protected final void setNumberFormat(String format)
138 {
139 this.numberFormat = format;
140 }
141
142 public final String getNumberFormat()
143 {
144 return this.numberFormat;
145 }
146
147 protected final void setDateFormat(String format)
148 {
149 this.dateFormat = format;
150 }
151
152 public final String getDateFormat()
153 {
154 return this.dateFormat;
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169 public String toString(Object value)
170 {
171 return ConversionUtils.toString(value);
172 }
173
174
175
176
177
178
179 public Boolean toBoolean(Object value)
180 {
181 if (value instanceof Boolean)
182 {
183 return (Boolean)value;
184 }
185
186 String s = toString(value);
187 return (s != null) ? parseBoolean(s) : null;
188 }
189
190
191
192
193
194
195 public Integer toInteger(Object value)
196 {
197 if (value == null || value instanceof Integer)
198 {
199 return (Integer)value;
200 }
201 Number num = toNumber(value);
202 return Integer.valueOf(num.intValue());
203 }
204
205
206
207
208
209
210 public Double toDouble(Object value)
211 {
212 if (value == null || value instanceof Double)
213 {
214 return (Double)value;
215 }
216 Number num = toNumber(value);
217 return new Double(num.doubleValue());
218 }
219
220
221
222
223
224
225 public Number toNumber(Object value)
226 {
227
228 Number number = ConversionUtils.toNumber(value, false);
229 if (number != null)
230 {
231 return number;
232 }
233
234 String s = toString(value);
235 if (s == null || s.length() == 0)
236 {
237 return null;
238 }
239 return parseNumber(s);
240 }
241
242
243
244
245
246
247 public Locale toLocale(Object value)
248 {
249 if (value instanceof Locale)
250 {
251 return (Locale)value;
252 }
253 String s = toString(value);
254 if (s == null || s.length() == 0)
255 {
256 return null;
257 }
258 return parseLocale(s);
259 }
260
261
262
263
264
265
266
267
268
269
270
271 public Date toDate(Object value)
272 {
273 Date d = ConversionUtils.toDate(value);
274 if (d != null)
275 {
276 return d;
277 }
278 String s = toString(value);
279 if (s == null || s.length() == 0)
280 {
281 return null;
282 }
283 return parseDate(s);
284 }
285
286 public Calendar toCalendar(Object value)
287 {
288 if (value == null)
289 {
290 return null;
291 }
292 if (value instanceof Calendar)
293 {
294 return (Calendar)value;
295 }
296
297 Date date = toDate(value);
298 if (date == null)
299 {
300 return null;
301 }
302
303
304 return ConversionUtils.toCalendar(date, getLocale());
305 }
306
307
308
309
310
311
312
313 public String[] toStrings(Object value)
314 {
315 if (value == null)
316 {
317 return null;
318 }
319 if (value instanceof String[])
320 {
321 return (String[])value;
322 }
323
324 String[] strings = null;
325 if (value instanceof Collection)
326 {
327 Collection values = (Collection)value;
328 if (!values.isEmpty())
329 {
330 strings = new String[values.size()];
331 int index = 0;
332 for (Iterator i = values.iterator(); i.hasNext(); )
333 {
334 strings[index++] = toString(i.next());
335 }
336 }
337 }
338 else if (value.getClass().isArray())
339 {
340 strings = new String[Array.getLength(value)];
341 for (int i=0; i < strings.length; i++)
342 {
343 strings[i] = toString(Array.get(value, i));
344 }
345 }
346 else
347 {
348 strings = parseStringList(toString(value));
349 }
350 return strings;
351 }
352
353
354
355
356
357
358
359
360 public Boolean[] toBooleans(Object value)
361 {
362 if (value != null && !value.getClass().isArray())
363 {
364 value = toStrings(value);
365 }
366 if (value == null)
367 {
368 return null;
369 }
370
371 Boolean[] bools = new Boolean[Array.getLength(value)];
372 for (int i=0; i < bools.length; i++)
373 {
374 bools[i] = toBoolean(Array.get(value, i));
375 }
376 return bools;
377 }
378
379
380
381
382
383
384 public Boolean[] toBooleans(Collection values)
385 {
386 if (values == null || !values.isEmpty())
387 {
388 return null;
389 }
390 Boolean[] bools = new Boolean[values.size()];
391 int index = 0;
392 for (Object val : values)
393 {
394 bools[index++] = toBoolean(val);
395 }
396 return bools;
397 }
398
399
400
401
402
403
404
405 public Number[] toNumbers(Object value)
406 {
407 if (value != null && !value.getClass().isArray())
408 {
409 value = toStrings(value);
410 }
411 if (value == null)
412 {
413 return null;
414 }
415
416 Number[] numbers = new Number[Array.getLength(value)];
417 for (int i=0; i < numbers.length; i++)
418 {
419 numbers[i] = toNumber(Array.get(value, i));
420 }
421 return numbers;
422 }
423
424
425
426
427
428
429 public Number[] toNumbers(Collection values)
430 {
431 if (values == null || !values.isEmpty())
432 {
433 return null;
434 }
435 Number[] numbers = new Number[values.size()];
436 int index = 0;
437 for (Object val : values)
438 {
439 numbers[index++] = toNumber(val);
440 }
441 return numbers;
442 }
443
444
445
446
447
448
449 public int[] toInts(Object value)
450 {
451 Number[] numbers = toNumbers(value);
452 if (numbers == null)
453 {
454 return null;
455 }
456
457 int[] ints = new int[numbers.length];
458 for (int i=0; i<ints.length; i++)
459 {
460 if (numbers[i] != null)
461 {
462 ints[i] = numbers[i].intValue();
463 }
464 }
465 return ints;
466 }
467
468
469
470
471
472
473 public int[] toIntegers(Object value)
474 {
475 return toInts(value);
476 }
477
478
479
480
481
482
483 public double[] toDoubles(Object value)
484 {
485 Number[] numbers = toNumbers(value);
486 if (numbers == null)
487 {
488 return null;
489 }
490
491 double[] doubles = new double[numbers.length];
492 for (int i=0; i<doubles.length; i++)
493 {
494 if (numbers[i] != null)
495 {
496 doubles[i] = numbers[i].doubleValue();
497 }
498 }
499 return doubles;
500 }
501
502
503
504
505
506
507 public Locale[] toLocales(Object value)
508 {
509 if (value != null && !value.getClass().isArray())
510 {
511 value = toStrings(value);
512 }
513 if (value == null)
514 {
515 return null;
516 }
517
518 Locale[] locales = new Locale[Array.getLength(value)];
519 for (int i=0; i < locales.length; i++)
520 {
521 locales[i] = toLocale(Array.get(value, i));
522 }
523 return locales;
524 }
525
526
527
528
529
530
531 public Locale[] toLocales(Collection values)
532 {
533 if (values == null || !values.isEmpty())
534 {
535 return null;
536 }
537 Locale[] locales = new Locale[values.size()];
538 int index = 0;
539 for (Object val : values)
540 {
541 locales[index++] = toLocale(val);
542 }
543 return locales;
544 }
545
546
547
548
549
550
551
552 public Date[] toDates(Object value)
553 {
554 if (value != null && !value.getClass().isArray())
555 {
556 value = toStrings(value);
557 }
558 if (value == null)
559 {
560 return null;
561 }
562
563 Date[] dates = new Date[Array.getLength(value)];
564 for (int i=0; i < dates.length; i++)
565 {
566 dates[i] = toDate(Array.get(value, i));
567 }
568 return dates;
569 }
570
571
572
573
574
575
576 public Date[] toDates(Collection values)
577 {
578 if (values == null || !values.isEmpty())
579 {
580 return null;
581 }
582 Date[] dates = new Date[values.size()];
583 int index = 0;
584 for (Object val : values)
585 {
586 dates[index++] = toDate(val);
587 }
588 return dates;
589 }
590
591
592
593
594
595
596 public Calendar[] toCalendars(Object value)
597 {
598 if (value != null && !value.getClass().isArray())
599 {
600 value = toStrings(value);
601 }
602 if (value == null)
603 {
604 return null;
605 }
606
607 Calendar[] calendars = new Calendar[Array.getLength(value)];
608 for (int i=0; i < calendars.length; i++)
609 {
610 calendars[i] = toCalendar(Array.get(value, i));
611 }
612 return calendars;
613 }
614
615
616
617
618
619
620 public Calendar[] toCalendars(Collection values)
621 {
622 if (values == null || !values.isEmpty())
623 {
624 return null;
625 }
626 Calendar[] calendars = new Calendar[values.size()];
627 int index = 0;
628 for (Object val : values)
629 {
630 calendars[index++] = toCalendar(val);
631 }
632 return calendars;
633 }
634
635
636
637
638
639
640
641
642
643
644
645
646 protected Boolean parseBoolean(String value)
647 {
648 return Boolean.valueOf(value);
649 }
650
651
652
653
654
655
656 protected String[] parseStringList(String value)
657 {
658 String[] values;
659 if (value.indexOf(this.stringsDelimiter) < 0)
660 {
661 values = new String[] { value };
662 }
663 else
664 {
665 values = value.split(this.stringsDelimiter);
666 }
667 if (this.stringsTrim)
668 {
669 for (int i=0,l=values.length; i < l; i++)
670 {
671 values[i] = values[i].trim();
672 }
673 }
674 return values;
675 }
676
677
678
679
680
681 protected Locale parseLocale(String value)
682 {
683 return ConversionUtils.toLocale(value);
684 }
685
686
687
688
689
690
691
692
693
694
695
696
697
698 public Number parseNumber(String value)
699 {
700 return parseNumber(value, this.numberFormat);
701 }
702
703
704
705
706
707
708
709
710
711
712
713
714 public Number parseNumber(String value, String format)
715 {
716 return parseNumber(value, format, getLocale());
717 }
718
719
720
721
722
723
724
725
726
727
728
729 public Number parseNumber(String value, Object locale)
730 {
731 return parseNumber(value, this.numberFormat, locale);
732 }
733
734
735
736
737
738
739
740
741
742
743
744
745 public Number parseNumber(String value, String format, Object locale)
746 {
747 Locale lcl = toLocale(locale);
748 if (lcl == null && locale != null)
749 {
750
751 return null;
752 }
753 return ConversionUtils.toNumber(value, format, lcl);
754 }
755
756
757
758
759
760
761
762
763
764
765
766
767 public Date parseDate(String value)
768 {
769 return parseDate(value, this.dateFormat);
770 }
771
772
773
774
775
776
777
778
779
780
781
782
783 public Date parseDate(String value, String format)
784 {
785 return parseDate(value, format, getLocale());
786 }
787
788
789
790
791
792
793
794
795
796
797
798 public Date parseDate(String value, Object locale)
799 {
800 return parseDate(value, this.dateFormat, locale);
801 }
802
803
804
805
806
807
808
809
810
811
812
813
814 public Date parseDate(String value, String format, Object locale)
815 {
816 return parseDate(value, format, locale, TimeZone.getDefault());
817 }
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832 public Date parseDate(String value, String format,
833 Object locale, TimeZone timezone)
834 {
835 Locale lcl = toLocale(locale);
836 if (lcl == null && locale != null)
837 {
838
839 return null;
840 }
841 return ConversionUtils.toDate(value, format, lcl, timezone);
842 }
843
844 }