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