1 16 17 package org.apache.taglibs.i18n; 18 19 import java.io.IOException ; 20 21 import java.text.MessageFormat ; 22 23 import java.util.Arrays ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import java.util.ResourceBundle ; 27 28 import javax.servlet.ServletContext ; 29 30 import javax.servlet.jsp.JspException ; 31 import javax.servlet.jsp.JspTagException ; 32 import javax.servlet.jsp.PageContext ; 33 import javax.servlet.jsp.tagext.BodyTag ; 34 import javax.servlet.jsp.tagext.BodyTagSupport ; 35 36 57 public class MessageTag extends BodyTagSupport 58 { 59 private String _key = null; 61 private String _value = null; 62 private ResourceBundle _bundle = null; 63 private String _bundleRef = null; 64 private Object [] _args = null; 65 private boolean _debug = false; 66 67 private MessageFormat _messageFormat = new MessageFormat (""); 69 private List _arguments = new ArrayList (); 70 71 74 public final void setKey(String key) 75 { 76 _key = key; 77 } 78 79 84 public final void setBundleRef(String varName) 85 { 86 _bundleRef = varName; 87 } 88 89 94 public final void setBundle(ResourceBundle aBundle) 95 { 96 _bundle = aBundle; 97 } 98 99 102 private ResourceBundle getBundle() throws JspException 103 { 104 ResourceBundle bundle = _bundle; 105 if ( bundle == null ) { 106 if ( _bundleRef != null ) { 107 bundle = (ResourceBundle )pageContext.getAttribute(_bundleRef); 108 if ( bundle == null ) { 109 throw new JspTagException ( 110 "i18n:message tag, could not find bundleRef=" + _bundleRef); 111 } 112 } else { 113 BundleTag bundleTag = (BundleTag)this.findAncestorWithClass(this,BundleTag.class); 114 if (bundleTag != null) { 115 return bundleTag.getBundle(); 116 } 117 bundle = ResourceHelper.getBundle(pageContext); 118 } 119 } 120 return bundle; 121 } 122 123 126 public final void setDebug(boolean value) 127 { 128 _debug = value; 129 } 130 131 134 protected final void addArg(Object arg) 135 { 136 _arguments.add(arg); 137 if (_debug) { 138 ServletContext sc = pageContext.getServletContext(); 139 sc.log("i18n:message added arg: " + arg.toString()); 140 } 141 } 142 143 146 public final void setArgs(Object [] args) 147 { 148 _args = args; 149 } 150 151 156 public final void release() 157 { 158 super.release(); 159 if (_arguments != null) 160 { 161 _arguments.clear(); 162 } 163 _key = null; 164 _value = null; 165 _bundle = null; 166 _bundleRef = null; 167 _args = null; 168 _debug = false; 169 _messageFormat = null; 170 _arguments = null; 171 } 172 173 176 public final int doStartTag() throws JspException 177 { 178 _value = null; 180 181 if ( _key == null ) { 183 throw new JspTagException ("i18n:message tag requires a key attribute."); 184 } 185 186 ResourceBundle bundle = this.getBundle(); 187 if ( bundle == null ) { 188 throw new JspTagException ( 189 "i18n:message tag, no bundle available for use."); 190 } 191 192 if (_arguments != null) { 194 _arguments.clear(); 195 } 196 if ( _args != null ) { 197 _arguments.addAll(Arrays.asList(_args)); 198 } 199 200 try { 202 _value = bundle.getString(_key); 203 if (_debug) { 204 ServletContext sc = pageContext.getServletContext(); 205 sc.log("i18n:message tag: template for " + _key + " is: " + _value); 206 } 207 } catch (java.util.MissingResourceException e) { 208 ServletContext sc = pageContext.getServletContext(); 209 sc.log("i18n:message tag, value not found for key:" + _key); 210 } 211 212 return EVAL_BODY_TAG; 213 } 214 215 218 public int doAfterBody() throws JspException 219 { 220 if ( _value == null ) { 222 _value = bodyContent.getString(); 223 } 224 bodyContent.clearBody(); 226 return SKIP_BODY; 227 } 228 229 234 public final int doEndTag() throws JspException 235 { 236 try { 237 if ( _value != null && _arguments != null && _arguments.size() > 0) { 239 _messageFormat.setLocale(getBundle().getLocale()); 241 _messageFormat.applyPattern(_value); 242 _value = _messageFormat.format(_arguments.toArray()); 243 } 244 245 if ( _value == null) { 246 if (_debug) { 247 ServletContext sc = pageContext.getServletContext(); 248 sc.log("i18n: message: skipping null value for " + _key); 249 } 250 } else if (id != null) { 251 pageContext.setAttribute(id,_value); 253 } else { 254 this.pageContext.getOut().print(_value); 256 } 257 } catch (java.io.IOException e) { 258 throw new JspTagException ("i18n:message tag IO Error: " + e.getMessage()); 259 } 260 261 return EVAL_PAGE; 263 } 264 265 } 266 | Popular Tags |