1 4 5 package com.ibm.icu.impl; 6 7 import java.lang.ref.SoftReference ; 8 import java.util.Locale ; 9 import java.util.MissingResourceException ; 10 import java.util.ResourceBundle ; 11 import java.util.Arrays ; 12 import java.util.Collections ; 13 import java.util.HashMap ; 14 import java.util.HashSet ; 15 import java.util.Map ; 16 import java.util.Set ; 17 18 import com.ibm.icu.util.ULocale; 19 20 26 public class ICULocaleData { 27 28 31 private static final String ICU_PACKAGE = "com.ibm.icu.impl.data"; 32 33 36 static final String LOCALE_ELEMENTS = "LocaleElements"; 37 38 46 public static ResourceBundle getLocaleElements(ULocale locale) { 47 if (locale == null) { 48 locale = ULocale.getDefault(); 49 } 50 return getResourceBundle(ICU_PACKAGE, LOCALE_ELEMENTS, locale.getBaseName()); 51 } 52 53 61 public static ResourceBundle getLocaleElements(String localeName) { 62 return getResourceBundle(ICU_PACKAGE, LOCALE_ELEMENTS, localeName); 63 } 64 65 66 76 public static ResourceBundle getResourceBundle(String bundleName, String localeName) { 77 return getResourceBundle(ICU_PACKAGE, bundleName, localeName); 78 } 79 80 90 public static ResourceBundle getResourceBundle(String bundleName, ULocale locale) { 91 if (locale == null) { 92 locale = ULocale.getDefault(); 93 } 94 return getResourceBundle(ICU_PACKAGE, bundleName, locale.getBaseName()); 95 } 96 97 109 public static ResourceBundle getResourceBundle(String packageName, String bundleName, String localeName) { 110 try { 111 String path = packageName + "." + bundleName; 112 if (DEBUG) System.out.println("calling instantiate: " + path + "_" + localeName); 113 return instantiate(path, localeName); 114 } catch (MissingResourceException e) { 115 if (DEBUG) System.out.println(bundleName + "_" + localeName + " not found in " + packageName); 116 throw e; 117 } 118 } 119 120 132 public static ResourceBundle getResourceBundle(String [] packages, String bundleName, String localeName) { 133 ResourceBundle r=null; 134 for (int i=0; r==null && i<packages.length; ++i) { 135 r = getResourceBundle(packages[i], bundleName, localeName); 136 } 137 return r; 138 } 139 140 144 public static ResourceBundle loadResourceBundle(String bundleName, ULocale locale) { 145 if (locale == null) { 146 locale = ULocale.getDefault(); 147 } 148 return loadResourceBundle(bundleName, locale.getBaseName()); 149 } 150 151 155 public static ResourceBundle loadResourceBundle(String bundleName, String localeName) { 156 if (localeName != null && localeName.length() > 0) { 157 bundleName = bundleName + "_" + localeName; 158 } 159 String name = ICU_PACKAGE + "." + bundleName; 160 try { 161 if (name.indexOf("_zh_") == -1) { Class rbclass = Class.forName(name); 163 ResourceBundle rb = (ResourceBundle )rbclass.newInstance(); 164 return rb; 165 } 166 } 167 catch (ClassNotFoundException e) 168 { 169 if (DEBUG) { 170 System.out.println(name + " not found"); 171 } 172 } 174 catch (Exception e) { 175 if (DEBUG) { 176 e.printStackTrace(); 177 System.out.println(e.getMessage()); 178 } 179 } 180 if (DEBUG) { 181 System.out.println(bundleName + " not found."); 182 } 183 184 return null; 185 } 186 187 189 private static final boolean DEBUG = ICUDebug.enabled("localedata"); 191 192 private static SoftReference GET_AVAILABLE_CACHE; 194 195 private static SoftReference BUNDLE_CACHE; 197 198 private static ResourceBundle loadFromCache(String key) { 199 if (BUNDLE_CACHE != null) { 200 Map m = (Map )BUNDLE_CACHE.get(); 201 if (m != null) { 202 return (ResourceBundle )m.get(key); 203 } 204 } 205 return null; 206 } 207 208 private static void addToCache(String key, ResourceBundle b) { 209 Map m = null; 210 if (BUNDLE_CACHE != null) { 211 m = (Map )BUNDLE_CACHE.get(); 212 } 213 if (m == null) { 214 m = new HashMap (); 215 BUNDLE_CACHE = new SoftReference (m); 216 } 217 m.put(key, b); 218 } 219 220 private static ResourceBundle instantiate(String name) { 222 ResourceBundle b = loadFromCache(name); 223 if (b == null) { 224 ResourceBundle parent = null; 225 int i = name.lastIndexOf('_'); 226 227 final Locale rootLocale = new Locale ("", "", ""); 229 230 if (i != -1) { 231 parent = instantiate(name.substring(0, i)); 232 } 233 try { 234 Locale locale = rootLocale; 235 i = name.indexOf('_'); 236 if (i != -1) { 237 locale = LocaleUtility.getLocaleFromName(name.substring(i+1)); 238 } else { 239 i = name.length(); 240 } 241 242 ClassLoader cl = ICULocaleData.class.getClassLoader(); 243 if (cl == null) { 244 cl = ClassLoader.getSystemClassLoader(); 246 } 247 Class cls = cl.loadClass(name); 248 if (ICUListResourceBundle.class.isAssignableFrom(cls)) { 249 ICUListResourceBundle bx = (ICUListResourceBundle)cls.newInstance(); 250 251 if (parent != null) { 252 bx.setParentX(parent); 253 } 254 bx.icuLocale = locale; 255 b = bx; 256 } else { 258 b = ResourceBundle.getBundle(name.substring(0, i), locale); 259 } 261 addToCache(name, b); 262 } 263 catch (ClassNotFoundException e) { 264 265 int j = name.indexOf('_'); 266 int k = name.lastIndexOf('_'); 267 if(k==j && j!=-1){ 270 271 String locName = name.substring(j+1,name.length()); 272 String defaultName = ULocale.getDefault().toString(); 273 274 if(!locName.equals(rootLocale.toString()) && 275 defaultName.indexOf(locName)==-1){ 276 String bundle = name.substring(0,j); 277 parent = instantiate(bundle+"_"+defaultName); 278 } 279 } 280 b = parent; 281 } 282 catch (Exception e) { 283 System.out.println("failure"); 284 System.out.println(e); 285 } 286 } 287 return b; 291 } 292 293 298 private synchronized static ResourceBundle instantiate(String name, String localeName) { 299 if (localeName.length() != 0) { 300 name = name + "_" + localeName; 301 } 302 ResourceBundle b = instantiate(name); 303 if(b==null){ 304 throw new MissingResourceException ("Could not load data "+name,"",""); 305 } 306 return b; 307 } 308 309 private static Set createLocaleNameSet(String bundleName) { 310 try { 311 ResourceBundle index = getResourceBundle(bundleName, "index"); 312 Object [][] localeStrings = (Object [][]) index.getObject("InstalledLocales"); 313 String [] localeNames = new String [localeStrings.length]; 314 315 for (int i = 0; i < localeNames.length; ++i) { 320 localeNames[i] = LocaleUtility.getLocaleFromName((String )localeStrings[i][0]).toString(); 321 } 322 323 HashSet set = new HashSet (); 324 set.addAll(Arrays.asList(localeNames)); 325 return Collections.unmodifiableSet(set); 326 } 327 catch (MissingResourceException e) { 328 if (DEBUG) System.out.println("couldn't find index for bundleName: " + bundleName); 329 Thread.dumpStack(); 330 } 331 return Collections.EMPTY_SET; 332 } 333 334 352 } 353 | Popular Tags |