1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.coerce; |
16 | |
|
17 | |
import org.apache.hivemind.ApplicationRuntimeException; |
18 | |
import org.apache.hivemind.util.ConstructorUtils; |
19 | |
import org.apache.hivemind.util.Defense; |
20 | |
|
21 | |
import java.beans.PropertyEditor; |
22 | |
import java.beans.PropertyEditorManager; |
23 | |
import java.util.HashMap; |
24 | |
import java.util.Iterator; |
25 | |
import java.util.List; |
26 | |
import java.util.Map; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public class ValueConverterImpl implements ValueConverter |
36 | |
{ |
37 | |
|
38 | |
|
39 | |
public List _contributions; |
40 | |
|
41 | 0 | private Map _converterMap = new HashMap(); |
42 | |
|
43 | 0 | private Map _primitiveToWrapper = new HashMap(); |
44 | |
|
45 | 0 | private Map _wrapperToPrimitive = new HashMap(); |
46 | |
|
47 | |
{ |
48 | 0 | store(boolean.class, Boolean.class); |
49 | 0 | store(byte.class, Byte.class); |
50 | 0 | store(short.class, Short.class); |
51 | 0 | store(char.class, Character.class); |
52 | 0 | store(int.class, Integer.class); |
53 | 0 | store(long.class, Long.class); |
54 | 0 | store(float.class, Float.class); |
55 | 0 | store(double.class, Double.class); |
56 | 0 | } |
57 | |
|
58 | |
private void store(Class primitive, Class wrapper) |
59 | |
{ |
60 | 0 | _primitiveToWrapper.put(primitive, wrapper); |
61 | |
|
62 | 0 | _wrapperToPrimitive.put(wrapper, primitive); |
63 | 0 | } |
64 | |
|
65 | |
public void initializeService() |
66 | |
{ |
67 | 0 | Iterator i = _contributions.iterator(); |
68 | 0 | while (i.hasNext()) |
69 | |
{ |
70 | 0 | TypeConverterContribution c = (TypeConverterContribution) i.next(); |
71 | |
|
72 | 0 | _converterMap.put(c.getSubjectClass(), c.getConverter()); |
73 | 0 | } |
74 | 0 | } |
75 | |
|
76 | |
public Object coerceValue(Object value, Class desiredType) |
77 | |
{ |
78 | 0 | Defense.notNull(desiredType, "desiredType"); |
79 | |
|
80 | 0 | Class effectiveType = convertType(desiredType); |
81 | |
|
82 | |
|
83 | |
|
84 | 0 | if (value != null && effectiveType.isAssignableFrom(value.getClass())) |
85 | 0 | return value; |
86 | |
|
87 | 0 | Object result = convertNumberToNumber(value, effectiveType); |
88 | |
|
89 | 0 | if (result != null) |
90 | 0 | return result; |
91 | |
|
92 | 0 | result = convertUsingPropertyEditor(value, effectiveType); |
93 | |
|
94 | 0 | if (result != null) |
95 | 0 | return result; |
96 | |
|
97 | 0 | TypeConverter converter = (TypeConverter) _converterMap.get(effectiveType); |
98 | |
|
99 | |
|
100 | |
|
101 | 0 | if (value == null && converter == null) |
102 | 0 | return null; |
103 | |
|
104 | 0 | if (converter == null) |
105 | 0 | throw new ApplicationRuntimeException(CoerceMessages.noConverter(value.getClass(), effectiveType)); |
106 | |
|
107 | 0 | return converter.convertValue(value); |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
private Number convertUsingPropertyEditor(Object value, Class targetType) |
121 | |
{ |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | 0 | if (value == null || value.getClass() != String.class |
127 | |
|| !Number.class.isAssignableFrom(targetType)) |
128 | 0 | return null; |
129 | |
|
130 | 0 | Class primitiveType = (Class) _wrapperToPrimitive.get(targetType); |
131 | |
|
132 | |
|
133 | |
|
134 | 0 | if (primitiveType == null) |
135 | 0 | return null; |
136 | |
|
137 | |
|
138 | |
|
139 | 0 | PropertyEditor editor = PropertyEditorManager.findEditor(primitiveType); |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | 0 | if (editor == null) |
145 | 0 | return null; |
146 | |
|
147 | 0 | String text = (String) value; |
148 | |
|
149 | |
try |
150 | |
{ |
151 | 0 | editor.setAsText(text); |
152 | |
|
153 | 0 | return (Number) editor.getValue(); |
154 | |
} |
155 | 0 | catch (Exception ex) |
156 | |
{ |
157 | 0 | throw new ApplicationRuntimeException(CoerceMessages.stringToNumberConversionError( |
158 | |
text, |
159 | |
targetType, |
160 | |
ex), ex); |
161 | |
} |
162 | |
|
163 | |
} |
164 | |
|
165 | |
private Number convertNumberToNumber(Object value, Class targetType) |
166 | |
{ |
167 | 0 | if (value == null || !Number.class.isAssignableFrom(value.getClass()) |
168 | |
|| !Number.class.isAssignableFrom(targetType)) |
169 | 0 | return null; |
170 | |
|
171 | 0 | String valueAsString = value.toString(); |
172 | |
|
173 | 0 | return (Number) ConstructorUtils.invokeConstructor(targetType, new Object[] { valueAsString }); |
174 | |
} |
175 | |
|
176 | |
private Class convertType(Class possiblePrimitiveType) |
177 | |
{ |
178 | 0 | Class wrapperType = (Class) _primitiveToWrapper.get(possiblePrimitiveType); |
179 | |
|
180 | 0 | return wrapperType == null ? possiblePrimitiveType : wrapperType; |
181 | |
} |
182 | |
|
183 | |
public void setContributions(List contributions) |
184 | |
{ |
185 | 0 | _contributions = contributions; |
186 | 0 | } |
187 | |
} |