01: package org.gomba.utils.convert;
02:
03: import java.math.BigDecimal;
04: import java.math.BigInteger;
05:
06: import org.apache.commons.beanutils.ConvertUtils;
07: import org.apache.commons.beanutils.Converter;
08: import org.apache.commons.beanutils.converters.BigDecimalConverter;
09: import org.apache.commons.beanutils.converters.BigIntegerConverter;
10: import org.apache.commons.beanutils.converters.BooleanConverter;
11: import org.apache.commons.beanutils.converters.ByteConverter;
12: import org.apache.commons.beanutils.converters.CharacterConverter;
13: import org.apache.commons.beanutils.converters.DoubleConverter;
14: import org.apache.commons.beanutils.converters.FloatConverter;
15: import org.apache.commons.beanutils.converters.IntegerConverter;
16: import org.apache.commons.beanutils.converters.LongConverter;
17: import org.apache.commons.beanutils.converters.ShortConverter;
18:
19: /**
20: * Configures the ConvertUtils package.
21: *
22: * @author Flavio Tordini
23: * @version $Id: ConvertUtilsWrapper.java,v 1.1.1.1 2004/06/16 13:15:12 flaviotordini Exp $
24: */
25: public class ConvertUtilsWrapper {
26:
27: /**
28: * Private constructor.
29: */
30: private ConvertUtilsWrapper() {
31: // dummy
32: }
33:
34: static {
35:
36: // setup Jakarta Commons BeanUtils -> ConvertUtils
37: // All dates in ISO8601 format
38: ConvertUtils.register(new ISO8601DateConverter(),
39: java.util.Date.class);
40:
41: // No-args constructor gets the version that throws exceptions
42: Converter converter;
43:
44: converter = new IntegerConverter();
45: ConvertUtils.register(converter, Integer.TYPE);
46: ConvertUtils.register(converter, Integer.class);
47:
48: converter = new ShortConverter();
49: ConvertUtils.register(converter, Short.TYPE);
50: ConvertUtils.register(converter, Short.class);
51:
52: converter = new LongConverter();
53: ConvertUtils.register(converter, Long.TYPE);
54: ConvertUtils.register(converter, Long.class);
55:
56: converter = new DoubleConverter();
57: ConvertUtils.register(converter, Double.TYPE);
58: ConvertUtils.register(converter, Double.class);
59:
60: converter = new BooleanConverter();
61: ConvertUtils.register(converter, Boolean.TYPE);
62: ConvertUtils.register(converter, Boolean.class);
63:
64: converter = new FloatConverter();
65: ConvertUtils.register(converter, Float.TYPE);
66: ConvertUtils.register(converter, Float.class);
67:
68: converter = new ByteConverter();
69: ConvertUtils.register(converter, Byte.TYPE);
70: ConvertUtils.register(converter, Byte.class);
71:
72: converter = new CharacterConverter();
73: ConvertUtils.register(converter, Character.TYPE);
74: ConvertUtils.register(converter, Character.class);
75:
76: converter = new BigDecimalConverter();
77: ConvertUtils.register(converter, BigDecimal.class);
78:
79: converter = new BigIntegerConverter();
80: ConvertUtils.register(converter, BigInteger.class);
81:
82: ConvertUtils.deregister(java.sql.Date.class);
83: ConvertUtils.deregister(java.sql.Time.class);
84: ConvertUtils.deregister(java.sql.Timestamp.class);
85: ConvertUtils.deregister(java.lang.Class.class);
86: }
87:
88: /**
89: * Wrap the ConvertUtils.convert() method. Other classes must use this
90: * method rather than directly calling ConvertUtils. This is because
91: * ConvertUtils has a JVM wide configuration mechanism for converters and
92: * the right converters are statically configured in this class.
93: */
94: public static Object convert(String string, Class type) {
95: return ConvertUtils.convert(string, type);
96: }
97:
98: }
|