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