KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > html > FileUpload


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.html;
19
20 import org.apache.beehive.netui.pageflow.internal.InternalUtils;
21 import org.apache.beehive.netui.tags.ByRef;
22 import org.apache.beehive.netui.tags.rendering.AbstractHtmlState;
23 import org.apache.beehive.netui.tags.rendering.InputFileTag;
24 import org.apache.beehive.netui.tags.rendering.TagRenderingBase;
25 import org.apache.beehive.netui.tags.rendering.WriteRenderAppender;
26 import org.apache.beehive.netui.util.Bundle;
27
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.jsp.JspException JavaDoc;
30
31 /**
32  * Renders an input tag with type="file", with the given attributes. Usage of this tag requires the parent
33  * Form tag to have enctype="multipart/form-data".
34  *
35  *
36  *
37  * <p><b>Notes</b></p>
38  * <ul>
39  * <li>The dataSource for the FileUpload tag is write-only. This means that the value of the FileUpload
40  * cannot get read back into the FileUpload on postback.</li>
41  * <li>The page flow or form bean property pointed to with the FileUpload's dataSource
42  * should be of type <code>org.apache.struts.upload.FormFile</code>.</li>
43  * </ul>
44  * @jsptagref.tagdescription Renders an HTML input tag with which users can browse, select, and upload files
45  * from their local machine.
46  *
47  * <p>To successfully upload files, the following two conditions must be met:
48  *
49  * <blockquote>
50  * <ul>
51  * <li>The &lt;netui:fileUpload> tag must have a parent
52  * {@link Form} tag with the attribute <code>enctype="multipart/form-data"</code>
53  *
54  * <pre> &lt;netui:form action="uploadFile" <b>enctype="multipart/form-data"</b>>
55  * ...
56  * &lt;netui:fileUpload tagId="inputbox" dataSource="actionForm.theFile" />
57  * ...
58  * &lt;/netui:form></pre>
59  *
60  * </li>
61  * <li>The <code>dataSource</code>
62  * attribute must point to a Form Bean field of
63  * type <code>org.apache.struts.upload.FormFile</code>.
64  *
65  * <pre> public static class UploadFileForm extends FormData
66  * {
67  * private <b>org.apache.struts.upload.FormFile</b> theFile;
68  *
69  * public void setTheFile(org.apache.struts.upload.FormFile theFile)
70  * {
71  * this.theFile = theFile;
72  * }
73  *
74  * public org.apache.struts.upload.FormFile getTheFile()
75  * {
76  * return this.theFile;
77  * }
78  * }</pre>
79  * </li>
80  * </ul>
81  * </blockquote>
82  *
83  * The <code>dataSource</code> attribute for the &lt;netui:fileUpload> tag is write-only.
84  * This means that the value of the &lt;netui:fileUpload> tag
85  * cannot be read back into the tag on postback.
86  * @example In the following example, note the <code>enctype</code> attribute of the
87  * enclosing &lt;netui:form> tag.
88  * <pre> &lt;netui:form action="uploadFile" enctype="multipart/form-data">
89  * &lt;netui:fileUpload tagId="inputbox" dataSource="actionForm.theFile" />
90  * &lt;netui:button value="Upload File" />
91  * &lt;/netui:form></pre>
92  *
93  * Note that the &lt;netui:fileUpload> tag submits the file to Form Bean property <code>theFile</code>. This
94  * property must be of type <code>org.apache.struts.upload.FormFile</code>:
95  *
96  * <pre> public static class UploadFileForm extends FormData
97  * {
98  * private <b>org.apache.struts.upload.FormFile</b> theFile;
99  *
100  * public void setTheFile(<b>org.apache.struts.upload.FormFile</b> theFile)
101  * {
102  * this.theFile = theFile;
103  * }
104  *
105  * public <b>org.apache.struts.upload.FormFile</b> getTheFile()
106  * {
107  * return this.theFile;
108  * }
109  * }</pre>
110  * @netui:tag name="fileUpload" description="Upload a file from the client to the server."
111  */

