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.jsp.JspException ; 10 import javax.servlet.jsp.JspWriter ; 11 import javax.servlet.jsp.tagext.TagSupport ; 12 13 import org.jahia.data.JahiaData; 14 import org.jahia.resourcebundle.JahiaResourceBundle; 15 16 17 23 public class ResourceBundleTag extends TagSupport { 24 25 private static org.apache.log4j.Logger logger = 26 org.apache.log4j.Logger.getLogger(ResourceBundleTag.class); 27 28 private String resourceBundle = ""; 29 private String resourceName = ""; 30 private String defaultValue = ""; 31 private String localeLangage = ""; 32 private String localeCountry = ""; 33 private String localeVariant = ""; 34 private Locale locale = null; 35 private String namePostFix = null; 36 private String name = null; 37 38 public void setResourceBundle(String resourceBundle) { 39 if ( resourceBundle == null ) 40 resourceBundle = ""; 41 this.resourceBundle = resourceBundle.trim(); 42 } 43 44 public void setResourceName(String resourceName) { 45 if ( resourceName == null ) 46 resourceName = ""; 47 this.resourceName = resourceName; 48 } 49 50 public void setDefaultValue(String value) { 51 this.defaultValue = value; 52 } 53 54 public void setLocaleLangage(String localeLangage) { 55 if ( localeLangage != null ) 56 this.localeLangage = localeLangage.trim(); 57 } 58 59 public void setLocaleCountry(String localeCountry) { 60 if ( localeCountry != null ) 61 this.localeCountry = localeCountry.trim(); 62 } 63 64 public void setLocaleVariant(String localeVariant) { 65 if ( localeVariant != null ) 66 this.localeVariant = localeVariant.trim(); 67 } 68 69 public String getNamePostFix() { 70 return namePostFix; 71 } 72 73 public void setNamePostFix(String namePostFix) { 74 this.namePostFix = namePostFix; 75 } 76 77 public String getName() { 78 return name; 79 } 80 public void setName(String name) { 81 this.name = name; 82 } 83 84 public int doStartTag() { 85 86 HttpServletRequest request = (HttpServletRequest )pageContext.getRequest(); 87 JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData"); 88 89 ResourceBundle res = null; 90 String resValue = null; 91 92 if ( localeLangage.equals("") ){ 93 locale = jData.params().getLocale(); 94 } else { 95 locale = new Locale (localeLangage,localeCountry,localeVariant); 96 } 97 try { 98 res = ResourceBundle.getBundle(resourceBundle,locale); 99 if (name != null) { 101 String keyName = (String ) pageContext.getAttribute(name); 102 if (keyName != null) { 103 if (namePostFix != null) { 104 keyName = keyName + namePostFix; 105 } 106 resValue = JahiaResourceBundle.getString(res, keyName, 107 locale); 108 } else { 109 logger.error("Unable to find a key name for page context attribute " + name); 110 } 111 } else { 112 resValue = JahiaResourceBundle.getString(res, resourceName, 113 locale); 114 } 115 } catch ( MissingResourceException mre ) { 116 logger.warn("Error accessing resource " + resourceName + 117 " in bundle " + resourceBundle + " for locale " + 118 locale + ":" + mre.getMessage()); 119 } 120 if ( resValue == null ){ 121 resValue = this.defaultValue; 122 } 123 124 if (resValue != null) { 125 try { 126 JspWriter out = pageContext.getOut(); 127 out.print( resValue ); 128 } catch (IOException ioe) { 129 logger.error("Error:", ioe); 130 } 131 } 132 return SKIP_BODY; 133 } 134 135 public int doEndTag() throws JspException { 136 resourceBundle = ""; 139 resourceName = ""; 140 localeLangage = ""; 141 localeCountry = ""; 142 localeVariant = ""; 143 locale = null; 144 name = null; 145 namePostFix = null; 146 return EVAL_PAGE; 147 } 148 149 } 150 | Popular Tags |