1 | |
package org.apache.tapestry.form.translator; |
2 | |
|
3 | |
import org.apache.hivemind.ApplicationRuntimeException; |
4 | |
import org.apache.tapestry.form.IFormComponent; |
5 | |
import org.apache.tapestry.form.ValidationMessages; |
6 | |
import org.apache.tapestry.valid.ValidationConstraint; |
7 | |
import org.apache.tapestry.valid.ValidationStrings; |
8 | |
import org.apache.tapestry.valid.ValidatorException; |
9 | |
|
10 | |
import java.math.BigDecimal; |
11 | |
import java.util.Locale; |
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
public class BigDecimalTranslator extends AbstractTranslator { |
17 | |
|
18 | |
public BigDecimalTranslator() |
19 | 0 | { |
20 | 0 | } |
21 | |
|
22 | |
|
23 | |
public BigDecimalTranslator(String initializer) |
24 | |
{ |
25 | 0 | super(initializer); |
26 | 0 | } |
27 | |
|
28 | |
protected String formatObject(IFormComponent field, Locale locale, Object object) |
29 | |
{ |
30 | 0 | if (!BigDecimal.class.isInstance(object)) |
31 | 0 | throw new ApplicationRuntimeException("BigDecimalTranslator translates values of type BigDecimal, not: " + object.getClass()); |
32 | |
|
33 | 0 | return object.toString(); |
34 | |
} |
35 | |
|
36 | |
protected Object parseText(IFormComponent field, ValidationMessages messages, String text) |
37 | |
throws ValidatorException |
38 | |
{ |
39 | |
try { |
40 | |
|
41 | 0 | return new BigDecimal(text); |
42 | |
} |
43 | 0 | catch (NumberFormatException e) { |
44 | 0 | throw new ValidatorException(buildMessage(messages, field, ValidationStrings.INVALID_NUMBER), ValidationConstraint.NUMBER_FORMAT); |
45 | |
} |
46 | |
} |
47 | |
} |