1 25 package org.ofbiz.webapp.taglib; 26 27 import java.text.MessageFormat ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.util.ResourceBundle ; 31 import javax.servlet.jsp.JspException ; 32 import javax.servlet.jsp.tagext.BodyTagSupport ; 33 import javax.servlet.jsp.tagext.TagSupport ; 34 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.base.util.UtilJ2eeCompat; 37 38 48 public class I18nMessageTag extends BodyTagSupport { 49 50 public static final String module = I18nMessageTag.class.getName(); 51 52 private String key = null; 53 54 private String value = null; 55 56 private ResourceBundle bundle = null; 57 58 private final List arguments = new ArrayList (); 59 60 public void setKey(String key) { 61 this.key = key; 62 } 63 64 public String getKey() { 65 return this.key; 66 } 67 68 public void setValue(String value) { 69 this.value = value; 70 } 71 72 public String getValue() { 73 return this.value; 74 } 75 76 public void setBundleId(String bundleId) { 77 this.bundle = (ResourceBundle ) pageContext.getAttribute(bundleId); 78 } 79 80 public void addArgument(Object argument) { 81 this.arguments.add(argument); 82 } 83 84 public int doStartTag() throws JspException { 85 try { 86 if (this.bundle == null) { 87 I18nBundleTag bundleTag = (I18nBundleTag) TagSupport.findAncestorWithClass(this, I18nBundleTag.class); 88 89 if (bundleTag != null) { 90 this.bundle = bundleTag.getBundle(); 91 } 92 } 93 94 if (this.bundle != null) this.value = this.bundle.getString(this.key); 95 96 99 } catch (Exception e) { 100 if (UtilJ2eeCompat.useNestedJspException(pageContext.getServletContext())) { 101 throw new JspException (e.getMessage(), e); 102 } else { 103 Debug.logError(e, "Server does not support nested exceptions, here is the exception", module); 104 throw new JspException (e.toString()); 105 } 106 } 107 108 return EVAL_BODY_AGAIN; 109 } 110 111 public int doEndTag() throws JspException { 112 try { 113 if (this.value != null && this.arguments != null && this.arguments.size() > 0) { 114 MessageFormat messageFormat = new MessageFormat (this.value); 115 116 messageFormat.setLocale(this.bundle.getLocale()); 117 this.value = messageFormat.format(arguments.toArray()); 118 } 119 120 if (this.value != null) this.pageContext.getOut().print(this.value); 121 } catch (Exception e) { 122 if (UtilJ2eeCompat.useNestedJspException(pageContext.getServletContext())) { 123 throw new JspException (e.getMessage(), e); 124 } else { 125 Debug.logError(e, "Server does not support nested exceptions, here is the exception", module); 126 throw new JspException (e.toString()); 127 } 128 } 129 130 return EVAL_PAGE; 131 } 132 } 133 | Popular Tags |