112 public class FileUpload extends HtmlDataSourceTag
113 {
114     private InputFileTag.State _state = new InputFileTag.State();
115
116     /**
117      * Return the name of the Tag.
118      */

119     public String JavaDoc getTagName()
120     {
121         return "FileUpload";
122     }
123
124     /**
125      * Base support for the attribute tag. This is overridden to prevent setting the <code>type</code>
126      * attribute.
127      * @param name The name of the attribute. This value may not be null or the empty string.
128      * @param value The value of the attribute. This may contain an expression.
129      * @param facet The name of a facet to which the attribute will be applied. This is optional.
130      * @throws JspException A JspException may be thrown if there is an error setting the attribute.
131      */

132     public void setAttribute(String JavaDoc name, String JavaDoc value, String JavaDoc facet)
133             throws JspException JavaDoc
134     {
135         if (name != null) {
136             if (name.equals(TYPE)) {
137                 String JavaDoc s = Bundle.getString("Tags_AttributeMayNotBeSet", new Object JavaDoc[]{name});
138                 registerTagError(s, null);
139             }
140             else if (name.equals(READONLY)) {
141                 _state.readonly = Boolean.valueOf(value).booleanValue();
142                 return;
143             }
144         }
145         super.setAttribute(name, value, facet);
146     }
147
148     /**
149      * This method will return the state associated with the tag. This is used by this
150      * base class to access the individual state objects created by the tags.
151      * @return a subclass of the <code>AbstractHtmlState</code> class.
152      */

153     protected AbstractHtmlState getState()
154     {
155         return _state;
156     }
157
158     /**
159      * Set the mime-types accepted for the FileUpload.
160      * @param accept the accepted mime-types
161      * @jsptagref.attributedescription The set of MIME types accepted for file upload.
162      * @jsptagref.databindable false
163      * @jsptagref.attributesyntaxvalue <i>string_acceptTheseMIMETypes</i>
164      * @netui:attribute required="false" rtexprvalue="true"
165      * description="The set of MIME types accepted for file upload."
166      */

167     public void setAccept(String JavaDoc accept)
168     {
169         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ACCEPT, accept);
170     }
171
172     /**
173      * Set if this FileUpload is read-only.
174      * @param readonly the read-only state
175      * @jsptagref.attributedescription Boolean. Determines whether or not the file upload field is read-only.
176      * @jsptagref.databindable false
177      * @jsptagref.attributesyntaxvalue <i>boolean_readOnly</i>
178      * @netui:attribute required="false" rtexprvalue="true" type="boolean"
179      * description="Determines whether or not the file upload field is read-only."
180      */

181     public void setReadonly(boolean readonly)
182     {
183         _state.readonly = readonly;
184     }
185
186     /**
187      * Set the size (in characters) of the FileUpload.
188      * @param size the size
189      * @jsptagref.attributedescription Integer. The number of characters visible in the file upload field.
190      * @jsptagref.databindable false
191      * @jsptagref.attributesyntaxvalue <i>integer_fieldSize</i>
192      * @netui:attribute required="false" rtexprvalue="true"
193      * description="The number of characters visible in the file upload field."
194      */

195     public void setSize(String JavaDoc size)
196     {
197         _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, SIZE, size);
198     }
199
200     /**
201      * Render the fileUpLoad tag. Cause the body to be buffered to look
202      * for contained tags.
203      * @throws JspException if a JSP exception has occurred
204      */

205     public int doStartTag() throws JspException JavaDoc
206     {
207         return EVAL_BODY_BUFFERED;
208     }
209
210     /**
211      * Render the FileUpload.
212      * @throws JspException if a JSP exception has occurred
213      */

214     public int doEndTag() throws JspException JavaDoc
215     {
216         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) pageContext.getRequest();
217
218         // if we are not handling multipart requests then we can't output the file upload tag and we will
219
// report an error
220
if (!InternalUtils.isMultipartHandlingEnabled(req)) {
221             String JavaDoc s = Bundle.getString("Tags_FileMultiOff", null);
222             registerTagError(s, null);
223             return EVAL_PAGE;
224         }
225
226         // Create the state for the input tag.
227
ByRef ref = new ByRef();
228         nameHtmlControl(_state, ref);
229
230         if (hasErrors())
231             return reportAndExit(EVAL_PAGE);
232
233         WriteRenderAppender writer = new WriteRenderAppender(pageContext);
234         TagRenderingBase br = TagRenderingBase.Factory.getRendering(TagRenderingBase.INPUT_FILE_TAG, req);
235         br.doStartTag(writer, _state);
236
237         if (!ref.isNull())
238             write((String JavaDoc) ref.getRef());
239
240         localRelease();
241         return EVAL_PAGE;
242     }
243
244     /**
245      * Release any acquired resources.
246      */

247     protected void localRelease()
248     {
249         super.localRelease();
250         _state.clear();
251     }
252 }
253
Popular Tags