KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > i18n > LocaleTag


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

16
17 package org.apache.taglibs.i18n;
18
19 import java.util.Enumeration JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import javax.servlet.ServletRequest JavaDoc;
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
25 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
26
27
28 /** This tag defines a {@link java.util.Locale} context for use by other
29   * inner JSP tags.
30   *
31   * If no locale has been configured directly via the "locale" property
32   * then the language, country and optional varient properties are used
33   * to create a new Locale instance.
34   * If these properties are not specified then the Locale is taken from
35   * {@link javax.servlet.ServletRequest} is used.
36   * If still no {@link java.util.Locale} could be found then the default JVM
37   * {@link java.util.Locale} is used.
38   *
39   * @author James Strachan
40   * @version $Revision: 1.7 $
41   */

42 public class LocaleTag extends TagSupport JavaDoc
43 {
44     /** Holds value of property locale. */
45     private Locale JavaDoc locale;
46     /** Holds value of property localeRef. */
47     private String JavaDoc localeRef;
48     /** Holds value of property language. */
49     private String JavaDoc language;
50     /** Holds value of property country. */
51     private String JavaDoc country;
52     /** Holds value of property variant. */
53     private String JavaDoc variant;
54     /** specifies whether or not the response locale should be changed to match
55      * the locale used by this tag */

56     private boolean changeResponseLocale = true;
57
58
59     // Tag interface
60
//-------------------------------------------------------------------------
61
public int doStartTag() throws JspException JavaDoc
62     {
63         // set the locale as a variable in the page
64
if ( this.getId() != null ) {
65             pageContext.setAttribute(getId(),getLocale());
66         }
67     
68     return EVAL_BODY_INCLUDE;
69     }
70
71     /**
72      * Sets the response locale if the changeResponseLocale attribute was set
73      * to true, OR if changeResponseLocale was unset and the tag was empty
74      */

75     public int doEndTag() throws JspException JavaDoc
76     {
77         if (this.changeResponseLocale) {
78             // set the locale for the response
79
pageContext.getResponse().setLocale(getLocale());
80         }
81         
82     return EVAL_PAGE;
83     }
84
85     public void setChangeResponseLocale(boolean value)
86     {
87         changeResponseLocale = value;
88     }
89
90     public void release()
91     {
92         super.release();
93         locale = null;
94         language = null;
95         country = null;
96         variant = null;
97         changeResponseLocale = true;
98     }
99
100     // Properties
101
//-------------------------------------------------------------------------
102
protected final Locale JavaDoc getLocale()
103     {
104         if (localeRef != null) {
105             locale = (Locale JavaDoc)pageContext.findAttribute(localeRef);
106         }
107         if (locale == null) {
108             locale = createLocale();
109         }
110         return locale;
111     }
112
113     public final void setLocale(Locale JavaDoc locale)
114     {
115         this.locale = locale;
116     }
117
118     /**
119      * Provides a key to retrieve a locale via findAttribute()
120      */

121     public final void setLocaleRef(String JavaDoc value)
122     {
123         this.localeRef = value;
124     }
125
126     public final void setLanguage(String JavaDoc language)
127     {
128         this.language = language;
129     }
130
131     public final void setCountry(String JavaDoc country)
132     {
133         this.country = country;
134     }
135
136     public final void setVariant(String JavaDoc variant)
137     {
138         this.variant = variant;
139     }
140
141     // Implementation methods
142
//-------------------------------------------------------------------------
143
private Locale JavaDoc createLocale()
144     {
145         // let's try use the language & country properties first...
146
Locale JavaDoc locale = null;
147         if (language == null) {
148             locale = pageContext.getResponse().getLocale();
149         } else if (country == null) {
150             locale = new Locale JavaDoc(language,"");
151         } else if (variant == null) {
152             locale = new Locale JavaDoc(language, country);
153         } else {
154             locale = new Locale JavaDoc(language, country, variant);
155         }
156
157         return locale;
158     }
159
160 }
161
Popular Tags