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