KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > i18n > tools > I18NCreator


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.tools;
17
18 import com.google.gwt.user.tools.util.ArgHandlerEclipse;
19 import com.google.gwt.user.tools.util.ArgHandlerIgnore;
20 import com.google.gwt.user.tools.util.ArgHandlerOverwrite;
21 import com.google.gwt.util.tools.ArgHandlerExtra;
22 import com.google.gwt.util.tools.ArgHandlerFlag;
23 import com.google.gwt.util.tools.ArgHandlerOutDir;
24 import com.google.gwt.util.tools.ToolBase;
25 import com.google.gwt.util.tools.Utility;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * Command line assistant for i18n.
34  */

35
36 public final class I18NCreator extends ToolBase {
37
38   /**
39    * Utility class to handle class name argument.
40    *
41    */

42   protected class ArgHandlerClassName extends ArgHandlerExtra {
43
44     public boolean addExtraArg(String JavaDoc arg) {
45       if (fullInterfaceName != null) {
46         System.err.println("Too many arguments.");
47         return false;
48       }
49
50       // Check className for certain properties
51
if (!arg.matches("[\\w\\$]+(\\.[\\w\\$]+)+")) {
52         System.err.println("'"
53             + arg
54             + "' does not appear to be a valid fully-qualified Java class name.");
55         return false;
56       }
57
58       // Check out the class name.
59
//
60
if (arg.indexOf('$') != -1) {
61         System.err.println("'" + arg
62             + "': This version of the tool does not support nested classes");
63         return false;
64       }
65
66       String JavaDoc[] parts = arg.split("\\.");
67       if (parts.length < 2) {
68         System.err.println("'" + arg
69             + "': Cannot live in the root package. Please specify a package.");
70         return false;
71       }
72
73       fullInterfaceName = arg;
74       return true;
75     }
76
77     public String JavaDoc getPurpose() {
78       return "The fully qualified name of the interface to create";
79     }
80
81     public String JavaDoc[] getTagArgs() {
82       return new String JavaDoc[] {"interfaceName"};
83     }
84
85     public boolean isRequired() {
86       return true;
87     }
88   }
89
90   private static final String JavaDoc PACKAGE_PATH;
91
92   static {
93     String JavaDoc path = I18NCreator.class.getName();
94     path = path.substring(0, path.lastIndexOf('.') + 1);
95     PACKAGE_PATH = path.replace('.', '/');
96   }
97
98   public static void main(String JavaDoc[] args) {
99     I18NCreator creator = new I18NCreator();
100     if (creator.processArgs(args)) {
101       if (creator.run()) {
102         return;
103       }
104     }
105
106     System.exit(1);
107   }
108
109   /**
110    * @param fullInterfaceName Name of the fully-qualified Java class to create
111    * as an Application.
112    * @param outDir Where to put the output files
113    * @param eclipse The name of a project to attach a .launch config to
114    * @param overwrite Overwrite an existing files if they exist.
115    * @param ignore Ignore existing files if they exist.
116    * @throws IOException
117    */

118   static void createLocalizable(String JavaDoc fullInterfaceName, File JavaDoc outDir,
119       String JavaDoc eclipse, boolean createMessagesInterface, boolean overwrite,
120       boolean ignore) throws IOException JavaDoc {
121
122     // Figure out the installation directory
123
String JavaDoc installPath = Utility.getInstallPath();
124     String JavaDoc gwtUserPath = installPath + '/' + "gwt-user.jar";
125     String JavaDoc gwtDevPath = installPath + '/' + Utility.getDevJarName();
126
127     // Figure out what platform we're on
128
//
129
boolean isWindows = gwtDevPath.substring(gwtDevPath.lastIndexOf('/') + 1).indexOf(
130         "windows") >= 0;
131
132     // If the path from here to the install directory is relative, we need to
133
// set specific "base" directory tags; this is for sample creation during
134
// the
135
// build.
136
String JavaDoc basePathEnv;
137     if (!new File JavaDoc(installPath).isAbsolute()) {
138       if (isWindows) {
139         basePathEnv = "%~dp0\\";
140       } else {
141         basePathEnv = "$APPDIR/";
142       }
143     } else {
144       basePathEnv = "";
145     }
146
147     // Check out the class and package names.
148
//
149
int pos = fullInterfaceName.lastIndexOf('.');
150     String JavaDoc clientPackageName = fullInterfaceName.substring(0, pos);
151     String JavaDoc interfaceName = fullInterfaceName.substring(pos + 1);
152
153     // Compute module name and directories
154
//
155
pos = clientPackageName.lastIndexOf('.');
156     File JavaDoc clientDir = Utility.getDirectory(outDir, "src", true);
157     if (pos >= 0) {
158       String JavaDoc clientPackage = clientPackageName.replace('.', '/');
159       clientDir = Utility.getDirectory(clientDir, clientPackage, true);
160     }
161
162     // Create a map of replacements
163
//
164
Map JavaDoc replacements = new HashMap JavaDoc();
165     replacements.put("@className", fullInterfaceName);
166     replacements.put("@shortClassName", interfaceName);
167     replacements.put("@gwtUserPath", basePathEnv + gwtUserPath);
168     replacements.put("@gwtDevPath", basePathEnv + gwtDevPath);
169     replacements.put("@compileClass", "com.google.gwt.dev.GWTCompiler");
170     replacements.put("@i18nClass", "com.google.gwt.i18n.tools.I18NSync");
171     if (createMessagesInterface) {
172       replacements.put("@createMessages", "-createMessages");
173     } else {
174       replacements.put("@createMessages", "");
175     }
176
177     if (createMessagesInterface) {
178       // Create a skeleton i18n messages properties class
179
File JavaDoc i18nMessageProperties = Utility.createNormalFile(clientDir,
180           interfaceName + ".properties", overwrite, ignore);
181       if (i18nMessageProperties != null) {
182         String JavaDoc out = Utility.getFileFromClassPath(PACKAGE_PATH
183             + "i18nMessages.propertiessrc");
184         Utility.writeTemplateFile(i18nMessageProperties, out, replacements);
185       }
186     } else {
187       // Create a skeleton i18n constants properties class
188
File JavaDoc i18nConstantProperties = Utility.createNormalFile(clientDir,
189           interfaceName + ".properties", overwrite, ignore);
190       if (i18nConstantProperties != null) {
191         String JavaDoc out = Utility.getFileFromClassPath(PACKAGE_PATH
192             + "i18nConstants.propertiessrc");
193         Utility.writeTemplateFile(i18nConstantProperties, out, replacements);
194       }
195     }
196
197     if (eclipse != null) {
198       // Create an eclipse localizable creator launch config
199
replacements.put("@projectName", eclipse);
200       File JavaDoc updateLaunchConfig = Utility.createNormalFile(outDir, interfaceName
201           + "-i18n" + ".launch", overwrite, ignore);
202       if (updateLaunchConfig != null) {
203         String JavaDoc out = Utility.getFileFromClassPath(PACKAGE_PATH
204             + "I18N-update.launchsrc");
205         Utility.writeTemplateFile(updateLaunchConfig, out, replacements);
206       }
207     }
208
209     // create startup files
210
String JavaDoc extension;
211     if (isWindows) {
212       extension = ".cmd";
213     } else {
214       extension = "";
215     }
216     File JavaDoc gwti18n = Utility.createNormalFile(outDir, interfaceName + "-i18n"
217         + extension, overwrite, ignore);
218     if (gwti18n != null) {
219       String JavaDoc out = Utility.getFileFromClassPath(PACKAGE_PATH + "gwti18n"
220           + extension + "src");
221       Utility.writeTemplateFile(gwti18n, out, replacements);
222       if (extension.length() == 0) {
223         Runtime.getRuntime().exec("chmod u+x " + gwti18n.getAbsolutePath());
224       }
225     }
226   }
227
228   private boolean createMessagesInterface = false;
229
230   private String JavaDoc eclipse = null;
231
232   private String JavaDoc fullInterfaceName = null;
233   private boolean ignore = false;
234   private File JavaDoc outDir;
235   private boolean overwrite = false;
236
237   protected I18NCreator() {
238
239     registerHandler(new ArgHandlerEclipse() {
240       public String JavaDoc getPurpose() {
241         return "Creates a i18n update launch config for the named eclipse project";
242       }
243
244       public boolean setString(String JavaDoc str) {
245         eclipse = str;
246         return true;
247       }
248     });
249
250     registerHandler(new ArgHandlerOutDir() {
251
252       public void setDir(File JavaDoc dir) {
253         outDir = dir;
254       }
255     });
256
257     registerHandler(new ArgHandlerOverwrite() {
258
259       public boolean setFlag() {
260         if (ignore) {
261           System.err.println("-overwrite cannot be used with -ignore.");
262           return false;
263         }
264         overwrite = true;
265         return true;
266       }
267     });
268
269     registerHandler(new ArgHandlerFlag() {
270
271       public String JavaDoc getPurpose() {
272         return "Create scripts for a Messages interface "
273             + "rather than a Constants one";
274       }
275
276       public String JavaDoc getTag() {
277         return "-createMessages";
278       }
279
280       public boolean setFlag() {
281         createMessagesInterface = true;
282         return true;
283       }
284     });
285
286     registerHandler(new ArgHandlerIgnore() {
287
288       public boolean setFlag() {
289         if (overwrite) {
290           System.err.println("-ignore cannot be used with -overwrite.");
291           return false;
292         }
293         ignore = true;
294         return true;
295       }
296     });
297
298     registerHandler(new ArgHandlerClassName());
299   }
300
301   protected boolean run() {
302     try {
303       createLocalizable(fullInterfaceName, outDir, eclipse,
304           createMessagesInterface, overwrite, ignore);
305       return true;
306     } catch (IOException JavaDoc e) {
307       System.err.println(e.getClass().getName() + ": " + e.getMessage());
308       return false;
309     }
310   }
311 }
312
Popular Tags