001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.rave.web.ui.renderer;
042:
043: import java.io.IOException;
044: import java.util.Map;
045: import javax.faces.FacesException;
046: import javax.faces.component.UIComponent;
047: import javax.faces.context.ResponseWriter;
048: import javax.faces.context.FacesContext;
049: import com.sun.rave.web.ui.component.Upload;
050: import com.sun.rave.web.ui.theme.Theme;
051: import com.sun.rave.web.ui.util.MessageUtil;
052: import com.sun.rave.web.ui.util.RenderingUtilities;
053: import com.sun.rave.web.ui.util.ThemeUtilities;
054: import javax.faces.application.FacesMessage;
055:
056: /**
057: * <p>Renderer for a {@link Upload} component.</p>
058: */
059:
060: public class UploadRenderer extends FieldRenderer {
061:
062: private static final boolean DEBUG = false;
063:
064: /**
065: * <p>Override the default implementation to conditionally trim the
066: * leading and trailing spaces from the submitted value.</p>
067: *
068: * @param context <code>FacesContext</code> for the current request
069: * @param component <code>Upload</code> component being processed
070: */
071: public void decode(FacesContext context, UIComponent component) {
072:
073: boolean DEBUG = true;
074: if (DEBUG)
075: log("decode()");
076: Upload upload = (Upload) component;
077: String id = component.getClientId(context).concat(
078: upload.INPUT_ID);
079: if (DEBUG)
080: log("\tLooking for id " + id);
081: Map map = context.getExternalContext().getRequestMap();
082: if (map.containsKey(id)) {
083: if (DEBUG)
084: log("\tFound id " + id);
085: upload.setSubmittedValue(id);
086: }
087: /*
088: else if(map.containsKey(Upload.ERROR)) {
089: if(DEBUG) log("\tFound error ");
090: upload.setSubmittedValue(Upload.ERROR);
091: }
092: */
093: return;
094: }
095:
096: public void encodeEnd(FacesContext context, UIComponent component)
097: throws IOException {
098:
099: if (!(component instanceof Upload)) {
100: Object[] params = { component.toString(),
101: this .getClass().getName(), Upload.class.getName() };
102: String message = MessageUtil.getMessage(
103: "com.sun.rave.web.ui.resources.LogMessages", //NOI18N
104: "Upload.renderer", params); //NOI18N
105: throw new FacesException(message);
106: }
107:
108: Theme theme = ThemeUtilities.getTheme(context);
109: Map map = context.getExternalContext().getRequestMap();
110: Object error = map.get(Upload.UPLOAD_ERROR_KEY);
111: if (error != null) {
112: if (error instanceof Throwable) {
113: if (error instanceof org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException) {
114: // Caused by the file size is too big
115: String maxSize = (String) map
116: .get(Upload.FILE_SIZE_KEY);
117: String[] detailArgs = { maxSize };
118: String summaryMsg = theme
119: .getMessage("FileUpload.noFile");
120: String detailMsg = theme.getMessage("Upload.error",
121: detailArgs);
122: FacesMessage fmsg = new FacesMessage(summaryMsg,
123: detailMsg);
124: context.addMessage(((Upload) component)
125: .getClientId(context), fmsg);
126: } else {
127: String summaryMsg = theme
128: .getMessage("FileUpload.noFile");
129: FacesException fe = new FacesException(summaryMsg);
130: fe.initCause((Throwable) error);
131: throw fe;
132: }
133: }
134: }
135:
136: boolean spanRendered = super .renderField(context,
137: (Upload) component, "file", getStyles(context));
138:
139: StringBuffer jsString = new StringBuffer(200);
140: String id = component.getClientId(context);
141: jsString.append("upload_setEncodingType(\'"); //NOI18N
142: jsString.append(id);
143: jsString.append("\');"); //NOI18N
144:
145: ResponseWriter writer = context.getResponseWriter();
146: writer.writeText("\n", null); //NOI18N
147: writer.startElement("script", component); //NOI18N
148: writer.writeText(jsString.toString(), null);
149: writer.endElement("script"); //NOI18N
150: if (!spanRendered) {
151: String param = id.concat(Upload.INPUT_PARAM_ID);
152: RenderingUtilities.renderHiddenField(component, writer,
153: param, id);
154: }
155: }
156: }
|