KickJava   Java API By Example, From Geeks To Geeks.

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


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.http.HttpSession JavaDoc;
10 import javax.servlet.jsp.JspException JavaDoc;
11 import javax.servlet.jsp.JspWriter JavaDoc;
12 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
13
14 import org.jahia.data.JahiaData;
15 import org.jahia.params.ParamBean;
16 import org.jahia.resourcebundle.JahiaResourceBundle;
17 import org.jahia.utils.JahiaConsole;
18 import org.jahia.utils.JahiaTools;
19
20
21 /**
22  * Support for Jahia Admin's ResourceBundle within Jahia
23  *
24  * Returns the requested resource.
25  *
26  * If the requested resource bundle is missing and useDefault is true,
27  * Jahia will look for another resource bundle in that order :
28  *
29  * 1. Look for the site's default engine resource bundle.
30  * Each site can have a default engine resource bundle. It's name
31  * must be of this form : "JahiaAdminResourcesMYJAHIASITE"
32  * where MYJAHIASITE is the virtual site's sitekey in uppercase.
33  *
34  * 2. Finally if none of the previous resource bundle are available,
35  * Jahia will return the default resource bundle
36  * named "JahiaAdminResources".
37  *
38  *
39  * @see JahiaResourceBundle
40  * @see SetAdminResourceBundleTag
41  * @see JahiaEnginesResources.properties
42  *
43  * @author Khue Nguyen
44  */

45 public class AdminResourceBundleTag extends TagSupport JavaDoc {
46
47     private static final String JavaDoc CLASS_NAME = AdminResourceBundleTag.class.getName();
48
49     public static final String JavaDoc JAHIA_ENGINE_TAG = "<jahiaEngine>";
50     public static final String JavaDoc SITE_TEMPLATE_TAG = "<siteTemplate>";
51
52     private String JavaDoc resourceName = "";
53     private String JavaDoc defaultValue = "";
54
55     public void setResourceName(String JavaDoc resourceName) {
56         if ( resourceName == null )
57             resourceName = "";
58         this.resourceName = resourceName;
59     }
60
61     public void setDefaultValue(String JavaDoc value) {
62         this.defaultValue = value;
63     }
64
65     public int doStartTag() {
66
67         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)pageContext.getRequest();
68         JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
69
70         Locale JavaDoc currentLocale = request.getLocale();
71         HttpSession JavaDoc session = pageContext.getSession();
72         if (session != null) {
73             if (session.getAttribute(ParamBean.SESSION_LOCALE) != null) {
74                 currentLocale = (Locale JavaDoc) session.getAttribute(ParamBean.
75                     SESSION_LOCALE);
76             }
77         }
78
79         String JavaDoc resValue = null;
80
81         try {
82
83             if ( jData!= null ){
84                 resValue = JahiaResourceBundle
85                             .getAdminResource( resourceName,
86                                                 jData.params(),
87                                                 jData.params().getLocale() );
88             } else {
89                 // for any reason the jData wasn't loaded correctly
90
ResourceBundle JavaDoc resBundle = JahiaResourceBundle
91                          .getAdminDefaultResourceBundle(null,currentLocale);
92                 resValue = JahiaResourceBundle.getString(resBundle, resourceName, currentLocale);
93             }
94         } catch ( MissingResourceException JavaDoc mre ) {
95             JahiaConsole.println(CLASS_NAME+"doStartTag", mre.toString());
96         }
97
98         if (resValue == null) {
99             resValue = this.defaultValue;
100         }
101
102         try {
103             JspWriter JavaDoc out = pageContext.getOut();
104             if ( jData != null ){
105                 out.print( parseResourceValue(resValue,jData.params()) );
106             } else {
107                 out.print(parseResourceValue(resValue,null));
108             }
109         } catch (IOException JavaDoc ioe) {
110             JahiaConsole.println(CLASS_NAME+"doStartTag", ioe.toString());
111         }
112
113         return SKIP_BODY;
114
115     }
116
117     //--------------------------------------------------------------------------
118
/**
119      * parse the resource value and replace :
120      * <jahiaEngine> : by /jsp/jahia/engines/
121      * <siteTemplate> : by /jsp/jahia/templates/<mysite>/
122      * where <mysite> is the sitekey of the current site
123      *
124      * @param String val, the String to parse
125      * @param ParamBean, the paramBean
126      * @return String , the parsed value
127      */

128     public static String JavaDoc parseResourceValue(String JavaDoc val,ParamBean jParams){
129
130         if (val == null){
131             return val;
132         }
133         val = JahiaTools.replacePattern(val,JAHIA_ENGINE_TAG,
134                                         "/jsp/jahia/engines/");
135         if ( jParams != null ){
136             val = JahiaTools.replacePattern(val,SITE_TEMPLATE_TAG,
137                     "/jsp/jahia/templates/" + jParams.getSite().getSiteKey() + "/");
138         }
139         return val;
140     }
141
142     public int doEndTag() throws JspException JavaDoc {
143         // let's reinitialize the tag variables to allow tag object reuse in
144
// pooling.
145
resourceName = "";
146         return EVAL_PAGE;
147     }
148
149 }
150
Popular Tags