View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts.faces.renderer;
23  
24  
25  import java.io.IOException;
26  import java.util.Locale;
27  
28  import org.apache.struts.Globals;
29  import org.apache.struts.faces.component.HtmlComponent;
30  
31  import jakarta.faces.component.UIComponent;
32  import jakarta.faces.context.FacesContext;
33  import jakarta.faces.context.ResponseWriter;
34  import jakarta.servlet.http.HttpSession;
35  
36  
37  /**
38   * <p><code>Renderer</code> implementation for the <code>html</code> tag
39   * from the <em>Struts-Faces Integration Library</em>.</p>
40   *
41   * @version $Rev$ $Date$
42   */
43  
44  public class HtmlRenderer extends AbstractRenderer {
45  
46  
47      // -------------------------------------------------------- Static Variables
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          final HtmlComponent htmlComponent = (HtmlComponent) component;
71  
72          Locale currentLocale = getCurrentLocale(context, component);
73          String lang = currentLocale.getLanguage();
74          boolean validLanguage = ((lang != null) && (lang.length() > 0));
75  
76          ResponseWriter writer = context.getResponseWriter();
77          writer.startElement("html", component);
78          if (isXhtml(component)) {
79              // FIXME -- page scope attribute Globals.XHTML_KEY to "true"?
80              writer.writeAttribute("xmlns",
81                                    "http://www.w3.org/1999/xhtml", null);
82          }
83          if ((htmlComponent.isLocale() || htmlComponent.isXhtml()) && validLanguage) {
84              writer.writeAttribute("lang", lang, null);
85          }
86          if (htmlComponent.isXhtml() && validLanguage) {
87              writer.writeAttribute("xml:lang", lang, null);
88          }
89          writer.writeText("\n", null);
90  
91      }
92  
93  
94      /**
95       * <p>Render the end of the <code>html</code> element.</p>
96       *
97       * @param context FacesContext for the request we are processing
98       * @param component UIComponent to be rendered
99       *
100      * @exception IOException if an input/output error occurs while rendering
101      * @exception NullPointerException if <code>context</code>
102      *  or <code>component</code> is null
103      */
104     public void encodeEnd(FacesContext context, UIComponent component)
105         throws IOException {
106 
107         if ((context == null) || (component == null)) {
108             throw new NullPointerException();
109         }
110 
111         ResponseWriter writer = context.getResponseWriter();
112         writer.endElement("html");
113 
114     }
115 
116 
117 
118     // ------------------------------------------------------ Protected Methods
119 
120 
121     /**
122      * <p>Return the current <code>Locale</code> for this request, creating a
123      * new one if necessary.</p>
124      *
125      * @param context FacesContext for this request
126      * @param component UIComponent we are rendering
127      */
128     protected Locale getCurrentLocale
129         (FacesContext context, UIComponent component) {
130 
131         final HtmlComponent htmlComponent = (HtmlComponent) component;
132 
133         // If locale support not requested, just extract one from the request
134         if (!htmlComponent.isLocale()) {
135             return context.getExternalContext().getRequestLocale();
136         }
137 
138         // Create a new session if necessary
139         HttpSession session = (HttpSession)
140             context.getExternalContext().getSession(true);
141 
142         // Return current locale or a new one that is created
143         Locale current = (Locale) session.getAttribute(Globals.LOCALE_KEY);
144         if (current != null) {
145             return (current);
146         }
147         current = context.getExternalContext().getRequestLocale();
148         session.setAttribute(Globals.LOCALE_KEY, current);
149         return (current);
150 
151     }
152 
153 
154 }