1 16 package com.google.gwt.i18n.tools; 17 18 import com.google.gwt.i18n.client.Constants; 19 import com.google.gwt.i18n.client.ConstantsWithLookup; 20 import com.google.gwt.i18n.client.Messages; 21 import com.google.gwt.i18n.rebind.LocalizableGenerator; 22 import com.google.gwt.i18n.rebind.util.AbstractLocalizableInterfaceCreator; 23 import com.google.gwt.i18n.rebind.util.ConstantsInterfaceCreator; 24 import com.google.gwt.i18n.rebind.util.MessagesInterfaceCreator; 25 import com.google.gwt.util.tools.ArgHandlerExtra; 26 import com.google.gwt.util.tools.ArgHandlerFlag; 27 import com.google.gwt.util.tools.ArgHandlerString; 28 import com.google.gwt.util.tools.ToolBase; 29 30 import java.io.File ; 31 import java.io.FileNotFoundException ; 32 import java.io.IOException ; 33 import java.net.URL ; 34 35 38 public class I18NSync extends ToolBase { 39 40 private class classNameArgHandler extends ArgHandlerExtra { 41 42 public boolean addExtraArg(String str) { 43 if (classNameArg != null) { 44 System.err.println("Too many arguments."); 45 return false; 46 } 47 try { 52 File resourceFile = urlToResourceFile(str); 53 checkValidResourceInputFile(resourceFile); 54 classNameArg = str; 55 } catch (IOException e) { 56 System.err.println("Error: " + e.getMessage()); 57 return false; 58 } 59 return true; 60 } 61 62 public String [] getDefaultArgs() { 63 return null; 64 } 65 66 public String getPurpose() { 67 return "Identifies the Constants/Messages class to be created. For example com.google.sample.i18n.client.Colors"; 68 } 69 70 public String [] getTagArgs() { 71 String [] interfaceArg = {"name of the Constants/Messages interface to create"}; 72 return interfaceArg; 73 } 74 75 public boolean isRequired() { 76 return true; 77 } 78 } 79 80 private class outDirHandler extends ArgHandlerString { 81 82 public String [] getDefaultArgs() { 83 return null; 84 } 85 86 public String getPurpose() { 87 return "Java source directory, defaults to the resource's class path."; 88 } 89 90 public String getTag() { 91 return "-out"; 92 } 93 94 public String [] getTagArgs() { 95 String [] resourceArgs = {"fileName"}; 96 return resourceArgs; 97 } 98 99 public boolean isRequired() { 100 return false; 101 } 102 103 public boolean setString(String str) { 104 105 outDirArg = new File (str); 110 try { 111 checkValidSourceDir(outDirArg); 112 } catch (IOException e) { 113 System.err.println("Error: " + e.getMessage()); 114 return false; 115 } 116 return true; 117 } 118 } 119 120 123 public static final String ID = "@" + LocalizableGenerator.GWT_KEY + " "; 124 125 133 public static void createConstantsInterfaceFromClassName(String className, 134 File outDir) throws IOException { 135 createLocalizableInterfaceFromClassName(className, outDir, Constants.class); 136 } 137 138 145 public static void createConstantsWithLookupInterfaceFromClassName( 146 String className) throws IOException { 147 createLocalizableInterfaceFromClassName(className, null, 148 ConstantsWithLookup.class); 149 } 150 151 159 public static void createConstantsWithLookupInterfaceFromClassName( 160 String className, File outDir) throws IOException { 161 createLocalizableInterfaceFromClassName(className, outDir, 162 ConstantsWithLookup.class); 163 } 164 165 172 public static void createMessagesInterfaceFromClassName(String className) 173 throws IOException { 174 createLocalizableInterfaceFromClassName(className, null, Messages.class); 175 } 176 177 185 public static void createMessagesInterfaceFromClassName(String className, 186 File outDir) throws IOException { 187 createLocalizableInterfaceFromClassName(className, outDir, Messages.class); 188 } 189 190 195 public static void main(String [] args) { 196 I18NSync creator = new I18NSync(); 197 if (creator.processArgs(args)) { 198 if (creator.run()) { 199 return; 200 } 201 } 202 System.exit(1); 203 } 204 205 static void checkValidJavaSourceOutputFile(File targetSource) 206 throws IOException { 207 208 if (targetSource.isDirectory()) { 209 throw new IOException ("Output file'" + targetSource 210 + "' exists and is a directory; cannot overwrite"); 211 } 212 if (targetSource.getParentFile().isDirectory() == false) { 213 throw new IOException ("The target source's directory '" 214 + targetSource.getParent() + "' must be an existing directory"); 215 } 216 } 217 218 static void checkValidResourceInputFile(File resource) throws IOException { 219 if (!resource.getPath().endsWith(".properties")) { 220 throw new IOException ("Properties files " + resource 221 + " should end with '.properties'"); 222 } 223 if (!resource.exists() || !resource.isFile()) { 224 throw new IOException ("Properties file not found: " + resource); 225 } 226 } 227 228 private static void checkValidSourceDir(File outDir) throws IOException { 229 if (outDir.isDirectory() == false) { 230 throw new IOException (outDir 231 + " must be an existing directory. Current path is " 232 + new File (".").getCanonicalPath()); 233 } 234 } 235 236 private static void createLocalizableInterfaceFromClassName(String className, 237 File sourceDir, Class interfaceClass) throws FileNotFoundException , 238 IOException { 239 File resource = urlToResourceFile(className); 240 File source; 241 if (sourceDir == null) { 242 source = synthesizeSourceFile(resource); 243 } else { 244 checkValidSourceDir(sourceDir); 245 String sourcePath = className.replace('.', File.separatorChar); 246 sourcePath = sourceDir.getCanonicalFile() + File.separator + sourcePath 247 + ".java"; 248 source = new File (sourcePath); 249 } 250 checkValidJavaSourceOutputFile(source); 252 checkValidResourceInputFile(resource); 253 254 int classDiv = className.lastIndexOf("."); 255 String packageName = className.substring(0, classDiv); 256 String name = className.substring(classDiv + 1); 257 AbstractLocalizableInterfaceCreator creator; 258 if (interfaceClass.equals(Messages.class)) { 259 creator = new MessagesInterfaceCreator(name, packageName, resource, 260 source); 261 } else { 262 creator = new ConstantsInterfaceCreator(name, packageName, resource, 263 source, interfaceClass); 264 } 265 creator.generate(); 266 } 267 268 private static File synthesizeSourceFile(File resource) { 269 String javaPath = resource.getName(); 270 javaPath = javaPath.substring(0, javaPath.lastIndexOf(".")); 271 javaPath = resource.getParentFile().getPath() + File.separator + javaPath 272 + ".java"; 273 File targetClassFile = new File (javaPath); 274 return targetClassFile; 275 } 276 277 private static File urlToResourceFile(String className) 278 throws FileNotFoundException , IOException { 279 if (className.endsWith(".java") || className.endsWith(".properties") 280 || className.endsWith(".class") 281 || className.indexOf(File.separator) > 0) { 282 throw new IllegalArgumentException ( 283 "class '" 284 + className 285 + "'should not contain an extension. \"com.google.gwt.SomeClass\" is an example of a correctly formed class string"); 286 } 287 String resourcePath = className.replace('.', '/') + ".properties"; 288 URL r = ClassLoader.getSystemResource(resourcePath); 289 if (r == null) { 290 throw new FileNotFoundException ("Could not find the resource '" 291 + resourcePath + " matching '" + className 292 + "' did you remember to add it to your classpath?"); 293 } 294 File resourceFile = new File (r.getFile()); 295 return resourceFile; 296 } 297 298 private String classNameArg; 299 300 private boolean createMessagesArg = false; 301 302 private File outDirArg; 303 304 private I18NSync() { 305 registerHandler(new classNameArgHandler()); 306 registerHandler(new outDirHandler()); 307 registerHandler(new ArgHandlerFlag() { 308 309 public String getPurpose() { 310 return "create Messages interface rather than Constants interface."; 311 } 312 313 public String getTag() { 314 return "-createMessages"; 315 } 316 317 public boolean setFlag() { 318 createMessagesArg = true; 319 return true; 320 } 321 }); 322 } 323 324 329 protected boolean run() { 330 try { 331 Class createMe; 332 if (createMessagesArg) { 333 createMe = Messages.class; 334 } else { 335 createMe = Constants.class; 336 } 337 createLocalizableInterfaceFromClassName(classNameArg, outDirArg, createMe); 338 return true; 339 } catch (Throwable e) { 340 System.err.println(e.getMessage()); 341 return false; 342 } 343 } 344 } 345 | Popular Tags |