View Javadoc
1   package org.apache.fulcrum.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  import java.util.regex.Pattern;
24  import java.util.regex.PatternSyntaxException;
25  
26  import org.apache.commons.lang3.StringUtils;
27  
28  /**
29   * A validator that will compare a testValue against the following
30   * constraints:
31   * <table>
32   * <caption>Validation rules</caption>
33   * <tr><th>Name</th><th>Valid Values</th><th>Default Value</th></tr>
34   * <tr><td>required</td><td>true|false</td><td>false</td></tr>
35   * <tr><td>mask</td><td>regexp</td><td>&nbsp;</td></tr>
36   * <tr><td>minLength</td><td>integer</td><td>0</td></tr>
37   * <tr><td>maxLength</td><td>integer</td><td>&nbsp;</td></tr>
38   * </table>
39   *
40   * This validator can serve as the base class for more specific validators
41   *
42   * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
43   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
44   * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
45   * @author <a href="mailto:jh@byteaction.de">J&uuml;rgen Hoffmann</a>
46   * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
47   * @version $Id$
48   */
49  public class StringValidator
50          extends DefaultValidator<String>
51  {
52      /** The matching mask String as supplied by the XML input */
53      protected String maskString = null;
54  
55      /** The compiled Regular Expression */
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       * Default constructor
63       */
64      public StringValidator()
65      {
66          super();
67      }
68  
69      /**
70       * Extract the relevant parameters from the constraints listed
71       * in &lt;rule&gt; tags within the intake.xml file.
72       *
73       * @param paramMap a <code>Map</code> of <code>Rule</code>'s
74       * containing constraints on the input.
75       * @throws InvalidMaskException An invalid mask was specified for one of the rules
76       */
77      @Override
78  	public void init(Map<String, ? extends Constraint> paramMap)
79              throws InvalidMaskException
80      {
81          super.init(paramMap);
82  
83          Constraint constraint = paramMap.get(MASK_RULE_NAME);
84          if (constraint != null)
85          {
86              String param = constraint.getValue();
87              setMask(param);
88              maskMessage = constraint.getMessage();
89          }
90  
91      }
92  
93      /**
94       * Determine whether a testValue meets the criteria specified
95       * in the constraints defined for this validator
96       *
97       * @param testValue a <code>String</code> to be tested
98       * @throws ValidationException containing an error message if the
99       * testValue did not pass the validation tests.
100      */
101     @Override
102 	public void assertValidity(String testValue)
103             throws ValidationException
104     {
105         super.assertValidity(testValue);
106 
107         if ((required || StringUtils.isNotEmpty(testValue)) && maskPattern != null)
108         {
109             /** JDK 1.4 matcher */
110             boolean patternMatch = maskPattern.matcher(testValue).matches();
111 
112             log.debug("Trying to match " + testValue
113                     + " to pattern " + maskString);
114 
115             if (!patternMatch)
116             {
117                 errorMessage = maskMessage;
118                 throw new ValidationException(maskMessage);
119             }
120         }
121     }
122 
123     // ************************************************************
124     // **                Bean accessor methods                   **
125     // ************************************************************
126 
127     /**
128      * Get the value of mask.
129      *
130      * @return value of mask.
131      */
132     public String getMask()
133     {
134         return maskString;
135     }
136 
137     /**
138      * Set the value of mask.
139      *
140      * @param mask  Value to assign to mask.
141      * @throws InvalidMaskException the mask could not be compiled.
142      */
143     public void setMask(String mask)
144             throws InvalidMaskException
145     {
146         maskString = mask;
147 
148         // Fixme. We should make this configureable by the XML file -- hps
149         int maskOptions = 0;
150 
151         try
152         {
153             log.debug("Compiling pattern " + maskString);
154             maskPattern = Pattern.compile(maskString, maskOptions);
155         }
156         catch (PatternSyntaxException pe)
157         {
158             throw new InvalidMaskException("Could not compile pattern " + maskString, pe);
159         }
160     }
161 
162     /**
163      * Get the value of maskMessage.
164      *
165      * @return value of maskMessage.
166      */
167     public String getMaskMessage()
168     {
169         return maskMessage;
170     }
171 
172     /**
173      * Set the value of maskMessage.
174      *
175      * @param message  Value to assign to maskMessage.
176      */
177     public void setMaskMessage(String message)
178     {
179         this.maskMessage = message;
180     }
181 }