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.fileupload.FileItem;
023:
024: import org.apache.turbine.services.intake.IntakeException;
025: import org.apache.turbine.services.intake.validator.FileValidator;
026: import org.apache.turbine.services.intake.validator.ValidationException;
027: import org.apache.turbine.services.intake.xmlmodel.XmlField;
028: import org.apache.turbine.util.TurbineRuntimeException;
029: import org.apache.turbine.util.parser.ParameterParser;
030: import org.apache.turbine.util.parser.ValueParser;
031:
032: /**
033: * This Intake field is intended to represent a File input element in a HTML form.
034: *
035: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
036: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
037: * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
038: * @version $Id: FileItemField.java 537514 2007-05-12 20:55:26Z tv $
039: */
040: public class FileItemField extends Field {
041:
042: /**
043: * Constructor.
044: *
045: * @param field xml field definition object
046: * @param group xml group definition object
047: * @throws IntakeException thrown by superclass
048: */
049: public FileItemField(XmlField field, Group group)
050: throws IntakeException {
051: super (field, group);
052: }
053:
054: /**
055: * It is not possible to set the default value for this field type.
056: * Calling this method with a non-null parameter will result in a
057: * TurbineRuntimeException
058: *
059: * @param prop Parameter for the default values
060: * @throws TurbineRuntimeException
061: */
062: public void setDefaultValue(String prop) {
063: if (prop != null) {
064: throw new TurbineRuntimeException(
065: "Default values are not valid for "
066: + this .getClass().getName());
067: }
068:
069: defaultValue = null;
070: }
071:
072: /**
073: * It is not possible to set the empty value for this field type.
074: * Calling this method with a non-null parameter will result in a
075: * TurbineRuntimeException
076: *
077: * @param prop Parameter for the empty values
078: * @throws TurbineRuntimeException
079: */
080: public void setEmptyValue(String prop) {
081: if (prop != null) {
082: throw new TurbineRuntimeException(
083: "Empty values are not valid for "
084: + this .getClass().getName());
085: }
086:
087: emptyValue = null;
088: }
089:
090: /**
091: * A suitable validator.
092: *
093: * @return A suitable validator
094: */
095: protected String getDefaultValidator() {
096: return FileValidator.class.getName();
097: }
098:
099: /**
100: * Method called when this field (the group it belongs to) is
101: * pulled from the pool. The request data is searched to determine
102: * if a value has been supplied for this field. if so, the value
103: * is validated.
104: *
105: * @param vp a <code>ValueParser</code> value
106: * @return a <code>Field</code> value
107: * @exception IntakeException if an error occurs
108: */
109: public Field init(ValueParser vp) throws IntakeException {
110: try {
111: super .parser = (ParameterParser) vp;
112: } catch (ClassCastException e) {
113: throw new IntakeException(
114: "FileItemFields can only be used with ParameterParser");
115: }
116:
117: validFlag = true;
118:
119: FileItem[] fileItems = ((ParameterParser) parser)
120: .getFileItems(getKey());
121:
122: if (fileItems != null) {
123: setFlag = true;
124: validate();
125: }
126:
127: initialized = true;
128: return this ;
129: }
130:
131: /**
132: * Compares request data with constraints and sets the valid flag.
133: *
134: * @return the valid flag
135: */
136: public boolean validate() {
137: ParameterParser pp = (ParameterParser) super .parser;
138: if (isMultiValued) {
139: FileItem[] ss = pp.getFileItems(getKey());
140: // this definition of not set might need refined. But
141: // not sure the situation will arise.
142: if (ss.length == 0) {
143: setFlag = false;
144: }
145:
146: if (validator != null) {
147: for (int i = 0; i < ss.length; i++) {
148: try {
149: ((FileValidator) validator)
150: .assertValidity(ss[i]);
151: } catch (ValidationException ve) {
152: setMessage(ve.getMessage());
153: }
154: }
155: }
156:
157: if (setFlag && validFlag) {
158: doSetValue();
159: }
160: } else {
161: FileItem s = pp.getFileItem(getKey());
162: if (s == null || s.getSize() == 0) {
163: setFlag = false;
164: }
165:
166: if (validator != null) {
167: try {
168: ((FileValidator) validator).assertValidity(s);
169:
170: if (setFlag) {
171: doSetValue();
172: }
173: } catch (ValidationException ve) {
174: setMessage(ve.getMessage());
175: }
176: } else if (setFlag) {
177: doSetValue();
178: }
179: }
180:
181: return validFlag;
182: }
183:
184: /**
185: * Sets the value of the field from data in the parser.
186: */
187: protected void doSetValue() {
188: ParameterParser pp = (ParameterParser) super.parser;
189: if (isMultiValued) {
190: setTestValue(pp.getFileItems(getKey()));
191: } else {
192: setTestValue(pp.getFileItem(getKey()));
193: }
194: }
195: }
|