View Javadoc

1   /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   *
19   * For further informations on the SmartWeb Framework please visit
20   *
21   *                        http://smartweb.sourceforge.net
22   */
23  package net.smartlab.web.bean;
24  
25  import java.math.BigDecimal;
26  import java.math.BigInteger;
27  import java.util.HashMap;
28  import java.util.Locale;
29  import java.util.Map;
30  
31  import net.smartlab.web.Enumeration;
32  import net.smartlab.web.StringEnumeration;
33  
34  /**
35   * TODO documentation
36   * 
37   * @author rlogiacco
38   */
39  public class ConverterManager {
40  
41  	/**
42  	 * @link aggregation <{net.smartlab.web.bean.Converter}>
43  	 * @directed directed
44  	 * @supplierCardinality 0..*
45  	 */
46  	private Map converters = new HashMap();
47  
48  
49  	/**
50  	 * TODO documentation
51  	 * 
52  	 * @param type
53  	 * @param value
54  	 * @param locale
55  	 * @return
56  	 * @throws ConversionException
57  	 */
58  	public Object convert(Class type, Object value, Locale locale) throws ConversionException {
59  		if (value != null) {
60  			try {
61  				if (type.isAssignableFrom(value.getClass())) {
62  					return value;
63  				}
64  				if (type.isArray() || !type.equals(String.class)) {
65  					return this.getConverter(type).convert(type, value, locale);
66  				} else {
67  					return this.getConverter(value.getClass()).convert(type, value, locale);
68  				}
69  			} catch (NullPointerException npe) {
70  				throw new ConversionException("No converter found for type `" + type.getName() + "`");
71  			}
72  		} else {
73  			return null;
74  		}
75  	}
76  
77  	private Converter getConverter(Class type) throws ConversionException {
78  		Converter converter = (Converter)converters.get(type);
79  		if (converter == null) {
80  			Class[] interfaces = type.getInterfaces();
81  			for (int i = 0; i < interfaces.length && converter == null; i++) {
82  				converter = (Converter)converters.get(interfaces[i]);
83  			}
84  			if (converter == null) {
85  				if (type.equals(Object.class)) {
86  					throw new NullPointerException();
87  				} else {
88  					return getConverter(type.getSuperclass());
89  				}
90  			}
91  		}
92  		return converter;
93  	}
94  
95  	/**
96  	 * TODO documentation
97  	 * 
98  	 * @return
99  	 */
100 	static public ConverterManager getDefault() {
101 		ConverterManager manager = new ConverterManager();
102 		// Booleans
103 		manager.converters.put(java.lang.Boolean.TYPE, new BooleanConverter(false));
104 		manager.converters.put(java.lang.Boolean.class, new BooleanConverter(false));
105 		// Chars
106 		manager.converters.put(java.lang.Character.TYPE, new CharConverter());
107 		manager.converters.put(java.lang.Character.class, new CharConverter());
108 		// Bytes
109 		manager.converters.put(java.lang.Byte.TYPE, new ByteConverter((byte)0));
110 		manager.converters.put(java.lang.Byte.class, new ByteConverter((byte)0));
111 		// Shorts
112 		manager.converters.put(java.lang.Short.TYPE, new ShortConverter((short)0));
113 		manager.converters.put(java.lang.Short.class, new ShortConverter((short)0));
114 		// Integers
115 		manager.converters.put(java.lang.Integer.TYPE, new IntConverter(0));
116 		manager.converters.put(java.lang.Integer.class, new IntConverter(0));
117 		// Longs
118 		manager.converters.put(java.lang.Long.TYPE, new LongConverter(0));
119 		manager.converters.put(java.lang.Long.class, new LongConverter(0));
120 		// Floats
121 		manager.converters.put(java.lang.Float.TYPE, new FloatConverter(0));
122 		manager.converters.put(java.lang.Float.class, new FloatConverter(0));
123 		// Doubles
124 		manager.converters.put(java.lang.Double.TYPE, new DoubleConverter(0));
125 		manager.converters.put(java.lang.Double.class, new DoubleConverter(0));
126 		// Strings
127 		manager.converters.put(java.lang.String.class, new StringConverter());
128 		// Dates and times
129 		manager.converters.put(java.util.Date.class, new DateConverter());
130 		manager.converters.put(java.sql.Date.class, new DateConverter());
131 		manager.converters.put(java.sql.Time.class, new TimeConverter());
132 		manager.converters.put(java.sql.Timestamp.class, new DateTimeConverter());
133 		// Enumerations
134 		manager.converters.put(Enumeration.class, new EnumerationConverter());
135 		manager.converters.put(StringEnumeration.class, new StringEnumerationConverter());
136 		// Big integers
137 		manager.converters.put(BigInteger.class, new BigIntegerConverter());
138 		// Currencies
139 		manager.converters.put(BigDecimal.class, new BigDecimalConverter());
140 		return manager;
141 	}
142 
143 	/**
144 	 * Replaces a converter for a specified class.
145 	 * 
146 	 * @param type the class for which the converter will be replaced.
147 	 * @param converter the converter to use for the specified class.
148 	 */
149 	public void setConverter(Class type, Converter converter) {
150 		converters.remove(type);
151 		converters.put(type, converter);
152 	}
153 }