KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > intake > model > BooleanField


1 package org.apache.turbine.services.intake.model;
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 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,v 1.12.2.6 2004/05/20 03:16:39 seade Exp $
33  */

34 public class BooleanField
35         extends Field
36 {
37     public BooleanField(XmlField field, Group group)
38             throws IntakeException
39     {
40         super(field, group);
41     }
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 JavaDoc prop)
49     {
50         defaultValue = null;
51
52         if (prop == null)
53         {
54             return;
55         }
56
57         defaultValue = new Boolean JavaDoc(prop);
58     }
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 JavaDoc prop)
69     {
70         emptyValue = null;
71
72         if (prop == null)
73         {
74             return;
75         }
76
77         emptyValue = new Boolean JavaDoc(prop);
78     }
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 JavaDoc getSafeEmptyValue()
91     {
92         if (isMultiValued)
93         {
94             return new boolean[0];
95         }
96         else
97         {
98             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 JavaDoc getDefaultValidator()
108     {
109         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         if (isMultiValued)
118         {
119             String JavaDoc[] inputs = parser.getStrings(getKey());
120             boolean[] values = new boolean[inputs.length];
121             for (int i = 0; i < inputs.length; i++)
122             {
123                 values[i] = StringUtils.isNotEmpty(inputs[i])
124                         ? getBoolean(inputs[i]).booleanValue()
125                         : ((Boolean JavaDoc) getEmptyValue()).booleanValue();
126             }
127             setTestValue(values);
128         }
129         else
130         {
131             String JavaDoc val = parser.getString(getKey());
132             setTestValue(StringUtils.isNotEmpty(val) ? getBoolean(val) : (Boolean JavaDoc) getEmptyValue());
133         }
134     }
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 JavaDoc getBoolean(String JavaDoc stringValue)
147     {
148         Boolean JavaDoc result = null;
149
150         if (validator != null && validator instanceof BooleanValidator)
151         {
152             BooleanValidator bValidator = (BooleanValidator) validator;
153             try
154             {
155                 result = bValidator.parse(stringValue);
156             }
157             catch (ParseException JavaDoc 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
}
163         }
164         else
165         {
166             result = new Boolean JavaDoc(stringValue);
167         }
168
169         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 booleanValue()
179     {
180         boolean result = false;
181         try
182         {
183             result = ((Boolean JavaDoc) getValue()).booleanValue();
184         }
185         catch (Exception JavaDoc e)
186         {
187             log.error(e);
188         }
189         return result;
190     }
191     
192 }
193
Popular Tags