001: package org.strecks.converter;
002:
003: import java.io.File;
004: import java.math.BigDecimal;
005: import java.math.BigInteger;
006: import java.net.URL;
007: import java.sql.Date;
008: import java.sql.Time;
009: import java.sql.Timestamp;
010:
011: import org.apache.commons.beanutils.ConvertUtils;
012: import org.apache.commons.beanutils.converters.BigDecimalConverter;
013: import org.apache.commons.beanutils.converters.BigIntegerConverter;
014: import org.apache.commons.beanutils.converters.BooleanArrayConverter;
015: import org.apache.commons.beanutils.converters.BooleanConverter;
016: import org.apache.commons.beanutils.converters.ByteArrayConverter;
017: import org.apache.commons.beanutils.converters.ByteConverter;
018: import org.apache.commons.beanutils.converters.CharacterArrayConverter;
019: import org.apache.commons.beanutils.converters.CharacterConverter;
020: import org.apache.commons.beanutils.converters.ClassConverter;
021: import org.apache.commons.beanutils.converters.DoubleArrayConverter;
022: import org.apache.commons.beanutils.converters.DoubleConverter;
023: import org.apache.commons.beanutils.converters.FileConverter;
024: import org.apache.commons.beanutils.converters.FloatArrayConverter;
025: import org.apache.commons.beanutils.converters.FloatConverter;
026: import org.apache.commons.beanutils.converters.IntegerArrayConverter;
027: import org.apache.commons.beanutils.converters.IntegerConverter;
028: import org.apache.commons.beanutils.converters.LongArrayConverter;
029: import org.apache.commons.beanutils.converters.LongConverter;
030: import org.apache.commons.beanutils.converters.ShortArrayConverter;
031: import org.apache.commons.beanutils.converters.ShortConverter;
032: import org.apache.commons.beanutils.converters.SqlDateConverter;
033: import org.apache.commons.beanutils.converters.SqlTimeConverter;
034: import org.apache.commons.beanutils.converters.SqlTimestampConverter;
035: import org.apache.commons.beanutils.converters.StringArrayConverter;
036: import org.apache.commons.beanutils.converters.StringConverter;
037: import org.apache.commons.beanutils.converters.URLConverter;
038:
039: /**
040: * Singleton access point for BeanUtils functionality. In singleton registration, registers all BeanUtils converters so
041: * that they do not default to any value, but throw a <code>ConversionException</code>, when a conversion error occurs.
042: * @author Phil Zoio
043: */
044: public class BeanUtilsConverter {
045:
046: private static BeanUtilsConverter instance = new BeanUtilsConverter();
047:
048: private BeanUtilsConverter() {
049: boolean booleanArray[] = new boolean[0];
050: byte byteArray[] = new byte[0];
051: char charArray[] = new char[0];
052: double doubleArray[] = new double[0];
053: float floatArray[] = new float[0];
054: int intArray[] = new int[0];
055: long longArray[] = new long[0];
056: short shortArray[] = new short[0];
057: String stringArray[] = new String[0];
058:
059: register(BigDecimal.class, new BigDecimalConverter());
060: register(BigInteger.class, new BigIntegerConverter());
061:
062: register(Boolean.TYPE, new BooleanConverter());
063: register(Boolean.class, new BooleanConverter());
064: register(booleanArray.getClass(), new BooleanArrayConverter());
065:
066: register(Byte.TYPE, new ByteConverter());
067: register(Byte.class, new ByteConverter());
068: register(byteArray.getClass(), new ByteArrayConverter());
069:
070: register(Character.TYPE, new CharacterConverter());
071: register(Character.class, new CharacterConverter());
072: register(charArray.getClass(), new CharacterArrayConverter());
073:
074: register(Class.class, new ClassConverter());
075: register(Double.TYPE, new DoubleConverter());
076: register(Double.class, new DoubleConverter());
077: register(doubleArray.getClass(), new DoubleArrayConverter());
078:
079: register(Float.TYPE, new FloatConverter());
080: register(Float.class, new FloatConverter());
081: register(floatArray.getClass(), new FloatArrayConverter());
082:
083: register(Integer.TYPE, new IntegerConverter());
084: register(Integer.class, new IntegerConverter());
085: register(intArray.getClass(), new IntegerArrayConverter());
086:
087: register(Long.TYPE, new LongConverter());
088: register(Long.class, new LongConverter());
089: register(longArray.getClass(), new LongArrayConverter());
090:
091: register(Short.TYPE, new ShortConverter());
092: register(Short.class, new ShortConverter());
093: register(shortArray.getClass(), new ShortArrayConverter());
094:
095: register(String.class, new StringConverter());
096: register(stringArray.getClass(), new StringArrayConverter());
097:
098: register(Date.class, new SqlDateConverter());
099: register(Time.class, new SqlTimeConverter());
100: register(Timestamp.class, new SqlTimestampConverter());
101: register(File.class, new FileConverter());
102: register(URL.class, new URLConverter());
103:
104: }
105:
106: private void register(Class<?> name,
107: org.apache.commons.beanutils.Converter converter) {
108: ConvertUtils.register(converter, name);
109: }
110:
111: public Object convert(String toConvert, Class clazz) {
112: return ConvertUtils.convert(toConvert, clazz);
113: }
114:
115: public String convert(Object toConvert) {
116: return ConvertUtils.convert(toConvert);
117: }
118:
119: public static BeanUtilsConverter getInstance() {
120: return instance;
121: }
122:
123: }
|