1 package org.apache.turbine.services.intake.validator;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.text.NumberFormat;
23 import java.text.ParseException;
24 import java.util.Locale;
25 import java.util.Map;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.turbine.services.intake.model.Field;
29
30 /***
31 * Validates Floats with the following constraints in addition to those
32 * listed in NumberValidator and DefaultValidator.
33 *
34 * <table>
35 * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
36 * <tr><td>minValue</td><td>greater than Float.MIN_VALUE</td>
37 * <td> </td></tr>
38 * <tr><td>maxValue</td><td>less than Float.MAX_VALUE</td>
39 * <td> </td></tr>
40 * <tr><td>invalidNumberMessage</td><td>Some text</td>
41 * <td>Entry was not a valid number</td></tr>
42 * </table>
43 *
44 * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
45 * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
46 * @author <a href="mailto:jh@byteaction.de">Jürgen Hoffmann</a>
47 * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
48 * @version $Id: FloatValidator.java 534527 2007-05-02 16:10:59Z tv $
49 */
50 public class FloatValidator
51 extends NumberValidator
52 {
53
54 private float minValue = Float.NEGATIVE_INFINITY;
55
56
57 private float maxValue = Float.POSITIVE_INFINITY;
58
59 /***
60 * Constructor to use when initialising Object
61 *
62 * @param paramMap
63 * @throws InvalidMaskException
64 */
65 public FloatValidator(Map paramMap)
66 throws InvalidMaskException
67 {
68 invalidNumberMessage = "Entry was not a valid Float";
69 init(paramMap);
70 }
71
72 /***
73 * Default Constructor
74 */
75 public FloatValidator()
76 {
77 }
78
79 /***
80 * Method to initialise Object
81 *
82 * @param paramMap
83 * @throws InvalidMaskException
84 */
85 public void init(Map paramMap)
86 throws InvalidMaskException
87 {
88 super.init(paramMap);
89
90 Constraint constraint = (Constraint) paramMap.get(MIN_VALUE_RULE_NAME);
91 if (constraint != null)
92 {
93 String param = constraint.getValue();
94 minValue = Float.parseFloat(param);
95 minValueMessage = constraint.getMessage();
96 }
97
98 constraint = (Constraint) paramMap.get(MAX_VALUE_RULE_NAME);
99 if (constraint != null)
100 {
101 String param = constraint.getValue();
102 maxValue = Float.parseFloat(param);
103 maxValueMessage = constraint.getMessage();
104 }
105 }
106
107 /***
108 * Determine whether a field meets the criteria specified
109 * in the constraints defined for this validator
110 *
111 * @param field a <code>Field</code> to be tested
112 * @exception ValidationException containing an error message if the
113 * testValue did not pass the validation tests.
114 */
115 public void assertValidity(Field field)
116 throws ValidationException
117 {
118 Locale locale = field.getLocale();
119
120 if (field.isMultiValued())
121 {
122 String[] stringValues = (String[])field.getTestValue();
123
124 for (int i = 0; i < stringValues.length; i++)
125 {
126 assertValidity(stringValues[i], locale);
127 }
128 }
129 else
130 {
131 assertValidity((String)field.getTestValue(), locale);
132 }
133 }
134
135 /***
136 * Determine whether a testValue meets the criteria specified
137 * in the constraints defined for this validator
138 *
139 * @param testValue a <code>String</code> to be tested
140 * @param locale the Locale of the associated field
141 * @exception ValidationException containing an error message if the
142 * testValue did not pass the validation tests.
143 */
144 public void assertValidity(String testValue, Locale locale)
145 throws ValidationException
146 {
147 super.assertValidity(testValue);
148
149 if (required || StringUtils.isNotEmpty(testValue))
150 {
151 float f = 0.0f;
152 NumberFormat nf = NumberFormat.getInstance(locale);
153
154 try
155 {
156 f = nf.parse(testValue).floatValue();
157 }
158 catch (ParseException e)
159 {
160 errorMessage = invalidNumberMessage;
161 throw new ValidationException(invalidNumberMessage);
162 }
163
164 if (f < minValue)
165 {
166 errorMessage = minValueMessage;
167 throw new ValidationException(minValueMessage);
168 }
169 if (f > maxValue)
170 {
171 errorMessage = maxValueMessage;
172 throw new ValidationException(maxValueMessage);
173 }
174 }
175 }
176
177
178
179
180
181
182 /***
183 * Get the value of minValue.
184 *
185 * @return value of minValue.
186 */
187 public float getMinValue()
188 {
189 return minValue;
190 }
191
192 /***
193 * Set the value of minValue.
194 *
195 * @param minValue Value to assign to minValue.
196 */
197 public void setMinValue(float minValue)
198 {
199 this.minValue = minValue;
200 }
201
202 /***
203 * Get the value of maxValue.
204 *
205 * @return value of maxValue.
206 */
207 public float getMaxValue()
208 {
209 return maxValue;
210 }
211
212 /***
213 * Set the value of maxValue.
214 *
215 * @param maxValue Value to assign to maxValue.
216 */
217 public void setMaxValue(float maxValue)
218 {
219 this.maxValue = maxValue;
220 }
221 }