KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > intake > model > FileItemField


1 package org.apache.turbine.services.intake.model;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
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  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
31  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
32  * @author <a HREF="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
33  * @version $Id: FileItemField.java,v 1.13.2.2 2004/05/20 03:16:39 seade Exp $
34  */

35 public class FileItemField
36         extends Field
37 {
38
39     /**
40      * Constructor.
41      *
42      * @param field xml field definition object
43      * @param group xml group definition object
44      * @throws IntakeException thrown by superclass
45      */

46     public FileItemField(XmlField field, Group group)
47             throws IntakeException
48     {
49         super(field, group);
50     }
51
52     /**
53      * It is not possible to set the default value for this field type.
54      * Calling this method with a non-null parameter will result in a
55      * TurbineRuntimeException
56      *
57      * @param prop Parameter for the default values
58      * @throws TurbineRuntimeException
59      */

60     public void setDefaultValue(String JavaDoc prop)
61     {
62         if (prop != null)
63         {
64             throw new TurbineRuntimeException(
65                     "Default values are not valid for "
66                     + this.getClass().getName());
67         }
68
69         defaultValue = null;
70     }
71
72     /**
73      * It is not possible to set the empty value for this field type.
74      * Calling this method with a non-null parameter will result in a
75      * TurbineRuntimeException
76      *
77      * @param prop Parameter for the empty values
78      * @throws TurbineRuntimeException
79      */

80     public void setEmptyValue(String JavaDoc prop)
81     {
82         if (prop != null)
83         {
84             throw new TurbineRuntimeException(
85                     "Empty values are not valid for "
86                     + this.getClass().getName());
87         }
88
89         emptyValue = null;
90     }
91
92     /**
93      * A suitable validator.
94      *
95      * @return A suitable validator
96      */

97     protected String JavaDoc getDefaultValidator()
98     {
99         return FileValidator.class.getName();
100     }
101
102     /**
103      * Method called when this field (the group it belongs to) is
104      * pulled from the pool. The request data is searched to determine
105      * if a value has been supplied for this field. if so, the value
106      * is validated.
107      *
108      * @param vp a <code>ValueParser</code> value
109      * @return a <code>Field</code> value
110      * @exception IntakeException if an error occurs
111      */

112     public Field init(ValueParser vp)
113             throws IntakeException
114     {
115         try
116         {
117             super.parser = (ParameterParser) vp;
118         }
119         catch (ClassCastException JavaDoc e)
120         {
121             throw new IntakeException(
122                     "FileItemFields can only be used with ParameterParser");
123         }
124
125         validFlag = true;
126
127         if (parser.containsKey(getKey()))
128         {
129             setFlag = true;
130             validate();
131         }
132
133         initialized = true;
134         return this;
135     }
136
137     /**
138      * Compares request data with constraints and sets the valid flag.
139      *
140      * @return the valid flag
141      */

142     protected boolean validate()
143     {
144         ParameterParser pp = (ParameterParser) super.parser;
145         if (isMultiValued)
146         {
147             FileItem[] ss = pp.getFileItems(getKey());
148             // this definition of not set might need refined. But
149
// not sure the situation will arise.
150
if (ss.length == 0)
151             {
152                 setFlag = false;
153             }
154
155             if (validator != null)
156             {
157                 for (int i = 0; i < ss.length; i++)
158                 {
159                     try
160                     {
161                         ((FileValidator) validator).assertValidity(ss[i]);
162                     }
163                     catch (ValidationException ve)
164                     {
165                         setMessage(ve.getMessage());
166                     }
167                 }
168             }
169
170             if (setFlag && validFlag)
171             {
172                 doSetValue();
173             }
174         }
175         else
176         {
177             FileItem s = pp.getFileItem(getKey());
178             if (s == null || s.getSize() == 0)
179             {
180                 setFlag = false;
181             }
182
183             if (validator != null)
184             {
185                 try
186                 {
187                     ((FileValidator) validator).assertValidity(s);
188
189                     if (setFlag)
190                     {
191                         doSetValue();
192                     }
193                 }
194                 catch (ValidationException ve)
195                 {
196                     setMessage(ve.getMessage());
197                 }
198             }
199             else if (setFlag)
200             {
201                 doSetValue();
202             }
203         }
204
205         return validFlag;
206     }
207
208     /**
209      * Sets the value of the field from data in the parser.
210      */

211     protected void doSetValue()
212     {
213         ParameterParser pp = (ParameterParser) super.parser;
214         if (isMultiValued)
215         {
216             setTestValue(pp.getFileItems(getKey()));
217         }
218         else
219         {
220             setTestValue(pp.getFileItem(getKey()));
221         }
222     }
223 }
224
Popular Tags