KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2007 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.i18n.rebind.util.MessagesInterfaceCreator;
22 import com.google.gwt.user.rebind.AbstractGeneratorClassCreator;
23 import com.google.gwt.user.rebind.AbstractMethodCreator;
24
25 import java.text.MessageFormat JavaDoc;
26 import java.text.ParseException JavaDoc;
27
28 /**
29  * Creator for methods of the form String getX(arg0,...,argN).
30  */

31 class MessagesMethodCreator extends AbstractMethodCreator {
32   /**
33    * Constructor for <code>MessagesMethodCreator</code>.
34    *
35    * @param classCreator associated class creator
36    */

37   public MessagesMethodCreator(AbstractGeneratorClassCreator classCreator) {
38     super(classCreator);
39   }
40
41   public void createMethodFor(TreeLogger logger, JMethod m, String JavaDoc template)
42       throws UnableToCompleteException {
43     int numParams = m.getParameters().length;
44
45     // Compile time checks of the message
46
Object JavaDoc[] expected;
47
48     // Find safe string to store 'real' quotes during escape.
49
// Using '~' rather than null string or one of a-X because we can
50
// easily test what happens with multiple '~'s.
51
String JavaDoc safeReplaceString = "~";
52
53     while (template.indexOf(safeReplaceString) >= 0) {
54       safeReplaceString += "~";
55     }
56
57     try {
58       int numArgs = MessagesInterfaceCreator.numberOfMessageArgs(template);
59       expected = new Object JavaDoc[numArgs];
60     } catch (ParseException JavaDoc e) {
61       logger.log(TreeLogger.INFO, "Failed to parse the message " + template
62           + " so cannot verify the number of passed-in arguments", e);
63       expected = new Object JavaDoc[numParams];
64     }
65
66     if (numParams != expected.length) {
67       StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
68       msg.append("The method has ");
69       msg.append(numParams);
70       msg.append(numParams == 1 ? " parameter" : " parameters");
71       msg.append(", but the message template has ");
72       msg.append(expected.length);
73       msg.append(expected.length == 1 ? " placeholder" : " placeholders");
74       throw error(logger, msg.toString());
75     }
76     for (int i = 0; i < expected.length; i++) {
77       expected[i] = safeReplaceString + " + arg" + i + " + "
78           + safeReplaceString;
79     }
80     String JavaDoc formattedString;
81     try {
82       formattedString = MessageFormat.format(template, expected);
83     } catch (IllegalArgumentException JavaDoc e) {
84       throw error(logger, "Message Template '" + template
85           + "' did not format correctly", e);
86     }
87     formattedString = wrap(formattedString);
88     formattedString = formattedString.replaceAll(safeReplaceString, "\"");
89     String JavaDoc templateToSplit = "return " + formattedString + ";";
90     println(templateToSplit);
91   }
92 }
93
Popular Tags