001: package org.apache.turbine.services.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 java.text.ParseException;
023:
024: import org.apache.commons.lang.StringUtils;
025: import org.apache.turbine.services.intake.IntakeException;
026: import org.apache.turbine.services.intake.validator.BooleanValidator;
027: import org.apache.turbine.services.intake.xmlmodel.XmlField;
028:
029: /**
030: * Processor for boolean fields.
031: *
032: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
033: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
034: * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
035: * @version $Id: BooleanField.java 534527 2007-05-02 16:10:59Z tv $
036: */
037: public class BooleanField extends Field {
038: public BooleanField(XmlField field, Group group)
039: throws IntakeException {
040: super (field, group);
041: }
042:
043: /**
044: * Sets the default value for a Boolean field
045: *
046: * @param prop Parameter for the default values
047: */
048: public void setDefaultValue(String prop) {
049: defaultValue = null;
050:
051: if (prop == null) {
052: return;
053: }
054:
055: defaultValue = Boolean.valueOf(prop);
056: }
057:
058: /**
059: * Set the empty Value. This value is used if Intake
060: * maps a field to a parameter returned by the user and
061: * the corresponding field is either empty (empty string)
062: * or non-existant.
063: *
064: * @param prop The value to use if the field is empty.
065: */
066: public void setEmptyValue(String prop) {
067: emptyValue = null;
068:
069: if (prop == null) {
070: return;
071: }
072:
073: emptyValue = Boolean.valueOf(prop);
074: }
075:
076: /**
077: * Provides access to emptyValue such that the value returned will be
078: * acceptable as an argument parameter to Method.invoke. Subclasses
079: * that deal with primitive types should ensure that they return an
080: * appropriate value wrapped in the object wrapper class for the
081: * primitive type.
082: *
083: * @return the value to use when the field is empty or an Object that
084: * wraps the empty value for primitive types.
085: */
086: protected Object getSafeEmptyValue() {
087: if (isMultiValued) {
088: return new boolean[0];
089: } else {
090: return (null == getEmptyValue()) ? Boolean.FALSE
091: : getEmptyValue();
092: }
093: }
094:
095: /**
096: * A suitable validator.
097: *
098: * @return class name of the validator
099: */
100: protected String getDefaultValidator() {
101: return BooleanValidator.class.getName();
102: }
103:
104: /**
105: * Sets the value of the field from data in the parser.
106: */
107: protected void doSetValue() {
108: if (isMultiValued) {
109: String[] inputs = parser.getStrings(getKey());
110: boolean[] values = new boolean[inputs.length];
111: for (int i = 0; i < inputs.length; i++) {
112: values[i] = StringUtils.isNotEmpty(inputs[i]) ? getBoolean(
113: inputs[i]).booleanValue()
114: : ((Boolean) getEmptyValue()).booleanValue();
115: }
116: setTestValue(values);
117: } else {
118: String val = parser.getString(getKey());
119: setTestValue(StringUtils.isNotEmpty(val) ? getBoolean(val)
120: : (Boolean) getEmptyValue());
121: }
122: }
123:
124: /**
125: * Parses a string into a Boolean object. If the field has a validator
126: * and the validator is an instance of BooleanValidator, the parse()
127: * method is used to convert the string into the Boolean. Otherwise,
128: * the string value is passed to the constructor to the Boolean
129: * object.
130: *
131: * @param stringValue string to parse
132: * @return a <code>Boolean</code> object
133: */
134: private Boolean getBoolean(String stringValue) {
135: Boolean result = null;
136:
137: if (validator != null && validator instanceof BooleanValidator) {
138: BooleanValidator bValidator = (BooleanValidator) validator;
139: try {
140: result = bValidator.parse(stringValue);
141: } catch (ParseException e) {
142: // do nothing. This should never be thrown since this method will not be
143: // executed unless the Validator has already been able to parse the
144: // string value
145: }
146: } else {
147: result = Boolean.valueOf(stringValue);
148: }
149:
150: return result;
151: }
152:
153: /**
154: * Gets the boolean value of the field. A value of false will be returned
155: * if the value of the field is null.
156: *
157: * @return value of the field.
158: */
159: public boolean booleanValue() {
160: boolean result = false;
161: try {
162: result = ((Boolean) getValue()).booleanValue();
163: } catch (Exception e) {
164: log.error(e);
165: }
166: return result;
167: }
168:
169: }
|