001: /*
002: ******************************************************************************
003: * Copyright (C) 2004-2006, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: ******************************************************************************
006: */
007:
008: package com.ibm.icu.impl;
009:
010: import java.io.InputStream;
011: import java.util.Enumeration;
012: import java.util.MissingResourceException;
013: import java.util.PropertyResourceBundle;
014: import java.util.ResourceBundle;
015: import java.util.Vector;
016:
017: import com.ibm.icu.util.ULocale;
018: import com.ibm.icu.util.UResourceBundle;
019:
020: /**
021: * just a wrapper for Java ListResourceBundles and
022: * @author ram
023: *
024: */
025: public class ResourceBundleWrapper extends UResourceBundle {
026: private ResourceBundle bundle = null;
027: private String localeID = null;
028: private String baseName = null;
029: private Vector keys = null;
030: private int loadingStatus = -1;
031:
032: private ResourceBundleWrapper(ResourceBundle bundle) {
033: this .bundle = bundle;
034: }
035:
036: protected void setLoadingStatus(int newStatus) {
037: loadingStatus = newStatus;
038: }
039:
040: protected Object handleGetObject(String key) {
041: ResourceBundleWrapper current = this ;
042: Object obj = null;
043: while (current != null) {
044: try {
045: obj = current.bundle.getObject(key);
046: break;
047: } catch (MissingResourceException ex) {
048: current = (ResourceBundleWrapper) current.getParent();
049: }
050: }
051: if (obj == null) {
052: throw new MissingResourceException(
053: "Can't find resource for bundle " + baseName
054: + ", key " + key,
055: this .getClass().getName(), key);
056: }
057: return obj;
058: }
059:
060: public Enumeration getKeys() {
061: return keys.elements();
062: }
063:
064: private void initKeysVector() {
065: ResourceBundleWrapper current = this ;
066: keys = new Vector();
067: while (current != null) {
068: Enumeration e = current.bundle.getKeys();
069: while (e.hasMoreElements()) {
070: String elem = (String) e.nextElement();
071: if (!keys.contains(elem)) {
072: keys.add(elem);
073: }
074: }
075: current = (ResourceBundleWrapper) current.getParent();
076: }
077: }
078:
079: protected String getLocaleID() {
080: return localeID;
081: }
082:
083: protected String getBaseName() {
084: return bundle.getClass().getName().replace('.', '/');
085: }
086:
087: public ULocale getULocale() {
088: return new ULocale(localeID);
089: }
090:
091: public UResourceBundle getParent() {
092: return (UResourceBundle) parent;
093: }
094:
095: // Flag for enabling/disabling debugging code
096: private static final boolean DEBUG = ICUDebug
097: .enabled("resourceBundleWrapper");
098:
099: // This method is for super class's instantiateBundle method
100: public static UResourceBundle getBundleInstance(String baseName,
101: String localeID, ClassLoader root, boolean disableFallback) {
102: UResourceBundle b = instantiateBundle(baseName, localeID, root,
103: disableFallback);
104: if (b == null) {
105: String separator = "_";
106: if (baseName.indexOf('/') >= 0) {
107: separator = "/";
108: }
109: throw new MissingResourceException(
110: "Could not find the bundle " + baseName + separator
111: + localeID, "", "");
112: }
113: return b;
114: }
115:
116: // recursively build bundle and override the super-class method
117: protected static synchronized UResourceBundle instantiateBundle(
118: String baseName, String localeID, ClassLoader root,
119: boolean disableFallback) {
120: if (root == null) {
121: // we're on the bootstrap
122: root = ClassLoader.getSystemClassLoader();
123: }
124: final ClassLoader cl = root;
125: String name = baseName;
126: ULocale defaultLocale = ULocale.getDefault();
127: if (localeID.length() != 0) {
128: name = name + "_" + localeID;
129: }
130:
131: ResourceBundleWrapper b = (ResourceBundleWrapper) loadFromCache(
132: cl, name, defaultLocale);
133: if (b == null) {
134: ResourceBundleWrapper parent = null;
135: int i = localeID.lastIndexOf('_');
136:
137: if (i != -1) {
138: String locName = localeID.substring(0, i);
139: parent = (ResourceBundleWrapper) loadFromCache(cl,
140: baseName + "_" + locName, defaultLocale);
141: if (parent == null) {
142: parent = (ResourceBundleWrapper) instantiateBundle(
143: baseName, locName, cl, disableFallback);
144: }
145: } else if (localeID.length() > 0) {
146: parent = (ResourceBundleWrapper) loadFromCache(cl,
147: baseName, defaultLocale);
148: if (parent == null) {
149: parent = (ResourceBundleWrapper) instantiateBundle(
150: baseName, "", cl, disableFallback);
151: }
152: }
153: try {
154: Class cls = cl.loadClass(name);
155: ResourceBundle bx = (ResourceBundle) cls.newInstance();
156: b = new ResourceBundleWrapper(bx);
157: if (parent != null) {
158: b.setParent(parent);
159: }
160: b.baseName = baseName;
161: b.localeID = localeID;
162:
163: } catch (ClassNotFoundException e) {
164:
165: final String resName = name.replace('.', '/')
166: + ".properties";
167: InputStream stream = (InputStream) java.security.AccessController
168: .doPrivileged(new java.security.PrivilegedAction() {
169: public Object run() {
170: if (cl != null) {
171: return cl
172: .getResourceAsStream(resName);
173: } else {
174: return ClassLoader
175: .getSystemResourceAsStream(resName);
176: }
177: }
178: });
179: if (stream != null) {
180: // make sure it is buffered
181: stream = new java.io.BufferedInputStream(stream);
182: try {
183: b = new ResourceBundleWrapper(
184: new PropertyResourceBundle(stream));
185: if (parent != null) {
186: b.setParent(parent);
187: }
188: b.baseName = baseName;
189: b.localeID = localeID;
190: } catch (Exception ex) {
191: // throw away exception
192: } finally {
193: try {
194: stream.close();
195: } catch (Exception ex) {
196: // throw away exception
197: }
198: }
199: }
200:
201: // if a bogus locale is passed then the parent should be
202: // the default locale not the root locale!
203: if (b == null) {
204: String defaultName = defaultLocale.toString();
205: if (localeID.length() > 0
206: && localeID.indexOf('_') < 0
207: && defaultName.indexOf(localeID) == -1) {
208: b = (ResourceBundleWrapper) loadFromCache(cl,
209: baseName + "_" + defaultName,
210: defaultLocale);
211: if (b == null) {
212: b = (ResourceBundleWrapper) instantiateBundle(
213: baseName, defaultName, cl,
214: disableFallback);
215: }
216: }
217: }
218: // if still could not find the bundle then return the parent
219: if (b == null) {
220: b = parent;
221: }
222: } catch (Exception e) {
223: if (DEBUG)
224: System.out.println("failure");
225: if (DEBUG)
226: System.out.println(e);
227: }
228:
229: addToCache(cl, name, defaultLocale, b);
230: }
231: if (b != null) {
232: b.initKeysVector();
233: } else {
234: if (DEBUG)
235: System.out.println("Returning null for " + baseName
236: + "_" + localeID);
237: }
238:
239: return b;
240: }
241: }
|