KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.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 import org.jahia.resourcebundle.JahiaResourceBundle;
15
16
17 /**
18  * Support for ResourceBundle within Jahia
19  * If locale is not set, the locale used is the one returned by paramBean.getLocale()
20  *oi
21  * @author Khue Nguyen
22  */

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