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
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
39
40
41
42
43
44 public class HtmlRenderer extends AbstractRenderer {
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
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
96
97
98
99
100
101
102
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
119
120
121
122
123
124
125
126
127
128 protected Locale getCurrentLocale
129 (FacesContext context, UIComponent component) {
130
131 final HtmlComponent htmlComponent = (HtmlComponent) component;
132
133
134 if (!htmlComponent.isLocale()) {
135 return context.getExternalContext().getRequestLocale();
136 }
137
138
139 HttpSession session = (HttpSession)
140 context.getExternalContext().getSession(true);
141
142
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 }