1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.valid; |
16 |
| |
17 |
| import java.math.BigDecimal; |
18 |
| import java.math.BigInteger; |
19 |
| import java.util.HashMap; |
20 |
| import java.util.Map; |
21 |
| |
22 |
| import org.apache.hivemind.ApplicationRuntimeException; |
23 |
| import org.apache.hivemind.lib.util.StrategyRegistry; |
24 |
| import org.apache.hivemind.lib.util.StrategyRegistryImpl; |
25 |
| import org.apache.hivemind.util.PropertyUtils; |
26 |
| import org.apache.tapestry.IMarkupWriter; |
27 |
| import org.apache.tapestry.IRequestCycle; |
28 |
| import org.apache.tapestry.Tapestry; |
29 |
| import org.apache.tapestry.form.IFormComponent; |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| public class NumberValidator extends AbstractNumericValidator |
40 |
| { |
41 |
| private static final Map TYPES = new HashMap(); |
42 |
| |
43 |
| static |
44 |
| { |
45 |
2
| TYPES.put("boolean", boolean.class);
|
46 |
2
| TYPES.put("Boolean", Boolean.class);
|
47 |
2
| TYPES.put("java.lang.Boolean", Boolean.class);
|
48 |
2
| TYPES.put("char", char.class);
|
49 |
2
| TYPES.put("Character", Character.class);
|
50 |
2
| TYPES.put("java.lang.Character", Character.class);
|
51 |
2
| TYPES.put("short", short.class);
|
52 |
2
| TYPES.put("Short", Short.class);
|
53 |
2
| TYPES.put("java.lang.Short", Short.class);
|
54 |
2
| TYPES.put("int", int.class);
|
55 |
2
| TYPES.put("Integer", Integer.class);
|
56 |
2
| TYPES.put("java.lang.Integer", Integer.class);
|
57 |
2
| TYPES.put("long", long.class);
|
58 |
2
| TYPES.put("Long", Long.class);
|
59 |
2
| TYPES.put("java.lang.Long", Long.class);
|
60 |
2
| TYPES.put("float", float.class);
|
61 |
2
| TYPES.put("Float", Float.class);
|
62 |
2
| TYPES.put("java.lang.Float", Float.class);
|
63 |
2
| TYPES.put("byte", byte.class);
|
64 |
2
| TYPES.put("Byte", Byte.class);
|
65 |
2
| TYPES.put("java.lang.Byte", Byte.class);
|
66 |
2
| TYPES.put("double", double.class);
|
67 |
2
| TYPES.put("Double", Double.class);
|
68 |
2
| TYPES.put("java.lang.Double", Double.class);
|
69 |
2
| TYPES.put("java.math.BigInteger", BigInteger.class);
|
70 |
2
| TYPES.put("java.math.BigDecimal", BigDecimal.class);
|
71 |
| } |
72 |
| |
73 |
| private Class _valueTypeClass = int.class; |
74 |
| |
75 |
| private Number _minimum; |
76 |
| |
77 |
| private Number _maximum; |
78 |
| |
79 |
| private static StrategyRegistry _numberAdaptors = new StrategyRegistryImpl(); |
80 |
| |
81 |
| public final static int NUMBER_TYPE_INTEGER = 0; |
82 |
| |
83 |
| public final static int NUMBER_TYPE_REAL = 1; |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| public static abstract class NumberStrategy |
90 |
| { |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| abstract public Number parse(String value); |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| abstract public int getNumberType(); |
108 |
| |
109 |
30
| public int compare(Number left, Number right)
|
110 |
| { |
111 |
30
| if (!left.getClass().equals(right.getClass()))
|
112 |
16
| right = coerce(right);
|
113 |
| |
114 |
30
| Comparable lc = (Comparable) left;
|
115 |
| |
116 |
30
| return lc.compareTo(right);
|
117 |
| } |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| protected abstract Number coerce(Number number); |
126 |
| } |
127 |
| |
128 |
| private static abstract class IntegerNumberAdaptor extends NumberStrategy |
129 |
| { |
130 |
10
| public int getNumberType()
|
131 |
| { |
132 |
10
| return NUMBER_TYPE_INTEGER;
|
133 |
| } |
134 |
| } |
135 |
| |
136 |
| private static abstract class RealNumberAdaptor extends NumberStrategy |
137 |
| { |
138 |
6
| public int getNumberType()
|
139 |
| { |
140 |
6
| return NUMBER_TYPE_REAL;
|
141 |
| } |
142 |
| } |
143 |
| |
144 |
| private static class ByteAdaptor extends IntegerNumberAdaptor |
145 |
| { |
146 |
2
| public Number parse(String value)
|
147 |
| { |
148 |
2
| return new Byte(value);
|
149 |
| } |
150 |
| |
151 |
2
| protected Number coerce(Number number)
|
152 |
| { |
153 |
2
| return new Byte(number.byteValue());
|
154 |
| } |
155 |
| } |
156 |
| |
157 |
| private static class ShortAdaptor extends IntegerNumberAdaptor |
158 |
| { |
159 |
2
| public Number parse(String value)
|
160 |
| { |
161 |
2
| return new Short(value);
|
162 |
| } |
163 |
| |
164 |
2
| protected Number coerce(Number number)
|
165 |
| { |
166 |
2
| return new Short(number.shortValue());
|
167 |
| } |
168 |
| } |
169 |
| |
170 |
| private static class IntAdaptor extends IntegerNumberAdaptor |
171 |
| { |
172 |
16
| public Number parse(String value)
|
173 |
| { |
174 |
16
| return new Integer(value);
|
175 |
| } |
176 |
| |
177 |
2
| protected Number coerce(Number number)
|
178 |
| { |
179 |
2
| return new Integer(number.intValue());
|
180 |
| } |
181 |
| } |
182 |
| |
183 |
| private static class LongAdaptor extends IntegerNumberAdaptor |
184 |
| { |
185 |
2
| public Number parse(String value)
|
186 |
| { |
187 |
2
| return new Long(value);
|
188 |
| } |
189 |
| |
190 |
2
| protected Number coerce(Number number)
|
191 |
| { |
192 |
2
| return new Long(number.longValue());
|
193 |
| } |
194 |
| } |
195 |
| |
196 |
| private static class FloatAdaptor extends RealNumberAdaptor |
197 |
| { |
198 |
2
| public Number parse(String value)
|
199 |
| { |
200 |
2
| return new Float(value);
|
201 |
| } |
202 |
| |
203 |
2
| protected Number coerce(Number number)
|
204 |
| { |
205 |
2
| return new Float(number.floatValue());
|
206 |
| } |
207 |
| } |
208 |
| |
209 |
| private static class DoubleAdaptor extends RealNumberAdaptor |
210 |
| { |
211 |
2
| public Number parse(String value)
|
212 |
| { |
213 |
2
| return new Double(value);
|
214 |
| } |
215 |
| |
216 |
2
| protected Number coerce(Number number)
|
217 |
| { |
218 |
2
| return new Double(number.doubleValue());
|
219 |
| } |
220 |
| } |
221 |
| |
222 |
| private static class BigDecimalAdaptor extends RealNumberAdaptor |
223 |
| { |
224 |
2
| public Number parse(String value)
|
225 |
| { |
226 |
2
| return new BigDecimal(value);
|
227 |
| } |
228 |
| |
229 |
2
| protected Number coerce(Number number)
|
230 |
| { |
231 |
2
| return new BigDecimal(number.doubleValue());
|
232 |
| } |
233 |
| } |
234 |
| |
235 |
| private static class BigIntegerAdaptor extends IntegerNumberAdaptor |
236 |
| { |
237 |
2
| public Number parse(String value)
|
238 |
| { |
239 |
2
| return new BigInteger(value);
|
240 |
| } |
241 |
| |
242 |
2
| protected Number coerce(Number number)
|
243 |
| { |
244 |
2
| return new BigInteger(number.toString());
|
245 |
| } |
246 |
| } |
247 |
| |
248 |
| static |
249 |
| { |
250 |
2
| NumberStrategy byteAdaptor = new ByteAdaptor();
|
251 |
2
| NumberStrategy shortAdaptor = new ShortAdaptor();
|
252 |
2
| NumberStrategy intAdaptor = new IntAdaptor();
|
253 |
2
| NumberStrategy longAdaptor = new LongAdaptor();
|
254 |
2
| NumberStrategy floatAdaptor = new FloatAdaptor();
|
255 |
2
| NumberStrategy doubleAdaptor = new DoubleAdaptor();
|
256 |
| |
257 |
2
| _numberAdaptors.register(Byte.class, byteAdaptor);
|
258 |
2
| _numberAdaptors.register(byte.class, byteAdaptor);
|
259 |
2
| _numberAdaptors.register(Short.class, shortAdaptor);
|
260 |
2
| _numberAdaptors.register(short.class, shortAdaptor);
|
261 |
2
| _numberAdaptors.register(Integer.class, intAdaptor);
|
262 |
2
| _numberAdaptors.register(int.class, intAdaptor);
|
263 |
2
| _numberAdaptors.register(Long.class, longAdaptor);
|
264 |
2
| _numberAdaptors.register(long.class, longAdaptor);
|
265 |
2
| _numberAdaptors.register(Float.class, floatAdaptor);
|
266 |
2
| _numberAdaptors.register(float.class, floatAdaptor);
|
267 |
2
| _numberAdaptors.register(Double.class, doubleAdaptor);
|
268 |
2
| _numberAdaptors.register(double.class, doubleAdaptor);
|
269 |
| |
270 |
2
| _numberAdaptors.register(BigDecimal.class, new BigDecimalAdaptor());
|
271 |
2
| _numberAdaptors.register(BigInteger.class, new BigIntegerAdaptor());
|
272 |
| } |
273 |
| |
274 |
48
| public NumberValidator()
|
275 |
| { |
276 |
| |
277 |
| } |
278 |
| |
279 |
| |
280 |
| |
281 |
| |
282 |
| |
283 |
| |
284 |
| |
285 |
0
| public NumberValidator(String initializer)
|
286 |
| { |
287 |
0
| PropertyUtils.configureProperties(this, initializer);
|
288 |
| } |
289 |
| |
290 |
26
| public String toString(IFormComponent field, Object value)
|
291 |
| { |
292 |
26
| if (value == null)
|
293 |
0
| return null;
|
294 |
| |
295 |
26
| if (getZeroIsNull())
|
296 |
| { |
297 |
0
| Number number = (Number) value;
|
298 |
| |
299 |
0
| if (number.doubleValue() == 0.0)
|
300 |
0
| return null;
|
301 |
| } |
302 |
| |
303 |
26
| return value.toString();
|
304 |
| } |
305 |
| |
306 |
30
| private NumberStrategy getStrategy(IFormComponent field)
|
307 |
| { |
308 |
30
| NumberStrategy result = getStrategy(_valueTypeClass);
|
309 |
| |
310 |
30
| if (result == null)
|
311 |
0
| throw new ApplicationRuntimeException(Tapestry.format(
|
312 |
| "NumberValidator.no-adaptor-for-field", |
313 |
| field, |
314 |
| _valueTypeClass.getName())); |
315 |
| |
316 |
30
| return result;
|
317 |
| } |
318 |
| |
319 |
| |
320 |
| |
321 |
| |
322 |
| |
323 |
| |
324 |
| |
325 |
| |
326 |
| |
327 |
| |
328 |
| |
329 |
| |
330 |
62
| public static NumberStrategy getStrategy(Class type)
|
331 |
| { |
332 |
62
| return (NumberStrategy) _numberAdaptors.getStrategy(type);
|
333 |
| } |
334 |
| |
335 |
30
| public Object toObject(IFormComponent field, String value) throws ValidatorException
|
336 |
| { |
337 |
30
| if (checkRequired(field, value))
|
338 |
0
| return null;
|
339 |
| |
340 |
30
| NumberStrategy adaptor = getStrategy(field);
|
341 |
30
| Number result = null;
|
342 |
| |
343 |
30
| try
|
344 |
| { |
345 |
30
| result = adaptor.parse(value);
|
346 |
| } |
347 |
| catch (NumberFormatException ex) |
348 |
| { |
349 |
4
| throw new ValidatorException(buildInvalidNumericFormatMessage(field),
|
350 |
| ValidationConstraint.NUMBER_FORMAT); |
351 |
| } |
352 |
| |
353 |
26
| if (_minimum != null && adaptor.compare(result, _minimum) < 0)
|
354 |
4
| throw new ValidatorException(buildNumberTooSmallMessage(field, _minimum),
|
355 |
| ValidationConstraint.TOO_SMALL); |
356 |
| |
357 |
22
| if (_maximum != null && adaptor.compare(result, _maximum) > 0)
|
358 |
4
| throw new ValidatorException(buildNumberTooLargeMessage(field, _maximum),
|
359 |
| ValidationConstraint.TOO_LARGE); |
360 |
| |
361 |
18
| return result;
|
362 |
| } |
363 |
| |
364 |
0
| public Number getMaximum()
|
365 |
| { |
366 |
0
| return _maximum;
|
367 |
| } |
368 |
| |
369 |
0
| public boolean getHasMaximum()
|
370 |
| { |
371 |
0
| return _maximum != null;
|
372 |
| } |
373 |
| |
374 |
8
| public void setMaximum(Number maximum)
|
375 |
| { |
376 |
8
| _maximum = maximum;
|
377 |
| } |
378 |
| |
379 |
0
| public Number getMinimum()
|
380 |
| { |
381 |
0
| return _minimum;
|
382 |
| } |
383 |
| |
384 |
0
| public boolean getHasMinimum()
|
385 |
| { |
386 |
0
| return _minimum != null;
|
387 |
| } |
388 |
| |
389 |
8
| public void setMinimum(Number minimum)
|
390 |
| { |
391 |
8
| _minimum = minimum;
|
392 |
| } |
393 |
| |
394 |
| |
395 |
| |
396 |
| |
397 |
| |
398 |
0
| public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
|
399 |
| IRequestCycle cycle) |
400 |
| { |
401 |
0
| if (!isClientScriptingEnabled())
|
402 |
0
| return;
|
403 |
| |
404 |
0
| if (!(isRequired() || _minimum != null || _maximum != null))
|
405 |
0
| return;
|
406 |
| |
407 |
0
| Map symbols = new HashMap();
|
408 |
| |
409 |
0
| if (isRequired())
|
410 |
0
| symbols.put("requiredMessage", buildRequiredMessage(field));
|
411 |
| |
412 |
0
| if (isIntegerNumber())
|
413 |
0
| symbols.put("formatMessage", buildInvalidIntegerFormatMessage(field));
|
414 |
| else |
415 |
0
| symbols.put("formatMessage", buildInvalidNumericFormatMessage(field));
|
416 |
| |
417 |
0
| if (_minimum != null || _maximum != null)
|
418 |
0
| symbols.put("rangeMessage", buildRangeMessage(field, _minimum, _maximum));
|
419 |
| |
420 |
0
| processValidatorScript(getScriptPath(), cycle, field, symbols);
|
421 |
| } |
422 |
| |
423 |
| |
424 |
| |
425 |
| |
426 |
| |
427 |
| |
428 |
| |
429 |
| |
430 |
| |
431 |
0
| public void setValueType(String typeName)
|
432 |
| { |
433 |
0
| Class typeClass = (Class) TYPES.get(typeName);
|
434 |
| |
435 |
0
| if (typeClass == null)
|
436 |
0
| throw new ApplicationRuntimeException(Tapestry.format(
|
437 |
| "NumberValidator.unknown-type", |
438 |
| typeName)); |
439 |
| |
440 |
0
| _valueTypeClass = typeClass;
|
441 |
| } |
442 |
| |
443 |
| |
444 |
| |
445 |
30
| public void setValueTypeClass(Class valueTypeClass)
|
446 |
| { |
447 |
30
| _valueTypeClass = valueTypeClass;
|
448 |
| } |
449 |
| |
450 |
| |
451 |
| |
452 |
| |
453 |
| |
454 |
| |
455 |
| |
456 |
0
| public Class getValueTypeClass()
|
457 |
| { |
458 |
0
| return _valueTypeClass;
|
459 |
| } |
460 |
| |
461 |
| |
462 |
| |
463 |
0
| public boolean isIntegerNumber()
|
464 |
| { |
465 |
0
| NumberStrategy strategy = (NumberStrategy) _numberAdaptors.getStrategy(_valueTypeClass);
|
466 |
0
| if (strategy == null)
|
467 |
0
| return false;
|
468 |
| |
469 |
0
| return strategy.getNumberType() == NUMBER_TYPE_INTEGER;
|
470 |
| } |
471 |
| |
472 |
48
| protected String getDefaultScriptPath()
|
473 |
| { |
474 |
48
| return "/org/apache/tapestry/valid/NumberValidator.script";
|
475 |
| } |
476 |
| } |