net.smartlab.web.bean.ConverterManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of smartweb Show documentation
Show all versions of smartweb Show documentation
SmartWeb is a web application development meta framework based on Jakarta Struts, Hibernate and other open source frameworks and libraries.
/*
* The SmartWeb Framework
* Copyright (C) 2004-2006
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* For further informations on the SmartWeb Framework please visit
*
* http://smartweb.sourceforge.net
*/
package net.smartlab.web.bean;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import net.smartlab.web.Enumeration;
import net.smartlab.web.StringEnumeration;
/**
* TODO documentation
*
* @author rlogiacco
*/
public class ConverterManager {
/**
* @link aggregation <{net.smartlab.web.bean.Converter}>
* @directed directed
* @supplierCardinality 0..*
*/
private Map converters = new HashMap();
/**
* TODO documentation
*
* @param type
* @param value
* @param locale
* @return
* @throws ConversionException
*/
public Object convert(Class type, Object value, Locale locale) throws ConversionException {
if (value != null) {
try {
if (type.isArray() || !type.equals(String.class)) {
return this.getConverter(type).convert(type, value, locale);
} else {
return this.getConverter(value.getClass()).convert(type, value, locale);
}
} catch (NullPointerException npe) {
throw new ConversionException("No converter found for type `" + type.getName() + "`");
}
} else {
return null;
}
}
private Converter getConverter(Class type) throws ConversionException {
Converter converter = (Converter)converters.get(type);
if (converter == null) {
Class[] interfaces = type.getInterfaces();
for (int i = 0; i < interfaces.length && converter == null; i++) {
converter = (Converter)converters.get(interfaces[i]);
}
if (converter == null) {
if (type.equals(Object.class)) {
throw new NullPointerException();
} else {
return getConverter(type.getSuperclass());
}
}
}
return converter;
}
/**
* TODO documentation
*
* @return
*/
static public ConverterManager getDefault() {
ConverterManager manager = new ConverterManager();
// Booleans
manager.converters.put(java.lang.Boolean.TYPE, new BooleanConverter(false));
manager.converters.put(java.lang.Boolean.class, new BooleanConverter(false));
// Chars
manager.converters.put(java.lang.Character.TYPE, new CharConverter());
manager.converters.put(java.lang.Character.class, new CharConverter());
// Bytes
manager.converters.put(java.lang.Byte.TYPE, new ByteConverter((byte)0));
manager.converters.put(java.lang.Byte.class, new ByteConverter((byte)0));
// Shorts
manager.converters.put(java.lang.Short.TYPE, new ShortConverter((short)0));
manager.converters.put(java.lang.Short.class, new ShortConverter((short)0));
// Integers
manager.converters.put(java.lang.Integer.TYPE, new IntConverter(0));
manager.converters.put(java.lang.Integer.class, new IntConverter(0));
// Longs
manager.converters.put(java.lang.Long.TYPE, new LongConverter(0));
manager.converters.put(java.lang.Long.class, new LongConverter(0));
// Floats
manager.converters.put(java.lang.Float.TYPE, new FloatConverter(0));
manager.converters.put(java.lang.Float.class, new FloatConverter(0));
// Doubles
manager.converters.put(java.lang.Double.TYPE, new DoubleConverter(0));
manager.converters.put(java.lang.Double.class, new DoubleConverter(0));
// Strings
manager.converters.put(java.lang.String.class, new StringConverter());
// Dates and times
manager.converters.put(java.util.Date.class, new DateConverter());
manager.converters.put(java.sql.Date.class, new DateConverter());
manager.converters.put(java.sql.Time.class, new TimeConverter());
manager.converters.put(java.sql.Timestamp.class, new DateTimeConverter());
// Enumerations
manager.converters.put(Enumeration.class, new EnumerationConverter());
manager.converters.put(StringEnumeration.class, new StringEnumerationConverter());
// Big integers
manager.converters.put(BigInteger.class, new BigIntegerConverter());
// Currencies
manager.converters.put(BigDecimal.class, new BigDecimalConverter());
return manager;
}
/**
* Replaces a converter for a specified class.
*
* @param type the class for which the converter will be replaced.
* @param converter the converter to use for the specified class.
*/
public void setConverter(Class type, Converter converter) {
converters.remove(type);
converters.put(type, converter);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy