KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.ParseException JavaDoc;
20
21 import java.util.Map JavaDoc;
22
23 import org.apache.commons.lang.StringUtils;
24
25 /**
26  * Validator for boolean field types.<br><br>
27  *
28  * Values are validated by attemting to match the value to
29  * a list of strings for true and false values. The string
30  * values are compared without reguard to case.<br>
31  *
32  * Valid values for Boolean.TRUE:
33  * <ul>
34  * <li>TRUE</li>
35  * <li>T</li>
36  * <li>YES</li>
37  * <li>Y</li>
38  * <li>1</li>
39  * </ul>
40  * Valid values for Boolean.FALSE:
41  * <ul>
42  * <li>FALSE</li>
43  * <li>F</li>
44  * <li>NO</li>
45  * <li>N</li>
46  * <li>0</li>
47  * </ul>
48  *
49  * @author <a HREF="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
50  * @author <a HREF="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
51  * @version $Id: BooleanValidator.java,v 1.8.2.4 2004/05/20 03:06:47 seade Exp $
52  */

53 public class BooleanValidator
54         extends DefaultValidator
55 {
56     /** String values which would evaluate to Boolean.TRUE */
57     private static String JavaDoc[] trueValues = {"TRUE","T","YES","Y","1"};
58
59     /** String values which would evaluate to Boolean.FALSE */
60     private static String JavaDoc[] falseValues = {"FALSE","F","NO","N","0"};
61
62     /**
63      * Default Constructor
64      */

65     public BooleanValidator()
66     {
67     }
68
69     /**
70      * Constructor to use when initialising Object
71      *
72      * @param paramMap
73      * @throws InvalidMaskException
74      */

75     public BooleanValidator(Map JavaDoc paramMap)
76             throws InvalidMaskException
77     {
78         super(paramMap);
79     }
80
81     /**
82      * Determine whether a testValue meets the criteria specified
83      * in the constraints defined for this validator
84      *
85      * @param testValue a <code>String</code> to be tested
86      * @exception ValidationException containing an error message if the
87      * testValue did not pass the validation tests.
88      */

89     public void assertValidity(String JavaDoc testValue)
90             throws ValidationException
91     {
92         super.assertValidity(testValue);
93
94         if (required || StringUtils.isNotEmpty(testValue))
95         {
96             try
97             {
98                 parse(testValue);
99             }
100             catch (ParseException JavaDoc e)
101             {
102                 throw new ValidationException(e.getMessage());
103             }
104         }
105     }
106
107     /**
108      * Parses a srting value into a Boolean object.
109      *
110      * @param stringValue the value to parse
111      * @return a <code>Boolean</a> object
112      */

113     public Boolean JavaDoc parse(String JavaDoc stringValue)
114             throws ParseException JavaDoc
115     {
116         Boolean JavaDoc result = null;
117
118         for (int cnt = 0;
119              cnt < Math.max(trueValues.length, falseValues.length); cnt++)
120         {
121             // Short-cut evaluation or bust!
122
if ((cnt < trueValues.length) &&
123                     stringValue.equalsIgnoreCase(trueValues[cnt]))
124             {
125                 result = Boolean.TRUE;
126                 break;
127             }
128
129             if ((cnt < falseValues.length) &&
130                     stringValue.equalsIgnoreCase(falseValues[cnt]))
131             {
132                 result = Boolean.FALSE;
133                 break;
134             }
135         }
136
137         if (result == null)
138         {
139             throw new ParseException JavaDoc(stringValue +
140                     " could not be converted to a Boolean", 0);
141         }
142         return result;
143     }
144 }
145
Popular Tags