KickJava   Java API By Example, From Geeks To Geeks.

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


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.TreeLogger;
19 import com.google.gwt.core.ext.UnableToCompleteException;
20 import com.google.gwt.core.ext.typeinfo.JMethod;
21 import com.google.gwt.user.rebind.AbstractGeneratorClassCreator;
22
23 import java.util.MissingResourceException JavaDoc;
24
25 /**
26  * Creator for methods of the form Map getX() .
27  */

28 class ConstantsMapMethodCreator extends AbstractLocalizableMethodCreator {
29   /**
30    * Constructor for localizable returnType method creator.
31    *
32    * @param classCreator
33    */

34   public ConstantsMapMethodCreator(AbstractGeneratorClassCreator classCreator) {
35     super(classCreator);
36   }
37
38   /**
39    * Generates Map of key/value pairs for a list of keys.
40    *
41    * @param method method body to create
42    * @param value value to create returnType from
43    * @throws UnableToCompleteException
44    */

45   public void createMethodFor(TreeLogger logger, JMethod method,
46       final String JavaDoc value) throws UnableToCompleteException {
47     String JavaDoc methodName = method.getName();
48
49     if (method.getParameters().length > 0) {
50       error(
51           logger,
52           methodName
53               + " cannot have parameters; extend Messages instead if you need to create parameterized messages");
54     }
55     // make sure cache exists
56
enableCache();
57     // check cache for array
58
println("java.util.Map args = (java.util.Map) cache.get("
59         + wrap(methodName) + ");");
60     // if not found create Map
61
println("if (args == null){");
62     indent();
63     println("args = new com.google.gwt.i18n.client.impl.ConstantMap();");
64     String JavaDoc[] args = ConstantsStringArrayMethodCreator.split(value);
65
66     for (int i = 0; i < args.length; i++) {
67       try {
68         String JavaDoc key = args[i];
69         String JavaDoc keyValue = getResources().getString(key);
70         println("args.put(" + wrap(key) + ", " + wrap(keyValue) + ");");
71       } catch (MissingResourceException JavaDoc e) {
72         String JavaDoc msg = "While implementing map for " + method.getName()
73             + "(), could not find key '" + args[i] + "'";
74         throw error(logger, msg);
75       }
76     }
77     println("cache.put(" + wrap(methodName) + ", args);");
78     println("}; ");
79     outdent();
80     println("return args;");
81   }
82 }
83
Popular Tags