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 import org.apache.commons.fileupload.FileItem;
20
21 import org.apache.turbine.services.intake.IntakeException;
22 import org.apache.turbine.services.intake.validator.FileValidator;
23 import org.apache.turbine.services.intake.validator.ValidationException;
24 import org.apache.turbine.services.intake.xmlmodel.XmlField;
25 import org.apache.turbine.util.TurbineRuntimeException;
26 import org.apache.turbine.util.parser.ParameterParser;
27 import org.apache.turbine.util.parser.ValueParser;
28
29 /***
30 * This Intake field is intended to represent a File input element in a HTML form.
31 *
32 * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
33 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
34 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
35 * @version $Id: FileItemField.java 264148 2005-08-29 14:21:04Z henning $
36 */
37 public class FileItemField
38 extends Field
39 {
40
41 /***
42 * Constructor.
43 *
44 * @param field xml field definition object
45 * @param group xml group definition object
46 * @throws IntakeException thrown by superclass
47 */
48 public FileItemField(XmlField field, Group group)
49 throws IntakeException
50 {
51 super(field, group);
52 }
53
54 /***
55 * It is not possible to set the default value for this field type.
56 * Calling this method with a non-null parameter will result in a
57 * TurbineRuntimeException
58 *
59 * @param prop Parameter for the default values
60 * @throws TurbineRuntimeException
61 */
62 public void setDefaultValue(String prop)
63 {
64 if (prop != null)
65 {
66 throw new TurbineRuntimeException(
67 "Default values are not valid for "
68 + this.getClass().getName());
69 }
70
71 defaultValue = null;
72 }
73
74 /***
75 * It is not possible to set the empty value for this field type.
76 * Calling this method with a non-null parameter will result in a
77 * TurbineRuntimeException
78 *
79 * @param prop Parameter for the empty values
80 * @throws TurbineRuntimeException
81 */
82 public void setEmptyValue(String prop)
83 {
84 if (prop != null)
85 {
86 throw new TurbineRuntimeException(
87 "Empty values are not valid for "
88 + this.getClass().getName());
89 }
90
91 emptyValue = null;
92 }
93
94 /***
95 * A suitable validator.
96 *
97 * @return A suitable validator
98 */
99 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 FileItem [] fileItems = ((ParameterParser) parser).getFileItems(getKey());
130
131 if (fileItems != null)
132 {
133 setFlag = true;
134 validate();
135 }
136
137 initialized = true;
138 return this;
139 }
140
141 /***
142 * Compares request data with constraints and sets the valid flag.
143 *
144 * @return the valid flag
145 */
146 protected boolean validate()
147 {
148 ParameterParser pp = (ParameterParser) super.parser;
149 if (isMultiValued)
150 {
151 FileItem[] ss = pp.getFileItems(getKey());
152
153
154 if (ss.length == 0)
155 {
156 setFlag = false;
157 }
158
159 if (validator != null)
160 {
161 for (int i = 0; i < ss.length; i++)
162 {
163 try
164 {
165 ((FileValidator) validator).assertValidity(ss[i]);
166 }
167 catch (ValidationException ve)
168 {
169 setMessage(ve.getMessage());
170 }
171 }
172 }
173
174 if (setFlag && validFlag)
175 {
176 doSetValue();
177 }
178 }
179 else
180 {
181 FileItem s = pp.getFileItem(getKey());
182 if (s == null || s.getSize() == 0)
183 {
184 setFlag = false;
185 }
186
187 if (validator != null)
188 {
189 try
190 {
191 ((FileValidator) validator).assertValidity(s);
192
193 if (setFlag)
194 {
195 doSetValue();
196 }
197 }
198 catch (ValidationException ve)
199 {
200 setMessage(ve.getMessage());
201 }
202 }
203 else if (setFlag)
204 {
205 doSetValue();
206 }
207 }
208
209 return validFlag;
210 }
211
212 /***
213 * Sets the value of the field from data in the parser.
214 */
215 protected void doSetValue()
216 {
217 ParameterParser pp = (ParameterParser) super.parser;
218 if (isMultiValued)
219 {
220 setTestValue(pp.getFileItems(getKey()));
221 }
222 else
223 {
224 setTestValue(pp.getFileItem(getKey()));
225 }
226 }
227 }