KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > LocaleTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib;
11
12 import org.mmbase.bridge.jsp.taglib.util.Attribute;
13 import org.mmbase.bridge.Cloud;
14
15 import java.io.IOException JavaDoc;
16 import javax.servlet.jsp.JspTagException JavaDoc;
17 import javax.servlet.jsp.PageContext JavaDoc;
18 import java.util.*;
19
20 import org.mmbase.util.logging.Logger;
21 import org.mmbase.util.logging.Logging;
22
23 /**
24  * Provides Locale (language, country) information to its body.
25  *
26  * @author Michiel Meeuwissen
27  * @version $Id: LocaleTag.java,v 1.28 2006/07/17 15:38:47 johannes Exp $
28  */

29
30 public class LocaleTag extends CloudReferrerTag {
31     private static final Logger log = Logging.getLoggerInstance(LocaleTag.class);
32
33     public static final String JavaDoc KEY = "javax.servlet.jsp.jstl.fmt.locale.request";
34     public static final String JavaDoc TZ_KEY = "org.mmbase.timezone";
35     public static final int SCOPE = PageContext.REQUEST_SCOPE;
36     private Attribute language = Attribute.NULL;
37     private Attribute country = Attribute.NULL;
38     private Attribute variant = Attribute.NULL;
39
40     private Attribute timezone = Attribute.NULL;
41
42     protected Locale locale;
43     protected Locale prevLocale = null;
44     protected Cloud cloud;
45     private String JavaDoc jspvar = null;
46
47     // ------------------------------------------------------------
48
// Attributes (documenation can be found in tld).
49

50     public void setLanguage(String JavaDoc lang) throws JspTagException JavaDoc {
51         language = getAttribute(lang);
52     }
53
54     public void setCountry(String JavaDoc c) throws JspTagException JavaDoc {
55         country = getAttribute(c);
56     }
57
58     public void setVariant(String JavaDoc v) throws JspTagException JavaDoc {
59         variant = getAttribute(v);
60     }
61     /**
62      * @since MMBase-1.8.1
63      */

64     public void setTimezone(String JavaDoc t) throws JspTagException JavaDoc {
65         timezone = getAttribute(t);
66     }
67
68     /**
69      * Child tags can call this function to obtain the Locale they must use.
70      */

71     public Locale getLocale() {
72 // if (locale == null) {
73
// locale = org.mmbase.bridge.ContextProvider.getDefaultCloudContext().getDefaultLocale();
74
// }
75
return locale;
76     }
77
78     public void setJspvar(String JavaDoc j) {
79         jspvar = j;
80     }
81
82
83     public int doStartTag() throws JspTagException JavaDoc {
84         determineLocale();
85         if (locale != null) {
86             if (jspvar != null) {
87                 pageContext.setAttribute(jspvar, locale);
88             }
89             // compatibility with jstl fmt tags:
90
// should use their constant, but that would make compile-time dependency.
91
prevLocale = (Locale) pageContext.findAttribute(KEY);
92             pageContext.setAttribute(KEY, locale, SCOPE);
93             CloudProvider cloudProvider = findCloudProvider(false);
94             if (cloudProvider != null) {
95                 cloud = cloudProvider.getCloudVar();
96                 prevLocale = cloud.getLocale();
97                 cloud.setLocale(locale);
98             } else {
99                 cloud = null;
100             }
101         }
102         String JavaDoc tz = timezone.getString(this);
103         if (timezone != null && ! tz.equals("")) {
104             pageContext.setAttribute(TZ_KEY, TimeZone.getTimeZone(tz), SCOPE);
105         } else {
106             if (pageContext.getAttribute(TZ_KEY, SCOPE) == null) {
107                 pageContext.setAttribute(TZ_KEY, org.mmbase.bridge.ContextProvider.getDefaultCloudContext().getDefaultTimeZone(), SCOPE);
108             }
109         }
110         return EVAL_BODY;
111     }
112
113     /**
114      * @throws JspTagException
115      */

116     protected void determineLocale() throws JspTagException JavaDoc {
117         determineLocaleFromAttributes();
118         if (locale == null) {
119             determineFromCloudProvider();
120         }
121         if (locale == null) {
122             locale = org.mmbase.bridge.ContextProvider.getDefaultCloudContext().getDefaultLocale();
123         }
124     }
125
126     /**
127      * @throws JspTagException
128      */

129     protected void determineFromCloudProvider() throws JspTagException JavaDoc {
130         CloudProvider cloudProvider = findCloudProvider(false);
131         if (cloudProvider != null) {
132             locale = cloudProvider.getCloudVar().getLocale();
133         }
134     }
135
136     /**
137      * @throws JspTagException
138      */

139     protected void determineLocaleFromAttributes() throws JspTagException JavaDoc {
140         String JavaDoc l = language.getString(this);
141         if (! l.equals("")) {
142             if (l.equalsIgnoreCase("client")) {
143                 locale = pageContext.getRequest().getLocale();
144             } else {
145                 String JavaDoc c = country.getString(this);
146                 if ("".equals(c)) {
147                     locale = org.mmbase.util.LocalizedString.getLocale(l);
148                 } else {
149                     locale = new Locale(l, c, variant.getString(this));
150                 }
151             }
152         }
153     }
154
155     public int doAfterBody() throws JspTagException JavaDoc {
156         if (EVAL_BODY == EVAL_BODY_BUFFERED) {
157             if (bodyContent != null) {
158                 try {
159                     bodyContent.writeOut(bodyContent.getEnclosingWriter());
160                 } catch (IOException JavaDoc ioe){
161                     throw new TaglibException(ioe);
162                 }
163             }
164         }
165         return SKIP_BODY;
166     }
167
168     public int doEndTag() throws JspTagException JavaDoc {
169         if (prevLocale != null) {
170             pageContext.setAttribute(KEY, prevLocale, SCOPE);
171             if (cloud != null) {
172                 cloud.setLocale(prevLocale);
173             }
174         } else {
175             pageContext.removeAttribute(KEY, SCOPE);
176         }
177         cloud = null;
178         return super.doEndTag();
179     }
180
181     public void doFinally() {
182         cloud = null;
183         locale = null;
184         prevLocale = null;
185         jspvar = null;
186         super.doFinally();
187     }
188 }
189
190
Popular Tags