View Javadoc

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;
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;
29  
30  
31  /***
32   * <p><code>Renderer</code> implementation for the <code>html</code> tag
33   * from the <em>Struts-Faces Integration Library</em>.</p>
34   *
35   * @version $Rev: 421138 $ $Date: 2006-07-11 22:41:40 -0700 (Tue, 11 Jul 2006) $
36   */
37  
38  public class HtmlRenderer extends AbstractRenderer {
39  
40  
41      // -------------------------------------------------------- Static Variables
42  
43  
44      /***
45       * <p>The <code>Log</code> instance for this class.</p>
46       */
47      private static Log log = LogFactory.getLog(HtmlRenderer.class);
48  
49  
50      // ---------------------------------------------------------- Public Methods
51  
52  
53      /***
54       * <p>Render the beginning <code>html</code> tag.</p>
55       *
56       * @param context FacesContext for the current request
57       * @param component UIComponent to be rendered
58       *
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>
62       */
63      public void encodeBegin(FacesContext context, UIComponent component)
64          throws IOException {
65  
66          if ((context == null) || (component == null)) {
67              throw new NullPointerException();
68          }
69  
70          Locale currentLocale = getCurrentLocale(context, component);
71          String lang = currentLocale.getLanguage();
72          boolean validLanguage = ((lang != null) && (lang.length() > 0));
73  
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);
80          }
81          if ((isLocale(component) || isXhtml(component)) && validLanguage) {
82              writer.writeAttribute("lang", lang, null);
83          }
84          if (isXhtml(component) && validLanguage) {
85              writer.writeAttribute("xml:lang", lang, null);
86          }
87          writer.writeText("\n", null);
88  
89      }
90  
91  
92      /***
93       * <p>Render the end of the <code>html</code> element.</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 encodeEnd(FacesContext context, UIComponent component)
103         throws IOException {
104 
105         if ((context == null) || (component == null)) {
106             throw new NullPointerException();
107         }
108 
109         ResponseWriter writer = context.getResponseWriter();
110         writer.endElement("html");
111 
112     }
113 
114 
115 
116     // ------------------------------------------------------ Protected Methods
117 
118 
119     /***
120      * <p>Return the current <code>Locale</code> for this request, creating a
121      * new one if necessary.</p>
122      *
123      * @param context FacesContext for this request
124      * @param component UIComponent we are rendering
125      */
126     protected Locale getCurrentLocale
127         (FacesContext context, UIComponent component) {
128 
129         // If locale support not requested, just extract one from the request
130         if (!isLocale(component)) {
131             return (context.getExternalContext().getRequestLocale());
132         }
133 
134         // Create a new session if necessary
135         HttpSession session = (HttpSession)
136             context.getExternalContext().getSession(true);
137 
138         // Return current locale or a new one that is created
139         Locale current = (Locale) session.getAttribute(Globals.LOCALE_KEY);
140         if (current != null) {
141             return (current);
142         }
143         current = context.getExternalContext().getRequestLocale();
144         session.setAttribute(Globals.LOCALE_KEY, current);
145         return (current);
146 
147     }
148 
149 
150 
151     /***
152      * <p>Return the state of the <code>locale</code> attribute.</p>
153      *
154      * @param component Component to process
155      */
156     protected boolean isLocale(UIComponent component) {
157 
158         Boolean locale = (Boolean) component.getAttributes().get("locale");
159         if (locale != null) {
160             return locale.booleanValue();
161         } else {
162             return (false);
163         }
164 
165     }
166 
167 
168     /***
169      * <p>Return the state of the <code>xhtml</code> attribute.</p>
170      *
171      * @param component Component to process
172      */
173     protected boolean isXhtml(UIComponent component) {
174 
175         Boolean xhtml = (Boolean) component.getAttributes().get("xhtml");
176         if (xhtml != null) {
177             return xhtml.booleanValue();
178         } else {
179             return (false);
180         }
181 
182     }
183 
184 
185 }