KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > deprecated > taglibs > resourcebundle > ResourceBundleTag


1 package org.jahia.deprecated.taglibs.resourcebundle;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Locale JavaDoc;
5 import java.util.MissingResourceException JavaDoc;
6 import java.util.ResourceBundle JavaDoc;
7
8 import javax.servlet.http.HttpServletRequest JavaDoc;
9 import javax.servlet.jsp.JspException JavaDoc;
10 import javax.servlet.jsp.JspWriter JavaDoc;
11 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
12
13 import org.jahia.data.JahiaData;
14
15
16 /**
17  * Support for ResourceBundle within Jahia
18  * If locale is not set, the locale used is the one returned by paramBean.getLocale()
19  *
20  * @author Khue Nguyen
21  */

22 public class ResourceBundleTag extends TagSupport JavaDoc {
23
24     private static org.apache.log4j.Logger logger =
25             org.apache.log4j.Logger.getLogger(ResourceBundleTag.class);
26
27     private String JavaDoc resourceBundle = "";
28     private String JavaDoc resourceName = "";
29     private String JavaDoc defaultValue = "";
30     private String JavaDoc localeLangage = "";
31     private String JavaDoc localeCountry = "";
32     private String JavaDoc localeVariant = "";
33     private Locale JavaDoc locale = null;
34
35     public void setResourceBundle(String JavaDoc resourceBundle) {
36         if ( resourceBundle == null )
37             resourceBundle = "";
38         this.resourceBundle = resourceBundle.trim();
39     }
40
41     public void setResourceName(String JavaDoc resourceName) {
42         if ( resourceName == null )
43             resourceName = "";
44         this.resourceName = resourceName;
45     }
46
47     public void setDefaultValue(String JavaDoc value) {
48         this.defaultValue = value;
49     }
50
51     public void setLocaleLangage(String JavaDoc localeLangage) {
52         if ( localeLangage != null )
53             this.localeLangage = localeLangage.trim();
54     }
55
56     public void setLocaleCountry(String JavaDoc localeCountry) {
57         if ( localeCountry != null )
58             this.localeCountry = localeCountry.trim();
59     }
60
61     public void setLocaleVariant(String JavaDoc localeCountry) {
62         if ( localeVariant != null )
63             this.localeVariant = localeVariant.trim();
64     }
65
66
67     public int doStartTag() {
68
69         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)pageContext.getRequest();
70         JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
71
72         ResourceBundle JavaDoc res = null;
73         String JavaDoc resValue = null;
74
75         if ( localeLangage.equals("") ){
76             locale = jData.params().getLocale();
77         } else {
78             locale = new Locale JavaDoc(localeLangage,localeCountry,localeVariant);
79         }
80         try {
81             res = ResourceBundle.getBundle(resourceBundle,locale);
82             resValue = res.getString(resourceName);
83         } catch ( MissingResourceException JavaDoc mre ) {
84             logger.warn("Error accessing resource " + resourceName +
85                         " in bundle " + resourceBundle + " for locale " +
86                         locale + ":" + mre.getMessage());
87         }
88         if ( resValue == null ){
89             resValue = this.defaultValue;
90         }
91
92         if (resValue != null) {
93             try {
94                 JspWriter JavaDoc out = pageContext.getOut();
95                 out.print( resValue );
96             } catch (IOException JavaDoc ioe) {
97                 logger.error("Error:", ioe);
98             }
99         }
100         return SKIP_BODY;
101     }
102
103     public int doEndTag() throws JspException JavaDoc {
104         // let's reinitialize the tag variables to allow tag object reuse in
105
// pooling.
106
resourceBundle = "";
107         resourceName = "";
108         localeLangage = "";
109         localeCountry = "";
110         localeVariant = "";
111         locale = null;
112         return EVAL_PAGE;
113     }
114
115 }
116
Popular Tags