KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > i18n > rebind > util > MessagesInterfaceCreator


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.util;
17
18 import com.google.gwt.i18n.client.Messages;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23 import java.text.ParseException JavaDoc;
24
25 /**
26  * Creates a MessagesInterface from a Resource file.
27  */

28 public class MessagesInterfaceCreator extends
29     AbstractLocalizableInterfaceCreator {
30
31   /**
32    * Calculates the number of arguments <code>MessageFormat</code> expects to
33    * see in a template.
34    *
35    * @param template template to parse
36    * @return number of args
37    * @throws ParseException
38    */

39   public static int numberOfMessageArgs(String JavaDoc template) throws ParseException JavaDoc {
40     // As parse, unlike format, cannot deal with single quotes, we can escape
41
// them instead. If sub-formats are supported in the future, this code will
42
// have to change.
43
String JavaDoc escapedDefault = template.replaceAll("'", "x");
44     int numArgs = new MessageFormat JavaDoc(escapedDefault).parse(escapedDefault).length;
45     return numArgs;
46   }
47
48   /**
49    * Constructor for <code>MessagesInterfaceCreator</code>.
50    *
51    * @param className class name
52    * @param packageName package name
53    * @param resourceBundle resource bundle
54    * @param targetLocation target location
55    * @throws IOException
56    */

57   public MessagesInterfaceCreator(String JavaDoc className, String JavaDoc packageName,
58       File JavaDoc resourceBundle, File JavaDoc targetLocation) throws IOException JavaDoc {
59     super(className, packageName, resourceBundle, targetLocation,
60       Messages.class);
61   }
62
63   protected void genMethodArgs(String JavaDoc defaultValue) {
64     try {
65       int numArgs = numberOfMessageArgs(defaultValue);
66       for (int i = 0; i < numArgs; i++) {
67         if (i > 0) {
68           composer.print(", ");
69         }
70         composer.print("String arg" + i);
71       }
72     } catch (ParseException JavaDoc e) {
73       throw new RuntimeException JavaDoc(defaultValue
74         + " could not be parsed as a MessageFormat string.", e);
75     }
76   }
77
78   protected String JavaDoc javaDocComment(String JavaDoc path) {
79     return "Interface to represent the messages contained in resource bundle:\n\t"
80       + path + "'.";
81   }
82 }
83
Popular Tags