KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > intake > model > FileItemField


1 package org.apache.fulcrum.intake.model;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache Turbine" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache Turbine", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import org.apache.commons.fileupload.FileItem;
58 import org.apache.fulcrum.ServiceException;
59 import org.apache.fulcrum.intake.validator.FileValidator;
60 import org.apache.fulcrum.intake.validator.ValidationException;
61 import org.apache.fulcrum.intake.xmlmodel.XmlField;
62 import org.apache.fulcrum.parser.ParameterParser;
63 import org.apache.fulcrum.parser.ValueParser;
64
65 /**
66  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
67  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
68  * @version $Id: FileItemField.java,v 1.1 2004/11/12 10:25:37 epugh Exp $
69  */

70 public class FileItemField
71     extends Field
72 {
73     public FileItemField(XmlField field, Group group)
74         throws Exception JavaDoc
75     {
76         super(field, group);
77     }
78
79     /**
80      * Sets the default value for an FileItemField
81      */

82
83     protected void setDefaultValue(String JavaDoc prop)
84     {
85         defaultValue = prop;
86     }
87
88     /**
89      * A suitable validator.
90      *
91      * @return "FileValidator"
92      */

93     protected String JavaDoc getDefaultValidator()
94     {
95         return "org.apache.fulcrum.intake.validator.FileValidator";
96     }
97
98     /**
99      * Method called when this field (the group it belongs to) is
100      * pulled from the pool. The request data is searched to determine
101      * if a value has been supplied for this field. if so, the value
102      * is validated.
103      *
104      * @param pp a <code>ParameterParser</code> value
105      * @return a <code>Field</code> value
106      * @exception ServiceException if an error occurs
107      */

108     public Field init(ValueParser vp)
109         throws ServiceException
110     {
111         try
112         {
113             super.pp = (ParameterParser) vp;
114         }
115         catch (ClassCastException JavaDoc e)
116         {
117             throw new ServiceException(
118                 "FileItemFields can only be used with ParameterParser");
119         }
120
121         valid_flag = true;
122
123         if ( pp.containsKey(getKey()) )
124         {
125             set_flag = true;
126             validate(pp);
127         }
128
129         initialized = true;
130         return this;
131     }
132
133     /**
134      * Compares request data with constraints and sets the valid flag.
135      */

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

207     protected void doSetValue()
208     {
209         ParameterParser pp = (ParameterParser) super.pp;
210         if ( isMultiValued )
211         {
212             setTestValue(pp.getFileItems(getKey()));
213         }
214         else
215         {
216             setTestValue(pp.getFileItem(getKey()));
217         }
218     }
219 }
220
Popular Tags