KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > impl > ICULocaleData


1 /*
2  * (C) Copyright IBM Corp. 2002-2005 - All Rights Reserved
3  */

4
5 package com.ibm.icu.impl;
6
7 import java.lang.ref.SoftReference JavaDoc;
8 import java.util.Locale JavaDoc;
9 import java.util.MissingResourceException JavaDoc;
10 import java.util.ResourceBundle JavaDoc;
11 import java.util.Arrays JavaDoc;
12 import java.util.Collections JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import com.ibm.icu.util.ULocale;
19
20 /**
21  * Provides information about and access to resource bundles in the
22  * com.ibm.text.resources package. Unlike the java version, this does
23  * not include resources from any other location. In particular, it
24  * does not look in the boot or system class path.
25  */

26 public class ICULocaleData {
27
28     /**
29      * The package for ICU locale data.
30      */

31     private static final String JavaDoc ICU_PACKAGE = "com.ibm.icu.impl.data";
32
33     /**
34      * The base name (bundle name) for ICU locale data.
35      */

36     static final String JavaDoc LOCALE_ELEMENTS = "LocaleElements";
37
38     /**
39      * Creates a LocaleElements resource bundle for the given locale
40      * in the default ICU package.
41      * @param locale the locale of the bundle to retrieve, or
42      * null to use the default locale
43      * @return a ResourceBundle for the LocaleElements of the given
44      * locale, or null on failure
45      */

46     public static ResourceBundle JavaDoc getLocaleElements(ULocale locale) {
47         if (locale == null) {
48             locale = ULocale.getDefault();
49         }
50         return getResourceBundle(ICU_PACKAGE, LOCALE_ELEMENTS, locale.getBaseName());
51     }
52
53     /**
54      * Creates a LocaleElements resource bundle for the given locale
55      * in the default ICU package.
56      * @param localeName the string name of the locale of the bundle
57      * to retrieve, e.g., "en_US".
58      * @return a ResourceBundle for the LocaleElements of the given
59      * locale, or null on failure
60      */

61     public static ResourceBundle JavaDoc getLocaleElements(String JavaDoc localeName) {
62         return getResourceBundle(ICU_PACKAGE, LOCALE_ELEMENTS, localeName);
63     }
64
65
66     /**
67      * Creates a resource bundle for the given base name and locale
68      * in the default ICU package.
69      * @param bundleName the base name of the bundle to retrieve,
70      * e.g. "LocaleElements".
71      * @param localeName the string name of the locale of the bundle
72      * to retrieve, e.g. "en_US".
73      * @return a ResourceBundle with the given base name for the given
74      * locale, or null on failure
75      */

76     public static ResourceBundle JavaDoc getResourceBundle(String JavaDoc bundleName, String JavaDoc localeName) {
77         return getResourceBundle(ICU_PACKAGE, bundleName, localeName);
78     }
79
80     /**
81      * Creates a resource bundle for the given base name and locale
82      * in the default ICU package.
83      * @param bundleName the base name of the bundle to retrieve,
84      * e.g. "LocaleElements".
85      * @param locale the locale of the bundle to retrieve, or
86      * null to use the default locale
87      * @return a ResourceBundle with the given base name for the given
88      * locale, or null on failure
89      */

90     public static ResourceBundle JavaDoc getResourceBundle(String JavaDoc bundleName, ULocale locale) {
91         if (locale == null) {
92             locale = ULocale.getDefault();
93         }
94         return getResourceBundle(ICU_PACKAGE, bundleName, locale.getBaseName());
95     }
96
97     /**
98      * Creates a resource bundle for the given package, base name, and
99      * locale.
100      * @param packageName a package name, e.g., "com.ibm.icu.impl.data".
101      * @param bundleName the base name of the bundle to retrieve,
102      * e.g. "LocaleElements".
103      * @param localeName the string name of the locale of the bundle
104      * to retrieve, e.g. "en_US".
105      * @return the first ResourceBundle with the given base name for
106      * the given locale found in each of the packages, or null on
107      * failure
108      */

109     public static ResourceBundle JavaDoc getResourceBundle(String JavaDoc packageName, String JavaDoc bundleName, String JavaDoc localeName) {
110         try {
111             String JavaDoc path = packageName + "." + bundleName;
112             if (DEBUG) System.out.println("calling instantiate: " + path + "_" + localeName);
113             return instantiate(path, localeName);
114         } catch (MissingResourceException JavaDoc e) {
115             if (DEBUG) System.out.println(bundleName + "_" + localeName + " not found in " + packageName);
116             throw e;
117         }
118     }
119
120     /**
121      * Creates a resource bundle for the given base name and locale in
122      * one of the given packages, trying each package in order.
123      * @param packages a list of one or more package names
124      * @param bundleName the base name of the bundle to retrieve,
125      * e.g. "LocaleElements".
126      * @param localeName the string name of the locale of the bundle
127      * to retrieve, e.g. "en_US".
128      * @return the first ResourceBundle with the given base name for
129      * the given locale found in each of the packages, or null on
130      * failure
131      */

132     public static ResourceBundle JavaDoc getResourceBundle(String JavaDoc[] packages, String JavaDoc bundleName, String JavaDoc localeName) {
133         ResourceBundle JavaDoc 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     /**
141      * Get a resource bundle from the resource bundle path. Unlike getResourceBundle, this
142      * returns an 'unparented' bundle that exactly matches the bundle name and locale name.
143      */

144     public static ResourceBundle JavaDoc loadResourceBundle(String JavaDoc bundleName, ULocale locale) {
145         if (locale == null) {
146             locale = ULocale.getDefault();
147         }
148         return loadResourceBundle(bundleName, locale.getBaseName());
149     }
150
151     /**
152      * Get a resource bundle from the resource bundle path. Unlike getResourceBundle, this
153      * returns an 'unparented' bundle that exactly matches the bundle name and locale name.
154      */

155     public static ResourceBundle JavaDoc loadResourceBundle(String JavaDoc bundleName, String JavaDoc localeName) {
156         if (localeName != null && localeName.length() > 0) {
157             bundleName = bundleName + "_" + localeName;
158         }
159         String JavaDoc name = ICU_PACKAGE + "." + bundleName;
160         try {
161             if (name.indexOf("_zh_") == -1) { // DLF temporary hack
162
Class JavaDoc rbclass = Class.forName(name);
163                 ResourceBundle JavaDoc rb = (ResourceBundle JavaDoc)rbclass.newInstance();
164                 return rb;
165             }
166         }
167         catch (ClassNotFoundException JavaDoc e)
168         {
169             if (DEBUG) {
170                 System.out.println(name + " not found");
171             }
172             // ignore, keep looking
173
}
174         catch (Exception JavaDoc 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     // ========== privates ==========
188

189     // Flag for enabling/disabling debugging code
190
private static final boolean DEBUG = ICUDebug.enabled("localedata");
191
192     // Cache for getAvailableLocales
193
private static SoftReference JavaDoc GET_AVAILABLE_CACHE;
194
195     // Cache for ResourceBundle instantiation
196
private static SoftReference JavaDoc BUNDLE_CACHE;
197
198     private static ResourceBundle JavaDoc loadFromCache(String JavaDoc key) {
199         if (BUNDLE_CACHE != null) {
200             Map JavaDoc m = (Map JavaDoc)BUNDLE_CACHE.get();
201             if (m != null) {
202                 return (ResourceBundle JavaDoc)m.get(key);
203             }
204         }
205         return null;
206     }
207
208     private static void addToCache(String JavaDoc key, ResourceBundle JavaDoc b) {
209         Map JavaDoc m = null;
210         if (BUNDLE_CACHE != null) {
211             m = (Map JavaDoc)BUNDLE_CACHE.get();
212         }
213         if (m == null) {
214             m = new HashMap JavaDoc();
215             BUNDLE_CACHE = new SoftReference JavaDoc(m);
216         }
217         m.put(key, b);
218     }
219
220     // recursively build bundle
221
private static ResourceBundle JavaDoc instantiate(String JavaDoc name) {
222         ResourceBundle JavaDoc b = loadFromCache(name);
223         if (b == null) {
224             ResourceBundle JavaDoc parent = null;
225             int i = name.lastIndexOf('_');
226
227             // TODO: convert this to use ULocale
228
final Locale JavaDoc rootLocale = new Locale JavaDoc("", "", "");
229  
230             if (i != -1) {
231                 parent = instantiate(name.substring(0, i));
232             }
233             try {
234                 Locale JavaDoc 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 JavaDoc cl = ICULocaleData.class.getClassLoader();
243                 if (cl == null) {
244                     // we're on the bootstrap
245
cl = ClassLoader.getSystemClassLoader();
246                 }
247                 Class JavaDoc 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                     // System.out.println("iculistresourcebundle: " + name + " is " + b);
257
} else {
258                     b = ResourceBundle.getBundle(name.substring(0, i), locale);
259                     // System.out.println("resourcebundle: " + name + " is " + b);
260
}
261                 addToCache(name, b);
262             }
263             catch (ClassNotFoundException JavaDoc e) {
264
265                 int j = name.indexOf('_');
266                 int k = name.lastIndexOf('_');
267                 // if a bogus locale is passed then the parent should be
268
// the default locale not the root locale!
269
if(k==j && j!=-1){
270                     
271                     String JavaDoc locName = name.substring(j+1,name.length());
272                     String JavaDoc defaultName = ULocale.getDefault().toString();
273                     
274                     if(!locName.equals(rootLocale.toString()) &&
275                        defaultName.indexOf(locName)==-1){
276                         String JavaDoc bundle = name.substring(0,j);
277                         parent = instantiate(bundle+"_"+defaultName);
278                     }
279                 }
280                 b = parent;
281             }
282             catch (Exception JavaDoc e) {
283                 System.out.println("failure");
284                 System.out.println(e);
285             }
286         }
287         //if(b==null){
288
// throw new MissingResourceException("Could not load data "+name,"","");
289
//}
290
return b;
291     }
292
293     /**
294      * Still need permissions to use our own class loader, is there no way
295      * to load class resources from new locations that aren't already on the
296      * class path?
297      */

298     private synchronized static ResourceBundle JavaDoc instantiate(String JavaDoc name, String JavaDoc localeName) {
299         if (localeName.length() != 0) {
300             name = name + "_" + localeName;
301         }
302         ResourceBundle JavaDoc b = instantiate(name);
303         if(b==null){
304             throw new MissingResourceException JavaDoc("Could not load data "+name,"","");
305         }
306         return b;
307     }
308
309     private static Set JavaDoc createLocaleNameSet(String JavaDoc bundleName) {
310         try {
311             ResourceBundle JavaDoc index = getResourceBundle(bundleName, "index");
312             Object JavaDoc[][] localeStrings = (Object JavaDoc[][]) index.getObject("InstalledLocales");
313             String JavaDoc[] localeNames = new String JavaDoc[localeStrings.length];
314
315             // barf gag choke spit hack...
316
// since java's Locale 'fixes' the locale string for some locales,
317
// we have to fix our names to match, otherwise the Locale[] list
318
// won't match the locale name set. What were they thinking?!?
319
for (int i = 0; i < localeNames.length; ++i) {
320                 localeNames[i] = LocaleUtility.getLocaleFromName((String JavaDoc)localeStrings[i][0]).toString();
321             }
322
323             HashSet JavaDoc set = new HashSet JavaDoc();
324             set.addAll(Arrays.asList(localeNames));
325             return Collections.unmodifiableSet(set);
326         }
327         catch (MissingResourceException JavaDoc 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     /*
335     private static Locale[] createLocaleList(String bundleName) {
336         try {
337             ResourceBundle index = getResourceBundle(bundleName, "index");
338             Object[][] localeStrings = (Object[][]) index.getObject("InstalledLocales");
339             Locale[] locales = new Locale[localeStrings.length];
340             for (int i = 0; i < localeStrings.length; ++i) {
341                 locales[i] = LocaleUtility.getLocaleFromName((String)localeStrings[i][0]);
342             }
343             return locales;
344         }
345         catch (MissingResourceException e) {
346             if (DEBUG) System.out.println("couldn't find index for bundleName: " + bundleName);
347             Thread.dumpStack();
348         }
349         return new Locale[0];
350     }
351     */

352 }
353
Popular Tags