001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.i18n.rebind;
017:
018: import com.google.gwt.core.ext.TreeLogger;
019: import com.google.gwt.core.ext.UnableToCompleteException;
020: import com.google.gwt.core.ext.typeinfo.JMethod;
021: import com.google.gwt.i18n.rebind.util.MessagesInterfaceCreator;
022: import com.google.gwt.user.rebind.AbstractGeneratorClassCreator;
023: import com.google.gwt.user.rebind.AbstractMethodCreator;
024:
025: import java.text.MessageFormat;
026: import java.text.ParseException;
027:
028: /**
029: * Creator for methods of the form String getX(arg0,...,argN).
030: */
031: class MessagesMethodCreator extends AbstractMethodCreator {
032: /**
033: * Constructor for <code>MessagesMethodCreator</code>.
034: *
035: * @param classCreator associated class creator
036: */
037: public MessagesMethodCreator(
038: AbstractGeneratorClassCreator classCreator) {
039: super (classCreator);
040: }
041:
042: @Override
043: public void createMethodFor(TreeLogger logger, JMethod m,
044: String template) throws UnableToCompleteException {
045: int numParams = m.getParameters().length;
046:
047: // Compile time checks of the message
048: Object[] expected;
049:
050: // Find safe string to store 'real' quotes during escape.
051: // Using '~' rather than null string or one of a-X because we can
052: // easily test what happens with multiple '~'s.
053: String safeReplaceString = "~";
054:
055: while (template.indexOf(safeReplaceString) >= 0) {
056: safeReplaceString += "~";
057: }
058:
059: try {
060: int numArgs = MessagesInterfaceCreator
061: .numberOfMessageArgs(template);
062: expected = new Object[numArgs];
063: } catch (ParseException e) {
064: logger
065: .log(
066: TreeLogger.INFO,
067: "Failed to parse the message "
068: + template
069: + " so cannot verify the number of passed-in arguments",
070: e);
071: expected = new Object[numParams];
072: }
073:
074: if (numParams != expected.length) {
075: StringBuffer msg = new StringBuffer();
076: msg.append("The method has ");
077: msg.append(numParams);
078: msg.append(numParams == 1 ? " parameter" : " parameters");
079: msg.append(", but the message template has ");
080: msg.append(expected.length);
081: msg.append(expected.length == 1 ? " placeholder"
082: : " placeholders");
083: throw error(logger, msg.toString());
084: }
085: for (int i = 0; i < expected.length; i++) {
086: expected[i] = safeReplaceString + " + arg" + i + " + "
087: + safeReplaceString;
088: }
089: String formattedString;
090: try {
091: formattedString = MessageFormat.format(template, expected);
092: } catch (IllegalArgumentException e) {
093: throw error(logger, "Message Template '" + template
094: + "' did not format correctly", e);
095: }
096: formattedString = wrap(formattedString);
097: formattedString = formattedString.replaceAll(safeReplaceString,
098: "\"");
099: String templateToSplit = "return " + formattedString + ";";
100: println(templateToSplit);
101: }
102: }
|