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