KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > i18n > rebind > LocalizableGenerator


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

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 JavaDoc;
33
34 /**
35  * Generator used to bind classes extending the <code>Localizable</code> and
36  * <code>Constants</code> interfaces.
37  */

38 public class LocalizableGenerator extends Generator {
39   /**
40    * GWT method to override default use of method name as resource key.
41    */

42   public static final String JavaDoc GWT_KEY = "gwt.key";
43
44   static final String JavaDoc CONSTANTS_NAME = Constants.class.getName();
45
46   static final String JavaDoc CONSTANTS_WITH_LOOKUP_NAME = ConstantsWithLookup.class.getName();
47
48   /**
49    * Represents default locale.
50    */

51   static final String JavaDoc DEFAULT_TOKEN = "default";
52   static final String JavaDoc MESSAGES_NAME = Messages.class.getName();
53   private static long lastReloadCount = -1;
54   /**
55    * The token representing the locale property controlling Localization.
56    */

57   private static final String JavaDoc PROP_LOCALE = "locale";
58
59   private LocalizableLinkageCreator linkageCreator = new LocalizableLinkageCreator();
60
61   /**
62    * Generate an implementation for the given type.
63    *
64    * @param logger error logger
65    * @param context generator context
66    * @param typeName target type name
67    * @return generated class name
68    * @throws UnableToCompleteException
69    */

70   public final String JavaDoc generate(TreeLogger logger, GeneratorContext context,
71       String JavaDoc typeName) throws UnableToCompleteException {
72     // Clear cache if reset was done.
73
TypeOracle typeOracle = context.getTypeOracle();
74     if (lastReloadCount != typeOracle.getReloadCount()) {
75       ResourceFactory.clearCache();
76       lastReloadCount = typeOracle.getReloadCount();
77     }
78
79     // Get the current locale and interface type.
80
PropertyOracle propertyOracle = context.getPropertyOracle();
81     Locale JavaDoc locale;
82     try {
83       String JavaDoc localeID = propertyOracle.getPropertyValue(logger, PROP_LOCALE);
84       if ("default".equals(localeID)) {
85         locale = null;
86       } else {
87         String JavaDoc[] 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 JavaDoc(localeChunks[0]);
97         } else if (localeChunks.length == 2) {
98           // Ignore the localized locale string if present, just use language
99
// and country.
100
locale = new Locale JavaDoc(localeChunks[0], localeChunks[1]);
101         } else if (localeChunks.length == 3) {
102           locale = new Locale JavaDoc(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     // Link current locale and interface type to correct implementation class.
123
String JavaDoc 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