001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsf.html;
030:
031: import java.io.*;
032: import java.util.*;
033: import java.util.logging.*;
034:
035: import javax.faces.application.*;
036: import javax.faces.component.*;
037: import javax.faces.component.html.*;
038: import javax.faces.context.*;
039: import javax.faces.render.*;
040:
041: /**
042: * The HTML form renderer
043: */
044: class HtmlFormRenderer extends BaseRenderer {
045: private static final Logger log = Logger
046: .getLogger(HtmlFormRenderer.class.getName());
047:
048: public static final Renderer RENDERER = new HtmlFormRenderer();
049:
050: /**
051: * True if the renderer is responsible for rendering the children.
052: */
053: @Override
054: public boolean getRendersChildren() {
055: return true;
056: }
057:
058: /**
059: * Decodes the data from the form.
060: */
061: @Override
062: public void decode(FacesContext context, UIComponent component) {
063: String clientId = component.getClientId(context);
064:
065: ExternalContext ext = context.getExternalContext();
066: Map<String, String> paramMap = ext.getRequestParameterMap();
067:
068: String value = paramMap.get(clientId);
069:
070: ((UIForm) component).setSubmitted(value != null);
071:
072: if (log.isLoggable(Level.FINE) && value != null) {
073: log.fine("JSF[" + context.getViewRoot().getViewId()
074: + "] decoding form data for '" + clientId + "'");
075: }
076: }
077:
078: /**
079: * Renders the open tag for the text.
080: */
081: @Override
082: public void encodeBegin(FacesContext context, UIComponent component)
083: throws IOException {
084: ResponseWriter out = context.getResponseWriter();
085:
086: String id = component.getId();
087:
088: String accept;
089: String acceptcharset;
090: String dir;
091: String enctype;
092: String lang;
093:
094: String onclick;
095: String ondblclick;
096: String onfocus;
097:
098: String onkeydown;
099: String onkeypress;
100: String onkeyup;
101:
102: String onmousedown;
103: String onmousemove;
104: String onmouseout;
105: String onmouseover;
106: String onmouseup;
107:
108: String onreset;
109: String onsubmit;
110:
111: String style;
112: String styleClass;
113: String tabindex;
114: String target;
115: String title;
116:
117: if (component instanceof HtmlForm) {
118: HtmlForm htmlForm = (HtmlForm) component;
119:
120: accept = htmlForm.getAccept();
121: acceptcharset = htmlForm.getAcceptcharset();
122: dir = htmlForm.getDir();
123: enctype = htmlForm.getEnctype();
124: lang = htmlForm.getLang();
125:
126: onclick = htmlForm.getOnclick();
127: ondblclick = htmlForm.getOndblclick();
128:
129: onkeydown = htmlForm.getOnkeydown();
130: onkeypress = htmlForm.getOnkeypress();
131: onkeyup = htmlForm.getOnkeyup();
132:
133: onmousedown = htmlForm.getOnmousedown();
134: onmousemove = htmlForm.getOnmousemove();
135: onmouseout = htmlForm.getOnmouseout();
136: onmouseover = htmlForm.getOnmouseover();
137: onmouseup = htmlForm.getOnmouseup();
138:
139: onreset = htmlForm.getOnreset();
140: onsubmit = htmlForm.getOnsubmit();
141:
142: style = htmlForm.getStyle();
143: styleClass = htmlForm.getStyleClass();
144: target = htmlForm.getTarget();
145: title = htmlForm.getTitle();
146: } else {
147: Map<String, Object> attrMap = component.getAttributes();
148:
149: accept = (String) attrMap.get("accept");
150: acceptcharset = (String) attrMap.get("acceptcharset");
151: dir = (String) attrMap.get("dir");
152: enctype = (String) attrMap.get("enctype");
153: lang = (String) attrMap.get("lang");
154:
155: onclick = (String) attrMap.get("onclick");
156: ondblclick = (String) attrMap.get("ondblclick");
157: onfocus = (String) attrMap.get("onfocus");
158:
159: onkeydown = (String) attrMap.get("onkeydown");
160: onkeypress = (String) attrMap.get("onkeypress");
161: onkeyup = (String) attrMap.get("onkeyup");
162:
163: onmousedown = (String) attrMap.get("onmousedown");
164: onmousemove = (String) attrMap.get("onmousemove");
165: onmouseout = (String) attrMap.get("onmouseout");
166: onmouseover = (String) attrMap.get("onmouseover");
167: onmouseup = (String) attrMap.get("onmouseup");
168:
169: onreset = (String) attrMap.get("onreset");
170: onsubmit = (String) attrMap.get("onsubmit");
171:
172: style = (String) attrMap.get("style");
173: styleClass = (String) attrMap.get("styleClass");
174: target = (String) attrMap.get("target");
175: title = (String) attrMap.get("title");
176: }
177:
178: out.startElement("form", component);
179:
180: out.writeAttribute("name", component.getClientId(context),
181: "name");
182: if (id != null && !id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
183: out.writeAttribute("id", component.getClientId(context),
184: "id");
185:
186: out.writeAttribute("method", "post", "method");
187:
188: String viewId = context.getViewRoot().getViewId();
189:
190: Application app = context.getApplication();
191: ExternalContext extContext = context.getExternalContext();
192:
193: ViewHandler view = app.getViewHandler();
194: String action = view.getActionURL(context, viewId);
195: String encodedAction = extContext.encodeActionURL(action);
196:
197: out.writeAttribute("action", encodedAction, "action");
198:
199: if (accept != null)
200: out.writeAttribute("accept", accept, "accept");
201:
202: if (acceptcharset != null)
203: out.writeAttribute("accept-charset", acceptcharset,
204: "acceptcharset");
205:
206: if (dir != null)
207: out.writeAttribute("dir", dir, "dir");
208:
209: if (enctype != null)
210: out.writeAttribute("enctype", enctype, "enctype");
211: else
212: out.writeAttribute("enctype",
213: "application/x-www-form-urlencoded", "enctype");
214:
215: if (lang != null)
216: out.writeAttribute("lang", lang, "lang");
217:
218: if (onclick != null)
219: out.writeAttribute("onclick", onclick, "onclick");
220:
221: if (ondblclick != null)
222: out.writeAttribute("ondblclick", ondblclick, "ondblclick");
223:
224: if (onkeydown != null)
225: out.writeAttribute("onkeydown", onkeydown, "onkeydown");
226:
227: if (onkeypress != null)
228: out.writeAttribute("onkeypress", onkeypress, "onkeypress");
229:
230: if (onkeyup != null)
231: out.writeAttribute("onkeyup", onkeyup, "onkeyup");
232:
233: if (onmousedown != null)
234: out.writeAttribute("onmousedown", onmousedown,
235: "onmousedown");
236:
237: if (onmousemove != null)
238: out.writeAttribute("onmousemove", onmousemove,
239: "onmousemove");
240:
241: if (onmouseout != null)
242: out.writeAttribute("onmouseout", onmouseout, "onmouseout");
243:
244: if (onmouseover != null)
245: out.writeAttribute("onmouseover", onmouseover,
246: "onmouseover");
247:
248: if (onmouseup != null)
249: out.writeAttribute("onmouseup", onmouseup, "onmouseup");
250:
251: if (onreset != null)
252: out.writeAttribute("onreset", onreset, "onreset");
253:
254: if (onsubmit != null)
255: out.writeAttribute("onsubmit", onsubmit, "onsubmit");
256:
257: if (style != null)
258: out.writeAttribute("style", style, "style");
259:
260: if (styleClass != null)
261: out.writeAttribute("class", styleClass, "class");
262:
263: if (target != null)
264: out.writeAttribute("target", target, "target");
265:
266: if (title != null)
267: out.writeAttribute("title", title, "title");
268:
269: out.write("\n");
270:
271: out.startElement("input", component);
272: out.writeAttribute("type", "hidden", "type");
273: out.writeAttribute("name", component.getClientId(context),
274: "name");
275: //out.writeAttribute("value", "true", "value");
276: out.endElement("input");
277: out.write("\n");
278: }
279:
280: /**
281: * Renders the closing tag for the component.
282: */
283: @Override
284: public void encodeEnd(FacesContext context, UIComponent component)
285: throws IOException {
286: ViewHandler handler = context.getApplication().getViewHandler();
287:
288: // jsf/1136 -- needed by facelets
289: handler.writeState(context);
290:
291: ResponseWriter out = context.getResponseWriter();
292:
293: Set<String> params = findCommandLinkParamSet(context, component
294: .getClientId(context), false);
295: if (params != null) {
296: for (String param : params) {
297: out.startElement("input", component);
298:
299: out.writeAttribute("name", param, "name");
300: out.writeAttribute("id", param, "id");
301: out.writeAttribute("type", "hidden", "type");
302:
303: out.endElement("input");
304: }
305: }
306:
307: out.endElement("form");
308: out.write("\n");
309: }
310:
311: static void addCommandLinkParam(FacesContext context,
312: String formClientId, String name) {
313: findCommandLinkParamSet(context, formClientId, true).add(name);
314: }
315:
316: private static Set<String> findCommandLinkParamSet(
317: FacesContext context, String formClientId, boolean create) {
318: final String setKey = "com.caucho.jsf.html.form."
319: + formClientId + ".commandLinkParams";
320: Map requestMap = context.getExternalContext().getRequestMap();
321: Set<String> params = (Set<String>) requestMap.get(setKey);
322: if (params == null && create) {
323: params = new HashSet<String>();
324: requestMap.put(setKey, params);
325: }
326: return params;
327: }
328: }
|