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.typeinfo.JMethod;
20: import com.google.gwt.user.rebind.AbstractGeneratorClassCreator;
21:
22: /**
23: * Creator for methods of the form String[] getX().
24: */
25: class ConstantsStringArrayMethodCreator extends
26: AbstractLocalizableMethodCreator {
27:
28: static String[] split(String target) {
29: // We add an artificial end character to avoid the odd split() behavior
30: // that drops the last item if it is only whitespace.
31: target = target + "~";
32:
33: // Do not split on escaped commas.
34: String[] args = target.split("(?<![\\\\]),");
35:
36: // Now remove the artificial ending we added above.
37: // We have to do it before we escape and trim because otherwise
38: // the artificial trailing '~' would prevent the last item from being
39: // properly trimmed.
40: if (args.length > 0) {
41: int last = args.length - 1;
42: args[last] = args[last].substring(0,
43: args[last].length() - 1);
44: }
45:
46: for (int i = 0; i < args.length; i++) {
47: args[i] = args[i].replaceAll("\\\\,", ",").trim();
48: }
49:
50: return args;
51: }
52:
53: /**
54: * Constructor for localizable string array method creator.
55: *
56: * @param classCreator
57: */
58: public ConstantsStringArrayMethodCreator(
59: AbstractGeneratorClassCreator classCreator) {
60: super (classCreator);
61: }
62:
63: @Override
64: public void createMethodFor(TreeLogger logger, JMethod method,
65: String template) {
66: String methodName = method.getName();
67: // Make sure cache exists.
68: enableCache();
69: // Check cache for array.
70: println("String args[] = (String[]) cache.get("
71: + wrap(methodName) + ");");
72: // If not found, create String[].
73: print("if (args == null){\n String [] writer= {");
74: String[] args = split(template);
75: for (int i = 0; i < args.length; i++) {
76: if (i != 0) {
77: print(", ");
78: }
79: String toPrint = args[i].replaceAll("\\,", ",");
80: print(wrap(toPrint));
81: }
82: println("}; ");
83: // add to cache, and return
84: println("cache.put(" + wrap(methodName) + ", writer);");
85: println("return writer;");
86: println("} else");
87: println("return args;");
88: }
89: }
|