1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts.faces.renderer;
23
24
25 import java.io.IOException;
26 import java.util.Locale;
27 import javax.faces.component.UIComponent;
28 import javax.faces.context.FacesContext;
29 import javax.faces.context.ResponseWriter;
30 import javax.servlet.http.HttpSession;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.struts.Globals;
34
35
36
37
38
39
40
41
42
43 public class HtmlRenderer extends AbstractRenderer {
44
45
46
47
48
49
50
51
52 private static Log log = LogFactory.getLog(HtmlRenderer.class);
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public void encodeBegin(FacesContext context, UIComponent component)
69 throws IOException {
70
71 if ((context == null) || (component == null)) {
72 throw new NullPointerException();
73 }
74
75 Locale currentLocale = getCurrentLocale(context, component);
76 String lang = currentLocale.getLanguage();
77 boolean validLanguage = ((lang != null) && (lang.length() > 0));
78
79 ResponseWriter writer = context.getResponseWriter();
80 writer.startElement("html", component);
81 if (isXhtml(component)) {
82
83 writer.writeAttribute("xmlns",
84 "http://www.w3.org/1999/xhtml", null);
85 }
86 if ((isLocale(component) || isXhtml(component)) && validLanguage) {
87 writer.writeAttribute("lang", lang, null);
88 }
89 if (isXhtml(component) && validLanguage) {
90 writer.writeAttribute("xml:lang", lang, null);
91 }
92 writer.writeText("\n", null);
93
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107 public void encodeEnd(FacesContext context, UIComponent component)
108 throws IOException {
109
110 if ((context == null) || (component == null)) {
111 throw new NullPointerException();
112 }
113
114 ResponseWriter writer = context.getResponseWriter();
115 writer.endElement("html");
116
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131 protected Locale getCurrentLocale
132 (FacesContext context, UIComponent component) {
133
134
135 if (!isLocale(component)) {
136 return (context.getExternalContext().getRequestLocale());
137 }
138
139
140 HttpSession session = (HttpSession)
141 context.getExternalContext().getSession(true);
142
143
144 Locale current = (Locale) session.getAttribute(Globals.LOCALE_KEY);
145 if (current != null) {
146 return (current);
147 }
148 current = context.getExternalContext().getRequestLocale();
149 session.setAttribute(Globals.LOCALE_KEY, current);
150 return (current);
151
152 }
153
154
155
156
157
158
159
160
161 protected boolean isLocale(UIComponent component) {
162
163 Boolean locale = (Boolean) component.getAttributes().get("locale");
164 if (locale != null) {
165 return locale.booleanValue();
166 } else {
167 return (false);
168 }
169
170 }
171
172
173
174
175
176
177
178 protected boolean isXhtml(UIComponent component) {
179
180 Boolean xhtml = (Boolean) component.getAttributes().get("xhtml");
181 if (xhtml != null) {
182 return xhtml.booleanValue();
183 } else {
184 return (false);
185 }
186
187 }
188
189
190 }