1 16 package com.google.gwt.i18n.rebind; 17 18 import com.google.gwt.core.ext.TreeLogger; 19 import com.google.gwt.core.ext.UnableToCompleteException; 20 import com.google.gwt.core.ext.typeinfo.JClassType; 21 import com.google.gwt.user.rebind.AbstractSourceCreator; 22 23 import java.util.HashMap ; 24 import java.util.Locale ; 25 import java.util.Map ; 26 27 30 class LocalizableLinkageCreator extends AbstractSourceCreator { 31 private static Map findDerivedClasses(TreeLogger logger, JClassType baseClass) 32 throws UnableToCompleteException { 33 Map matchingClasses = new HashMap (); 35 if (baseClass.isInterface() == null && baseClass.isAbstract() == false) { 37 matchingClasses.put(LocalizableGenerator.DEFAULT_TOKEN, baseClass); 38 } 39 String baseName = baseClass.getSimpleSourceName(); 40 41 JClassType[] x = baseClass.getSubtypes(); 43 for (int i = 0; i < x.length; i++) { 44 JClassType subType = x[i]; 45 if ((subType.isInterface() == null) && (subType.isAbstract() == false)) { 46 String name = subType.getSimpleSourceName(); 47 int localeIndex = name.indexOf("_"); 49 String subTypeBaseName = name; 50 if (localeIndex != -1) { 51 subTypeBaseName = name.substring(0, localeIndex); 52 } 53 boolean matches = subTypeBaseName.equals(baseName); 54 if (matches) { 55 boolean isDefault = localeIndex == -1 56 || localeIndex == name.length() - 1; 57 if (isDefault) { 58 JClassType defaultClass = (JClassType) matchingClasses.get(LocalizableGenerator.DEFAULT_TOKEN); 60 if (defaultClass != null) { 61 throw error(logger, defaultClass + " and " + baseName 62 + " are both potencial default classes for " + baseClass); 63 } else { 64 matchingClasses.put(LocalizableGenerator.DEFAULT_TOKEN, subType); 65 } 66 } else { 67 String localeSubString = name.substring(localeIndex + 1); 70 JClassType dopClass = (JClassType) matchingClasses.get(localeSubString); 71 if (dopClass != null) { 72 throw error(logger, dopClass.getQualifiedSourceName() + " and " 73 + subType.getQualifiedSourceName() 74 + " are both potential matches to " + baseClass 75 + " in locale" + localeSubString); 76 } 77 matchingClasses.put(localeSubString, subType); 78 } 79 } 80 } 81 } 82 return matchingClasses; 83 } 84 85 88 private final Map implCache = new HashMap (); 90 91 103 String linkWithImplClass(TreeLogger logger, JClassType baseClass, 104 Locale locale) throws UnableToCompleteException { 105 106 String baseName = baseClass.getQualifiedSourceName(); 107 111 String className = (String ) implCache.get(baseName + locale); 112 if (className != null) { 113 return className; 114 } 115 116 if (baseClass.getName().indexOf("_") == 0) { 117 throw error(logger, "Cannot have a '_' in the base localizable class " 118 + baseClass); 119 } 120 Map matchingClasses = findDerivedClasses(logger, baseClass); 121 String localeSuffix; 123 JClassType result = null; 124 if (locale == null) { 125 localeSuffix = LocalizableGenerator.DEFAULT_TOKEN; 126 } else { 127 localeSuffix = locale.toString(); 128 } 129 while (true) { 130 result = (JClassType) matchingClasses.get(localeSuffix); 132 if (result != null) { 133 break; 134 } 135 int strip = localeSuffix.lastIndexOf("_"); 137 if (localeSuffix == LocalizableGenerator.DEFAULT_TOKEN) { 138 throw error(logger, "Cannot find a class to bind to argument type " 140 + baseClass.getQualifiedSourceName()); 141 } else if (strip == -1) { 142 localeSuffix = LocalizableGenerator.DEFAULT_TOKEN; 144 } else { 145 localeSuffix = localeSuffix.substring(0, strip); 147 } 148 } 149 implCache.put(baseName + locale, className); 150 return result.getQualifiedSourceName(); 151 } 152 } 153 | Popular Tags |