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