KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 ******************************************************************************
3 * Copyright (C) 2004-2006, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 ******************************************************************************
6 */

7
8 package com.ibm.icu.impl;
9
10 import java.io.InputStream JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.MissingResourceException JavaDoc;
13 import java.util.PropertyResourceBundle JavaDoc;
14 import java.util.ResourceBundle JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 import com.ibm.icu.util.ULocale;
18 import com.ibm.icu.util.UResourceBundle;
19
20 /**
21  * just a wrapper for Java ListResourceBundles and
22  * @author ram
23  *
24  */

25 public class ResourceBundleWrapper extends UResourceBundle {
26     private ResourceBundle JavaDoc bundle = null;
27     private String JavaDoc localeID = null;
28     private String JavaDoc baseName = null;
29     private Vector JavaDoc keys=null;
30     private int loadingStatus = -1;
31     
32     private ResourceBundleWrapper(ResourceBundle JavaDoc bundle){
33         this.bundle=bundle;
34     }
35
36     protected void setLoadingStatus(int newStatus){
37         loadingStatus = newStatus;
38     }
39     
40     protected Object JavaDoc handleGetObject(String JavaDoc key){
41         ResourceBundleWrapper current = this;
42         Object JavaDoc obj = null;
43         while(current!=null){
44             try{
45                 obj = current.bundle.getObject(key);
46                 break;
47             }catch(MissingResourceException JavaDoc ex){
48                 current = (ResourceBundleWrapper)current.getParent();
49             }
50         }
51         if (obj == null){
52             throw new MissingResourceException JavaDoc("Can't find resource for bundle "
53                                                +baseName
54                                                +", key "+key,
55                                                this.getClass().getName(),
56                                                key);
57         }
58         return obj;
59     }
60     
61     public Enumeration JavaDoc getKeys(){
62         return keys.elements();
63     }
64     
65     private void initKeysVector(){
66         ResourceBundleWrapper current = this;
67         keys = new Vector JavaDoc();
68         while(current!=null){
69             Enumeration JavaDoc e = current.bundle.getKeys();
70             while(e.hasMoreElements()){
71                 String JavaDoc elem = (String JavaDoc)e.nextElement();
72                 if(!keys.contains(elem)){
73                     keys.add(elem);
74                 }
75             }
76             current = (ResourceBundleWrapper)current.getParent();
77         }
78     }
79     protected String JavaDoc getLocaleID(){
80         return localeID;
81     }
82  
83     protected String JavaDoc getBaseName(){
84         return bundle.getClass().getName().replace('.','/');
85     }
86     
87     public ULocale getULocale(){
88         return new ULocale(localeID);
89     }
90     
91     public UResourceBundle getParent(){
92         return (UResourceBundle)parent;
93     }
94
95     // Flag for enabling/disabling debugging code
96
private static final boolean DEBUG = ICUDebug.enabled("resourceBundleWrapper");
97     
98     // This method is for super class's instantiateBundle method
99
public static UResourceBundle getBundleInstance(String JavaDoc baseName, String JavaDoc localeID,
100                                                     ClassLoader JavaDoc root, boolean disableFallback){
101         UResourceBundle b = instantiateBundle(baseName, localeID, root, disableFallback);
102         if(b==null){
103             String JavaDoc separator ="_";
104             if(baseName.indexOf('/')>=0){
105                 separator = "/";
106             }
107             throw new MissingResourceException JavaDoc("Could not find the bundle "+ baseName+separator+ localeID,"","");
108         }
109         return b;
110     }
111     // recursively build bundle and override the super-class method
112
protected static synchronized UResourceBundle instantiateBundle(String JavaDoc baseName, String JavaDoc localeID,
113                                                                     ClassLoader JavaDoc root, boolean disableFallback) {
114         if (root == null) {
115             // we're on the bootstrap
116
root = ClassLoader.getSystemClassLoader();
117         }
118         final ClassLoader JavaDoc cl = root;
119         String JavaDoc name = baseName;
120         ULocale defaultLocale = ULocale.getDefault();
121         if (localeID.length() != 0) {
122             name = name + "_" + localeID;
123         }
124
125         ResourceBundleWrapper b = (ResourceBundleWrapper)loadFromCache(cl, name, defaultLocale);
126         if(b==null){
127             ResourceBundleWrapper parent = null;
128             int i = localeID.lastIndexOf('_');
129     
130             if (i != -1) {
131                 String JavaDoc locName = localeID.substring(0, i);
132                 parent = (ResourceBundleWrapper)loadFromCache(cl, baseName+"_"+locName,defaultLocale);
133                 if(parent == null){
134                     parent = (ResourceBundleWrapper)instantiateBundle(baseName, locName , cl, disableFallback);
135                 }
136             }else if(localeID.length()>0){
137                 parent = (ResourceBundleWrapper)loadFromCache(cl, baseName,defaultLocale);
138                 if(parent==null){
139                     parent = (ResourceBundleWrapper)instantiateBundle(baseName, "", cl, disableFallback);
140                 }
141             }
142             try {
143                 Class JavaDoc cls = cl.loadClass(name);
144                 ResourceBundle JavaDoc bx = (ResourceBundle JavaDoc) cls.newInstance();
145                 b = new ResourceBundleWrapper(bx);
146                 if (parent != null) {
147                     b.setParent(parent);
148                 }
149                 b.baseName=baseName;
150                 b.localeID = localeID;
151     
152             } catch (ClassNotFoundException JavaDoc e) {
153                 
154                 final String JavaDoc resName = name.replace('.', '/') + ".properties";
155                 InputStream JavaDoc stream = (InputStream JavaDoc)java.security.AccessController.doPrivileged(
156                     new java.security.PrivilegedAction JavaDoc() {
157                         public Object JavaDoc run() {
158                             if (cl != null) {
159                                 return cl.getResourceAsStream(resName);
160                             } else {
161                                 return ClassLoader.getSystemResourceAsStream(resName);
162                             }
163                         }
164                     }
165                 );
166                 if (stream != null) {
167                     // make sure it is buffered
168
stream = new java.io.BufferedInputStream JavaDoc(stream);
169                     try {
170                         b = new ResourceBundleWrapper(new PropertyResourceBundle JavaDoc(stream));
171                         if (parent != null) {
172                             b.setParent(parent);
173                         }
174                         b.baseName=baseName;
175                         b.localeID=localeID;
176                     } catch (Exception JavaDoc ex) {
177                         // throw away exception
178
} finally {
179                         try {
180                             stream.close();
181                         } catch (Exception JavaDoc ex) {
182                             // throw away exception
183
}
184                     }
185                 }
186     
187                 // if a bogus locale is passed then the parent should be
188
// the default locale not the root locale!
189
if (b==null) {
190                     String JavaDoc defaultName = defaultLocale.toString();
191                     if (localeID.length()>0 && localeID.indexOf('_')< 0 && defaultName.indexOf(localeID) == -1) {
192                         b = (ResourceBundleWrapper)loadFromCache(cl,baseName+"_"+defaultName, defaultLocale);
193                         if(b==null){
194                             b = (ResourceBundleWrapper)instantiateBundle(baseName , defaultName, cl, disableFallback);
195                         }
196                     }
197                 }
198                 // if still could not find the bundle then return the parent
199
if(b==null){
200                     b=parent;
201                 }
202             } catch (Exception JavaDoc e) {
203                 if (DEBUG)
204                     System.out.println("failure");
205                 if (DEBUG)
206                     System.out.println(e);
207             }
208
209             addToCache(cl, name, defaultLocale, b);
210         }
211         if(b!=null){
212             b.initKeysVector();
213         }else{
214             if(DEBUG)System.out.println("Returning null for "+baseName+"_"+localeID);
215         }
216         
217         return b;
218     }
219 }
220
Popular Tags