2 ******************************************************************************
\r
3 * Copyright (C) 2004-2008, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 ******************************************************************************
\r
8 package com.ibm.icu.impl;
\r
10 import java.io.InputStream;
\r
11 import java.util.Enumeration;
\r
12 import java.util.MissingResourceException;
\r
13 import java.util.PropertyResourceBundle;
\r
14 import java.util.ResourceBundle;
\r
15 import java.util.Vector;
\r
17 import com.ibm.icu.util.ULocale;
\r
18 import com.ibm.icu.util.UResourceBundle;
\r
21 * just a wrapper for Java ListResourceBundles and
\r
25 public class ResourceBundleWrapper extends UResourceBundle {
\r
26 private ResourceBundle bundle = null;
\r
27 private String localeID = null;
\r
28 private String baseName = null;
\r
29 private Vector keys=null;
\r
30 // private int loadingStatus = -1;
\r
32 private ResourceBundleWrapper(ResourceBundle bundle){
\r
36 protected void setLoadingStatus(int newStatus){
\r
37 // loadingStatus = newStatus;
\r
40 protected Object handleGetObject(String aKey){
\r
41 ResourceBundleWrapper current = this;
\r
43 while(current!=null){
\r
45 obj = current.bundle.getObject(aKey);
\r
47 }catch(MissingResourceException ex){
\r
48 current = (ResourceBundleWrapper)current.getParent();
\r
52 throw new MissingResourceException("Can't find resource for bundle "
\r
55 this.getClass().getName(),
\r
61 public Enumeration getKeys(){
\r
62 return keys.elements();
\r
65 private void initKeysVector(){
\r
66 ResourceBundleWrapper current = this;
\r
67 keys = new Vector();
\r
68 while(current!=null){
\r
69 Enumeration e = current.bundle.getKeys();
\r
70 while(e.hasMoreElements()){
\r
71 String elem = (String)e.nextElement();
\r
72 if(!keys.contains(elem)){
\r
76 current = (ResourceBundleWrapper)current.getParent();
\r
79 protected String getLocaleID(){
\r
83 protected String getBaseName(){
\r
84 return bundle.getClass().getName().replace('.','/');
\r
87 public ULocale getULocale(){
\r
88 return new ULocale(localeID);
\r
91 public UResourceBundle getParent(){
\r
92 return (UResourceBundle)parent;
\r
95 // Flag for enabling/disabling debugging code
\r
96 private static final boolean DEBUG = ICUDebug.enabled("resourceBundleWrapper");
\r
98 // This method is for super class's instantiateBundle method
\r
99 public static UResourceBundle getBundleInstance(String baseName, String localeID,
\r
100 ClassLoader root, boolean disableFallback){
\r
101 UResourceBundle b = instantiateBundle(baseName, localeID, root, disableFallback);
\r
103 String separator ="_";
\r
104 if(baseName.indexOf('/')>=0){
\r
107 throw new MissingResourceException("Could not find the bundle "+ baseName+separator+ localeID,"","");
\r
111 // recursively build bundle and override the super-class method
\r
112 protected static synchronized UResourceBundle instantiateBundle(String baseName, String localeID,
\r
113 ClassLoader root, boolean disableFallback) {
\r
114 if (root == null) {
\r
115 // we're on the bootstrap
\r
116 root = ClassLoader.getSystemClassLoader();
\r
118 final ClassLoader cl = root;
\r
119 String name = baseName;
\r
120 ULocale defaultLocale = ULocale.getDefault();
\r
121 if (localeID.length() != 0) {
\r
122 name = name + "_" + localeID;
\r
125 ResourceBundleWrapper b = (ResourceBundleWrapper)loadFromCache(cl, name, defaultLocale);
\r
127 ResourceBundleWrapper parent = null;
\r
128 int i = localeID.lastIndexOf('_');
\r
131 String locName = localeID.substring(0, i);
\r
132 parent = (ResourceBundleWrapper)loadFromCache(cl, baseName+"_"+locName,defaultLocale);
\r
133 if(parent == null){
\r
134 parent = (ResourceBundleWrapper)instantiateBundle(baseName, locName , cl, disableFallback);
\r
136 }else if(localeID.length()>0){
\r
137 parent = (ResourceBundleWrapper)loadFromCache(cl, baseName,defaultLocale);
\r
139 parent = (ResourceBundleWrapper)instantiateBundle(baseName, "", cl, disableFallback);
\r
143 Class cls = cl.loadClass(name);
\r
144 ResourceBundle bx = (ResourceBundle) cls.newInstance();
\r
145 b = new ResourceBundleWrapper(bx);
\r
146 if (parent != null) {
\r
147 b.setParent(parent);
\r
149 b.baseName=baseName;
\r
150 b.localeID = localeID;
\r
152 } catch (ClassNotFoundException e) {
\r
154 final String resName = name.replace('.', '/') + ".properties";
\r
155 InputStream stream = (InputStream)java.security.AccessController.doPrivileged(
\r
156 new java.security.PrivilegedAction() {
\r
157 public Object run() {
\r
159 return cl.getResourceAsStream(resName);
\r
161 return ClassLoader.getSystemResourceAsStream(resName);
\r
166 if (stream != null) {
\r
167 // make sure it is buffered
\r
168 stream = new java.io.BufferedInputStream(stream);
\r
170 b = new ResourceBundleWrapper(new PropertyResourceBundle(stream));
\r
171 if (parent != null) {
\r
172 b.setParent(parent);
\r
174 b.baseName=baseName;
\r
175 b.localeID=localeID;
\r
176 } catch (Exception ex) {
\r
177 // throw away exception
\r
181 } catch (Exception ex) {
\r
182 // throw away exception
\r
187 // if a bogus locale is passed then the parent should be
\r
188 // the default locale not the root locale!
\r
190 String defaultName = defaultLocale.toString();
\r
191 if (localeID.length()>0 && localeID.indexOf('_')< 0 && defaultName.indexOf(localeID) == -1) {
\r
192 b = (ResourceBundleWrapper)loadFromCache(cl,baseName+"_"+defaultName, defaultLocale);
\r
194 b = (ResourceBundleWrapper)instantiateBundle(baseName , defaultName, cl, disableFallback);
\r
198 // if still could not find the bundle then return the parent
\r
202 } catch (Exception e) {
\r
204 System.out.println("failure");
\r
206 System.out.println(e);
\r
209 addToCache(cl, name, defaultLocale, b);
\r
212 b.initKeysVector();
\r
214 if(DEBUG)System.out.println("Returning null for "+baseName+"_"+localeID);
\r