1 package org.apache.turbine.services.intake.validator;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 * </ul>
43 * Valid values for Boolean.FALSE:
44 * <ul>
45 * <li>FALSE</li>
46 * <li>F</li>
47 * <li>NO</li>
48 * <li>N</li>
49 * <li>0</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 * @version $Id: BooleanValidator.java 534527 2007-05-02 16:10:59Z tv $
55 */
56 public class BooleanValidator
57 extends DefaultValidator
58 {
59 /*** String values which would evaluate to Boolean.TRUE */
60 private static String[] trueValues = {"TRUE","T","YES","Y","1"};
61
62 /*** String values which would evaluate to Boolean.FALSE */
63 private static String[] falseValues = {"FALSE","F","NO","N","0"};
64
65 /***
66 * Default Constructor
67 */
68 public BooleanValidator()
69 {
70 }
71
72 /***
73 * Constructor to use when initialising Object
74 *
75 * @param paramMap
76 * @throws InvalidMaskException
77 */
78 public BooleanValidator(Map paramMap)
79 throws InvalidMaskException
80 {
81 super(paramMap);
82 }
83
84 /***
85 * Determine whether a testValue meets the criteria specified
86 * in the constraints defined for this validator
87 *
88 * @param testValue a <code>String</code> to be tested
89 * @exception ValidationException containing an error message if the
90 * testValue did not pass the validation tests.
91 */
92 public void assertValidity(String testValue)
93 throws ValidationException
94 {
95 super.assertValidity(testValue);
96
97 if (required || StringUtils.isNotEmpty(testValue))
98 {
99 try
100 {
101 parse(testValue);
102 }
103 catch (ParseException e)
104 {
105 throw new ValidationException(e.getMessage());
106 }
107 }
108 }
109
110 /***
111 * Parses a srting value into a Boolean object.
112 *
113 * @param stringValue the value to parse
114 * @return a <code>Boolean</a> object
115 */
116 public Boolean parse(String stringValue)
117 throws ParseException
118 {
119 Boolean result = null;
120
121 for (int cnt = 0;
122 cnt < Math.max(trueValues.length, falseValues.length); cnt++)
123 {
124
125 if ((cnt < trueValues.length) &&
126 stringValue.equalsIgnoreCase(trueValues[cnt]))
127 {
128 result = Boolean.TRUE;
129 break;
130 }
131
132 if ((cnt < falseValues.length) &&
133 stringValue.equalsIgnoreCase(falseValues[cnt]))
134 {
135 result = Boolean.FALSE;
136 break;
137 }
138 }
139
140 if (result == null)
141 {
142 throw new ParseException(stringValue +
143 " could not be converted to a Boolean", 0);
144 }
145 return result;
146 }
147 }