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 org.apache.commons.lang.StringUtils;
023:
024: import org.apache.turbine.services.intake.IntakeException;
025: import org.apache.turbine.services.intake.validator.DoubleValidator;
026: import org.apache.turbine.services.intake.xmlmodel.XmlField;
027:
028: /**
029: * Creates Double Field objects.
030: *
031: * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
032: * @version $Id: DoubleField.java 534527 2007-05-02 16:10:59Z tv $
033: */
034: public class DoubleField extends AbstractNumberField {
035: /**
036: * Constructor.
037: *
038: * @param field xml field definition object
039: * @param group xml group definition object
040: * @throws IntakeException thrown by superclass
041: */
042: public DoubleField(XmlField field, Group group)
043: throws IntakeException {
044: super (field, group);
045: }
046:
047: /**
048: * Sets the default value for a Double Field
049: *
050: * @param prop Parameter for the default values
051: */
052: public void setDefaultValue(String prop) {
053: defaultValue = null;
054:
055: if (prop == null) {
056: return;
057: }
058:
059: defaultValue = new Double(prop);
060: }
061:
062: /**
063: * Set the empty Value. This value is used if Intake
064: * maps a field to a parameter returned by the user and
065: * the corresponding field is either empty (empty string)
066: * or non-existant.
067: *
068: * @param prop The value to use if the field is empty.
069: */
070: public void setEmptyValue(String prop) {
071: emptyValue = null;
072:
073: if (prop == null) {
074: return;
075: }
076:
077: emptyValue = new Double(prop);
078: }
079:
080: /**
081: * Provides access to emptyValue such that the value returned will be
082: * acceptable as an argument parameter to Method.invoke. Subclasses
083: * that deal with primitive types should ensure that they return an
084: * appropriate value wrapped in the object wrapper class for the
085: * primitive type.
086: *
087: * @return the value to use when the field is empty or an Object that
088: * wraps the empty value for primitive types.
089: */
090: protected Object getSafeEmptyValue() {
091: if (isMultiValued) {
092: return new double[0];
093: } else {
094: return (null == getEmptyValue()) ? new Double(0.0)
095: : getEmptyValue();
096: }
097: }
098:
099: /**
100: * A suitable validator.
101: *
102: * @return A suitable validator
103: */
104: protected String getDefaultValidator() {
105: return DoubleValidator.class.getName();
106: }
107:
108: /**
109: * Sets the value of the field from data in the parser.
110: */
111: protected void doSetValue() {
112: if (isMultiValued) {
113: String[] inputs = parser.getStrings(getKey());
114: double[] values = new double[inputs.length];
115: for (int i = 0; i < inputs.length; i++) {
116: values[i] = StringUtils.isNotEmpty(inputs[i]) ? new Double(
117: canonicalizeDecimalInput(inputs[i]))
118: .doubleValue()
119: : ((Double) getEmptyValue()).doubleValue();
120: }
121: setTestValue(values);
122: } else {
123: String val = parser.getString(getKey());
124: setTestValue(StringUtils.isNotEmpty(val) ? new Double(
125: canonicalizeDecimalInput(val))
126: : (Double) getEmptyValue());
127: }
128: }
129:
130: }
|