1 package org.apache.turbine.services.intake.model;
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 org.apache.commons.lang.StringUtils;
25 import org.apache.turbine.services.intake.IntakeException;
26 import org.apache.turbine.services.intake.validator.BooleanValidator;
27 import org.apache.turbine.services.intake.xmlmodel.XmlField;
28
29 /***
30 * Processor for boolean fields.
31 *
32 * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
33 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
34 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
35 * @version $Id: BooleanField.java 534527 2007-05-02 16:10:59Z tv $
36 */
37 public class BooleanField
38 extends Field
39 {
40 public BooleanField(XmlField field, Group group)
41 throws IntakeException
42 {
43 super(field, group);
44 }
45
46 /***
47 * Sets the default value for a Boolean field
48 *
49 * @param prop Parameter for the default values
50 */
51 public void setDefaultValue(String prop)
52 {
53 defaultValue = null;
54
55 if (prop == null)
56 {
57 return;
58 }
59
60 defaultValue = Boolean.valueOf(prop);
61 }
62
63 /***
64 * Set the empty Value. This value is used if Intake
65 * maps a field to a parameter returned by the user and
66 * the corresponding field is either empty (empty string)
67 * or non-existant.
68 *
69 * @param prop The value to use if the field is empty.
70 */
71 public void setEmptyValue(String prop)
72 {
73 emptyValue = null;
74
75 if (prop == null)
76 {
77 return;
78 }
79
80 emptyValue = Boolean.valueOf(prop);
81 }
82
83 /***
84 * Provides access to emptyValue such that the value returned will be
85 * acceptable as an argument parameter to Method.invoke. Subclasses
86 * that deal with primitive types should ensure that they return an
87 * appropriate value wrapped in the object wrapper class for the
88 * primitive type.
89 *
90 * @return the value to use when the field is empty or an Object that
91 * wraps the empty value for primitive types.
92 */
93 protected Object getSafeEmptyValue()
94 {
95 if (isMultiValued)
96 {
97 return new boolean[0];
98 }
99 else
100 {
101 return (null == getEmptyValue()) ? Boolean.FALSE : getEmptyValue();
102 }
103 }
104
105 /***
106 * A suitable validator.
107 *
108 * @return class name of the validator
109 */
110 protected String getDefaultValidator()
111 {
112 return BooleanValidator.class.getName();
113 }
114
115 /***
116 * Sets the value of the field from data in the parser.
117 */
118 protected void doSetValue()
119 {
120 if (isMultiValued)
121 {
122 String[] inputs = parser.getStrings(getKey());
123 boolean[] values = new boolean[inputs.length];
124 for (int i = 0; i < inputs.length; i++)
125 {
126 values[i] = StringUtils.isNotEmpty(inputs[i])
127 ? getBoolean(inputs[i]).booleanValue()
128 : ((Boolean) getEmptyValue()).booleanValue();
129 }
130 setTestValue(values);
131 }
132 else
133 {
134 String val = parser.getString(getKey());
135 setTestValue(StringUtils.isNotEmpty(val) ? getBoolean(val) : (Boolean) getEmptyValue());
136 }
137 }
138
139 /***
140 * Parses a string into a Boolean object. If the field has a validator
141 * and the validator is an instance of BooleanValidator, the parse()
142 * method is used to convert the string into the Boolean. Otherwise,
143 * the string value is passed to the constructor to the Boolean
144 * object.
145 *
146 * @param stringValue string to parse
147 * @return a <code>Boolean</code> object
148 */
149 private Boolean getBoolean(String stringValue)
150 {
151 Boolean result = null;
152
153 if (validator != null && validator instanceof BooleanValidator)
154 {
155 BooleanValidator bValidator = (BooleanValidator) validator;
156 try
157 {
158 result = bValidator.parse(stringValue);
159 }
160 catch (ParseException e)
161 {
162
163
164
165 }
166 }
167 else
168 {
169 result = Boolean.valueOf(stringValue);
170 }
171
172 return result;
173 }
174
175 /***
176 * Gets the boolean value of the field. A value of false will be returned
177 * if the value of the field is null.
178 *
179 * @return value of the field.
180 */
181 public boolean booleanValue()
182 {
183 boolean result = false;
184 try
185 {
186 result = ((Boolean) getValue()).booleanValue();
187 }
188 catch (Exception e)
189 {
190 log.error(e);
191 }
192 return result;
193 }
194
195 }