1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.taglib.html;
19
20 import org.apache.struts.Globals;
21 import org.apache.struts.taglib.TagUtils;
22 import org.apache.struts.util.MessageResources;
23
24 import javax.servlet.jsp.JspException;
25 import javax.servlet.jsp.PageContext;
26 import javax.servlet.jsp.tagext.TagSupport;
27
28 import java.util.Locale;
29
30 /***
31 * Renders an HTML <html> element with appropriate language attributes if
32 * there is a current Locale available in the user's session.
33 *
34 * @version $Rev: 376841 $ $Date: 2005-08-21 19:08:45 -0400 (Sun, 21 Aug 2005)
35 * $
36 */
37 public class HtmlTag extends TagSupport {
38
39
40 /***
41 * The message resources for this package.
42 */
43 protected static MessageResources messages =
44 MessageResources.getMessageResources(Constants.Package
45 + ".LocalStrings");
46
47 /***
48 * Are we rendering an xhtml page?
49 */
50 protected boolean xhtml = false;
51
52 /***
53 * Are we rendering a lang attribute?
54 *
55 * @since Struts 1.2
56 */
57 protected boolean lang = false;
58
59 public boolean getXhtml() {
60 return this.xhtml;
61 }
62
63 public void setXhtml(boolean xhtml) {
64 this.xhtml = xhtml;
65 }
66
67 /***
68 * Returns true if the tag should render a lang attribute.
69 *
70 * @since Struts 1.2
71 */
72 public boolean getLang() {
73 return this.lang;
74 }
75
76 /***
77 * Sets whether the tag should render a lang attribute.
78 *
79 * @since Struts 1.2
80 */
81 public void setLang(boolean lang) {
82 this.lang = lang;
83 }
84
85 /***
86 * Process the start of this tag.
87 *
88 * @throws JspException if a JSP exception has occurred
89 */
90 public int doStartTag() throws JspException {
91 TagUtils.getInstance().write(this.pageContext,
92 this.renderHtmlStartElement());
93
94 return EVAL_BODY_INCLUDE;
95 }
96
97 /***
98 * Renders an <html> element with appropriate language attributes.
99 *
100 * @since Struts 1.2
101 */
102 protected String renderHtmlStartElement() {
103 StringBuffer sb = new StringBuffer("<html");
104
105 String language = null;
106 String country = "";
107
108 Locale currentLocale =
109 TagUtils.getInstance().getUserLocale(pageContext, Globals.LOCALE_KEY);
110
111 language = currentLocale.getLanguage();
112 country = currentLocale.getCountry();
113
114 boolean validLanguage = ((language != null) && (language.length() > 0));
115 boolean validCountry = country.length() > 0;
116
117 if (this.xhtml) {
118 this.pageContext.setAttribute(Globals.XHTML_KEY, "true",
119 PageContext.PAGE_SCOPE);
120
121 sb.append(" xmlns=\"http://www.w3.org/1999/xhtml\"");
122 }
123
124 if ((this.lang || this.xhtml) && validLanguage) {
125 sb.append(" lang=\"");
126 sb.append(language);
127
128 if (validCountry) {
129 sb.append("-");
130 sb.append(country);
131 }
132
133 sb.append("\"");
134 }
135
136 if (this.xhtml && validLanguage) {
137 sb.append(" xml:lang=\"");
138 sb.append(language);
139
140 if (validCountry) {
141 sb.append("-");
142 sb.append(country);
143 }
144
145 sb.append("\"");
146 }
147
148 sb.append(">");
149
150 return sb.toString();
151 }
152
153 /***
154 * Process the end of this tag.
155 *
156 * @throws JspException if a JSP exception has occurred
157 */
158 public int doEndTag() throws JspException {
159 TagUtils.getInstance().write(pageContext, "</html>");
160
161
162 return (EVAL_PAGE);
163 }
164
165 /***
166 * Release any acquired resources.
167 */
168 public void release() {
169 this.xhtml = false;
170 this.lang = false;
171 }
172 }