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.StringValidator;
026: import org.apache.turbine.services.intake.xmlmodel.XmlField;
027:
028: /**
029: * Text field.
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: StringField.java 534527 2007-05-02 16:10:59Z tv $
035: */
036: public class StringField 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 StringField(XmlField field, Group group)
046: throws IntakeException {
047: super (field, group);
048: }
049:
050: /**
051: * Produces the fully qualified class name of the default validator.
052: *
053: * @return class name of the default validator
054: */
055: protected String getDefaultValidator() {
056: return StringValidator.class.getName();
057: }
058:
059: /**
060: * Sets the default value for a String field
061: *
062: * @param prop Parameter for the default values
063: */
064: public void setDefaultValue(String prop) {
065: defaultValue = prop;
066: }
067:
068: /**
069: * Set the empty Value. This value is used if Intake
070: * maps a field to a parameter returned by the user and
071: * the corresponding field is either empty (empty string)
072: * or non-existant.
073: *
074: * @param prop The value to use if the field is empty.
075: */
076: public void setEmptyValue(String prop) {
077: emptyValue = prop;
078: }
079:
080: /**
081: * Sets the value of the field from data in the parser.
082: */
083: protected void doSetValue() {
084: if (isMultiValued) {
085: String[] ss = parser.getStrings(getKey());
086: String[] sval = new String[ss.length];
087: for (int i = 0; i < ss.length; i++) {
088: sval[i] = (StringUtils.isNotEmpty(ss[i])) ? ss[i]
089: : (String) getEmptyValue();
090: }
091: setTestValue(sval);
092: } else {
093: String val = parser.getString(getKey());
094: setTestValue(StringUtils.isNotEmpty(val) ? val
095: : (String) getEmptyValue());
096: }
097: }
098:
099: /**
100: * Set the value of required.
101: *
102: * @param v Value to assign to required.
103: * @param message an error message
104: */
105: public void setRequired(boolean v, String message) {
106: this .required = v;
107: if (v) {
108: if (isMultiValued) {
109: String[] ss = (String[]) getTestValue();
110: if (ss == null || ss.length == 0) {
111: validFlag = false;
112: this .message = message;
113: } else {
114: boolean set = false;
115: for (int i = 0; i < ss.length; i++) {
116: set |= StringUtils.isNotEmpty(ss[i]);
117: if (set) {
118: break;
119: }
120: }
121: if (!set) {
122: validFlag = false;
123: this .message = message;
124: }
125: }
126: } else {
127: if (!setFlag
128: || StringUtils.isEmpty((String) getTestValue())) {
129: validFlag = false;
130: this.message = message;
131: }
132: }
133: }
134: }
135: }
|