1 31 32 package org.opencms.i18n; 33 34 import org.opencms.util.CmsFileUtil; 35 36 import java.io.File ; 37 import java.io.FileInputStream ; 38 import java.io.IOException ; 39 import java.io.InputStream ; 40 import java.net.URL ; 41 import java.util.HashMap ; 42 import java.util.Locale ; 43 import java.util.Map ; 44 import java.util.MissingResourceException ; 45 import java.util.ResourceBundle ; 46 47 70 public final class CmsResourceBundleLoader { 71 72 77 private static class BundleKey { 78 79 private String m_baseName; 80 private int m_hashcode; 81 private Locale m_locale; 82 83 86 BundleKey() { 87 88 } 90 91 97 BundleKey(String s, Locale l) { 98 99 set(s, l); 100 } 101 102 105 public boolean equals(Object o) { 106 107 if (!(o instanceof BundleKey)) { 108 return false; 109 } 110 BundleKey key = (BundleKey)o; 111 return m_hashcode == key.m_hashcode && m_baseName.equals(key.m_baseName) && m_locale.equals(key.m_locale); 112 } 113 114 117 public int hashCode() { 118 119 return m_hashcode; 120 } 121 122 125 public String toString() { 126 127 return m_baseName + "_" + m_locale; 128 } 129 130 136 void set(String s, Locale l) { 137 138 m_baseName = s; 139 m_locale = l; 140 m_hashcode = m_baseName.hashCode() ^ m_locale.hashCode(); 141 } 142 } 143 144 145 private static Map m_bundleCache; 146 147 148 private static Locale m_lastDefaultLocale; 149 150 151 private static BundleKey m_lookupKey = new BundleKey(); 152 153 154 private static final Object NULL_ENTRY = new Object (); 155 156 159 private CmsResourceBundleLoader() { 160 161 } 163 164 167 public static synchronized void flushBundleCache() { 168 169 m_bundleCache = new HashMap (); 170 } 171 172 210 public static synchronized ResourceBundle getBundle(String baseName, Locale locale) { 213 214 Locale defaultLocale = Locale.getDefault(); 217 if (defaultLocale != m_lastDefaultLocale) { 218 m_bundleCache = new HashMap (); 219 m_lastDefaultLocale = defaultLocale; 220 } 221 222 m_lookupKey.set(baseName, locale); 224 225 Object obj = m_bundleCache.get(m_lookupKey); 226 227 if (obj instanceof CmsResourceBundle) { 228 return (CmsResourceBundle)obj; 229 } else if (obj == NULL_ENTRY) { 230 } else { 232 boolean wantBase = locale.equals(defaultLocale); 235 CmsResourceBundle bundle = tryBundle(baseName, locale, wantBase); 236 237 if (bundle == null && !locale.equals(defaultLocale)) { 239 bundle = tryBundle(baseName, defaultLocale, true); 240 } 241 242 BundleKey key = new BundleKey(baseName, locale); 243 if (bundle == null) { 244 m_bundleCache.put(key, NULL_ENTRY); 246 } else { 247 m_bundleCache.put(key, bundle); 249 return bundle; 250 } 251 } 252 253 return ResourceBundle.getBundle(baseName, locale); 256 } 257 258 264 private static CmsResourceBundle tryBundle(String localizedName) { 265 266 CmsResourceBundle bundle = null; 267 268 try { 269 270 InputStream is = null; 271 String resourceName = localizedName.replace('.', '/') + ".properties"; 272 URL url = CmsResourceBundleLoader.class.getClassLoader().getResource(resourceName); 273 274 if (url != null) { 275 String path = CmsFileUtil.normalizePath(url); 276 File file = new File (path); 277 try { 278 is = new FileInputStream (file); 283 } catch (IOException ex) { 284 is = CmsResourceBundleLoader.class.getClassLoader().getResourceAsStream(resourceName); 286 } 287 } 288 if (is != null) { 289 bundle = new CmsResourceBundle(is); 290 } 291 } catch (IOException ex) { 292 MissingResourceException mre = new MissingResourceException ( 294 "Failed to load bundle '" + localizedName + "'", 295 localizedName, 296 ""); 297 mre.initCause(ex); 298 throw mre; 299 } 300 301 return bundle; 302 } 303 304 314 private static CmsResourceBundle tryBundle(String baseName, Locale locale, boolean wantBase) { 315 316 String language = locale.getLanguage(); 317 String country = locale.getCountry(); 318 String variant = locale.getVariant(); 319 320 int baseLen = baseName.length(); 321 322 StringBuffer sb = new StringBuffer (baseLen + variant.length() + 7); 325 326 sb.append(baseName); 327 328 if (language.length() > 0) { 329 sb.append('_'); 330 sb.append(language); 331 332 if (country.length() > 0) { 333 sb.append('_'); 334 sb.append(country); 335 336 if (variant.length() > 0) { 337 sb.append('_'); 338 sb.append(variant); 339 } 340 } 341 } 342 343 String bundleName = sb.toString(); 346 CmsResourceBundle first = null; CmsResourceBundle last = null; 349 while (true) { 350 CmsResourceBundle foundBundle = tryBundle(bundleName); 351 if (foundBundle != null) { 352 if (first == null) { 353 first = foundBundle; 354 } 355 356 if (last != null) { 357 last.setParent(foundBundle); 358 } 359 foundBundle.setLocale(locale); 360 361 last = foundBundle; 362 } 363 int idx = bundleName.lastIndexOf('_'); 364 if (idx > baseLen || (idx == baseLen && (first != null || wantBase))) { 367 bundleName = bundleName.substring(0, idx); 368 } else { 369 break; 370 } 371 } 372 373 return first; 374 } 375 }
| Popular Tags
|