KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 Engine'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 engine resource bundle in that order :
28  *
29  * 1. Look for the engine resource bundle of the page.
30  * This resource bundle can be set in the template used by the page
31  * with the SetEngineResourceBundleTag.
32  *
33  * 2. Look for the site's default engine resource bundle.
34  * Each site can have a default engine resource bundle. It's name
35  * must be of this form : "JahiaEnginesResourcesMYJAHIASITE"
36  * where MYJAHIASITE is the virtual site's sitekey in uppercase.
37  *
38  * 3. Finally if none of the previous resource bundle are available,
39  * Jahia will return the internal engine's default resource bundle
40  * named "JahiaEnginesResources".
41  *
42  *
43  * @see JahiaResourceBundle
44  * @see SetEngineResourceBundleTag
45  * @see JahiaEnginesResources.properties
46  *
47  * @author Khue Nguyen
48  */

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

140     public static String JavaDoc parseResourceValue(String JavaDoc val,ParamBean jParams){
141
142         if ( (val == null) || (jParams == null) || (jParams.getSite()== null) )
143             return val;
144         val = JahiaTools.replacePattern(val,JAHIA_ENGINE_TAG,
145                                                          "/jsp/jahia/engines/");
146
147         val = JahiaTools.replacePattern(val,SITE_TEMPLATE_TAG,
148                 "/jsp/jahia/templates/" + jParams.getSite().getSiteKey() + "/");
149
150         return val;
151
152     }
153
154     public int doEndTag() throws JspException JavaDoc {
155         // let's reinitialize the tag variables to allow tag object reuse in
156
// pooling.
157
resourceName = "";
158         return EVAL_PAGE;
159     }
160
161
162 }
163
Popular Tags