package org.strecks.converter;
import java.io.File;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.BigIntegerConverter;
import org.apache.commons.beanutils.converters.BooleanArrayConverter;
import org.apache.commons.beanutils.converters.BooleanConverter;
import org.apache.commons.beanutils.converters.ByteArrayConverter;
import org.apache.commons.beanutils.converters.ByteConverter;
import org.apache.commons.beanutils.converters.CharacterArrayConverter;
import org.apache.commons.beanutils.converters.CharacterConverter;
import org.apache.commons.beanutils.converters.ClassConverter;
import org.apache.commons.beanutils.converters.DoubleArrayConverter;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.FileConverter;
import org.apache.commons.beanutils.converters.FloatArrayConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.IntegerArrayConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongArrayConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.beanutils.converters.ShortArrayConverter;
import org.apache.commons.beanutils.converters.ShortConverter;
import org.apache.commons.beanutils.converters.SqlDateConverter;
import org.apache.commons.beanutils.converters.SqlTimeConverter;
import org.apache.commons.beanutils.converters.SqlTimestampConverter;
import org.apache.commons.beanutils.converters.StringArrayConverter;
import org.apache.commons.beanutils.converters.StringConverter;
import org.apache.commons.beanutils.converters.URLConverter;
/**
* Singleton access point for BeanUtils functionality. In singleton registration, registers all BeanUtils converters so
* that they do not default to any value, but throw a <code>ConversionException</code>, when a conversion error occurs.
* @author Phil Zoio
*/
public class BeanUtilsConverter
{
private static BeanUtilsConverter instance = new BeanUtilsConverter();
private BeanUtilsConverter()
{
boolean booleanArray[] = new boolean[0];
byte byteArray[] = new byte[0];
char charArray[] = new char[0];
double doubleArray[] = new double[0];
float floatArray[] = new float[0];
int intArray[] = new int[0];
long longArray[] = new long[0];
short shortArray[] = new short[0];
String stringArray[] = new String[0];
register(BigDecimal.class, new BigDecimalConverter());
register(BigInteger.class, new BigIntegerConverter());
register(Boolean.TYPE, new BooleanConverter());
register(Boolean.class, new BooleanConverter());
register(booleanArray.getClass(), new BooleanArrayConverter());
register(Byte.TYPE, new ByteConverter());
register(Byte.class, new ByteConverter());
register(byteArray.getClass(), new ByteArrayConverter());
register(Character.TYPE, new CharacterConverter());
register(Character.class, new CharacterConverter());
register(charArray.getClass(), new CharacterArrayConverter());
register(Class.class, new ClassConverter());
register(Double.TYPE, new DoubleConverter());
register(Double.class, new DoubleConverter());
register(doubleArray.getClass(), new DoubleArrayConverter());
register(Float.TYPE, new FloatConverter());
register(Float.class, new FloatConverter());
register(floatArray.getClass(), new FloatArrayConverter());
register(Integer.TYPE, new IntegerConverter());
register(Integer.class, new IntegerConverter());
register(intArray.getClass(), new IntegerArrayConverter());
register(Long.TYPE, new LongConverter());
register(Long.class, new LongConverter());
register(longArray.getClass(), new LongArrayConverter());
register(Short.TYPE, new ShortConverter());
register(Short.class, new ShortConverter());
register(shortArray.getClass(), new ShortArrayConverter());
register(String.class, new StringConverter());
register(stringArray.getClass(), new StringArrayConverter());
register(Date.class, new SqlDateConverter());
register(Time.class, new SqlTimeConverter());
register(Timestamp.class, new SqlTimestampConverter());
register(File.class, new FileConverter());
register(URL.class, new URLConverter());
}
private void register(Class<?> name, org.apache.commons.beanutils.Converter converter)
{
ConvertUtils.register(converter, name);
}
public Object convert(String toConvert, Class clazz)
{
return ConvertUtils.convert(toConvert, clazz);
}
public String convert(Object toConvert)
{
return ConvertUtils.convert(toConvert);
}
public static BeanUtilsConverter getInstance()
{
return instance;
}
}