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.IntegerValidator;
026: import org.apache.turbine.services.intake.xmlmodel.XmlField;
027:
028: /**
029: * Processor for int fields.
030: *
031: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
032: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
033: * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
034: * @version $Id: IntegerField.java 538406 2007-05-16 02:01:32Z seade $
035: */
036: public class IntegerField extends Field {
037:
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 IntegerField(XmlField field, Group group)
046: throws IntakeException {
047: super (field, group);
048: }
049:
050: /**
051: * Sets the default value for an Integer 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 Integer(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-existent.
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 Integer(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 int[0];
096: } else {
097: return (null == getEmptyValue()) ? new Integer(0)
098: : getEmptyValue();
099: }
100: }
101:
102: /**
103: * A suitable validator.
104: *
105: * @return A suitable validator
106: */
107: protected String getDefaultValidator() {
108: return IntegerValidator.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: int[] values = new int[inputs.length];
118: for (int i = 0; i < inputs.length; i++) {
119: values[i] = StringUtils.isNotEmpty(inputs[i]) ? new Integer(
120: inputs[i]).intValue()
121: : ((Integer) getEmptyValue()).intValue();
122: }
123: setTestValue(values);
124: } else {
125: String val = parser.getString(getKey());
126: setTestValue(StringUtils.isNotEmpty(val) ? new Integer(val)
127: : (Integer) getEmptyValue());
128: }
129: }
130:
131: }
|