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.text.ParseException;
23  
24  import java.util.Map;
25  
26  import org.apache.commons.lang.StringUtils;
27  
28  /***
29   * Validator for boolean field types.<br><br>
30   *
31   * Values are validated by attemting to match the value to
32   * a list of strings for true and false values.  The string
33   * values are compared without reguard to case.<br>
34   *
35   * Valid values for Boolean.TRUE:
36   * <ul>
37   * <li>TRUE</li>
38   * <li>T</li>
39   * <li>YES</li>
40   * <li>Y</li>
41   * <li>1</li>
42   * <li>ON</li>
43   * </ul>
44   * Valid values for Boolean.FALSE:
45   * <ul>
46   * <li>FALSE</li>
47   * <li>F</li>
48   * <li>NO</li>
49   * <li>N</li>
50   * <li>0</li>
51   * <li>OFF</li>
52   * </ul>
53   *
54   * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
55   * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
56   * @version $Id: BooleanValidator.java 646752 2008-04-10 10:56:15Z tv $
57   */
58  public class BooleanValidator
59          extends DefaultValidator
60  {
61      /*** String values which would evaluate to Boolean.TRUE */
62      private static String[] trueValues = {"TRUE","T","YES","Y","1","ON"};
63  
64      /*** String values which would evaluate to Boolean.FALSE */
65      private static String[] falseValues = {"FALSE","F","NO","N","0","OFF"};
66  
67      /***
68       * Default Constructor
69       */
70      public BooleanValidator()
71      {
72      }
73  
74      /***
75       * Constructor to use when initialising Object
76       *
77       * @param paramMap
78       * @throws InvalidMaskException
79       */
80      public BooleanValidator(Map paramMap)
81              throws InvalidMaskException
82      {
83          super(paramMap);
84      }
85  
86      /***
87       * Determine whether a testValue meets the criteria specified
88       * in the constraints defined for this validator
89       *
90       * @param testValue a <code>String</code> to be tested
91       * @exception ValidationException containing an error message if the
92       * testValue did not pass the validation tests.
93       */
94      public void assertValidity(String testValue)
95              throws ValidationException
96      {
97          super.assertValidity(testValue);
98  
99          if (required || StringUtils.isNotEmpty(testValue))
100         {
101             try
102             {
103                 parse(testValue);
104             }
105             catch (ParseException e)
106             {
107                 throw new ValidationException(e.getMessage());
108             }
109         }
110     }
111 
112     /***
113      * Parses a srting value into a Boolean object.
114      *
115      * @param stringValue the value to parse
116      * @return a <code>Boolean</a> object
117      */
118     public Boolean parse(String stringValue)
119             throws ParseException
120     {
121         Boolean result = null;
122 
123         for (int cnt = 0;
124              cnt < Math.max(trueValues.length, falseValues.length); cnt++)
125         {
126             // Short-cut evaluation or bust!
127             if ((cnt < trueValues.length) &&
128                     stringValue.equalsIgnoreCase(trueValues[cnt]))
129             {
130                 result = Boolean.TRUE;
131                 break;
132             }
133 
134             if ((cnt < falseValues.length) &&
135                     stringValue.equalsIgnoreCase(falseValues[cnt]))
136             {
137                 result = Boolean.FALSE;
138                 break;
139             }
140         }
141 
142         if (result == null)
143         {
144             throw new ParseException(stringValue +
145                     " could not be converted to a Boolean", 0);
146         }
147         return result;
148     }
149 }