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