1 9 package org.jboss.portal.core.servlet.jsp.taglib; 10 11 import java.util.LinkedList ; 12 import java.util.Locale ; 13 import java.util.MissingResourceException ; 14 import java.util.ResourceBundle ; 15 16 import javax.portlet.PortletConfig; 17 import javax.servlet.http.HttpServletRequest ; 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 28 public class PortalLib 29 { 30 31 36 public static String getMessage(String key) 37 { 38 ThreadLocal threadRequest = PortalJsp.request; 39 HttpServletRequest request = (HttpServletRequest )(threadRequest.get()); 40 Locale locale = request.getLocale(); 41 PortletConfig portletConfig = (PortletConfig)request.getAttribute("javax.portlet.config"); 42 ResourceBundle resourceBundle = portletConfig.getResourceBundle(locale); 43 PortalJsp.log.debug("Use locale:" + locale); 44 try 45 { 46 return resourceBundle.getString(key); 47 } catch (MissingResourceException e) { 48 PortalJsp.log.error("No such resource key in resource file: " + key); 49 return key; 50 } 51 } 52 53 58 public static String i18nOut(String key) 59 { 60 return getMessage(out(key)); 61 } 62 63 64 69 public static String out(String key) 70 { 71 ThreadLocal contextStackLocal = PortalJsp.contextStack; 72 LinkedList contextStack = (LinkedList )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 String [] ctxNames = key.split("[.]"); 81 82 NamedContext tmp = null; 83 int i=0; 84 85 for (i=0; i < ctxNames.length - 1; i++) { 87 try { 88 tmp = (NamedContext)contextStack.get(i+1); 89 } catch (IndexOutOfBoundsException 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 |