1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.beanutils;
20
21
22 import java.io.File;
23 import java.lang.reflect.Array;
24 import java.math.BigDecimal;
25 import java.math.BigInteger;
26 import java.net.URL;
27 import java.sql.Timestamp;
28 import java.util.Calendar;
29 import java.util.Collection;
30
31 import org.apache.commons.beanutils.converters.ArrayConverter;
32 import org.apache.commons.beanutils.converters.BigDecimalConverter;
33 import org.apache.commons.beanutils.converters.BigIntegerConverter;
34 import org.apache.commons.beanutils.converters.BooleanConverter;
35 import org.apache.commons.beanutils.converters.ByteConverter;
36 import org.apache.commons.beanutils.converters.CalendarConverter;
37 import org.apache.commons.beanutils.converters.CharacterConverter;
38 import org.apache.commons.beanutils.converters.ClassConverter;
39 import org.apache.commons.beanutils.converters.ConverterFacade;
40 import org.apache.commons.beanutils.converters.DateConverter;
41 import org.apache.commons.beanutils.converters.DoubleConverter;
42 import org.apache.commons.beanutils.converters.FileConverter;
43 import org.apache.commons.beanutils.converters.FloatConverter;
44 import org.apache.commons.beanutils.converters.IntegerConverter;
45 import org.apache.commons.beanutils.converters.LongConverter;
46 import org.apache.commons.beanutils.converters.ShortConverter;
47 import org.apache.commons.beanutils.converters.SqlDateConverter;
48 import org.apache.commons.beanutils.converters.SqlTimeConverter;
49 import org.apache.commons.beanutils.converters.SqlTimestampConverter;
50 import org.apache.commons.beanutils.converters.StringConverter;
51 import org.apache.commons.beanutils.converters.URLConverter;
52 import org.apache.commons.logging.Log;
53 import org.apache.commons.logging.LogFactory;
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 public class ConvertUtilsBean {
128
129 private static final Integer ZERO = new Integer(0);
130 private static final Character SPACE = new Character(' ');
131
132
133
134
135
136
137 protected static ConvertUtilsBean getInstance() {
138 return BeanUtilsBean.getInstance().getConvertUtils();
139 }
140
141
142
143
144
145
146
147
148 private final WeakFastHashMap<Class<?>, Converter> converters =
149 new WeakFastHashMap<Class<?>, Converter>();
150
151
152
153
154 private final Log log = LogFactory.getLog(ConvertUtils.class);
155
156
157
158
159 public ConvertUtilsBean() {
160 converters.setFast(false);
161 deregister();
162 converters.setFast(true);
163 }
164
165
166
167
168
169
170
171
172 @Deprecated
173 private Boolean defaultBoolean = Boolean.FALSE;
174
175
176
177
178
179
180
181 @Deprecated
182 public boolean getDefaultBoolean() {
183 return (defaultBoolean.booleanValue());
184 }
185
186
187
188
189
190
191
192 @Deprecated
193 public void setDefaultBoolean(final boolean newDefaultBoolean) {
194 defaultBoolean = (newDefaultBoolean ? Boolean.TRUE : Boolean.FALSE);
195 register(new BooleanConverter(defaultBoolean), Boolean.TYPE);
196 register(new BooleanConverter(defaultBoolean), Boolean.class);
197 }
198
199
200
201
202
203
204
205 @Deprecated
206 private Byte defaultByte = new Byte((byte) 0);
207
208
209
210
211
212
213
214 @Deprecated
215 public byte getDefaultByte() {
216 return (defaultByte.byteValue());
217 }
218
219
220
221
222
223
224
225 @Deprecated
226 public void setDefaultByte(final byte newDefaultByte) {
227 defaultByte = new Byte(newDefaultByte);
228 register(new ByteConverter(defaultByte), Byte.TYPE);
229 register(new ByteConverter(defaultByte), Byte.class);
230 }
231
232
233
234
235
236
237
238 @Deprecated
239 private Character defaultCharacter = new Character(' ');
240
241
242
243
244
245
246
247 @Deprecated
248 public char getDefaultCharacter() {
249 return (defaultCharacter.charValue());
250 }
251
252
253
254
255
256
257
258 @Deprecated
259 public void setDefaultCharacter(final char newDefaultCharacter) {
260 defaultCharacter = new Character(newDefaultCharacter);
261 register(new CharacterConverter(defaultCharacter),
262 Character.TYPE);
263 register(new CharacterConverter(defaultCharacter),
264 Character.class);
265 }
266
267
268
269
270
271
272
273 @Deprecated
274 private Double defaultDouble = new Double(0.0);
275
276
277
278
279
280
281
282 @Deprecated
283 public double getDefaultDouble() {
284 return (defaultDouble.doubleValue());
285 }
286
287
288
289
290
291
292
293 @Deprecated
294 public void setDefaultDouble(final double newDefaultDouble) {
295 defaultDouble = new Double(newDefaultDouble);
296 register(new DoubleConverter(defaultDouble), Double.TYPE);
297 register(new DoubleConverter(defaultDouble), Double.class);
298 }
299
300
301
302
303
304
305
306 @Deprecated
307 private Float defaultFloat = new Float((float) 0.0);
308
309
310
311
312
313
314
315 @Deprecated
316 public float getDefaultFloat() {
317 return (defaultFloat.floatValue());
318 }
319
320
321
322
323
324
325
326 @Deprecated
327 public void setDefaultFloat(final float newDefaultFloat) {
328 defaultFloat = new Float(newDefaultFloat);
329 register(new FloatConverter(defaultFloat), Float.TYPE);
330 register(new FloatConverter(defaultFloat), Float.class);
331 }
332
333
334
335
336
337
338
339 @Deprecated
340 private Integer defaultInteger = new Integer(0);
341
342
343
344
345
346
347
348 @Deprecated
349 public int getDefaultInteger() {
350 return (defaultInteger.intValue());
351 }
352
353
354
355
356
357
358
359 @Deprecated
360 public void setDefaultInteger(final int newDefaultInteger) {
361 defaultInteger = new Integer(newDefaultInteger);
362 register(new IntegerConverter(defaultInteger), Integer.TYPE);
363 register(new IntegerConverter(defaultInteger), Integer.class);
364 }
365
366
367
368
369
370
371
372 @Deprecated
373 private Long defaultLong = new Long(0);
374
375
376
377
378
379
380
381 @Deprecated
382 public long getDefaultLong() {
383 return (defaultLong.longValue());
384 }
385
386
387
388
389
390
391
392 @Deprecated
393 public void setDefaultLong(final long newDefaultLong) {
394 defaultLong = new Long(newDefaultLong);
395 register(new LongConverter(defaultLong), Long.TYPE);
396 register(new LongConverter(defaultLong), Long.class);
397 }
398
399
400
401
402
403
404
405 @Deprecated
406 private static Short defaultShort = new Short((short) 0);
407
408
409
410
411
412
413
414 @Deprecated
415 public short getDefaultShort() {
416 return (defaultShort.shortValue());
417 }
418
419
420
421
422
423
424
425 @Deprecated
426 public void setDefaultShort(final short newDefaultShort) {
427 defaultShort = new Short(newDefaultShort);
428 register(new ShortConverter(defaultShort), Short.TYPE);
429 register(new ShortConverter(defaultShort), Short.class);
430 }
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445 public String convert(Object value) {
446
447 if (value == null) {
448 return null;
449 } else if (value.getClass().isArray()) {
450 if (Array.getLength(value) < 1) {
451 return (null);
452 }
453 value = Array.get(value, 0);
454 if (value == null) {
455 return null;
456 } else {
457 final Converter converter = lookup(String.class);
458 return (converter.convert(String.class, value));
459 }
460 } else {
461 final Converter converter = lookup(String.class);
462 return (converter.convert(String.class, value));
463 }
464
465 }
466
467
468
469
470
471
472
473
474
475
476
477
478 public Object convert(final String value, final Class<?> clazz) {
479
480 if (log.isDebugEnabled()) {
481 log.debug("Convert string '" + value + "' to class '" +
482 clazz.getName() + "'");
483 }
484 Converter converter = lookup(clazz);
485 if (converter == null) {
486 converter = lookup(String.class);
487 }
488 if (log.isTraceEnabled()) {
489 log.trace(" Using converter " + converter);
490 }
491 return (converter.convert(clazz, value));
492
493 }
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509 public Object convert(final String[] values, final Class<?> clazz) {
510
511 Class<?> type = clazz;
512 if (clazz.isArray()) {
513 type = clazz.getComponentType();
514 }
515 if (log.isDebugEnabled()) {
516 log.debug("Convert String[" + values.length + "] to class '" +
517 type.getName() + "[]'");
518 }
519 Converter converter = lookup(type);
520 if (converter == null) {
521 converter = lookup(String.class);
522 }
523 if (log.isTraceEnabled()) {
524 log.trace(" Using converter " + converter);
525 }
526 final Object array = Array.newInstance(type, values.length);
527 for (int i = 0; i < values.length; i++) {
528 Array.set(array, i, converter.convert(type, values[i]));
529 }
530 return (array);
531
532 }
533
534
535
536
537
538
539
540
541
542
543
544
545
546 public Object convert(final Object value, final Class<?> targetType) {
547
548 final Class<?> sourceType = value == null ? null : value.getClass();
549
550 if (log.isDebugEnabled()) {
551 if (value == null) {
552 log.debug("Convert null value to type '" +
553 targetType.getName() + "'");
554 } else {
555 log.debug("Convert type '" + sourceType.getName() + "' value '" + value +
556 "' to type '" + targetType.getName() + "'");
557 }
558 }
559
560 Object converted = value;
561 Converter converter = lookup(sourceType, targetType);
562 if (converter != null) {
563 if (log.isTraceEnabled()) {
564 log.trace(" Using converter " + converter);
565 }
566 converted = converter.convert(targetType, value);
567 }
568 if (String.class.equals(targetType) && converted != null &&
569 !(converted instanceof String)) {
570
571
572
573
574 converter = lookup(String.class);
575 if (converter != null) {
576 if (log.isTraceEnabled()) {
577 log.trace(" Using converter " + converter);
578 }
579 converted = converter.convert(String.class, converted);
580 }
581
582
583 if (converted != null && !(converted instanceof String)) {
584 converted = converted.toString();
585 }
586
587 }
588 return converted;
589
590 }
591
592
593
594
595
596 public void deregister() {
597
598 converters.clear();
599
600 registerPrimitives(false);
601 registerStandard(false, false);
602 registerOther(true);
603 registerArrays(false, 0);
604 register(BigDecimal.class, new BigDecimalConverter());
605 register(BigInteger.class, new BigIntegerConverter());
606 }
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623 public void register(final boolean throwException, final boolean defaultNull, final int defaultArraySize) {
624 registerPrimitives(throwException);
625 registerStandard(throwException, defaultNull);
626 registerOther(throwException);
627 registerArrays(throwException, defaultArraySize);
628 }
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648 private void registerPrimitives(final boolean throwException) {
649 register(Boolean.TYPE, throwException ? new BooleanConverter() : new BooleanConverter(Boolean.FALSE));
650 register(Byte.TYPE, throwException ? new ByteConverter() : new ByteConverter(ZERO));
651 register(Character.TYPE, throwException ? new CharacterConverter() : new CharacterConverter(SPACE));
652 register(Double.TYPE, throwException ? new DoubleConverter() : new DoubleConverter(ZERO));
653 register(Float.TYPE, throwException ? new FloatConverter() : new FloatConverter(ZERO));
654 register(Integer.TYPE, throwException ? new IntegerConverter() : new IntegerConverter(ZERO));
655 register(Long.TYPE, throwException ? new LongConverter() : new LongConverter(ZERO));
656 register(Short.TYPE, throwException ? new ShortConverter() : new ShortConverter(ZERO));
657 }
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684 private void registerStandard(final boolean throwException, final boolean defaultNull) {
685
686 final Number defaultNumber = defaultNull ? null : ZERO;
687 final BigDecimal bigDecDeflt = defaultNull ? null : new BigDecimal("0.0");
688 final BigInteger bigIntDeflt = defaultNull ? null : new BigInteger("0");
689 final Boolean booleanDefault = defaultNull ? null : Boolean.FALSE;
690 final Character charDefault = defaultNull ? null : SPACE;
691 final String stringDefault = defaultNull ? null : "";
692
693 register(BigDecimal.class, throwException ? new BigDecimalConverter() : new BigDecimalConverter(bigDecDeflt));
694 register(BigInteger.class, throwException ? new BigIntegerConverter() : new BigIntegerConverter(bigIntDeflt));
695 register(Boolean.class, throwException ? new BooleanConverter() : new BooleanConverter(booleanDefault));
696 register(Byte.class, throwException ? new ByteConverter() : new ByteConverter(defaultNumber));
697 register(Character.class, throwException ? new CharacterConverter() : new CharacterConverter(charDefault));
698 register(Double.class, throwException ? new DoubleConverter() : new DoubleConverter(defaultNumber));
699 register(Float.class, throwException ? new FloatConverter() : new FloatConverter(defaultNumber));
700 register(Integer.class, throwException ? new IntegerConverter() : new IntegerConverter(defaultNumber));
701 register(Long.class, throwException ? new LongConverter() : new LongConverter(defaultNumber));
702 register(Short.class, throwException ? new ShortConverter() : new ShortConverter(defaultNumber));
703 register(String.class, throwException ? new StringConverter() : new StringConverter(stringDefault));
704
705 }
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725 private void registerOther(final boolean throwException) {
726 register(Class.class, throwException ? new ClassConverter() : new ClassConverter(null));
727 register(java.util.Date.class, throwException ? new DateConverter() : new DateConverter(null));
728 register(Calendar.class, throwException ? new CalendarConverter() : new CalendarConverter(null));
729 register(File.class, throwException ? new FileConverter() : new FileConverter(null));
730 register(java.sql.Date.class, throwException ? new SqlDateConverter() : new SqlDateConverter(null));
731 register(java.sql.Time.class, throwException ? new SqlTimeConverter() : new SqlTimeConverter(null));
732 register(Timestamp.class, throwException ? new SqlTimestampConverter() : new SqlTimestampConverter(null));
733 register(URL.class, throwException ? new URLConverter() : new URLConverter(null));
734 }
735
736
737
738
739
740
741
742
743
744
745
746
747 private void registerArrays(final boolean throwException, final int defaultArraySize) {
748
749
750 registerArrayConverter(Boolean.TYPE, new BooleanConverter(), throwException, defaultArraySize);
751 registerArrayConverter(Byte.TYPE, new ByteConverter(), throwException, defaultArraySize);
752 registerArrayConverter(Character.TYPE, new CharacterConverter(), throwException, defaultArraySize);
753 registerArrayConverter(Double.TYPE, new DoubleConverter(), throwException, defaultArraySize);
754 registerArrayConverter(Float.TYPE, new FloatConverter(), throwException, defaultArraySize);
755 registerArrayConverter(Integer.TYPE, new IntegerConverter(), throwException, defaultArraySize);
756 registerArrayConverter(Long.TYPE, new LongConverter(), throwException, defaultArraySize);
757 registerArrayConverter(Short.TYPE, new ShortConverter(), throwException, defaultArraySize);
758
759
760 registerArrayConverter(BigDecimal.class, new BigDecimalConverter(), throwException, defaultArraySize);
761 registerArrayConverter(BigInteger.class, new BigIntegerConverter(), throwException, defaultArraySize);
762 registerArrayConverter(Boolean.class, new BooleanConverter(), throwException, defaultArraySize);
763 registerArrayConverter(Byte.class, new ByteConverter(), throwException, defaultArraySize);
764 registerArrayConverter(Character.class, new CharacterConverter(), throwException, defaultArraySize);
765 registerArrayConverter(Double.class, new DoubleConverter(), throwException, defaultArraySize);
766 registerArrayConverter(Float.class, new FloatConverter(), throwException, defaultArraySize);
767 registerArrayConverter(Integer.class, new IntegerConverter(), throwException, defaultArraySize);
768 registerArrayConverter(Long.class, new LongConverter(), throwException, defaultArraySize);
769 registerArrayConverter(Short.class, new ShortConverter(), throwException, defaultArraySize);
770 registerArrayConverter(String.class, new StringConverter(), throwException, defaultArraySize);
771
772
773 registerArrayConverter(Class.class, new ClassConverter(), throwException, defaultArraySize);
774 registerArrayConverter(java.util.Date.class, new DateConverter(), throwException, defaultArraySize);
775 registerArrayConverter(Calendar.class, new DateConverter(), throwException, defaultArraySize);
776 registerArrayConverter(File.class, new FileConverter(), throwException, defaultArraySize);
777 registerArrayConverter(java.sql.Date.class, new SqlDateConverter(), throwException, defaultArraySize);
778 registerArrayConverter(java.sql.Time.class, new SqlTimeConverter(), throwException, defaultArraySize);
779 registerArrayConverter(Timestamp.class, new SqlTimestampConverter(), throwException, defaultArraySize);
780 registerArrayConverter(URL.class, new URLConverter(), throwException, defaultArraySize);
781
782 }
783
784
785
786
787
788
789
790
791
792
793
794 private void registerArrayConverter(final Class<?> componentType, final Converter componentConverter,
795 final boolean throwException, final int defaultArraySize) {
796 final Class<?> arrayType = Array.newInstance(componentType, 0).getClass();
797 Converter arrayConverter = null;
798 if (throwException) {
799 arrayConverter = new ArrayConverter(arrayType, componentConverter);
800 } else {
801 arrayConverter = new ArrayConverter(arrayType, componentConverter, defaultArraySize);
802 }
803 register(arrayType, arrayConverter);
804 }
805
806
807 private void register(final Class<?> clazz, final Converter converter) {
808 register(new ConverterFacade(converter), clazz);
809 }
810
811
812
813
814
815
816
817 public void deregister(final Class<?> clazz) {
818
819 converters.remove(clazz);
820
821 }
822
823
824
825
826
827
828
829
830
831
832 public Converter lookup(final Class<?> clazz) {
833
834 return (converters.get(clazz));
835
836 }
837
838
839
840
841
842
843
844
845
846
847 public Converter lookup(final Class<?> sourceType, final Class<?> targetType) {
848
849 if (targetType == null) {
850 throw new IllegalArgumentException("Target type is missing");
851 }
852 if (sourceType == null) {
853 return lookup(targetType);
854 }
855
856 Converter converter = null;
857
858 if (targetType == String.class) {
859 converter = lookup(sourceType);
860 if (converter == null && (sourceType.isArray() ||
861 Collection.class.isAssignableFrom(sourceType))) {
862 converter = lookup(String[].class);
863 }
864 if (converter == null) {
865 converter = lookup(String.class);
866 }
867 return converter;
868 }
869
870
871 if (targetType == String[].class) {
872 if (sourceType.isArray() || Collection.class.isAssignableFrom(sourceType)) {
873 converter = lookup(sourceType);
874 }
875 if (converter == null) {
876 converter = lookup(String[].class);
877 }
878 return converter;
879 }
880
881 return lookup(targetType);
882
883 }
884
885
886
887
888
889
890
891
892
893 public void register(final Converter converter, final Class<?> clazz) {
894
895 converters.put(clazz, converter);
896
897 }
898 }