%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.services.intake.validator.DefaultValidator |
|
|
1 | package org.apache.turbine.services.intake.validator; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import java.util.Map; |
|
20 | ||
21 | import org.apache.commons.lang.StringUtils; |
|
22 | import org.apache.commons.logging.Log; |
|
23 | import org.apache.commons.logging.LogFactory; |
|
24 | ||
25 | /** |
|
26 | * DefaultValidator that will compare a testValue against the following |
|
27 | * constraints: |
|
28 | * |
|
29 | * <table> |
|
30 | * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr> |
|
31 | * <tr><td>required</td><td>true|false</td><td>false</td></tr> |
|
32 | * <tr><td>mask</td><td>regexp</td><td> </td></tr> |
|
33 | * <tr><td>minLength</td><td>integer</td><td>0</td></tr> |
|
34 | * <tr><td>maxLength</td><td>integer</td><td> </td></tr> |
|
35 | * </table> |
|
36 | * |
|
37 | * This validator can serve as the base class for more specific validators |
|
38 | * |
|
39 | * @author <a href="mailto:jmcnally@collab.net">John McNally</a> |
|
40 | * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a> |
|
41 | * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a> |
|
42 | * @version $Id: DefaultValidator.java 264148 2005-08-29 14:21:04Z henning $ |
|
43 | */ |
|
44 | abstract public class DefaultValidator |
|
45 | implements Validator, InitableByConstraintMap |
|
46 | { |
|
47 | /** A boolean value to signify if the field is definately required or not */ |
|
48 | 0 | protected boolean required = false; |
49 | ||
50 | /** The message to show if field fails required test */ |
|
51 | 0 | protected String requiredMessage = null; |
52 | ||
53 | /** The minimum length of the field */ |
|
54 | 0 | protected int minLength = 0; |
55 | ||
56 | /** The message to show if field fails min-length test */ |
|
57 | 0 | protected String minLengthMessage = null; |
58 | ||
59 | /** The maximum length of the field */ |
|
60 | 0 | protected int maxLength = 0; |
61 | ||
62 | /** The message to show if field fails max-length test */ |
|
63 | 0 | protected String maxLengthMessage = null; |
64 | ||
65 | /** Error message pertaining to Rule that was broken */ |
|
66 | 0 | protected String errorMessage = null; |
67 | ||
68 | /** Logging */ |
|
69 | 0 | protected Log log = LogFactory.getLog(this.getClass()); |
70 | ||
71 | /** |
|
72 | * Constructor |
|
73 | * |
|
74 | * @param paramMap a <code>Map</code> of <code>Rule</code>'s |
|
75 | * containing constraints on the input. |
|
76 | * @exception InvalidMaskException An invalid mask was specified for one of the rules |
|
77 | ||
78 | */ |
|
79 | public DefaultValidator(Map paramMap) |
|
80 | throws InvalidMaskException |
|
81 | 0 | { |
82 | 0 | init(paramMap); |
83 | 0 | } |
84 | ||
85 | /** |
|
86 | * Default constructor |
|
87 | */ |
|
88 | public DefaultValidator() |
|
89 | 0 | { |
90 | 0 | } |
91 | ||
92 | /** |
|
93 | * Extract the relevant parameters from the constraints listed |
|
94 | * in <rule> tags within the intake.xml file. |
|
95 | * |
|
96 | * @param paramMap a <code>Map</code> of <code>Rule</code>'s |
|
97 | * containing constraints on the input. |
|
98 | * @exception InvalidMaskException An invalid mask was specified for one of the rules |
|
99 | */ |
|
100 | public void init(Map paramMap) |
|
101 | throws InvalidMaskException |
|
102 | { |
|
103 | 0 | Constraint constraint = (Constraint) paramMap.get(REQUIRED_RULE_NAME); |
104 | 0 | if (constraint != null) |
105 | { |
|
106 | 0 | String param = constraint.getValue(); |
107 | 0 | required = Boolean.valueOf(param).booleanValue(); |
108 | 0 | requiredMessage = constraint.getMessage(); |
109 | } |
|
110 | ||
111 | 0 | constraint = (Constraint) paramMap.get(MIN_LENGTH_RULE_NAME); |
112 | 0 | if (constraint != null) |
113 | { |
|
114 | 0 | String param = constraint.getValue(); |
115 | 0 | minLength = Integer.parseInt(param); |
116 | 0 | minLengthMessage = constraint.getMessage(); |
117 | } |
|
118 | ||
119 | 0 | constraint = (Constraint) paramMap.get(MAX_LENGTH_RULE_NAME); |
120 | 0 | if (constraint != null) |
121 | { |
|
122 | 0 | String param = constraint.getValue(); |
123 | 0 | maxLength = Integer.parseInt(param); |
124 | 0 | maxLengthMessage = constraint.getMessage(); |
125 | } |
|
126 | 0 | } |
127 | ||
128 | /** |
|
129 | * Determine whether a testValue meets the criteria specified |
|
130 | * in the constraints defined for this validator |
|
131 | * |
|
132 | * @param testValue a <code>String</code> to be tested |
|
133 | * @return true if valid, false otherwise |
|
134 | */ |
|
135 | public boolean isValid(String testValue) |
|
136 | { |
|
137 | 0 | boolean valid = false; |
138 | try |
|
139 | { |
|
140 | 0 | assertValidity(testValue); |
141 | 0 | valid = true; |
142 | } |
|
143 | 0 | catch (ValidationException ve) |
144 | { |
|
145 | 0 | valid = false; |
146 | 0 | } |
147 | 0 | return valid; |
148 | } |
|
149 | ||
150 | /** |
|
151 | * Determine whether a testValue meets the criteria specified |
|
152 | * in the constraints defined for this validator |
|
153 | * |
|
154 | * @param testValue a <code>String</code> to be tested |
|
155 | * @exception ValidationException containing an error message if the |
|
156 | * testValue did not pass the validation tests. |
|
157 | */ |
|
158 | public void assertValidity(String testValue) |
|
159 | throws ValidationException |
|
160 | { |
|
161 | 0 | if (!required && StringUtils.isEmpty(testValue)) |
162 | { |
|
163 | 0 | return; |
164 | } |
|
165 | 0 | if (required && StringUtils.isEmpty(testValue)) |
166 | { |
|
167 | 0 | errorMessage = requiredMessage; |
168 | 0 | throw new ValidationException(requiredMessage); |
169 | } |
|
170 | ||
171 | 0 | if (minLength > 0 && testValue.length() < minLength) |
172 | { |
|
173 | 0 | errorMessage = minLengthMessage; |
174 | 0 | throw new ValidationException(minLengthMessage); |
175 | } |
|
176 | 0 | if (maxLength > 0 && testValue.length() > maxLength) |
177 | { |
|
178 | 0 | errorMessage = maxLengthMessage; |
179 | 0 | throw new ValidationException(maxLengthMessage); |
180 | } |
|
181 | 0 | } |
182 | ||
183 | ||
184 | /** |
|
185 | * Get the error message resulting from invalid input. |
|
186 | * |
|
187 | * @return a <code>String</code> message, or the empty String "". |
|
188 | */ |
|
189 | public String getMessage() |
|
190 | { |
|
191 | 0 | String retValue = ""; |
192 | ||
193 | 0 | if(errorMessage != null) |
194 | { |
|
195 | 0 | retValue = errorMessage; |
196 | } |
|
197 | ||
198 | 0 | return retValue; |
199 | } |
|
200 | ||
201 | // ************************************************************ |
|
202 | // ** Bean accessor methods ** |
|
203 | // ************************************************************ |
|
204 | ||
205 | /** |
|
206 | * Get the value of required. |
|
207 | * |
|
208 | * @return value of required. |
|
209 | */ |
|
210 | public boolean isRequired() |
|
211 | { |
|
212 | 0 | return required; |
213 | } |
|
214 | ||
215 | /** |
|
216 | * Set the value of required. |
|
217 | * |
|
218 | * @param required Value to assign to required. |
|
219 | */ |
|
220 | public void setRequired(boolean required) |
|
221 | { |
|
222 | 0 | this.required = required; |
223 | 0 | } |
224 | ||
225 | /** |
|
226 | * Get the value of requiredMessage. |
|
227 | * |
|
228 | * @return value of requiredMessage. |
|
229 | */ |
|
230 | public String getRequiredMessage() |
|
231 | { |
|
232 | 0 | return requiredMessage; |
233 | } |
|
234 | ||
235 | /** |
|
236 | * Set the value of requiredMessage. |
|
237 | * |
|
238 | * @param requiredMessage Value to assign to requiredMessage. |
|
239 | */ |
|
240 | public void setRequiredMessage(String requiredMessage) |
|
241 | { |
|
242 | 0 | this.requiredMessage = requiredMessage; |
243 | 0 | } |
244 | ||
245 | /** |
|
246 | * Get the value of minLength. |
|
247 | * |
|
248 | * @return value of minLength. |
|
249 | */ |
|
250 | public int getMinLength() |
|
251 | { |
|
252 | 0 | return minLength; |
253 | } |
|
254 | ||
255 | /** |
|
256 | * Set the value of minLength. |
|
257 | * |
|
258 | * @param minLength Value to assign to minLength. |
|
259 | */ |
|
260 | public void setMinLength(int minLength) |
|
261 | { |
|
262 | 0 | this.minLength = minLength; |
263 | 0 | } |
264 | ||
265 | /** |
|
266 | * Get the value of minLengthMessage. |
|
267 | * |
|
268 | * @return value of minLengthMessage. |
|
269 | */ |
|
270 | public String getMinLengthMessage() |
|
271 | { |
|
272 | 0 | return minLengthMessage; |
273 | } |
|
274 | ||
275 | /** |
|
276 | * Set the value of minLengthMessage. |
|
277 | * |
|
278 | * @param minLengthMessage Value to assign to minLengthMessage. |
|
279 | */ |
|
280 | public void setMinLengthMessage(String minLengthMessage) |
|
281 | { |
|
282 | 0 | this.minLengthMessage = minLengthMessage; |
283 | 0 | } |
284 | ||
285 | /** |
|
286 | * Get the value of maxLength. |
|
287 | * |
|
288 | * @return value of maxLength. |
|
289 | */ |
|
290 | public int getMaxLength() |
|
291 | { |
|
292 | 0 | return maxLength; |
293 | } |
|
294 | ||
295 | /** |
|
296 | * Set the value of maxLength. |
|
297 | * |
|
298 | * @param maxLength Value to assign to maxLength. |
|
299 | */ |
|
300 | public void setMaxLength(int maxLength) |
|
301 | { |
|
302 | 0 | this.maxLength = maxLength; |
303 | 0 | } |
304 | ||
305 | /** |
|
306 | * Get the value of maxLengthMessage. |
|
307 | * |
|
308 | * @return value of maxLengthMessage. |
|
309 | */ |
|
310 | public String getMaxLengthMessage() |
|
311 | { |
|
312 | 0 | return maxLengthMessage; |
313 | } |
|
314 | ||
315 | /** |
|
316 | * Set the value of maxLengthMessage. |
|
317 | * |
|
318 | * @param maxLengthMessage Value to assign to maxLengthMessage. |
|
319 | */ |
|
320 | public void setMaxLengthMessage(String maxLengthMessage) |
|
321 | { |
|
322 | 0 | this.maxLengthMessage = maxLengthMessage; |
323 | 0 | } |
324 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |