2 ******************************************************************************
3 * Copyright (C) 2004-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 ******************************************************************************
8 package com.ibm.icu.impl;
10 import java.io.InputStream;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Enumeration;
14 import java.util.List;
15 import java.util.MissingResourceException;
16 import java.util.PropertyResourceBundle;
17 import java.util.ResourceBundle;
19 import com.ibm.icu.util.ULocale;
20 import com.ibm.icu.util.UResourceBundle;
23 * just a wrapper for Java ListResourceBundles and
27 public class ResourceBundleWrapper extends UResourceBundle {
28 private ResourceBundle bundle = null;
29 private String localeID = null;
30 private String baseName = null;
31 private List<String> keys = null;
32 // private int loadingStatus = -1;
34 private ResourceBundleWrapper(ResourceBundle bundle){
38 protected void setLoadingStatus(int newStatus){
39 // loadingStatus = newStatus;
42 protected Object handleGetObject(String aKey){
43 ResourceBundleWrapper current = this;
47 obj = current.bundle.getObject(aKey);
49 }catch(MissingResourceException ex){
50 current = (ResourceBundleWrapper)current.getParent();
54 throw new MissingResourceException("Can't find resource for bundle "
57 this.getClass().getName(),
63 public Enumeration<String> getKeys(){
64 return Collections.enumeration(keys);
67 private void initKeysVector(){
68 ResourceBundleWrapper current = this;
69 keys = new ArrayList<String>();
71 Enumeration<String> e = current.bundle.getKeys();
72 while(e.hasMoreElements()){
73 String elem = e.nextElement();
74 if(!keys.contains(elem)){
78 current = (ResourceBundleWrapper)current.getParent();
81 protected String getLocaleID(){
85 protected String getBaseName(){
86 return bundle.getClass().getName().replace('.','/');
89 public ULocale getULocale(){
90 return new ULocale(localeID);
93 public UResourceBundle getParent(){
94 return (UResourceBundle)parent;
97 // Flag for enabling/disabling debugging code
98 private static final boolean DEBUG = ICUDebug.enabled("resourceBundleWrapper");
100 // This method is for super class's instantiateBundle method
101 public static UResourceBundle getBundleInstance(String baseName, String localeID,
102 ClassLoader root, boolean disableFallback){
103 UResourceBundle b = instantiateBundle(baseName, localeID, root, disableFallback);
105 String separator ="_";
106 if(baseName.indexOf('/')>=0){
109 throw new MissingResourceException("Could not find the bundle "+ baseName+separator+ localeID,"","");
113 // recursively build bundle and override the super-class method
114 protected static synchronized UResourceBundle instantiateBundle(String baseName, String localeID,
115 ClassLoader root, boolean disableFallback) {
117 root = Utility.getFallbackClassLoader();
119 final ClassLoader cl = root;
120 String name = baseName;
121 ULocale defaultLocale = ULocale.getDefault();
122 if (localeID.length() != 0) {
123 name = name + "_" + localeID;
126 ResourceBundleWrapper b = (ResourceBundleWrapper)loadFromCache(cl, name, defaultLocale);
128 ResourceBundleWrapper parent = null;
129 int i = localeID.lastIndexOf('_');
131 boolean loadFromProperties = false;
133 String locName = localeID.substring(0, i);
134 parent = (ResourceBundleWrapper)loadFromCache(cl, baseName+"_"+locName,defaultLocale);
136 parent = (ResourceBundleWrapper)instantiateBundle(baseName, locName , cl, disableFallback);
138 }else if(localeID.length()>0){
139 parent = (ResourceBundleWrapper)loadFromCache(cl, baseName,defaultLocale);
141 parent = (ResourceBundleWrapper)instantiateBundle(baseName, "", cl, disableFallback);
145 Class<? extends ResourceBundle> cls = cl.loadClass(name).asSubclass(ResourceBundle.class);
146 ResourceBundle bx = cls.newInstance();
147 b = new ResourceBundleWrapper(bx);
148 if (parent != null) {
152 b.localeID = localeID;
154 } catch (ClassNotFoundException e) {
155 loadFromProperties = true;
156 } catch (NoClassDefFoundError e) {
157 loadFromProperties = true;
158 } catch (Exception e) {
160 System.out.println("failure");
162 System.out.println(e);
165 if (loadFromProperties) {
167 final String resName = name.replace('.', '/') + ".properties";
168 InputStream stream = java.security.AccessController.doPrivileged(
169 new java.security.PrivilegedAction<InputStream>() {
170 public InputStream run() {
172 return cl.getResourceAsStream(resName);
174 return ClassLoader.getSystemResourceAsStream(resName);
179 if (stream != null) {
180 // make sure it is buffered
181 stream = new java.io.BufferedInputStream(stream);
183 b = new ResourceBundleWrapper(new PropertyResourceBundle(stream));
184 if (parent != null) {
189 } catch (Exception ex) {
190 // throw away exception
194 } catch (Exception ex) {
195 // throw away exception
200 // if a bogus locale is passed then the parent should be
201 // the default locale not the root locale!
203 String defaultName = defaultLocale.toString();
204 if (localeID.length()>0 && localeID.indexOf('_')< 0 && defaultName.indexOf(localeID) == -1) {
205 b = (ResourceBundleWrapper)loadFromCache(cl,baseName+"_"+defaultName, defaultLocale);
207 b = (ResourceBundleWrapper)instantiateBundle(baseName , defaultName, cl, disableFallback);
211 // if still could not find the bundle then return the parent
215 } catch (Exception e) {
217 System.out.println("failure");
219 System.out.println(e);
222 b = (ResourceBundleWrapper)addToCache(cl, name, defaultLocale, b);
228 if(DEBUG)System.out.println("Returning null for "+baseName+"_"+localeID);