1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.taglib.html;
22
23 import org.apache.struts.Globals;
24 import org.apache.struts.taglib.TagUtils;
25 import org.apache.struts.util.MessageResources;
26
27 import javax.servlet.jsp.JspException;
28 import javax.servlet.jsp.PageContext;
29 import javax.servlet.jsp.tagext.TagSupport;
30
31 import java.util.Locale;
32
33
34
35
36
37
38
39
40 public class HtmlTag extends TagSupport {
41
42
43
44
45
46 protected static MessageResources messages =
47 MessageResources.getMessageResources(Constants.Package
48 + ".LocalStrings");
49
50
51
52
53 protected boolean xhtml = false;
54
55
56
57
58
59
60 protected boolean lang = false;
61
62 public boolean getXhtml() {
63 return this.xhtml;
64 }
65
66 public void setXhtml(boolean xhtml) {
67 this.xhtml = xhtml;
68 }
69
70
71
72
73
74
75 public boolean getLang() {
76 return this.lang;
77 }
78
79
80
81
82
83
84 public void setLang(boolean lang) {
85 this.lang = lang;
86 }
87
88
89
90
91
92
93 public int doStartTag() throws JspException {
94 TagUtils.getInstance().write(this.pageContext,
95 this.renderHtmlStartElement());
96
97 return EVAL_BODY_INCLUDE;
98 }
99
100
101
102
103
104
105 protected String renderHtmlStartElement() {
106 StringBuffer sb = new StringBuffer("<html");
107
108 String language = null;
109 String country = "";
110
111 Locale currentLocale =
112 TagUtils.getInstance().getUserLocale(pageContext, Globals.LOCALE_KEY);
113
114 language = currentLocale.getLanguage();
115 country = currentLocale.getCountry();
116
117 boolean validLanguage = isValidRfc2616(language);
118 boolean validCountry = isValidRfc2616(country);
119
120 if (this.xhtml) {
121 this.pageContext.setAttribute(Globals.XHTML_KEY, "true",
122 PageContext.PAGE_SCOPE);
123
124 sb.append(" xmlns=\"http://www.w3.org/1999/xhtml\"");
125 }
126
127 if ((this.lang || this.xhtml) && validLanguage) {
128 sb.append(" lang=\"");
129 sb.append(language);
130
131 if (validCountry) {
132 sb.append("-");
133 sb.append(country);
134 }
135
136 sb.append("\"");
137 }
138
139 if (this.xhtml && validLanguage) {
140 sb.append(" xml:lang=\"");
141 sb.append(language);
142
143 if (validCountry) {
144 sb.append("-");
145 sb.append(country);
146 }
147
148 sb.append("\"");
149 }
150
151 sb.append(">");
152
153 return sb.toString();
154 }
155
156
157
158
159
160
161 public int doEndTag() throws JspException {
162 TagUtils.getInstance().write(pageContext, "</html>");
163
164
165 return (EVAL_PAGE);
166 }
167
168
169
170
171 public void release() {
172 this.xhtml = false;
173 this.lang = false;
174 }
175
176
177
178
179
180
181
182
183 private boolean isValidRfc2616(String value) {
184 if (value == null || value.length() == 0) {
185 return false;
186 }
187 for (int i = 0; i < value.length(); i++) {
188 char c = value.charAt(i);
189
190 if (!(Character.isLetter(c) || c == '-')) {
191 return false;
192 }
193 }
194 return true;
195 }
196 }