1 package org.apache.turbine.services.intake.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.fileupload.FileItem;
23
24 import org.apache.turbine.services.intake.IntakeException;
25 import org.apache.turbine.services.intake.validator.FileValidator;
26 import org.apache.turbine.services.intake.validator.ValidationException;
27 import org.apache.turbine.services.intake.xmlmodel.XmlField;
28 import org.apache.turbine.util.TurbineRuntimeException;
29 import org.apache.turbine.util.parser.ParameterParser;
30 import org.apache.turbine.util.parser.ValueParser;
31
32 /***
33 * This Intake field is intended to represent a File input element in a HTML form.
34 *
35 * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
36 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
37 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
38 * @version $Id: FileItemField.java 537514 2007-05-12 20:55:26Z tv $
39 */
40 public class FileItemField
41 extends Field
42 {
43
44 /***
45 * Constructor.
46 *
47 * @param field xml field definition object
48 * @param group xml group definition object
49 * @throws IntakeException thrown by superclass
50 */
51 public FileItemField(XmlField field, Group group)
52 throws IntakeException
53 {
54 super(field, group);
55 }
56
57 /***
58 * It is not possible to set the default value for this field type.
59 * Calling this method with a non-null parameter will result in a
60 * TurbineRuntimeException
61 *
62 * @param prop Parameter for the default values
63 * @throws TurbineRuntimeException
64 */
65 public void setDefaultValue(String prop)
66 {
67 if (prop != null)
68 {
69 throw new TurbineRuntimeException(
70 "Default values are not valid for "
71 + this.getClass().getName());
72 }
73
74 defaultValue = null;
75 }
76
77 /***
78 * It is not possible to set the empty value for this field type.
79 * Calling this method with a non-null parameter will result in a
80 * TurbineRuntimeException
81 *
82 * @param prop Parameter for the empty values
83 * @throws TurbineRuntimeException
84 */
85 public void setEmptyValue(String prop)
86 {
87 if (prop != null)
88 {
89 throw new TurbineRuntimeException(
90 "Empty values are not valid for "
91 + this.getClass().getName());
92 }
93
94 emptyValue = null;
95 }
96
97 /***
98 * A suitable validator.
99 *
100 * @return A suitable validator
101 */
102 protected String getDefaultValidator()
103 {
104 return FileValidator.class.getName();
105 }
106
107 /***
108 * Method called when this field (the group it belongs to) is
109 * pulled from the pool. The request data is searched to determine
110 * if a value has been supplied for this field. if so, the value
111 * is validated.
112 *
113 * @param vp a <code>ValueParser</code> value
114 * @return a <code>Field</code> value
115 * @exception IntakeException if an error occurs
116 */
117 public Field init(ValueParser vp)
118 throws IntakeException
119 {
120 try
121 {
122 super.parser = (ParameterParser) vp;
123 }
124 catch (ClassCastException e)
125 {
126 throw new IntakeException(
127 "FileItemFields can only be used with ParameterParser");
128 }
129
130 validFlag = true;
131
132 FileItem [] fileItems = ((ParameterParser) parser).getFileItems(getKey());
133
134 if (fileItems != null)
135 {
136 setFlag = true;
137 validate();
138 }
139
140 initialized = true;
141 return this;
142 }
143
144 /***
145 * Compares request data with constraints and sets the valid flag.
146 *
147 * @return the valid flag
148 */
149 public boolean validate()
150 {
151 ParameterParser pp = (ParameterParser) super.parser;
152 if (isMultiValued)
153 {
154 FileItem[] ss = pp.getFileItems(getKey());
155
156
157 if (ss.length == 0)
158 {
159 setFlag = false;
160 }
161
162 if (validator != null)
163 {
164 for (int i = 0; i < ss.length; i++)
165 {
166 try
167 {
168 ((FileValidator) validator).assertValidity(ss[i]);
169 }
170 catch (ValidationException ve)
171 {
172 setMessage(ve.getMessage());
173 }
174 }
175 }
176
177 if (setFlag && validFlag)
178 {
179 doSetValue();
180 }
181 }
182 else
183 {
184 FileItem s = pp.getFileItem(getKey());
185 if (s == null || s.getSize() == 0)
186 {
187 setFlag = false;
188 }
189
190 if (validator != null)
191 {
192 try
193 {
194 ((FileValidator) validator).assertValidity(s);
195
196 if (setFlag)
197 {
198 doSetValue();
199 }
200 }
201 catch (ValidationException ve)
202 {
203 setMessage(ve.getMessage());
204 }
205 }
206 else if (setFlag)
207 {
208 doSetValue();
209 }
210 }
211
212 return validFlag;
213 }
214
215 /***
216 * Sets the value of the field from data in the parser.
217 */
218 protected void doSetValue()
219 {
220 ParameterParser pp = (ParameterParser) super.parser;
221 if (isMultiValued)
222 {
223 setTestValue(pp.getFileItems(getKey()));
224 }
225 else
226 {
227 setTestValue(pp.getFileItem(getKey()));
228 }
229 }
230 }