1 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 ; 28 import java.io.IOException ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 32 35 36 public final class I18NCreator extends ToolBase { 37 38 42 protected class ArgHandlerClassName extends ArgHandlerExtra { 43 44 public boolean addExtraArg(String arg) { 45 if (fullInterfaceName != null) { 46 System.err.println("Too many arguments."); 47 return false; 48 } 49 50 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 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 [] 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 getPurpose() { 78 return "The fully qualified name of the interface to create"; 79 } 80 81 public String [] getTagArgs() { 82 return new String [] {"interfaceName"}; 83 } 84 85 public boolean isRequired() { 86 return true; 87 } 88 } 89 90 private static final String PACKAGE_PATH; 91 92 static { 93 String 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 [] 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 118 static void createLocalizable(String fullInterfaceName, File outDir, 119 String eclipse, boolean createMessagesInterface, boolean overwrite, 120 boolean ignore) throws IOException { 121 122 String installPath = Utility.getInstallPath(); 124 String gwtUserPath = installPath + '/' + "gwt-user.jar"; 125 String gwtDevPath = installPath + '/' + Utility.getDevJarName(); 126 127 boolean isWindows = gwtDevPath.substring(gwtDevPath.lastIndexOf('/') + 1).indexOf( 130 "windows") >= 0; 131 132 String basePathEnv; 137 if (!new File (installPath).isAbsolute()) { 138 if (isWindows) { 139 basePathEnv = "%~dp0\\"; 140 } else { 141 basePathEnv = "$APPDIR/"; 142 } 143 } else { 144 basePathEnv = ""; 145 } 146 147 int pos = fullInterfaceName.lastIndexOf('.'); 150 String clientPackageName = fullInterfaceName.substring(0, pos); 151 String interfaceName = fullInterfaceName.substring(pos + 1); 152 153 pos = clientPackageName.lastIndexOf('.'); 156 File clientDir = Utility.getDirectory(outDir, "src", true); 157 if (pos >= 0) { 158 String clientPackage = clientPackageName.replace('.', '/'); 159 clientDir = Utility.getDirectory(clientDir, clientPackage, true); 160 } 161 162 Map replacements = new HashMap (); 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 File i18nMessageProperties = Utility.createNormalFile(clientDir, 180 interfaceName + ".properties", overwrite, ignore); 181 if (i18nMessageProperties != null) { 182 String out = Utility.getFileFromClassPath(PACKAGE_PATH 183 + "i18nMessages.propertiessrc"); 184 Utility.writeTemplateFile(i18nMessageProperties, out, replacements); 185 } 186 } else { 187 File i18nConstantProperties = Utility.createNormalFile(clientDir, 189 interfaceName + ".properties", overwrite, ignore); 190 if (i18nConstantProperties != null) { 191 String out = Utility.getFileFromClassPath(PACKAGE_PATH 192 + "i18nConstants.propertiessrc"); 193 Utility.writeTemplateFile(i18nConstantProperties, out, replacements); 194 } 195 } 196 197 if (eclipse != null) { 198 replacements.put("@projectName", eclipse); 200 File updateLaunchConfig = Utility.createNormalFile(outDir, interfaceName 201 + "-i18n" + ".launch", overwrite, ignore); 202 if (updateLaunchConfig != null) { 203 String out = Utility.getFileFromClassPath(PACKAGE_PATH 204 + "I18N-update.launchsrc"); 205 Utility.writeTemplateFile(updateLaunchConfig, out, replacements); 206 } 207 } 208 209 String extension; 211 if (isWindows) { 212 extension = ".cmd"; 213 } else { 214 extension = ""; 215 } 216 File gwti18n = Utility.createNormalFile(outDir, interfaceName + "-i18n" 217 + extension, overwrite, ignore); 218 if (gwti18n != null) { 219 String 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 eclipse = null; 231 232 private String fullInterfaceName = null; 233 private boolean ignore = false; 234 private File outDir; 235 private boolean overwrite = false; 236 237 protected I18NCreator() { 238 239 registerHandler(new ArgHandlerEclipse() { 240 public String getPurpose() { 241 return "Creates a i18n update launch config for the named eclipse project"; 242 } 243 244 public boolean setString(String str) { 245 eclipse = str; 246 return true; 247 } 248 }); 249 250 registerHandler(new ArgHandlerOutDir() { 251 252 public void setDir(File 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 getPurpose() { 272 return "Create scripts for a Messages interface " 273 + "rather than a Constants one"; 274 } 275 276 public String 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 e) { 307 System.err.println(e.getClass().getName() + ": " + e.getMessage()); 308 return false; 309 } 310 } 311 } 312 | Popular Tags |