KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > intake > validator > StringValidator


1 package org.apache.turbine.services.intake.validator;
2
3 /*
4  * Copyright 2001-2004 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 JavaDoc;
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>&nbsp;</td></tr>
35  * <tr><td>minLength</td><td>integer</td><td>0</td></tr>
36  * <tr><td>maxLength</td><td>integer</td><td>&nbsp;</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,v 1.2.2.4 2004/05/20 03:06:47 seade Exp $
45  */

46 public class StringValidator
47         extends DefaultValidator
48 {
49     /** The matching mask String as supplied by the XML input */
50     protected String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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
// ** Bean accessor methods **
162
// ************************************************************
163

164     /**
165      * Get the value of mask.
166      *
167      * @return value of mask.
168      */

169     public String JavaDoc 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 JavaDoc 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         // Fixme. We should make this configureable by the XML file -- hps
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 JavaDoc 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 JavaDoc message)
218     {
219         this.maskMessage = message;
220     }
221 }
222
Popular Tags