KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > faces > renderer > FormRenderer


1 /*
2  * Copyright 2002-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
17 package org.apache.struts.faces.renderer;
18
19
20 import java.io.IOException JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.faces.component.NamingContainer;
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26 import javax.faces.context.ResponseWriter;
27 import javax.servlet.http.HttpSession JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.struts.Globals;
32 import org.apache.struts.config.ActionConfig;
33 import org.apache.struts.config.ModuleConfig;
34 import org.apache.struts.faces.component.FormComponent;
35
36
37 /**
38  * <p><code>Renderer</code> implementation for the <code>form</code> tag
39  * from the <em>Struts-Faces Integration Library</em>.</p>
40  *
41  * @version $Rev: 55303 $ $Date: 2004-10-22 03:56:53 +0100 (Fri, 22 Oct 2004) $
42  */

43
44 public class FormRenderer extends AbstractRenderer {
45
46
47     // -------------------------------------------------------- Static Variables
48

49
50     /**
51      * <p>The <code>Log</code> instance for this class.</p>
52      */

53     private static Log log = LogFactory.getLog(FormRenderer.class);
54
55
56     // ---------------------------------------------------------- Public Methods
57

58
59     /**
60      * <p>Perform setup processing that will be required for decoding the
61      * incoming request.</p>
62      *
63      * @param context FacesContext for the request we are processing
64      * @param component UIComponent to be processed
65      *
66      * @exception NullPointerException if <code>context</code>
67      * or <code>component</code> is null
68      */

69     public void decode(FacesContext context, UIComponent component) {
70
71         if ((context == null) || (component == null)) {
72             throw new NullPointerException JavaDoc();
73         }
74         String JavaDoc clientId = component.getClientId(context);
75     Map JavaDoc map = context.getExternalContext().getRequestParameterMap();
76         if (log.isDebugEnabled()) {
77             log.debug("decode(" + clientId + ") --> " +
78                       map.containsKey(clientId));
79         }
80         component.getAttributes().put
81             ("submitted",
82              map.containsKey(clientId) ? Boolean.TRUE : Boolean.FALSE);
83
84     }
85
86
87     private static String JavaDoc passThrough[] =
88     { "enctype", "method", "onreset", "onsubmit", "style", "target", };
89
90
91     /**
92      * <p>Render the beginning of an HTML <code>&lt;form&gt;</code>
93      * control.</p>
94      *
95      * @param context FacesContext for the request we are processing
96      * @param component UIComponent to be rendered
97      *
98      * @exception IOException if an input/output error occurs while rendering
99      * @exception NullPointerException if <code>context</code>
100      * or <code>component</code> is null
101      */

102     public void encodeBegin(FacesContext context, UIComponent component)
103         throws IOException JavaDoc {
104
105         if ((context == null) || (component == null)) {
106             throw new NullPointerException JavaDoc();
107         }
108
109         // Calculate and cache the form name
110
FormComponent form = (FormComponent) component;
111         String JavaDoc action = form.getAction();
112         ModuleConfig moduleConfig = form.lookupModuleConfig(context);
113         ActionConfig actionConfig = moduleConfig.findActionConfig(action);
114         String JavaDoc beanName = actionConfig.getAttribute();
115         if (beanName != null) {
116             form.getAttributes().put("beanName", beanName);
117         }
118
119         // Look up attribute values we need
120
String JavaDoc clientId = component.getClientId(context);
121         if (log.isDebugEnabled()) {
122             log.debug("encodeBegin(" + clientId + ")");
123         }
124         String JavaDoc styleClass =
125             (String JavaDoc) component.getAttributes().get("styleClass");
126
127         // Render the beginning of this form
128
ResponseWriter writer = context.getResponseWriter();
129         writer.startElement("form", form);
130         writer.writeAttribute("id", clientId, "clientId");
131         if (beanName != null) {
132             writer.writeAttribute("name", beanName, null);
133         }
134         writer.writeAttribute("action", action(context, component), "action");
135         if (styleClass != null) {
136             writer.writeAttribute("class", styleClass, "styleClass");
137         }
138         if (component.getAttributes().get("method") == null) {
139             writer.writeAttribute("method", "post", null);
140         }
141         renderPassThrough(context, component, writer, passThrough);
142         writer.writeText("\n", null);
143
144         // Add a marker used by our decode() method to note this form is submitted
145
writer.startElement("input", form);
146         writer.writeAttribute("type", "hidden", null);
147         writer.writeAttribute("name", clientId, null);
148         writer.writeAttribute("value", clientId, null);
149         writer.endElement("input");
150         writer.writeText("\n", null);
151
152         // Add a transaction token if necessary
153
HttpSession JavaDoc session = (HttpSession JavaDoc)
154             context.getExternalContext().getSession(false);
155         if (session != null) {
156             String JavaDoc token = (String JavaDoc)
157                 session.getAttribute(Globals.TRANSACTION_TOKEN_KEY);
158             if (token != null) {
159                 writer.startElement("input", form);
160                 writer.writeAttribute("type", "hidden", null);
161                 writer.writeAttribute
162                     ("name", "org.apache.struts.taglib.html.TOKEN", null);
163                 writer.writeAttribute("value", token, null);
164                 writer.endElement("input");
165                 writer.writeText("\n", null);
166             }
167         }
168
169         // Create an instance of the form bean if necessary
170
if (component instanceof FormComponent) {
171             ((FormComponent) component).createActionForm(context);
172         }
173
174     }
175
176
177     /**
178      * <p>Render the ending of an HTML <code>&lt;form&gt;</code>
179      * control.</p>
180      *
181      * @param context FacesContext for the request we are processing
182      * @param component UIComponent to be rendered
183      *
184      * @exception IOException if an input/output error occurs while rendering
185      * @exception NullPointerException if <code>context</code>
186      * or <code>component</code> is null
187      */

188     public void encodeEnd(FacesContext context, UIComponent component)
189         throws IOException JavaDoc {
190
191         if ((context == null) || (component == null)) {
192             throw new NullPointerException JavaDoc();
193         }
194         String JavaDoc clientId = component.getClientId(context);
195         if (log.isDebugEnabled()) {
196             log.debug("encodeEnd(" + clientId + ")");
197         }
198         ResponseWriter writer = context.getResponseWriter();
199
200         // Render the hidden variable our decode() method uses to detect submits
201
writer.startElement("input", component);
202     writer.writeAttribute("type", "hidden", null);
203     writer.writeAttribute("name", component.getClientId(context), null);
204     writer.writeAttribute("value", component.getClientId(context), null);
205     writer.endElement("input");
206     writer.write("\n");
207
208         // Write our state information (if necessary)
209
context.getApplication().getViewHandler().writeState(context);
210
211         // Render the ending of this form
212
writer.endElement("form");
213         writer.writeText("\n", null);
214
215         // Render focus JavaScript if requested
216
if (!(component instanceof FormComponent)) {
217             return;
218         }
219         String JavaDoc focus = (String JavaDoc) component.getAttributes().get("focus");
220         if (focus == null) {
221             return;
222         }
223         String JavaDoc focusIndex =
224             (String JavaDoc) component.getAttributes().get("focusIndex");
225         writer.writeText("\n", null);
226         FormComponent form = (FormComponent) component;
227         writer.startElement("script", form);
228         writer.writeAttribute("type", "text/javascript", null);
229         if (!isXhtml(component)) {
230             writer.writeAttribute("language", "JavaScript", null);
231         }
232         writer.writeText("\n", null);
233         if (!isXhtml(component)) {
234             writer.write("<!--\n");
235         }
236
237         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("document.forms[\"");
238         sb.append(clientId);
239         sb.append("\"].elements[\"");
240         sb.append(component.getClientId(context));
241         sb.append(NamingContainer.SEPARATOR_CHAR);
242         sb.append(focus);
243         sb.append("\"]");
244         String JavaDoc focusControl = sb.toString();
245
246         writer.write(" var focusControl = ");
247         writer.write(focusControl);
248         writer.write(";\n");
249         writer.write(" if (focusControl.type != \"hidden\") {\n");
250         writer.write(" ");
251         writer.write(focusControl);
252         if (focusIndex != null) {
253             writer.write("[");
254             writer.write(focusIndex);
255             writer.write("]");
256         }
257         writer.write(".focus();\n");
258         writer.write(" }\n");
259
260         if (!isXhtml(component)) {
261             writer.write("// -->\n");
262         }
263         writer.endElement("script");
264         writer.writeText("\n", null);
265
266     }
267
268
269     // ------------------------------------------------------- Protected Methods
270

271
272     /**
273      * <p>Calculate and return the value to be specifed for the
274      * <code>action</action> attribute on the <code>&lt;form&gt;</code>
275      * element to be rendered.</p>
276      *
277      * @param context FacesContext for the current request
278      * @param component Component being processed
279      */

280     protected String JavaDoc action(FacesContext context, UIComponent component) {
281
282         String JavaDoc actionURL =
283             context.getApplication().getViewHandler().
284             getActionURL(context, context.getViewRoot().getViewId());
285         if (log.isTraceEnabled()) {
286             log.trace("getActionURL(" + context.getViewRoot().getViewId() +
287                       ") --> " + actionURL);
288         }
289         return (context.getExternalContext().encodeActionURL(actionURL));
290
291     }
292
293
294     /**
295      * <p>Return <code>true</code> if we should render as XHTML.</p>
296      *
297      * @param component The component we are rendering
298      */

299     protected boolean isXhtml(UIComponent component) {
300
301         return (false); // FIXME -- check up the hierarchy
302

303     }
304
305
306 }
307
Popular Tags