1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
36
37
38
39 public class ConverterManager {
40
41
42
43
44
45
46 private Map converters = new HashMap();
47
48
49
50
51
52
53
54
55
56
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
97
98
99
100 static public ConverterManager getDefault() {
101 ConverterManager manager = new ConverterManager();
102
103 manager.converters.put(java.lang.Boolean.TYPE, new BooleanConverter(false));
104 manager.converters.put(java.lang.Boolean.class, new BooleanConverter(false));
105
106 manager.converters.put(java.lang.Character.TYPE, new CharConverter());
107 manager.converters.put(java.lang.Character.class, new CharConverter());
108
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
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
115 manager.converters.put(java.lang.Integer.TYPE, new IntConverter(0));
116 manager.converters.put(java.lang.Integer.class, new IntConverter(0));
117
118 manager.converters.put(java.lang.Long.TYPE, new LongConverter(0));
119 manager.converters.put(java.lang.Long.class, new LongConverter(0));
120
121 manager.converters.put(java.lang.Float.TYPE, new FloatConverter(0));
122 manager.converters.put(java.lang.Float.class, new FloatConverter(0));
123
124 manager.converters.put(java.lang.Double.TYPE, new DoubleConverter(0));
125 manager.converters.put(java.lang.Double.class, new DoubleConverter(0));
126
127 manager.converters.put(java.lang.String.class, new StringConverter());
128
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
134 manager.converters.put(Enumeration.class, new EnumerationConverter());
135 manager.converters.put(StringEnumeration.class, new StringEnumerationConverter());
136
137 manager.converters.put(BigInteger.class, new BigIntegerConverter());
138
139 manager.converters.put(BigDecimal.class, new BigDecimalConverter());
140 return manager;
141 }
142
143
144
145
146
147
148
149 public void setConverter(Class type, Converter converter) {
150 converters.remove(type);
151 converters.put(type, converter);
152 }
153 }