1 16 17 package org.apache.taglibs.standard.tag.common.fmt; 18 19 import java.io.IOException ; 20 import java.util.Enumeration ; 21 import java.util.Locale ; 22 import java.util.MissingResourceException ; 23 import java.util.ResourceBundle ; 24 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.JspTagException ; 28 import javax.servlet.jsp.PageContext ; 29 import javax.servlet.jsp.jstl.core.Config; 30 import javax.servlet.jsp.jstl.fmt.LocalizationContext; 31 import javax.servlet.jsp.tagext.BodyTagSupport ; 32 33 import org.apache.taglibs.standard.tag.common.core.Util; 34 35 41 42 public abstract class BundleSupport extends BodyTagSupport { 43 44 45 48 private static final Locale EMPTY_LOCALE = new Locale ("", ""); 49 50 51 54 protected String basename; protected String prefix; 57 58 61 private Locale fallbackLocale; 62 private LocalizationContext locCtxt; 63 64 65 68 public BundleSupport() { 69 super(); 70 init(); 71 } 72 73 private void init() { 74 basename = prefix = null; 75 locCtxt = null; 76 } 77 78 79 82 public LocalizationContext getLocalizationContext() { 83 return locCtxt; 84 } 85 86 public String getPrefix() { 87 return prefix; 88 } 89 90 91 94 public int doStartTag() throws JspException { 95 locCtxt = getLocalizationContext(pageContext, basename); 96 return EVAL_BODY_BUFFERED; 97 } 98 99 public int doEndTag() throws JspException { 100 if (bodyContent != null) { 101 try { 102 pageContext.getOut().print(bodyContent.getString()); 103 } catch (IOException ioe) { 104 throw new JspTagException (ioe.toString(), ioe); 105 } 106 } 107 108 return EVAL_PAGE; 109 } 110 111 public void release() { 113 init(); 114 } 115 116 117 120 125 public static LocalizationContext getLocalizationContext(PageContext pc) { 126 LocalizationContext locCtxt = null; 127 128 Object obj = Config.find(pc, Config.FMT_LOCALIZATION_CONTEXT); 129 if (obj == null) { 130 return null; 131 } 132 133 if (obj instanceof LocalizationContext) { 134 locCtxt = (LocalizationContext) obj; 135 } else { 136 locCtxt = getLocalizationContext(pc, (String ) obj); 138 } 139 140 return locCtxt; 141 } 142 143 167 public static LocalizationContext getLocalizationContext(PageContext pc, 168 String basename) { 169 LocalizationContext locCtxt = null; 170 ResourceBundle bundle = null; 171 172 if ((basename == null) || basename.equals("")) { 173 return new LocalizationContext(); 174 } 175 176 Locale pref = SetLocaleSupport.getLocale(pc, Config.FMT_LOCALE); 178 if (pref != null) { 179 bundle = findMatch(basename, pref); 181 if (bundle != null) { 182 locCtxt = new LocalizationContext(bundle, pref); 183 } 184 } else { 185 locCtxt = findMatch(pc, basename); 187 } 188 189 if (locCtxt == null) { 190 pref = SetLocaleSupport.getLocale(pc, Config.FMT_FALLBACK_LOCALE); 192 if (pref != null) { 193 bundle = findMatch(basename, pref); 194 if (bundle != null) { 195 locCtxt = new LocalizationContext(bundle, pref); 196 } 197 } 198 } 199 200 if (locCtxt == null) { 201 try { 203 bundle = ResourceBundle.getBundle(basename, EMPTY_LOCALE, 204 Thread.currentThread().getContextClassLoader()); 205 if (bundle != null) { 206 locCtxt = new LocalizationContext(bundle, null); 207 } 208 } catch (MissingResourceException mre) { 209 } 211 } 212 213 if (locCtxt != null) { 214 if (locCtxt.getLocale() != null) { 216 SetLocaleSupport.setResponseLocale(pc, locCtxt.getLocale()); 217 } 218 } else { 219 locCtxt = new LocalizationContext(); 221 } 222 223 return locCtxt; 224 } 225 226 227 230 243 private static LocalizationContext findMatch(PageContext pageContext, 244 String basename) { 245 LocalizationContext locCtxt = null; 246 247 249 for (Enumeration enum_ = Util.getRequestLocales((HttpServletRequest )pageContext.getRequest()); 250 enum_.hasMoreElements(); ) { 251 Locale pref = (Locale ) enum_.nextElement(); 252 ResourceBundle match = findMatch(basename, pref); 253 if (match != null) { 254 locCtxt = new LocalizationContext(match, pref); 255 break; 256 } 257 } 258 259 return locCtxt; 260 } 261 262 277 private static ResourceBundle findMatch(String basename, Locale pref) { 278 ResourceBundle match = null; 279 280 try { 281 ResourceBundle bundle = 282 ResourceBundle.getBundle(basename, pref, 283 Thread.currentThread().getContextClassLoader()); 284 Locale avail = bundle.getLocale(); 285 if (pref.equals(avail)) { 286 match = bundle; 288 } else { 289 303 if (pref.getLanguage().equals(avail.getLanguage()) 304 && ("".equals(avail.getCountry()) || pref.getCountry().equals(avail.getCountry()))) { 305 320 match = bundle; 321 } 322 } 323 } catch (MissingResourceException mre) { 324 } 325 326 return match; 327 } 328 } 329 | Popular Tags |