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.util;
017:
018: import com.google.gwt.i18n.client.Messages;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.text.MessageFormat;
023: import java.text.ParseException;
024:
025: /**
026: * Creates a MessagesInterface from a Resource file.
027: */
028: public class MessagesInterfaceCreator extends
029: AbstractLocalizableInterfaceCreator {
030:
031: /**
032: * Calculates the number of arguments <code>MessageFormat</code> expects to
033: * see in a template.
034: *
035: * @param template template to parse
036: * @return number of args
037: * @throws ParseException
038: */
039: public static int numberOfMessageArgs(String template)
040: throws ParseException {
041: /*
042: * As parse, unlike format, cannot deal with single quotes, we have to remove
043: * them. First, we remove doubled quotes (which go into the output as single
044: * quotes. Then, we remove any quoted strings.
045: *
046: * If sub-formats are supported in the future, this code will
047: * have to change.
048: */
049: // strip doubled single-quotes
050: template = template.replace("''", "");
051:
052: // delete quoted sections
053: template = template.replaceAll("'[^']+'", "");
054:
055: if (template.length() == 0) {
056: // special case empty strings since MessageFormat.parse chokes on them.
057: return 0;
058: }
059: int numArgs = new MessageFormat(template).parse(template).length;
060: return numArgs;
061: }
062:
063: /**
064: * Constructor for <code>MessagesInterfaceCreator</code>.
065: *
066: * @param className class name
067: * @param packageName package name
068: * @param resourceBundle resource bundle
069: * @param targetLocation target location
070: * @throws IOException
071: */
072: public MessagesInterfaceCreator(String className,
073: String packageName, File resourceBundle, File targetLocation)
074: throws IOException {
075: super (className, packageName, resourceBundle, targetLocation,
076: Messages.class);
077: }
078:
079: @Override
080: protected void genMethodArgs(String defaultValue) {
081: try {
082: int numArgs = numberOfMessageArgs(defaultValue);
083: for (int i = 0; i < numArgs; i++) {
084: if (i > 0) {
085: composer.print(", ");
086: }
087: composer.print("String arg" + i);
088: }
089: } catch (ParseException e) {
090: throw new RuntimeException(
091: defaultValue
092: + " could not be parsed as a MessageFormat string.",
093: e);
094: }
095: }
096:
097: @Override
098: protected String javaDocComment(String path) {
099: return "Interface to represent the messages contained in resource bundle:\n\t"
100: + path + "'.";
101: }
102: }
|