KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > servlet > jsp > taglib > PortalLib


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.servlet.jsp.taglib;
10
11 import java.util.LinkedList JavaDoc;
12 import java.util.Locale JavaDoc;
13 import java.util.MissingResourceException JavaDoc;
14 import java.util.ResourceBundle JavaDoc;
15
16 import javax.portlet.PortletConfig;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18
19 import org.jboss.portal.common.context.Context;
20 import org.jboss.portal.common.context.NamedContext;
21 import org.jboss.portal.core.servlet.jsp.PortalJsp;
22
23 /**
24  * Expression language static functions for the JBoss library.
25  * @author <a HREF="theute@jboss.org">Thomas Heute</a>
26  * $Revision: 1.1 $
27  */

28 public class PortalLib
29 {
30    
31    /**
32     * Internationalize messages
33     * @param key Key of the message
34     * @return The corresponding value in Resource.
35     */

36    public static String JavaDoc getMessage(String JavaDoc key)
37    {
38       ThreadLocal JavaDoc threadRequest = PortalJsp.request;
39       HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)(threadRequest.get());
40       Locale JavaDoc locale = request.getLocale();
41       PortletConfig portletConfig = (PortletConfig)request.getAttribute("javax.portlet.config");
42       ResourceBundle JavaDoc resourceBundle = portletConfig.getResourceBundle(locale);
43       PortalJsp.log.debug("Use locale:" + locale);
44       try
45       {
46          return resourceBundle.getString(key);
47       } catch (MissingResourceException JavaDoc e) {
48          PortalJsp.log.error("No such resource key in resource file: " + key);
49          return key;
50       }
51    }
52    
53   /**
54    * Return a translated message for a context value
55    * @param key The context value
56    * @return Translated text
57    */

58    public static String JavaDoc i18nOut(String JavaDoc key)
59    {
60       return getMessage(out(key));
61    }
62
63    
64    /**
65     * Print a value from the context
66     * @param key The context path to the value requested
67     * @return The value defined in the context
68     */

69    public static String JavaDoc out(String JavaDoc key)
70    {
71       ThreadLocal JavaDoc contextStackLocal = PortalJsp.contextStack;
72       LinkedList JavaDoc contextStack = (LinkedList JavaDoc)contextStackLocal.get();
73       
74       if (contextStack.isEmpty()) {
75          PortalJsp.log.warn("No context has been defined when trying to access " + key);
76          return "";
77       }
78
79       // Split by the dot
80
String JavaDoc[] ctxNames = key.split("[.]");
81       
82       NamedContext tmp = null;
83       int i=0;
84       
85       // Check that the path is correct
86
for (i=0; i < ctxNames.length - 1; i++) {
87          try {
88             tmp = (NamedContext)contextStack.get(i+1);
89          } catch (IndexOutOfBoundsException JavaDoc e) {
90             PortalJsp.log.warn("The key you called: " + key + " is not valid, please check the key");
91             return "";
92          }
93          if (!ctxNames[i].equals(tmp.getName())) {
94             PortalJsp.log.warn("The context you called: " + ctxNames[i] + " does not match " + tmp.getName());
95             return "";
96          }
97       }
98       
99       if (contextStack.get(i) != null) {
100          NamedContext ctx = (NamedContext)contextStack.get(i);
101          return ((Context)ctx.getContext()).get(ctxNames[ctxNames.length - 1]);
102       } else {
103          PortalJsp.log.warn("There is no such context for " + key);
104          return "";
105       }
106    }
107 }
108
Popular Tags