KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > html > HtmlTag


1 /*
2  * $Id: HtmlTag.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.taglib.html;
20
21 import java.util.Locale JavaDoc;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpSession JavaDoc;
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
28
29 import org.apache.struts.Globals;
30 import org.apache.struts.taglib.TagUtils;
31 import org.apache.struts.util.MessageResources;
32
33 /**
34  * Renders an HTML <html> element with appropriate language attributes if
35  * there is a current Locale available in the user's session.
36  *
37  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
38  */

39 public class HtmlTag extends TagSupport JavaDoc {
40   
41
42     // ------------------------------------------------------------- Properties
43

44
45     /**
46      * The message resources for this package.
47      */

48     protected static MessageResources messages =
49      MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
50
51
52     /**
53      * Should we set the current Locale for this user if needed?
54      * @deprecated This will be removed after Struts 1.2.
55      */

56     protected boolean locale = false;
57
58     /**
59      * @deprecated This will be removed after Struts 1.2.
60      */

61     public boolean getLocale() {
62         return (locale);
63     }
64
65     /**
66      * @deprecated This will be removed after Struts 1.2.
67      */

68     public void setLocale(boolean locale) {
69         this.locale = locale;
70     }
71
72     /**
73      * Are we rendering an xhtml page?
74      */

75     protected boolean xhtml = false;
76     
77     /**
78      * Are we rendering a lang attribute?
79      * @since Struts 1.2
80      */

81     protected boolean lang = false;
82
83     public boolean getXhtml() {
84         return this.xhtml;
85     }
86
87     public void setXhtml(boolean xhtml) {
88         this.xhtml = xhtml;
89     }
90     
91     /**
92      * Returns true if the tag should render a lang attribute.
93      * @since Struts 1.2
94      */

95     public boolean getLang() {
96         return this.lang;
97     }
98
99     /**
100      * Sets whether the tag should render a lang attribute.
101      * @since Struts 1.2
102      */

103     public void setLang(boolean lang) {
104         this.lang = lang;
105     }
106
107     /**
108      * Process the start of this tag.
109      *
110      * @exception JspException if a JSP exception has occurred
111      */

112     public int doStartTag() throws JspException JavaDoc {
113
114         TagUtils.getInstance().write(this.pageContext, this.renderHtmlStartElement());
115
116         return EVAL_BODY_INCLUDE;
117     }
118
119     /**
120      * Renders an &lt;html&gt; element with appropriate language attributes.
121      * @since Struts 1.2
122      */

123     protected String JavaDoc renderHtmlStartElement() {
124         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("<html");
125
126         String JavaDoc language = null;
127         String JavaDoc country = "";
128                 
129         if (this.locale) {
130             // provided for 1.1 backward compatibility, remove after 1.2
131
language = this.getCurrentLocale().getLanguage();
132         } else {
133             Locale JavaDoc currentLocale =
134                 TagUtils.getInstance().getUserLocale(pageContext, Globals.LOCALE_KEY);
135
136             language = currentLocale.getLanguage();
137             country = currentLocale.getCountry();
138         }
139
140         boolean validLanguage = ((language != null) && (language.length() > 0));
141         boolean validCountry = country.length() > 0;
142
143         if (this.xhtml) {
144             this.pageContext.setAttribute(
145                 Globals.XHTML_KEY,
146                 "true",
147                 PageContext.PAGE_SCOPE);
148                 
149             sb.append(" xmlns=\"http://www.w3.org/1999/xhtml\"");
150         }
151
152         if ((this.lang || this.locale || this.xhtml) && validLanguage) {
153             sb.append(" lang=\"");
154             sb.append(language);
155             if (validCountry) {
156                 sb.append("-");
157                 sb.append(country);
158             }
159             sb.append("\"");
160         }
161
162         if (this.xhtml && validLanguage) {
163             sb.append(" xml:lang=\"");
164             sb.append(language);
165             if (validCountry) {
166                 sb.append("-");
167                 sb.append(country);
168             }
169             sb.append("\"");
170         }
171
172         sb.append(">");
173
174         return sb.toString();
175     }
176
177
178     /**
179      * Process the end of this tag.
180      *
181      * @exception JspException if a JSP exception has occurred
182      */

183     public int doEndTag() throws JspException JavaDoc {
184
185         TagUtils.getInstance().write(pageContext, "</html>");
186
187         // Evaluate the remainder of this page
188
return (EVAL_PAGE);
189
190     }
191
192     /**
193      * Release any acquired resources.
194      */

195     public void release() {
196         this.locale = false;
197         this.xhtml = false;
198         this.lang=false;
199     }
200
201
202     // ------------------------------------------------------ Protected Methods
203

204
205     /**
206      * Return the current Locale for this request. If there is no locale in the session and
207      * the locale attribute is set to "true", this method will create a Locale based on the
208      * client's Accept-Language header or the server's default locale and store it in the
209      * session. This will always return a Locale and never null.
210      * @since Struts 1.1
211      * @deprecated This will be removed after Struts 1.2.
212      */

213     protected Locale JavaDoc getCurrentLocale() {
214
215         Locale JavaDoc userLocale = TagUtils.getInstance().getUserLocale(pageContext, Globals.LOCALE_KEY);
216
217         // Store a new current Locale, if requested
218
if (this.locale) {
219             HttpSession JavaDoc session = ((HttpServletRequest JavaDoc) this.pageContext.getRequest()).getSession();
220             session.setAttribute(Globals.LOCALE_KEY, userLocale);
221         }
222
223         return userLocale;
224     }
225
226
227
228 }
229
Popular Tags