001: /*
002: * $Id: HtmlRenderer.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.faces.renderer;
023:
024: import java.io.IOException;
025: import java.util.Locale;
026: import javax.faces.component.UIComponent;
027: import javax.faces.context.FacesContext;
028: import javax.faces.context.ResponseWriter;
029: import javax.servlet.http.HttpSession;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.struts.Globals;
033:
034: /**
035: * <p><code>Renderer</code> implementation for the <code>html</code> tag
036: * from the <em>Struts-Faces Integration Library</em>.</p>
037: *
038: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
039: */
040:
041: public class HtmlRenderer extends AbstractRenderer {
042:
043: // -------------------------------------------------------- Static Variables
044:
045: /**
046: * <p>The <code>Log</code> instance for this class.</p>
047: */
048: private static Log log = LogFactory.getLog(HtmlRenderer.class);
049:
050: // ---------------------------------------------------------- Public Methods
051:
052: /**
053: * <p>Render the beginning <code>html</code> tag.</p>
054: *
055: * @param context FacesContext for the current request
056: * @param component UIComponent to be rendered
057: *
058: * @exception IOException if an input/output error occurs while rendering
059: * @exception NullPointerException if <code>context</code>
060: * or <code>component</code> is <code>null</code>
061: */
062: public void encodeBegin(FacesContext context, UIComponent component)
063: throws IOException {
064:
065: if ((context == null) || (component == null)) {
066: throw new NullPointerException();
067: }
068:
069: Locale currentLocale = getCurrentLocale(context, component);
070: String lang = currentLocale.getLanguage();
071: boolean validLanguage = ((lang != null) && (lang.length() > 0));
072:
073: ResponseWriter writer = context.getResponseWriter();
074: writer.startElement("html", component);
075: if (isXhtml(component)) {
076: // FIXME -- page scope attribute Globals.XHTML_KEY to "true"?
077: writer.writeAttribute("xmlns",
078: "http://www.w3.org/1999/xhtml", null);
079: }
080: if ((isLocale(component) || isXhtml(component))
081: && validLanguage) {
082: writer.writeAttribute("lang", lang, null);
083: }
084: if (isXhtml(component) && validLanguage) {
085: writer.writeAttribute("xml:lang", lang, null);
086: }
087: writer.writeText("\n", null);
088:
089: }
090:
091: /**
092: * <p>Render the end of the <code>html</code> element.</p>
093: *
094: * @param context FacesContext for the request we are processing
095: * @param component UIComponent to be rendered
096: *
097: * @exception IOException if an input/output error occurs while rendering
098: * @exception NullPointerException if <code>context</code>
099: * or <code>component</code> is null
100: */
101: public void encodeEnd(FacesContext context, UIComponent component)
102: throws IOException {
103:
104: if ((context == null) || (component == null)) {
105: throw new NullPointerException();
106: }
107:
108: ResponseWriter writer = context.getResponseWriter();
109: writer.endElement("html");
110:
111: }
112:
113: // ------------------------------------------------------ Protected Methods
114:
115: /**
116: * <p>Return the current <code>Locale</code> for this request, creating a
117: * new one if necessary.</p>
118: *
119: * @param context FacesContext for this request
120: * @param component UIComponent we are rendering
121: */
122: protected Locale getCurrentLocale(FacesContext context,
123: UIComponent component) {
124:
125: // If locale support not requested, just extract one from the request
126: if (!isLocale(component)) {
127: return (context.getExternalContext().getRequestLocale());
128: }
129:
130: // Create a new session if necessary
131: HttpSession session = (HttpSession) context
132: .getExternalContext().getSession(true);
133:
134: // Return current locale or a new one that is created
135: Locale current = (Locale) session
136: .getAttribute(Globals.LOCALE_KEY);
137: if (current != null) {
138: return (current);
139: }
140: current = context.getExternalContext().getRequestLocale();
141: session.setAttribute(Globals.LOCALE_KEY, current);
142: return (current);
143:
144: }
145:
146: /**
147: * <p>Return the state of the <code>locale</code> attribute.</p>
148: *
149: * @param component Component to process
150: */
151: protected boolean isLocale(UIComponent component) {
152:
153: Boolean locale = (Boolean) component.getAttributes().get(
154: "locale");
155: if (locale != null) {
156: return locale.booleanValue();
157: } else {
158: return (false);
159: }
160:
161: }
162:
163: /**
164: * <p>Return the state of the <code>xhtml</code> attribute.</p>
165: *
166: * @param component Component to process
167: */
168: protected boolean isXhtml(UIComponent component) {
169:
170: Boolean xhtml = (Boolean) component.getAttributes()
171: .get("xhtml");
172: if (xhtml != null) {
173: return xhtml.booleanValue();
174: } else {
175: return (false);
176: }
177:
178: }
179:
180: }
|