1 package org.apache.turbine.services.intake.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.math.BigDecimal;
20
21 import java.text.DecimalFormatSymbols;
22
23 import org.apache.commons.lang.StringUtils;
24
25 import org.apache.turbine.services.intake.IntakeException;
26 import org.apache.turbine.services.intake.validator.BigDecimalValidator;
27 import org.apache.turbine.services.intake.xmlmodel.XmlField;
28
29 /***
30 * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
31 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
32 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
33 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
34 * @version $Id: BigDecimalField.java 264148 2005-08-29 14:21:04Z henning $
35 */
36 public class BigDecimalField
37 extends Field
38 {
39 /***
40 * Constructor.
41 *
42 * @param field xml field definition object
43 * @param group xml group definition object
44 * @throws IntakeException thrown by superclass
45 */
46 public BigDecimalField(XmlField field, Group group)
47 throws IntakeException
48 {
49 super(field, group);
50 }
51
52 /***
53 * Sets the default value for a BigDecimal field
54 *
55 * @param prop Parameter for the default values
56 */
57 public void setDefaultValue(String prop)
58 {
59 defaultValue = null;
60
61 if (prop == null)
62 {
63 return;
64 }
65
66 defaultValue = new BigDecimal(prop);
67 }
68
69 /***
70 * Set the empty Value. This value is used if Intake
71 * maps a field to a parameter returned by the user and
72 * the corresponding field is either empty (empty string)
73 * or non-existant.
74 *
75 * @param prop The value to use if the field is empty.
76 */
77 public void setEmptyValue(String prop)
78 {
79 emptyValue = null;
80
81 if (prop == null)
82 {
83 return;
84 }
85
86 emptyValue = new BigDecimal(prop);
87 }
88
89 /***
90 * A suitable validator.
91 *
92 * @return A suitable validator
93 */
94 protected String getDefaultValidator()
95 {
96 return BigDecimalValidator.class.getName();
97 }
98
99 /***
100 * Sets the value of the field from data in the parser.
101 */
102 protected void doSetValue()
103 {
104 if (isMultiValued)
105 {
106 String[] inputs = parser.getStrings(getKey());
107 BigDecimal[] values = new BigDecimal[inputs.length];
108 for (int i = 0; i < inputs.length; i++)
109 {
110 values[i] = StringUtils.isNotEmpty(inputs[i])
111 ? canonicalizeDecimalInput(inputs[i]) : (BigDecimal) getEmptyValue();
112 }
113 setTestValue(values);
114 }
115 else
116 {
117 String val = parser.getString(getKey());
118 setTestValue(StringUtils.isNotEmpty(val) ? canonicalizeDecimalInput(val) : (BigDecimal) getEmptyValue());
119 }
120 }
121
122 /***
123 * Canonicalizes an user-inputted <code>BigDecimal</code> string
124 * to the system's internal format.
125 *
126 * @param bigDecimal Text conforming to a <code>BigDecimal</code>
127 * description for a set of <code>DecimalFormatSymbols</code>.
128 * @return The canonicalized representation.
129 */
130 protected final BigDecimal canonicalizeDecimalInput(String bigDecimal)
131 {
132 if (getLocale() != null)
133 {
134 DecimalFormatSymbols internal = new DecimalFormatSymbols();
135 DecimalFormatSymbols user = new DecimalFormatSymbols(getLocale());
136
137 if (!internal.equals(user))
138 {
139 bigDecimal = bigDecimal.replace(user.getDecimalSeparator(),
140 internal.getDecimalSeparator());
141 }
142 }
143 return new BigDecimal(bigDecimal);
144 }
145 }