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