1 16 package com.google.gwt.i18n.rebind.util; 17 18 import com.google.gwt.core.ext.typeinfo.JClassType; 19 20 import java.io.InputStream ; 21 import java.util.ArrayList ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Locale ; 25 import java.util.Map ; 26 import java.util.MissingResourceException ; 27 import java.util.Set ; 28 29 32 public abstract class ResourceFactory { 33 static class SimplePathTree extends AbstractPathTree { 34 String path; 35 36 SimplePathTree(String path) { 37 this.path = path; 38 } 39 40 public AbstractPathTree getChild(int i) { 41 throw new UnsupportedOperationException ( 42 "Simple paths have no children, therefore cannot get child: " + i); 43 } 44 45 public String getPath() { 46 return path; 47 } 48 49 public int numChildren() { 50 return 0; 51 } 52 } 53 54 private abstract static class AbstractPathTree { 55 abstract AbstractPathTree getChild(int i); 56 57 abstract String getPath(); 58 59 abstract int numChildren(); 60 } 61 62 private static class ClassPathTree extends AbstractPathTree { 63 Class javaInterface; 64 65 ClassPathTree(Class javaInterface) { 66 this.javaInterface = javaInterface; 67 } 68 69 AbstractPathTree getChild(int i) { 70 return new ClassPathTree(javaInterface.getInterfaces()[i]); 72 } 73 74 String getPath() { 75 return javaInterface.getName(); 76 } 77 78 int numChildren() { 79 return javaInterface.getInterfaces().length; 80 } 81 } 82 83 private static class JClassTypePathTree extends AbstractPathTree { 84 JClassType javaInterface; 85 86 JClassTypePathTree(JClassType javaInterface) { 87 this.javaInterface = javaInterface; 88 } 89 90 AbstractPathTree getChild(int i) { 91 return new JClassTypePathTree(javaInterface.getImplementedInterfaces()[i]); 93 } 94 95 101 String getPath() { 102 String name = getResourceName(javaInterface); 103 String packageName = javaInterface.getPackage().getName(); 104 return packageName + "." + name; 105 } 106 107 int numChildren() { 108 return javaInterface.getImplementedInterfaces().length; 109 } 110 } 111 112 public static final AbstractResource NOT_FOUND = new AbstractResource() { 113 114 void addToKeySet(Set s) { 115 throw new IllegalStateException ("Not found resource"); 116 } 117 118 Object handleGetObject(String key) { 119 throw new IllegalStateException ("Not found resource"); 120 } 121 }; 122 123 private static Map cache = new HashMap (); 124 125 private static List loaders = new ArrayList (); 126 static { 127 loaders.add(new LocalizedPropertiesResource.Factory()); 128 } 129 130 133 public static void clearCache() { 134 cache.clear(); 135 } 136 137 144 public static AbstractResource getBundle(Class javaInterface, Locale locale) { 145 if (javaInterface.isInterface() == false) { 146 throw new IllegalArgumentException (javaInterface 147 + " should be an interface."); 148 } 149 ClassPathTree path = new ClassPathTree(javaInterface); 150 return getBundleAux(path, locale, true); 151 } 152 153 160 public static AbstractResource getBundle(JClassType javaInterface, 161 Locale locale) { 162 return getBundleAux(new JClassTypePathTree(javaInterface), locale, true); 163 } 164 165 172 public static AbstractResource getBundle(String path, Locale locale) { 173 return getBundleAux(new SimplePathTree(path), locale, true); 174 } 175 176 public static String getResourceName(JClassType targetClass) { 177 String name = targetClass.getName(); 178 if (targetClass.isMemberType()) { 179 name = name.replace('.', '$'); 180 } 181 return name; 182 } 183 184 private static List findAlternativeParents( 185 ResourceFactory.AbstractPathTree tree, Locale locale) { 186 List altParents = null; 187 if (tree != null) { 188 altParents = new ArrayList (); 189 for (int i = 0; i < tree.numChildren(); i++) { 190 ResourceFactory.AbstractPathTree child = tree.getChild(i); 191 AbstractResource altParent = getBundleAux(child, locale, false); 192 if (altParent != null) { 193 altParents.add(altParent); 194 } 195 } 196 } 197 return altParents; 198 } 199 200 private static AbstractResource findPrimaryParent( 201 ResourceFactory.AbstractPathTree tree, Locale locale) { 202 AbstractResource parent = null; 204 205 if (locale != null) { 207 if (!("".equals(locale.getVariant()))) { 208 parent = getBundleAux(tree, new Locale (locale.getLanguage(), 210 locale.getCountry()), false); 211 } else if (!"".equals(locale.getCountry())) { 212 parent = getBundleAux(tree, new Locale (locale.getLanguage()), false); 213 } 214 215 if (parent == null) { 218 parent = getBundleAux(tree, null, false); 219 } 220 } 221 return parent; 222 } 223 224 private static AbstractResource getBundleAux( 225 ResourceFactory.AbstractPathTree tree, Locale locale, boolean required) { 226 String targetPath = tree.getPath(); 227 ClassLoader loader = AbstractResource.class.getClassLoader(); 228 String localizedPath = targetPath; 230 if (locale != null) { 231 localizedPath = targetPath + "_" + locale; 232 } 233 AbstractResource result = (AbstractResource) cache.get(localizedPath); 234 if (result != null) { 235 if (result == NOT_FOUND) { 236 return null; 237 } else { 238 return result; 239 } 240 } 241 String partualPath = localizedPath.replace('.', '/'); 242 AbstractResource parent = findPrimaryParent(tree, locale); 243 List altParents = findAlternativeParents(tree, locale); 244 245 AbstractResource found = null; 246 for (int i = 0; i < loaders.size(); i++) { 247 ResourceFactory element = (ResourceFactory) loaders.get(i); 248 String path = partualPath + "." + element.getExt(); 249 InputStream m = loader.getResourceAsStream(path); 250 if (m != null) { 251 found = element.load(m); 252 found.setPath(partualPath); 253 found.setPrimaryParent(parent); 254 found.setLocale(locale); 255 for (int j = 0; j < altParents.size(); j++) { 256 AbstractResource altParent = (AbstractResource) altParents.get(j); 257 found.addAlternativeParent(altParent); 258 } 259 found.checkKeys(); 260 break; 261 } 262 } 263 if (found == null) { 264 if (parent != null) { 265 found = parent; 266 } else { 267 found = NOT_FOUND; 268 } 269 found = parent; 270 } 271 272 cache.put(localizedPath, found); 273 if (found == null && required) { 274 throw new MissingResourceException ( 275 "Could not find any resource associated with " + tree.getPath(), 276 null, null); 277 } 278 return found; 279 } 280 281 abstract String getExt(); 282 283 abstract AbstractResource load(InputStream m); 284 } 285 | Popular Tags |