1 16 17 package org.apache.taglibs.standard.tag.common.fmt; 18 19 import java.text.DateFormat ; 20 import java.text.NumberFormat ; 21 import java.util.Enumeration ; 22 import java.util.Locale ; 23 import java.util.Vector ; 24 25 import javax.servlet.ServletResponse ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.jsp.JspException ; 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.Tag ; 32 import javax.servlet.jsp.tagext.TagSupport ; 33 34 import org.apache.taglibs.standard.resources.Resources; 35 import org.apache.taglibs.standard.tag.common.core.Util; 36 37 43 44 public abstract class SetLocaleSupport extends TagSupport { 45 46 47 50 private static final char HYPHEN = '-'; 51 private static final char UNDERSCORE = '_'; 52 53 54 57 protected Object value; protected String variant; 60 61 64 private int scope; 66 67 70 public SetLocaleSupport() { 71 super(); 72 init(); 73 } 74 75 private void init() { 76 value = variant = null; 77 scope = PageContext.PAGE_SCOPE; 78 } 79 80 81 84 public void setScope(String scope) { 85 this.scope = Util.getScope(scope); 86 } 87 88 89 92 public int doEndTag() throws JspException { 93 Locale locale = null; 94 95 if (value == null) { 96 locale = Locale.getDefault(); 97 } else if (value instanceof String ) { 98 if (((String ) value).trim().equals("")) { 99 locale = Locale.getDefault(); 100 } else { 101 locale = parseLocale((String ) value, variant); 102 } 103 } else { 104 locale = (Locale ) value; 105 } 106 107 Config.set(pageContext, Config.FMT_LOCALE, locale, scope); 108 setResponseLocale(pageContext, locale); 109 110 return EVAL_PAGE; 111 } 112 113 public void release() { 115 init(); 116 } 117 118 119 122 125 public static Locale parseLocale(String locale) { 126 return parseLocale(locale, null); 127 } 128 129 147 public static Locale parseLocale(String locale, String variant) { 148 149 Locale ret = null; 150 String language = locale; 151 String country = null; 152 int index = -1; 153 154 if (((index = locale.indexOf(HYPHEN)) > -1) 155 || ((index = locale.indexOf(UNDERSCORE)) > -1)) { 156 language = locale.substring(0, index); 157 country = locale.substring(index+1); 158 } 159 160 if ((language == null) || (language.length() == 0)) { 161 throw new IllegalArgumentException ( 162 Resources.getMessage("LOCALE_NO_LANGUAGE")); 163 } 164 165 if (country == null) { 166 if (variant != null) 167 ret = new Locale (language, "", variant); 168 else 169 ret = new Locale (language, ""); 170 } else if (country.length() > 0) { 171 if (variant != null) 172 ret = new Locale (language, country, variant); 173 else 174 ret = new Locale (language, country); 175 } else { 176 throw new IllegalArgumentException ( 177 Resources.getMessage("LOCALE_EMPTY_COUNTRY")); 178 } 179 180 return ret; 181 } 182 183 184 187 201 static void setResponseLocale(PageContext pc, Locale locale) { 202 ServletResponse response = pc.getResponse(); 204 response.setLocale(locale); 205 206 if (pc.getSession() != null) { 208 try { 209 pc.setAttribute(RequestEncodingSupport.REQUEST_CHAR_SET, 210 response.getCharacterEncoding(), 211 PageContext.SESSION_SCOPE); 212 } catch (IllegalStateException ex) {} } 214 } 215 216 231 static Locale getFormattingLocale(PageContext pc, 232 Tag fromTag, 233 boolean format, 234 Locale [] avail) { 235 236 LocalizationContext locCtxt = null; 237 238 Tag parent = findAncestorWithClass(fromTag, BundleSupport.class); 240 if (parent != null) { 241 245 locCtxt = ((BundleSupport) parent).getLocalizationContext(); 246 if (locCtxt.getLocale() != null) { 247 if (format) { 248 setResponseLocale(pc, locCtxt.getLocale()); 249 } 250 return locCtxt.getLocale(); 251 } 252 } 253 254 if ((locCtxt = BundleSupport.getLocalizationContext(pc)) != null) { 256 if (locCtxt.getLocale() != null) { 257 if (format) { 258 setResponseLocale(pc, locCtxt.getLocale()); 259 } 260 return locCtxt.getLocale(); 261 } 262 } 263 264 269 Locale match = null; 270 Locale pref = getLocale(pc, Config.FMT_LOCALE); 271 if (pref != null) { 272 match = findFormattingMatch(pref, avail); 274 } else { 275 match = findFormattingMatch(pc, avail); 277 } 278 if (match == null) { 279 pref = getLocale(pc, Config.FMT_FALLBACK_LOCALE); 281 if (pref != null) { 282 match = findFormattingMatch(pref, avail); 283 } 284 } 285 if (format && (match != null)) { 286 setResponseLocale(pc, match); 287 } 288 289 return match; 290 } 291 292 296 static Locale [] availableFormattingLocales; 297 static { 298 Locale [] dateLocales = DateFormat.getAvailableLocales(); 299 Locale [] numberLocales = NumberFormat.getAvailableLocales(); 300 Vector vec = new Vector (dateLocales.length); 301 for (int i=0; i<dateLocales.length; i++) { 302 for (int j=0; j<numberLocales.length; j++) { 303 if (dateLocales[i].equals(numberLocales[j])) { 304 vec.add(dateLocales[i]); 305 break; 306 } 307 } 308 } 309 availableFormattingLocales = new Locale [vec.size()]; 310 availableFormattingLocales = (Locale [])vec.toArray(availableFormattingLocales); 311 316 } 317 318 325 static Locale getFormattingLocale(PageContext pc) { 326 331 Locale match = null; 332 Locale pref = getLocale(pc, Config.FMT_LOCALE); 333 if (pref != null) { 334 match = findFormattingMatch(pref, availableFormattingLocales); 336 } else { 337 match = findFormattingMatch(pc, availableFormattingLocales); 339 } 340 if (match == null) { 341 pref = getLocale(pc, Config.FMT_FALLBACK_LOCALE); 343 if (pref != null) { 344 match = findFormattingMatch(pref, availableFormattingLocales); 345 } 346 } 347 if (match != null) { 348 setResponseLocale(pc, match); 349 } 350 351 return match; 352 } 353 354 372 static Locale getLocale(PageContext pageContext, String name) { 373 Locale loc = null; 374 375 Object obj = Config.find(pageContext, name); 376 if (obj != null) { 377 if (obj instanceof Locale ) { 378 loc = (Locale ) obj; 379 } else { 380 loc = parseLocale((String ) obj); 381 } 382 } 383 384 return loc; 385 } 386 387 388 391 401 private static Locale findFormattingMatch(PageContext pageContext, 402 Locale [] avail) { 403 Locale match = null; 404 for (Enumeration enum_ = Util.getRequestLocales((HttpServletRequest )pageContext.getRequest()); 405 enum_.hasMoreElements(); ) { 406 Locale locale = (Locale )enum_.nextElement(); 407 match = findFormattingMatch(locale, avail); 408 if (match != null) { 409 break; 410 } 411 } 412 413 return match; 414 } 415 416 435 private static Locale findFormattingMatch(Locale pref, Locale [] avail) { 436 Locale match = null; 437 boolean langAndCountryMatch = false; 438 for (int i=0; i<avail.length; i++) { 439 if (pref.equals(avail[i])) { 440 match = avail[i]; 442 break; 443 } else if ( 444 !"".equals(pref.getVariant()) && 445 "".equals(avail[i].getVariant()) && 446 pref.getLanguage().equals(avail[i].getLanguage()) && 447 pref.getCountry().equals(avail[i].getCountry())) { 448 match = avail[i]; 450 langAndCountryMatch = true; 451 } else if ( 452 !langAndCountryMatch && 453 pref.getLanguage().equals(avail[i].getLanguage()) && 454 ("".equals(avail[i].getCountry()))) { 455 if (match == null) { 457 match = avail[i]; 458 } 459 } 460 } 461 return match; 462 } 463 } 464 | Popular Tags |