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 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 protected String maskString = null;
51
52 /*** The compiled perl5 Regular expression from the ORO Perl5Compiler */
53 protected Pattern maskPattern = null;
54
55 /*** The message to report if the mask constraint is not satisfied */
56 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 {
70 init(paramMap);
71 }
72
73 /***
74 * Default constructor
75 */
76 public StringValidator()
77 {
78 }
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 super.init(paramMap);
92
93 Constraint constraint = (Constraint) paramMap.get(MASK_RULE_NAME);
94 if (constraint != null)
95 {
96 String param = constraint.getValue();
97 setMask(param);
98 maskMessage = constraint.getMessage();
99 }
100
101 }
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 boolean valid = false;
113 try
114 {
115 assertValidity(testValue);
116 valid = true;
117 }
118 catch (ValidationException ve)
119 {
120 valid = false;
121 }
122 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 super.assertValidity(testValue);
137
138 if (required || StringUtils.isNotEmpty(testValue))
139 {
140 if (maskPattern != null)
141 {
142 /*** perl5 matcher */
143 Perl5Matcher patternMatcher = new Perl5Matcher();
144
145 boolean patternMatch =
146 patternMatcher.matches(testValue, maskPattern);
147
148 log.debug("Trying to match " + testValue
149 + " to pattern " + maskString);
150
151 if (!patternMatch)
152 {
153 errorMessage = maskMessage;
154 throw new ValidationException(maskMessage);
155 }
156 }
157 }
158 }
159
160
161
162
163
164 /***
165 * Get the value of mask.
166 *
167 * @return value of mask.
168 */
169 public String getMask()
170 {
171 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 Perl5Compiler patternCompiler = new Perl5Compiler();
185
186 maskString = mask;
187
188
189 int maskOptions = Perl5Compiler.DEFAULT_MASK;
190
191 try
192 {
193 log.debug("Compiling pattern " + maskString);
194 maskPattern = patternCompiler.compile(maskString, maskOptions);
195 }
196 catch (MalformedPatternException mpe)
197 {
198 throw new InvalidMaskException("Could not compile pattern " + maskString, mpe);
199 }
200 }
201
202 /***
203 * Get the value of maskMessage.
204 *
205 * @return value of maskMessage.
206 */
207 public String getMaskMessage()
208 {
209 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 this.maskMessage = message;
220 }
221 }