View Javadoc

1   package org.apache.turbine.services.intake.validator;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Map;
23  
24  import org.apache.commons.lang.StringUtils;
25  
26  import org.apache.oro.text.regex.MalformedPatternException;
27  import org.apache.oro.text.regex.Pattern;
28  import org.apache.oro.text.regex.Perl5Compiler;
29  import org.apache.oro.text.regex.Perl5Matcher;
30  
31  /***
32   * A validator that will compare a testValue against the following
33   * constraints:
34   * <table>
35   * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
36   * <tr><td>required</td><td>true|false</td><td>false</td></tr>
37   * <tr><td>mask</td><td>regexp</td><td>&nbsp;</td></tr>
38   * <tr><td>minLength</td><td>integer</td><td>0</td></tr>
39   * <tr><td>maxLength</td><td>integer</td><td>&nbsp;</td></tr>
40   * </table>
41   *
42   * This validator can serve as the base class for more specific validators
43   *
44   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
45   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
46   * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
47   * @version $Id: StringValidator.java 534527 2007-05-02 16:10:59Z tv $
48   */
49  public class StringValidator
50          extends DefaultValidator
51  {
52      /*** The matching mask String as supplied by the XML input */
53      protected String maskString = null;
54  
55      /*** The compiled perl5 Regular expression from the ORO Perl5Compiler */
56      protected Pattern maskPattern = null;
57  
58      /*** The message to report if the mask constraint is not satisfied */
59      protected String maskMessage = null;
60  
61  
62      /***
63       * Constructor
64       *
65       * @param paramMap a <code>Map</code> of <code>Rule</code>'s
66       * containing constraints on the input.
67       * @exception InvalidMaskException An invalid mask was specified for one of the rules
68  
69      */
70      public StringValidator(Map paramMap)
71              throws InvalidMaskException
72      {
73          init(paramMap);
74      }
75  
76      /***
77       * Default constructor
78       */
79      public StringValidator()
80      {
81      }
82  
83      /***
84       * Extract the relevant parameters from the constraints listed
85       * in <rule> tags within the intake.xml file.
86       *
87       * @param paramMap a <code>Map</code> of <code>Rule</code>'s
88       * containing constraints on the input.
89       * @exception InvalidMaskException An invalid mask was specified for one of the rules
90       */
91      public void init(Map paramMap)
92              throws InvalidMaskException
93      {
94          super.init(paramMap);
95  
96          Constraint constraint = (Constraint) paramMap.get(MASK_RULE_NAME);
97          if (constraint != null)
98          {
99              String param = constraint.getValue();
100             setMask(param);
101             maskMessage = constraint.getMessage();
102         }
103 
104     }
105 
106     /***
107      * Determine whether a testValue meets the criteria specified
108      * in the constraints defined for this validator
109      *
110      * @param testValue a <code>String</code> to be tested
111      * @return true if valid, false otherwise
112      */
113     public boolean isValid(String testValue)
114     {
115         boolean valid = false;
116         try
117         {
118             assertValidity(testValue);
119             valid = true;
120         }
121         catch (ValidationException ve)
122         {
123             valid = false;
124         }
125         return valid;
126     }
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      * @exception ValidationException containing an error message if the
134      * testValue did not pass the validation tests.
135      */
136     public void assertValidity(String testValue)
137             throws ValidationException
138     {
139         super.assertValidity(testValue);
140 
141         if (required || StringUtils.isNotEmpty(testValue))
142         {
143             if (maskPattern != null)
144             {
145                 /*** perl5 matcher */
146                 Perl5Matcher patternMatcher = new Perl5Matcher();
147 
148                 boolean patternMatch =
149                         patternMatcher.matches(testValue, maskPattern);
150 
151                 log.debug("Trying to match " + testValue
152                         + " to pattern " + maskString);
153 
154                 if (!patternMatch)
155                 {
156                     errorMessage = maskMessage;
157                     throw new ValidationException(maskMessage);
158                 }
159             }
160         }
161     }
162 
163     // ************************************************************
164     // **                Bean accessor methods                   **
165     // ************************************************************
166 
167     /***
168      * Get the value of mask.
169      *
170      * @return value of mask.
171      */
172     public String getMask()
173     {
174         return maskString;
175     }
176 
177     /***
178      * Set the value of mask.
179      *
180      * @param mask  Value to assign to mask.
181      * @throws InvalidMaskException the mask could not be compiled.
182      */
183     public void setMask(String mask)
184             throws InvalidMaskException
185     {
186         /*** perl5 compiler, needed for setting up the masks */
187         Perl5Compiler patternCompiler = new Perl5Compiler();
188 
189         maskString = mask;
190 
191         // Fixme. We should make this configureable by the XML file -- hps
192         int maskOptions = Perl5Compiler.DEFAULT_MASK;
193 
194         try
195         {
196             log.debug("Compiling pattern " + maskString);
197             maskPattern = patternCompiler.compile(maskString, maskOptions);
198         }
199         catch (MalformedPatternException mpe)
200         {
201             throw new InvalidMaskException("Could not compile pattern " + maskString, mpe);
202         }
203     }
204 
205     /***
206      * Get the value of maskMessage.
207      *
208      * @return value of maskMessage.
209      */
210     public String getMaskMessage()
211     {
212         return maskMessage;
213     }
214 
215     /***
216      * Set the value of maskMessage.
217      *
218      * @param message  Value to assign to maskMessage.
219      */
220     public void setMaskMessage(String message)
221     {
222         this.maskMessage = message;
223     }
224 }