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.util.Map;
23
24 import org.apache.torque.om.NumberKey;
25
26 /***
27 * Validates numbers with the following constraints in addition to those
28 * listed in DefaultValidator.
29 *
30 * <table>
31 * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
32 * <tr><td>minValue</td><td>greater than Integer.MIN_VALUE</td>
33 * <td> </td></tr>
34 * <tr><td>maxValue</td><td>less than BigDecimal.MAX_VALUE</td>
35 * <td> </td></tr>
36 * <tr><td>notANumberMessage</td><td>Some text</td>
37 * <td>Entry was not a valid number</td></tr>
38 * </table>
39 *
40 * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
41 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
42 * @version $Id: NumberKeyValidator.java 534527 2007-05-02 16:10:59Z tv $
43 * @deprecated No replacement
44 */
45 public class NumberKeyValidator
46 extends NumberValidator
47 {
48 private static String INVALID_NUMBER = "Entry was not valid.";
49
50 private NumberKey minValue;
51 private NumberKey maxValue;
52
53 public NumberKeyValidator(Map paramMap)
54 throws InvalidMaskException
55 {
56 this();
57 init(paramMap);
58 }
59
60 public NumberKeyValidator()
61 {
62
63 super();
64 }
65
66 protected void doInit(Map paramMap)
67 {
68 minValue = null;
69 maxValue = null;
70
71 Constraint constraint = (Constraint) paramMap.get(MIN_VALUE_RULE_NAME);
72 if (constraint != null)
73 {
74 String param = constraint.getValue();
75 minValue = new NumberKey(param);
76 minValueMessage = constraint.getMessage();
77 }
78
79 constraint = (Constraint) paramMap.get(MAX_VALUE_RULE_NAME);
80 if (constraint != null)
81 {
82 String param = constraint.getValue();
83 maxValue = new NumberKey(param);
84 maxValueMessage = constraint.getMessage();
85 }
86 }
87
88 protected String getDefaultInvalidNumberMessage()
89 {
90 return INVALID_NUMBER;
91 }
92
93 /***
94 * Determine whether a testValue meets the criteria specified
95 * in the constraints defined for this validator
96 *
97 * @param testValue a <code>String</code> to be tested
98 * @exception ValidationException containing an error message if the
99 * testValue did not pass the validation tests.
100 */
101 public void assertValidity(String testValue)
102 throws ValidationException
103 {
104 NumberKey nk = null;
105 try
106 {
107 nk = new NumberKey(testValue);
108 }
109 catch (RuntimeException e)
110 {
111 errorMessage = invalidNumberMessage;
112 throw new ValidationException(invalidNumberMessage);
113 }
114 if (minValue != null && nk.compareTo(minValue) < 0)
115 {
116 errorMessage = minValueMessage;
117 throw new ValidationException(minValueMessage);
118 }
119 if (maxValue != null && nk.compareTo(maxValue) > 0)
120 {
121 errorMessage = maxValueMessage;
122 throw new ValidationException(maxValueMessage);
123 }
124 }
125
126
127
128
129
130
131 /***
132 * Get the value of minValue.
133 *
134 * @return value of minValue.
135 */
136 public NumberKey getMinValue()
137 {
138 return minValue;
139 }
140
141 /***
142 * Set the value of minValue.
143 *
144 * @param minValue Value to assign to minValue.
145 */
146 public void setMinValue(NumberKey minValue)
147 {
148 this.minValue = minValue;
149 }
150
151 /***
152 * Get the value of maxValue.
153 *
154 * @return value of maxValue.
155 */
156 public NumberKey getMaxValue()
157 {
158 return maxValue;
159 }
160
161 /***
162 * Set the value of maxValue.
163 *
164 * @param maxValue Value to assign to maxValue.
165 */
166 public void setMaxValue(NumberKey maxValue)
167 {
168 this.maxValue = maxValue;
169 }
170 }