View Javadoc

1   package org.apache.turbine.services.intake.model;
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 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 646752 2008-04-10 10:56:15Z tv $
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 prop)
49      {
50          defaultValue = null;
51  
52          if (prop == null)
53          {
54              return;
55          }
56  
57          defaultValue = Boolean.valueOf(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 prop)
69      {
70          emptyValue = null;
71  
72          if (prop == null)
73          {
74              return;
75          }
76  
77          emptyValue = Boolean.valueOf(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 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 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             Boolean[] inputs = parser.getBooleanObjects(getKey());
120             boolean[] values = new boolean[inputs.length];
121 
122             for (int i = 0; i < inputs.length; i++)
123             {
124                 values[i] = inputs[i] == null 
125                         ? ((Boolean) getEmptyValue()).booleanValue() 
126                         : inputs[i].booleanValue();
127             }
128 
129             setTestValue(values);
130         }
131         else
132         {
133             setTestValue(parser.getBooleanObject(getKey(), (Boolean)getEmptyValue()));
134         }
135     }
136 
137     /***
138      * Gets the boolean value of the field.  A value of false will be returned
139      * if the value of the field is null.
140      *
141      * @return value of the field.
142      */
143     public boolean booleanValue()
144     {
145         boolean result = false;
146         try
147         {
148             result = ((Boolean) getValue()).booleanValue();
149         }
150         catch (Exception e)
151         {
152             log.error(e);
153         }
154         return result;
155     }
156 
157 }