KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc[] split(String JavaDoc 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 JavaDoc[] 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, args[last].length() - 1);
43     }
44     
45     for (int i = 0; i < args.length; i++) {
46       args[i] = args[i].replaceAll("\\\\,", ",").trim();
47     }
48
49     return args;
50   }
51
52   /**
53    * Constructor for localizable string array method creator.
54    *
55    * @param classCreator
56    */

57   public ConstantsStringArrayMethodCreator(
58       AbstractGeneratorClassCreator classCreator) {
59     super(classCreator);
60   }
61
62   public void createMethodFor(TreeLogger logger, JMethod method, String JavaDoc template) {
63     String JavaDoc methodName = method.getName();
64     // Make sure cache exists.
65
enableCache();
66     // Check cache for array.
67
println("String args[] = (String[]) cache.get(" + wrap(methodName) + ");");
68     // If not found, create String[].
69
print("if (args == null){\n String [] writer= {");
70     String JavaDoc[] args = split(template);
71     for (int i = 0; i < args.length; i++) {
72       if (i != 0) {
73         print(", ");
74       }
75       String JavaDoc toPrint = args[i].replaceAll("\\,", ",");
76       print(wrap(toPrint));
77     }
78     println("}; ");
79     // add to cache, and return
80
println("cache.put(" + wrap(methodName) + ", writer);");
81     println("return writer;");
82     println("} else");
83     println("return args;");
84   }
85 }
86
Popular Tags