001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.component.inputfile;
035:
036: import com.icesoft.faces.context.BridgeFacesContext;
037: import com.icesoft.faces.utils.MessageUtils;
038: import org.apache.commons.fileupload.FileUploadBase;
039:
040: import javax.faces.component.UIComponent;
041: import javax.faces.context.FacesContext;
042: import javax.faces.context.ResponseWriter;
043: import javax.faces.render.Renderer;
044: import java.io.IOException;
045: import java.io.StringWriter;
046: import java.io.File;
047:
048: public class InputFileRenderer extends Renderer {
049:
050: public void encodeBegin(FacesContext context, UIComponent component)
051: throws IOException {
052: InputFile c = (InputFile) component;
053: BridgeFacesContext facesContext = (BridgeFacesContext) context;
054: ResponseWriter writer = context.getResponseWriter();
055: StringWriter iframeContentWriter = new StringWriter();
056: c.renderIFrame(iframeContentWriter, facesContext);
057: String pseudoURL = "javascript: document.write('"
058: + iframeContentWriter.toString()
059: .replaceAll("\"", "%22")
060: + "'); document.close();";
061:
062: writer.startElement("iframe", c);
063: writer.writeAttribute("src", pseudoURL, null);
064: writer.writeAttribute("class", c.getStyleClass(), null);
065: writer.writeAttribute("style", c.getStyle(), null);
066: writer.writeAttribute("width", c.getWidth() + "px", null);
067: writer.writeAttribute("height", c.getHeight() + "px", null);
068: writer.writeAttribute("title", "Input File Frame", null);
069: writer.writeAttribute("frameborder", "0", null);
070: writer.writeAttribute("marginwidth", "0", null);
071: writer.writeAttribute("marginheight", "0", null);
072: writer.writeAttribute("scrolling", "no", null);
073: // Need to set allowtransparency="true" on the IFRAME element tag so that,
074: // along with InputFile.renderIFrame()'s <body style="background-color: transparent;">, the IFRAME
075: // will be transparent, allowing background colors to show through.
076: writer.writeAttribute("allowtransparency", "true", null);
077: writer.endElement("iframe");
078:
079: Throwable uploadException = c.getUploadException();
080: if (uploadException != null) {
081: try {
082: throw uploadException;
083: } catch (FileUploadBase.FileSizeLimitExceededException e) {
084: context.addMessage(null, MessageUtils.getMessage(
085: context,
086: InputFile.SIZE_LIMIT_EXCEEDED_MESSAGE_ID));
087: } catch (FileUploadBase.UnknownSizeException e) {
088: context.addMessage(null, MessageUtils.getMessage(
089: context, InputFile.UNKNOWN_SIZE_MESSAGE_ID));
090: } catch (FileUploadBase.InvalidContentTypeException e) {
091: String fileName = c.getFileInfo().getFileName();
092: if (fileName == null) {
093: File file = c.getFile();
094: if (file != null)
095: fileName = file.getName();
096: }
097: if (fileName == null)
098: fileName = "";
099: context.addMessage(null, MessageUtils.getMessage(
100: context, InputFile.INVALID_FILE_MESSAGE_ID,
101: new Object[] { fileName }));
102: } catch (Throwable t) {
103: //ignore
104: }
105: }
106: }
107: }
|