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 |
13 |
public class ValueConverterImpl implements ValueConverter |
36 |
|
{ |
37 |
|
|
38 |
|
|
39 |
|
public List _contributions; |
40 |
|
|
41 |
13 |
private Map _converterMap = new HashMap(); |
42 |
|
|
43 |
13 |
private Map _primitiveToWrapper = new HashMap(); |
44 |
|
|
45 |
13 |
private Map _wrapperToPrimitive = new HashMap(); |
46 |
|
|
47 |
|
{ |
48 |
23 |
store(boolean.class, Boolean.class); |
49 |
13 |
store(byte.class, Byte.class); |
50 |
13 |
store(short.class, Short.class); |
51 |
13 |
store(char.class, Character.class); |
52 |
13 |
store(int.class, Integer.class); |
53 |
13 |
store(long.class, Long.class); |
54 |
13 |
store(float.class, Float.class); |
55 |
13 |
store(double.class, Double.class); |
56 |
13 |
} |
57 |
|
|
58 |
|
private void store(Class primitive, Class wrapper) |
59 |
|
{ |
60 |
104 |
_primitiveToWrapper.put(primitive, wrapper); |
61 |
|
|
62 |
104 |
_wrapperToPrimitive.put(wrapper, primitive); |
63 |
104 |
} |
64 |
|
|
65 |
|
public void initializeService() |
66 |
|
{ |
67 |
4 |
Iterator i = _contributions.iterator(); |
68 |
12 |
while (i.hasNext()) |
69 |
|
{ |
70 |
8 |
TypeConverterContribution c = (TypeConverterContribution) i.next(); |
71 |
|
|
72 |
8 |
_converterMap.put(c.getSubjectClass(), c.getConverter()); |
73 |
8 |
} |
74 |
4 |
} |
75 |
|
|
76 |
|
public Object coerceValue(Object value, Class desiredType) |
77 |
|
{ |
78 |
29 |
Defense.notNull(desiredType, "desiredType"); |
79 |
|
|
80 |
29 |
Class effectiveType = convertType(desiredType); |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
29 |
if (value != null && effectiveType.isAssignableFrom(value.getClass())) |
85 |
7 |
return value; |
86 |
|
|
87 |
22 |
Object result = convertNumberToNumber(value, effectiveType); |
88 |
|
|
89 |
22 |
if (result != null) |
90 |
1 |
return result; |
91 |
|
|
92 |
21 |
result = convertUsingPropertyEditor(value, effectiveType); |
93 |
|
|
94 |
20 |
if (result != null) |
95 |
11 |
return result; |
96 |
|
|
97 |
9 |
TypeConverter converter = (TypeConverter) _converterMap.get(effectiveType); |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
9 |
if (value == null && converter == null) |
102 |
1 |
return null; |
103 |
|
|
104 |
8 |
if (converter == null) |
105 |
2 |
throw new ApplicationRuntimeException(CoerceMessages.noConverter(value.getClass(), effectiveType)); |
106 |
|
|
107 |
6 |
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 |
21 |
if (value == null || value.getClass() != String.class |
127 |
|
|| !Number.class.isAssignableFrom(targetType)) |
128 |
9 |
return null; |
129 |
|
|
130 |
12 |
Class primitiveType = (Class) _wrapperToPrimitive.get(targetType); |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
12 |
if (primitiveType == null) |
135 |
0 |
return null; |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
12 |
PropertyEditor editor = PropertyEditorManager.findEditor(primitiveType); |
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
12 |
if (editor == null) |
145 |
0 |
return null; |
146 |
|
|
147 |
12 |
String text = (String) value; |
148 |
|
|
149 |
|
try |
150 |
|
{ |
151 |
12 |
editor.setAsText(text); |
152 |
|
|
153 |
11 |
return (Number) editor.getValue(); |
154 |
|
} |
155 |
1 |
catch (Exception ex) |
156 |
|
{ |
157 |
1 |
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 |
22 |
if (value == null || !Number.class.isAssignableFrom(value.getClass()) |
168 |
|
|| !Number.class.isAssignableFrom(targetType)) |
169 |
21 |
return null; |
170 |
|
|
171 |
1 |
String valueAsString = value.toString(); |
172 |
|
|
173 |
1 |
return (Number) ConstructorUtils.invokeConstructor(targetType, new Object[] { valueAsString }); |
174 |
|
} |
175 |
|
|
176 |
|
private Class convertType(Class possiblePrimitiveType) |
177 |
|
{ |
178 |
29 |
Class wrapperType = (Class) _primitiveToWrapper.get(possiblePrimitiveType); |
179 |
|
|
180 |
29 |
return wrapperType == null ? possiblePrimitiveType : wrapperType; |
181 |
|
} |
182 |
|
|
183 |
|
public void setContributions(List contributions) |
184 |
|
{ |
185 |
4 |
_contributions = contributions; |
186 |
4 |
} |
187 |
|
} |