KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > inputfile > InputFileRenderer


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.component.inputfile;
35
36 import com.icesoft.faces.context.BridgeFacesContext;
37 import com.icesoft.faces.utils.MessageUtils;
38 import org.apache.commons.fileupload.FileUploadBase;
39
40 import javax.faces.component.UIComponent;
41 import javax.faces.context.FacesContext;
42 import javax.faces.context.ResponseWriter;
43 import javax.faces.render.Renderer;
44 import java.io.IOException JavaDoc;
45 import java.io.StringWriter JavaDoc;
46 import java.io.File JavaDoc;
47
48 public class InputFileRenderer extends Renderer {
49
50     public void encodeBegin(FacesContext context, UIComponent component) throws IOException JavaDoc {
51         InputFile c = (InputFile) component;
52         BridgeFacesContext facesContext = (BridgeFacesContext) context;
53         ResponseWriter writer = context.getResponseWriter();
54         StringWriter JavaDoc iframeContentWriter = new StringWriter JavaDoc();
55         c.renderIFrame(iframeContentWriter, facesContext);
56         String JavaDoc pseudoURL = "javascript: document.write('" + iframeContentWriter.toString().replaceAll("\"", "%22") + "'); document.close();";
57
58         writer.startElement("iframe", c);
59         writer.writeAttribute("src", pseudoURL, null);
60         writer.writeAttribute("class", c.getStyleClass(), null);
61         writer.writeAttribute("style", c.getStyle(), null);
62         writer.writeAttribute("width", c.getWidth() + "px", null);
63         writer.writeAttribute("height", c.getHeight() + "px", null);
64         writer.writeAttribute("title", "Input File Frame", null);
65         writer.writeAttribute("frameborder", "0", null);
66         writer.writeAttribute("marginwidth", "0", null);
67         writer.writeAttribute("marginheight", "0", null);
68         writer.writeAttribute("scrolling", "no", null);
69         // Need to set allowtransparency="true" on the IFRAME element tag so that,
70
// along with InputFile.renderIFrame()'s <body style="background-color: transparent;">, the IFRAME
71
// will be transparent, allowing background colors to show through.
72
writer.writeAttribute("allowtransparency", "true", null);
73         writer.endElement("iframe");
74
75         Throwable JavaDoc uploadException = c.getUploadException();
76         if (uploadException != null) {
77             try {
78                 throw uploadException;
79             } catch (FileUploadBase.FileSizeLimitExceededException e) {
80                 context.addMessage(null, MessageUtils.getMessage(context, InputFile.SIZE_LIMIT_EXCEEDED_MESSAGE_ID));
81             } catch (FileUploadBase.UnknownSizeException e) {
82                 context.addMessage(null, MessageUtils.getMessage(context, InputFile.UNKNOWN_SIZE_MESSAGE_ID));
83             } catch (FileUploadBase.InvalidContentTypeException e) {
84                 String JavaDoc fileName = c.getFileInfo().getFileName();
85                 if(fileName == null) {
86                     File JavaDoc file = c.getFile();
87                     if(file != null)
88                         fileName = file.getName();
89                 }
90                 if(fileName == null)
91                     fileName = "";
92                 context.addMessage(null, MessageUtils.getMessage(context, InputFile.INVALID_FILE_MESSAGE_ID, new Object JavaDoc[] { fileName }));
93             } catch (Throwable JavaDoc t) {
94                 //ignore
95
}
96         }
97     }
98 }
Popular Tags