1 7 8 package com.ibm.icu.impl; 9 10 import java.io.InputStream ; 11 import java.util.Enumeration ; 12 import java.util.MissingResourceException ; 13 import java.util.PropertyResourceBundle ; 14 import java.util.ResourceBundle ; 15 import java.util.Vector ; 16 17 import com.ibm.icu.util.ULocale; 18 import com.ibm.icu.util.UResourceBundle; 19 20 25 public class ResourceBundleWrapper extends UResourceBundle { 26 private ResourceBundle bundle = null; 27 private String localeID = null; 28 private String baseName = null; 29 private Vector keys=null; 30 private int loadingStatus = -1; 31 32 private ResourceBundleWrapper(ResourceBundle bundle){ 33 this.bundle=bundle; 34 } 35 36 protected void setLoadingStatus(int newStatus){ 37 loadingStatus = newStatus; 38 } 39 40 protected Object handleGetObject(String key){ 41 ResourceBundleWrapper current = this; 42 Object obj = null; 43 while(current!=null){ 44 try{ 45 obj = current.bundle.getObject(key); 46 break; 47 }catch(MissingResourceException ex){ 48 current = (ResourceBundleWrapper)current.getParent(); 49 } 50 } 51 if (obj == null){ 52 throw new MissingResourceException ("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 getKeys(){ 62 return keys.elements(); 63 } 64 65 private void initKeysVector(){ 66 ResourceBundleWrapper current = this; 67 keys = new Vector (); 68 while(current!=null){ 69 Enumeration e = current.bundle.getKeys(); 70 while(e.hasMoreElements()){ 71 String elem = (String )e.nextElement(); 72 if(!keys.contains(elem)){ 73 keys.add(elem); 74 } 75 } 76 current = (ResourceBundleWrapper)current.getParent(); 77 } 78 } 79 protected String getLocaleID(){ 80 return localeID; 81 } 82 83 protected String 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 private static final boolean DEBUG = ICUDebug.enabled("resourceBundleWrapper"); 97 98 public static UResourceBundle getBundleInstance(String baseName, String localeID, 100 ClassLoader root, boolean disableFallback){ 101 UResourceBundle b = instantiateBundle(baseName, localeID, root, disableFallback); 102 if(b==null){ 103 String separator ="_"; 104 if(baseName.indexOf('/')>=0){ 105 separator = "/"; 106 } 107 throw new MissingResourceException ("Could not find the bundle "+ baseName+separator+ localeID,"",""); 108 } 109 return b; 110 } 111 protected static synchronized UResourceBundle instantiateBundle(String baseName, String localeID, 113 ClassLoader root, boolean disableFallback) { 114 if (root == null) { 115 root = ClassLoader.getSystemClassLoader(); 117 } 118 final ClassLoader cl = root; 119 String 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 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 cls = cl.loadClass(name); 144 ResourceBundle bx = (ResourceBundle ) 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 e) { 153 154 final String resName = name.replace('.', '/') + ".properties"; 155 InputStream stream = (InputStream )java.security.AccessController.doPrivileged( 156 new java.security.PrivilegedAction () { 157 public Object 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 stream = new java.io.BufferedInputStream (stream); 169 try { 170 b = new ResourceBundleWrapper(new PropertyResourceBundle (stream)); 171 if (parent != null) { 172 b.setParent(parent); 173 } 174 b.baseName=baseName; 175 b.localeID=localeID; 176 } catch (Exception ex) { 177 } finally { 179 try { 180 stream.close(); 181 } catch (Exception ex) { 182 } 184 } 185 } 186 187 if (b==null) { 190 String 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(b==null){ 200 b=parent; 201 } 202 } catch (Exception 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 |