1 16 package com.google.gwt.i18n.rebind; 17 18 import com.google.gwt.core.ext.BadPropertyValueException; 19 import com.google.gwt.core.ext.Generator; 20 import com.google.gwt.core.ext.GeneratorContext; 21 import com.google.gwt.core.ext.PropertyOracle; 22 import com.google.gwt.core.ext.TreeLogger; 23 import com.google.gwt.core.ext.UnableToCompleteException; 24 import com.google.gwt.core.ext.typeinfo.JClassType; 25 import com.google.gwt.core.ext.typeinfo.NotFoundException; 26 import com.google.gwt.core.ext.typeinfo.TypeOracle; 27 import com.google.gwt.i18n.client.Constants; 28 import com.google.gwt.i18n.client.ConstantsWithLookup; 29 import com.google.gwt.i18n.client.Messages; 30 import com.google.gwt.i18n.rebind.util.ResourceFactory; 31 32 import java.util.Locale ; 33 34 38 public class LocalizableGenerator extends Generator { 39 42 public static final String GWT_KEY = "gwt.key"; 43 44 static final String CONSTANTS_NAME = Constants.class.getName(); 45 46 static final String CONSTANTS_WITH_LOOKUP_NAME = ConstantsWithLookup.class.getName(); 47 48 51 static final String DEFAULT_TOKEN = "default"; 52 static final String MESSAGES_NAME = Messages.class.getName(); 53 private static long lastReloadCount = -1; 54 57 private static final String PROP_LOCALE = "locale"; 58 59 private LocalizableLinkageCreator linkageCreator = new LocalizableLinkageCreator(); 60 61 70 public final String generate(TreeLogger logger, GeneratorContext context, 71 String typeName) throws UnableToCompleteException { 72 TypeOracle typeOracle = context.getTypeOracle(); 74 if (lastReloadCount != typeOracle.getReloadCount()) { 75 ResourceFactory.clearCache(); 76 lastReloadCount = typeOracle.getReloadCount(); 77 } 78 79 PropertyOracle propertyOracle = context.getPropertyOracle(); 81 Locale locale; 82 try { 83 String localeID = propertyOracle.getPropertyValue(logger, PROP_LOCALE); 84 if ("default".equals(localeID)) { 85 locale = null; 86 } else { 87 String [] localeChunks = localeID.split("_"); 88 if (localeChunks.length > 0) { 89 if (!localeChunks[0].equals(localeChunks[0].toLowerCase())) { 90 logger.log(TreeLogger.ERROR, localeID 91 + "'s language code should be lower case", null); 92 throw new UnableToCompleteException(); 93 } 94 } 95 if (localeChunks.length == 1) { 96 locale = new Locale (localeChunks[0]); 97 } else if (localeChunks.length == 2) { 98 locale = new Locale (localeChunks[0], localeChunks[1]); 101 } else if (localeChunks.length == 3) { 102 locale = new Locale (localeChunks[0], localeChunks[1], localeChunks[2]); 103 } else { 104 logger.log(TreeLogger.ERROR, localeID 105 + " is not a correctly formatted locale", null); 106 throw new UnableToCompleteException(); 107 } 108 } 109 } catch (BadPropertyValueException e) { 110 logger.log(TreeLogger.ERROR, "Could not parse specified locale", e); 111 throw new UnableToCompleteException(); 112 } 113 114 JClassType targetClass; 115 try { 116 targetClass = typeOracle.getType(typeName); 117 } catch (NotFoundException e) { 118 logger.log(TreeLogger.ERROR, "No such type", e); 119 throw new UnableToCompleteException(); 120 } 121 122 String generatedClass = AbstractLocalizableImplCreator.generateConstantOrMessageClass( 124 logger, context, locale, targetClass); 125 if (generatedClass != null) { 126 return generatedClass; 127 } 128 return linkageCreator.linkWithImplClass(logger, targetClass, locale); 129 } 130 } 131 | Popular Tags |