001 package org.apache.fulcrum.intake.model;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import org.apache.fulcrum.intake.IntakeException;
023 import org.apache.fulcrum.intake.validator.BooleanValidator;
024 import org.apache.fulcrum.intake.xmlmodel.XmlField;
025
026 /**
027 * Processor for boolean fields.
028 *
029 * @version $Id: BooleanField.java 663702 2008-06-05 19:01:36Z tv $
030 */
031 public class BooleanField
032 extends Field
033 {
034 /**
035 * Constructor.
036 *
037 * @param field xml field definition object
038 * @param group xml group definition object
039 * @throws IntakeException thrown by superclass
040 */
041 public BooleanField(XmlField field, Group group)
042 throws IntakeException
043 {
044 super(field, group);
045 }
046
047 /**
048 * Sets the default value for a Boolean field
049 *
050 * @param prop Parameter for the default values
051 */
052 public void setDefaultValue(String prop)
053 {
054 defaultValue = null;
055
056 if (prop == null)
057 {
058 return;
059 }
060
061 defaultValue = Boolean.valueOf(prop);
062 }
063
064 /**
065 * Set the empty Value. This value is used if Intake
066 * maps a field to a parameter returned by the user and
067 * the corresponding field is either empty (empty string)
068 * or non-existant.
069 *
070 * @param prop The value to use if the field is empty.
071 */
072 public void setEmptyValue(String prop)
073 {
074 emptyValue = null;
075
076 if (prop == null)
077 {
078 return;
079 }
080
081 emptyValue = Boolean.valueOf(prop);
082 }
083
084 /**
085 * Provides access to emptyValue such that the value returned will be
086 * acceptable as an argument parameter to Method.invoke. Subclasses
087 * that deal with primitive types should ensure that they return an
088 * appropriate value wrapped in the object wrapper class for the
089 * primitive type.
090 *
091 * @return the value to use when the field is empty or an Object that
092 * wraps the empty value for primitive types.
093 */
094 protected Object getSafeEmptyValue()
095 {
096 if (isMultiValued)
097 {
098 return new boolean[0];
099 }
100 else
101 {
102 return (null == getEmptyValue()) ? Boolean.FALSE : getEmptyValue();
103 }
104 }
105
106 /**
107 * A suitable validator.
108 *
109 * @return class name of the validator
110 */
111 protected String getDefaultValidator()
112 {
113 return BooleanValidator.class.getName();
114 }
115
116 /**
117 * Sets the value of the field from data in the parser.
118 */
119 protected void doSetValue()
120 {
121 if (isMultiValued)
122 {
123 Boolean[] inputs = parser.getBooleanObjects(getKey());
124 boolean[] values = new boolean[inputs.length];
125
126 for (int i = 0; i < inputs.length; i++)
127 {
128 values[i] = inputs[i] == null
129 ? ((Boolean) getEmptyValue()).booleanValue()
130 : inputs[i].booleanValue();
131 }
132
133 setTestValue(values);
134 }
135 else
136 {
137 setTestValue(parser.getBooleanObject(getKey(), (Boolean)getEmptyValue()));
138 }
139 }
140
141 /**
142 * Gets the boolean value of the field. A value of false will be returned
143 * if the value of the field is null.
144 *
145 * @return value of the field.
146 */
147 public boolean booleanValue()
148 {
149 boolean result = false;
150 try
151 {
152 result = ((Boolean) getValue()).booleanValue();
153 }
154 catch (Exception e)
155 {
156 log.error(e);
157 }
158 return result;
159 }
160
161 }