%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.turbine.services.intake.validator.StringValidator |
|
|
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 | ||
23 | import org.apache.oro.text.regex.MalformedPatternException; |
|
24 | import org.apache.oro.text.regex.Pattern; |
|
25 | import org.apache.oro.text.regex.Perl5Compiler; |
|
26 | import org.apache.oro.text.regex.Perl5Matcher; |
|
27 | ||
28 | /** |
|
29 | * A validator that will compare a testValue against the following |
|
30 | * constraints: |
|
31 | * <table> |
|
32 | * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr> |
|
33 | * <tr><td>required</td><td>true|false</td><td>false</td></tr> |
|
34 | * <tr><td>mask</td><td>regexp</td><td> </td></tr> |
|
35 | * <tr><td>minLength</td><td>integer</td><td>0</td></tr> |
|
36 | * <tr><td>maxLength</td><td>integer</td><td> </td></tr> |
|
37 | * </table> |
|
38 | * |
|
39 | * This validator can serve as the base class for more specific validators |
|
40 | * |
|
41 | * @author <a href="mailto:jmcnally@collab.net">John McNally</a> |
|
42 | * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a> |
|
43 | * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a> |
|
44 | * @version $Id: StringValidator.java 264148 2005-08-29 14:21:04Z henning $ |
|
45 | */ |
|
46 | public class StringValidator |
|
47 | extends DefaultValidator |
|
48 | { |
|
49 | /** The matching mask String as supplied by the XML input */ |
|
50 | 0 | protected String maskString = null; |
51 | ||
52 | /** The compiled perl5 Regular expression from the ORO Perl5Compiler */ |
|
53 | 0 | protected Pattern maskPattern = null; |
54 | ||
55 | /** The message to report if the mask constraint is not satisfied */ |
|
56 | 0 | protected String maskMessage = null; |
57 | ||
58 | ||
59 | /** |
|
60 | * Constructor |
|
61 | * |
|
62 | * @param paramMap a <code>Map</code> of <code>Rule</code>'s |
|
63 | * containing constraints on the input. |
|
64 | * @exception InvalidMaskException An invalid mask was specified for one of the rules |
|
65 | ||
66 | */ |
|
67 | public StringValidator(Map paramMap) |
|
68 | throws InvalidMaskException |
|
69 | 0 | { |
70 | 0 | init(paramMap); |
71 | 0 | } |
72 | ||
73 | /** |
|
74 | * Default constructor |
|
75 | */ |
|
76 | public StringValidator() |
|
77 | 0 | { |
78 | 0 | } |
79 | ||
80 | /** |
|
81 | * Extract the relevant parameters from the constraints listed |
|
82 | * in <rule> tags within the intake.xml file. |
|
83 | * |
|
84 | * @param paramMap a <code>Map</code> of <code>Rule</code>'s |
|
85 | * containing constraints on the input. |
|
86 | * @exception InvalidMaskException An invalid mask was specified for one of the rules |
|
87 | */ |
|
88 | public void init(Map paramMap) |
|
89 | throws InvalidMaskException |
|
90 | { |
|
91 | 0 | super.init(paramMap); |
92 | ||
93 | 0 | Constraint constraint = (Constraint) paramMap.get(MASK_RULE_NAME); |
94 | 0 | if (constraint != null) |
95 | { |
|
96 | 0 | String param = constraint.getValue(); |
97 | 0 | setMask(param); |
98 | 0 | maskMessage = constraint.getMessage(); |
99 | } |
|
100 | ||
101 | 0 | } |
102 | ||
103 | /** |
|
104 | * Determine whether a testValue meets the criteria specified |
|
105 | * in the constraints defined for this validator |
|
106 | * |
|
107 | * @param testValue a <code>String</code> to be tested |
|
108 | * @return true if valid, false otherwise |
|
109 | */ |
|
110 | public boolean isValid(String testValue) |
|
111 | { |
|
112 | 0 | boolean valid = false; |
113 | try |
|
114 | { |
|
115 | 0 | assertValidity(testValue); |
116 | 0 | valid = true; |
117 | } |
|
118 | 0 | catch (ValidationException ve) |
119 | { |
|
120 | 0 | valid = false; |
121 | 0 | } |
122 | 0 | return valid; |
123 | } |
|
124 | ||
125 | /** |
|
126 | * Determine whether a testValue meets the criteria specified |
|
127 | * in the constraints defined for this validator |
|
128 | * |
|
129 | * @param testValue a <code>String</code> to be tested |
|
130 | * @exception ValidationException containing an error message if the |
|
131 | * testValue did not pass the validation tests. |
|
132 | */ |
|
133 | public void assertValidity(String testValue) |
|
134 | throws ValidationException |
|
135 | { |
|
136 | 0 | super.assertValidity(testValue); |
137 | ||
138 | 0 | if (required || StringUtils.isNotEmpty(testValue)) |
139 | { |
|
140 | 0 | if (maskPattern != null) |
141 | { |
|
142 | /** perl5 matcher */ |
|
143 | 0 | Perl5Matcher patternMatcher = new Perl5Matcher(); |
144 | ||
145 | 0 | boolean patternMatch = |
146 | patternMatcher.matches(testValue, maskPattern); |
|
147 | ||
148 | 0 | log.debug("Trying to match " + testValue |
149 | + " to pattern " + maskString); |
|
150 | ||
151 | 0 | if (!patternMatch) |
152 | { |
|
153 | 0 | errorMessage = maskMessage; |
154 | 0 | throw new ValidationException(maskMessage); |
155 | } |
|
156 | } |
|
157 | } |
|
158 | 0 | } |
159 | ||
160 | // ************************************************************ |
|
161 | // ** Bean accessor methods ** |
|
162 | // ************************************************************ |
|
163 | ||
164 | /** |
|
165 | * Get the value of mask. |
|
166 | * |
|
167 | * @return value of mask. |
|
168 | */ |
|
169 | public String getMask() |
|
170 | { |
|
171 | 0 | return maskString; |
172 | } |
|
173 | ||
174 | /** |
|
175 | * Set the value of mask. |
|
176 | * |
|
177 | * @param mask Value to assign to mask. |
|
178 | * @throws InvalidMaskException the mask could not be compiled. |
|
179 | */ |
|
180 | public void setMask(String mask) |
|
181 | throws InvalidMaskException |
|
182 | { |
|
183 | /** perl5 compiler, needed for setting up the masks */ |
|
184 | 0 | Perl5Compiler patternCompiler = new Perl5Compiler(); |
185 | ||
186 | 0 | maskString = mask; |
187 | ||
188 | // Fixme. We should make this configureable by the XML file -- hps |
|
189 | 0 | int maskOptions = Perl5Compiler.DEFAULT_MASK; |
190 | ||
191 | try |
|
192 | { |
|
193 | 0 | log.debug("Compiling pattern " + maskString); |
194 | 0 | maskPattern = patternCompiler.compile(maskString, maskOptions); |
195 | } |
|
196 | 0 | catch (MalformedPatternException mpe) |
197 | { |
|
198 | 0 | throw new InvalidMaskException("Could not compile pattern " + maskString, mpe); |
199 | 0 | } |
200 | 0 | } |
201 | ||
202 | /** |
|
203 | * Get the value of maskMessage. |
|
204 | * |
|
205 | * @return value of maskMessage. |
|
206 | */ |
|
207 | public String getMaskMessage() |
|
208 | { |
|
209 | 0 | return maskMessage; |
210 | } |
|
211 | ||
212 | /** |
|
213 | * Set the value of maskMessage. |
|
214 | * |
|
215 | * @param message Value to assign to maskMessage. |
|
216 | */ |
|
217 | public void setMaskMessage(String message) |
|
218 | { |
|
219 | 0 | this.maskMessage = message; |
220 | 0 | } |
221 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |