001: package org.apache.turbine.services.intake.model;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import org.apache.commons.lang.StringUtils;
020:
021: import org.apache.turbine.services.intake.IntakeException;
022: import org.apache.turbine.services.intake.validator.DoubleValidator;
023: import org.apache.turbine.services.intake.xmlmodel.XmlField;
024:
025: /**
026: * Creates Double Field objects.
027: *
028: * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
029: * @version $Id: DoubleField.java 264148 2005-08-29 14:21:04Z henning $
030: */
031: public class DoubleField extends Field {
032: /**
033: * Constructor.
034: *
035: * @param field xml field definition object
036: * @param group xml group definition object
037: * @throws IntakeException thrown by superclass
038: */
039: public DoubleField(XmlField field, Group group)
040: throws IntakeException {
041: super (field, group);
042: }
043:
044: /**
045: * Sets the default value for a Double Field
046: *
047: * @param prop Parameter for the default values
048: */
049: public void setDefaultValue(String prop) {
050: defaultValue = null;
051:
052: if (prop == null) {
053: return;
054: }
055:
056: defaultValue = new Double(prop);
057: }
058:
059: /**
060: * Set the empty Value. This value is used if Intake
061: * maps a field to a parameter returned by the user and
062: * the corresponding field is either empty (empty string)
063: * or non-existant.
064: *
065: * @param prop The value to use if the field is empty.
066: */
067: public void setEmptyValue(String prop) {
068: emptyValue = null;
069:
070: if (prop == null) {
071: return;
072: }
073:
074: emptyValue = new Double(prop);
075: }
076:
077: /**
078: * Provides access to emptyValue such that the value returned will be
079: * acceptable as an argument parameter to Method.invoke. Subclasses
080: * that deal with primitive types should ensure that they return an
081: * appropriate value wrapped in the object wrapper class for the
082: * primitive type.
083: *
084: * @return the value to use when the field is empty or an Object that
085: * wraps the empty value for primitive types.
086: */
087: protected Object getSafeEmptyValue() {
088: if (isMultiValued) {
089: return new double[0];
090: } else {
091: return (null == getEmptyValue()) ? new Double(0.0)
092: : getEmptyValue();
093: }
094: }
095:
096: /**
097: * A suitable validator.
098: *
099: * @return A suitable validator
100: */
101: protected String getDefaultValidator() {
102: return DoubleValidator.class.getName();
103: }
104:
105: /**
106: * Sets the value of the field from data in the parser.
107: */
108: protected void doSetValue() {
109: if (isMultiValued) {
110: String[] inputs = parser.getStrings(getKey());
111: double[] values = new double[inputs.length];
112: for (int i = 0; i < inputs.length; i++) {
113: values[i] = StringUtils.isNotEmpty(inputs[i]) ? new Double(
114: inputs[i]).doubleValue()
115: : ((Double) getEmptyValue()).doubleValue();
116: }
117: setTestValue(values);
118: } else {
119: String val = parser.getString(getKey());
120: setTestValue(StringUtils.isNotEmpty(val) ? new Double(val)
121: : (Double) getEmptyValue());
122: }
123: }
124:
125: }
|