01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
24:
25: /**
26: * Creator for methods of the form Map getX() .
27: */
28: class ConstantsMapMethodCreator extends
29: AbstractLocalizableMethodCreator {
30: /**
31: * Constructor for localizable returnType method creator.
32: *
33: * @param classCreator
34: */
35: public ConstantsMapMethodCreator(
36: AbstractGeneratorClassCreator classCreator) {
37: super (classCreator);
38: }
39:
40: /**
41: * Generates Map of key/value pairs for a list of keys.
42: *
43: * @param method method body to create
44: * @param value value to create returnType from
45: * @throws UnableToCompleteException
46: */
47: @Override
48: public void createMethodFor(TreeLogger logger, JMethod method,
49: final String value) throws UnableToCompleteException {
50: String methodName = method.getName();
51:
52: if (method.getParameters().length > 0) {
53: error(
54: logger,
55: methodName
56: + " cannot have parameters; extend Messages instead if you need to create parameterized messages");
57: }
58: // make sure cache exists
59: enableCache();
60: // check cache for array
61: println("java.util.Map args = (java.util.Map) cache.get("
62: + wrap(methodName) + ");");
63: // if not found create Map
64: println("if (args == null){");
65: indent();
66: println("args = new com.google.gwt.i18n.client.impl.ConstantMap();");
67: String[] args = ConstantsStringArrayMethodCreator.split(value);
68:
69: for (int i = 0; i < args.length; i++) {
70: try {
71: String key = args[i];
72: String keyValue = getResources().getString(key);
73: println("args.put(" + wrap(key) + ", " + wrap(keyValue)
74: + ");");
75: } catch (MissingResourceException e) {
76: String msg = "While implementing map for "
77: + method.getName() + "(), could not find key '"
78: + args[i] + "'";
79: throw error(logger, msg);
80: }
81: }
82: println("cache.put(" + wrap(methodName) + ", args);");
83: println("}; ");
84: outdent();
85: println("return args;");
86: }
87: }
|