1 16 17 package org.apache.taglibs.i18n; 18 19 import java.util.Locale ; 20 import java.util.ResourceBundle ; 21 import java.util.Enumeration ; 22 23 import javax.servlet.ServletContext ; 24 25 import javax.servlet.http.HttpSession ; 26 27 import javax.servlet.jsp.JspException ; 28 import javax.servlet.jsp.JspTagException ; 29 import javax.servlet.jsp.PageContext ; 30 import javax.servlet.jsp.tagext.Tag ; 31 import javax.servlet.jsp.tagext.TagSupport ; 32 33 34 51 public class BundleTag extends TagSupport 52 { 53 private String _baseName = null; 54 private String _localeAttribute = null; 55 private Locale _locale = null; 56 private boolean _changeResponseLocale = true; 57 private ResourceBundle _bundle = null; 58 private int _scope = PageContext.PAGE_SCOPE; 59 private boolean _debug = false; 60 61 65 public final void setBaseName(String value) 66 { 67 _baseName = value; 68 } 69 70 73 public final void setDebug(boolean value) 74 { 75 _debug = value; 76 } 77 78 82 public void setLocale(Locale value) 83 { 84 _locale = value; 85 } 86 87 93 public void setLocaleAttribute(String value) 94 { 95 _localeAttribute = value; 96 } 97 98 103 public void setLocaleRef(String value) 104 { 105 _localeAttribute = value; 106 } 107 108 112 public void setScope(String value) throws JspException 113 { 114 if (value == null) 115 { 116 throw new JspTagException ("i18n:bundle tag invalid scope attribute of null"); 117 } 118 else if (value.toLowerCase().equals("application")) 119 { 120 _scope = PageContext.APPLICATION_SCOPE; 121 } 122 else if (value.toLowerCase().equals("session")) 123 { 124 _scope = PageContext.SESSION_SCOPE; 125 } 126 else if (value.toLowerCase().equals("request")) 127 { 128 _scope = PageContext.REQUEST_SCOPE; 129 } 130 else if (value.toLowerCase().equals("page")) 131 { 132 _scope = PageContext.PAGE_SCOPE; 133 } 134 else 135 { 136 throw new JspTagException ("i18n:bundle tag invalid scope attribute=" + value); 137 } 138 } 139 140 144 public void setChangeResponseLocale(boolean value) 145 { 146 _changeResponseLocale = value; 147 } 148 149 public void release() 150 { 151 super.release(); 152 _baseName = null; 153 _localeAttribute = null; 154 _locale = null; 155 _changeResponseLocale = true; 156 _bundle = null; 157 _scope = PageContext.PAGE_SCOPE; 158 _debug = false; 159 } 160 161 166 protected final ResourceBundle getBundle() 167 { 168 return _bundle; 169 } 170 171 185 private void findBundle() throws JspException 186 { 187 if (_baseName == null) { 189 throw new JspTagException ( 190 "i18n:bundle tag, a baseName attribute must be specified."); 191 } 192 193 Locale locale = _locale; 195 196 if (locale == null && _localeAttribute != null) { 198 locale = (Locale )pageContext.findAttribute(_localeAttribute); 199 } 200 201 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 203 if (locale != null) { 204 _bundle = ResourceBundle.getBundle(_baseName,locale,cl); 205 if (_debug) { 206 ServletContext sc = pageContext.getServletContext(); 207 sc.log("i18n:bundle tag debug: found locale " + locale); 208 } 209 } 210 211 else { 214 Enumeration localeEnumerator = pageContext.getRequest().getLocales(); 215 while (localeEnumerator.hasMoreElements()) { 216 locale = (Locale )localeEnumerator.nextElement(); 217 218 if (_debug) { 219 ServletContext sc = pageContext.getServletContext(); 220 sc.log("i18n:bundle tag debug: enumerating locale = " + locale); 221 } 222 223 ResourceBundle test = 225 ResourceBundle.getBundle(_baseName,locale,cl); 226 String language = test.getLocale().getLanguage(); 227 String country = test.getLocale().getCountry(); 228 229 if (test.getLocale().equals(locale)) { 234 _bundle = test; 236 break; 237 } else if (locale.getLanguage().equals(language)) { 238 if (locale.getCountry().equals(country)) { 240 _bundle = test; 244 continue; 245 } else { 246 if (_bundle == null) { 250 _bundle = test; 251 } 252 continue; 253 } 254 } else if (_debug) { 255 ServletContext sc = pageContext.getServletContext(); 256 sc.log("i18n:bundle tag requested locale not available: " + locale); 257 } 258 } 259 260 if (_bundle == null) { 264 _bundle = ResourceBundle.getBundle(_baseName); 265 } 266 267 if (_debug) { 268 ServletContext sc = pageContext.getServletContext(); 269 sc.log("i18n:bundle tag debug: bundle (sensed locale) = " + _bundle); 270 } 271 272 if (_localeAttribute != null) { 275 HttpSession session = pageContext.getSession(); 276 session.setAttribute(_localeAttribute,_bundle.getLocale()); 277 } 278 } 279 280 } 281 282 286 public int doStartTag() throws JspException 287 { 288 _bundle = null; 290 findBundle(); 291 292 if (_debug) { 293 ServletContext sc = pageContext.getServletContext(); 294 sc.log("i18n:bundle tag debug: basename =" + _baseName); 295 } 296 297 if (this.getId() != null) { 299 pageContext.setAttribute(this.getId(),_bundle); 300 } 301 302 return EVAL_BODY_INCLUDE; 303 } 304 305 308 public int doEndTag() throws JspException 309 { 310 if (_changeResponseLocale) { 311 Locale bundleLocale = _bundle.getLocale(); 313 if ((bundleLocale != null) && !("".equals(bundleLocale.getLanguage()))) { 314 pageContext.getResponse().setLocale(bundleLocale); 315 } 316 } 317 318 ResourceHelper.setBundle(pageContext,_bundle,_scope); 320 321 return EVAL_PAGE; 322 } 323 324 } 325 | Popular Tags |