1 package org.jahia.taglibs.resourcebundle; 2 3 import java.io.IOException ; 4 import java.util.Locale ; 5 import java.util.MissingResourceException ; 6 import java.util.ResourceBundle ; 7 8 import javax.servlet.http.HttpServletRequest ; 9 import javax.servlet.http.HttpSession ; 10 import javax.servlet.jsp.JspException ; 11 import javax.servlet.jsp.JspWriter ; 12 import javax.servlet.jsp.tagext.TagSupport ; 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 45 public class AdminResourceBundleTag extends TagSupport { 46 47 private static final String CLASS_NAME = AdminResourceBundleTag.class.getName(); 48 49 public static final String JAHIA_ENGINE_TAG = "<jahiaEngine>"; 50 public static final String SITE_TEMPLATE_TAG = "<siteTemplate>"; 51 52 private String resourceName = ""; 53 private String defaultValue = ""; 54 55 public void setResourceName(String resourceName) { 56 if ( resourceName == null ) 57 resourceName = ""; 58 this.resourceName = resourceName; 59 } 60 61 public void setDefaultValue(String value) { 62 this.defaultValue = value; 63 } 64 65 public int doStartTag() { 66 67 HttpServletRequest request = (HttpServletRequest )pageContext.getRequest(); 68 JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData"); 69 70 Locale currentLocale = request.getLocale(); 71 HttpSession session = pageContext.getSession(); 72 if (session != null) { 73 if (session.getAttribute(ParamBean.SESSION_LOCALE) != null) { 74 currentLocale = (Locale ) session.getAttribute(ParamBean. 75 SESSION_LOCALE); 76 } 77 } 78 79 String 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 ResourceBundle resBundle = JahiaResourceBundle 91 .getAdminDefaultResourceBundle(null,currentLocale); 92 resValue = JahiaResourceBundle.getString(resBundle, resourceName, currentLocale); 93 } 94 } catch ( MissingResourceException 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 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 ioe) { 110 JahiaConsole.println(CLASS_NAME+"doStartTag", ioe.toString()); 111 } 112 113 return SKIP_BODY; 114 115 } 116 117 128 public static String parseResourceValue(String 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 { 143 resourceName = ""; 146 return EVAL_PAGE; 147 } 148 149 } 150 | Popular Tags |