2
* Copyright 2002-2004 The Apache Software Foundation.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.struts.faces.renderer;
20
import java.io.IOException;
21
import java.util.Locale;
22
import javax.faces.component.UIComponent;
23
import javax.faces.context.FacesContext;
24
import javax.faces.context.ResponseWriter;
25
import javax.servlet.http.HttpSession;
26
import org.apache.commons.logging.Log;
27
import org.apache.commons.logging.LogFactory;
28
import org.apache.struts.Globals;
32
* <p><code>Renderer</code> implementation for the <code>html</code> tag
33
* from the <em>Struts-Faces Integration Library</em>.</p>
35
* @version $Rev: 54934 $ $Date: 2004-10-16 18:07:50 +0100 (Sat, 16 Oct 2004) $
38
public class HtmlRenderer extends AbstractRenderer {
41
// -------------------------------------------------------- Static Variables
45
* <p>The <code>Log</code> instance for this class.</p>
47
private static Log log = LogFactory.getLog(HtmlRenderer.class);
50
// ---------------------------------------------------------- Public Methods
54
* <p>Render the beginning <code>html</code> tag.</p>
56
* @param context FacesContext for the current request
57
* @param component UIComponent to be rendered
59
* @exception IOException if an input/output error occurs while rendering
60
* @exception NullPointerException if <code>context</code>
61
* or <code>component</code> is <code>null</code>
63
public void encodeBegin(FacesContext context, UIComponent component)
66
if ((context == null) || (component == null)) {
67
throw new NullPointerException();
70
Locale currentLocale = getCurrentLocale(context, component);
71
String lang = currentLocale.getLanguage();
72
boolean validLanguage = ((lang != null) && (lang.length() > 0));
74
ResponseWriter writer = context.getResponseWriter();
75
writer.startElement("html", component);
76
if (isXhtml(component)) {
77
// FIXME -- page scope attribute Globals.XHTML_KEY to "true"?
78
writer.writeAttribute("xmlns",
79
"http://www.w3.org/1999/xhtml", null);
81
if ((isLocale(component) || isXhtml(component)) && validLanguage) {
82
writer.writeAttribute("lang", lang, null);
84
if (isXhtml(component) && validLanguage) {
85
writer.writeAttribute("xml:lang", lang, null);
87
writer.writeText("\n", null);
93
* <p>Render the end of the <code>html</code> element.</p>
95
* @param context FacesContext for the request we are processing
96
* @param component UIComponent to be rendered
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
102
public void encodeEnd(FacesContext context, UIComponent component)
105
if ((context == null) || (component == null)) {
106
throw new NullPointerException();
109
ResponseWriter writer = context.getResponseWriter();
110
writer.endElement("html");
116
// ------------------------------------------------------ Protected Methods
120
* <p>Return the current <code>Locale</code> for this request, creating a
121
* new one if necessary.</p>
123
* @param context FacesContext for this request
124
* @param component UIComponent we are rendering
126
protected Locale getCurrentLocale
127
(FacesContext context, UIComponent component) {
129
// If locale support not requested, just extract one from the request
130
if (!isLocale(component)) {
131
return (context.getExternalContext().getRequestLocale());
134
// Create a new session if necessary
135
HttpSession session = (HttpSession)
136
context.getExternalContext().getSession(true);
138
// Return current locale or a new one that is created
139
Locale current = (Locale) session.getAttribute(Globals.LOCALE_KEY);
140
if (current != null) {
143
current = context.getExternalContext().getRequestLocale();
144
session.setAttribute(Globals.LOCALE_KEY, current);
152
* <p>Return the state of the <code>locale</code> attribute.</p>
154
* @param component Component to process
156
protected boolean isLocale(UIComponent component) {
158
Boolean locale = (Boolean) component.getAttributes().get("locale");
159
if (locale != null) {
160
return locale.booleanValue();
169
* <p>Return the state of the <code>xhtml</code> attribute.</p>
171
* @param component Component to process
173
protected boolean isXhtml(UIComponent component) {
175
Boolean xhtml = (Boolean) component.getAttributes().get("xhtml");
177
return xhtml.booleanValue();