1 2 24 package org.enhydra.tool.codegen; 25 26 import org.enhydra.tool.ToolBoxInfo; 28 import org.enhydra.tool.codegen.wizard.CodeGenWizard; 29 import org.enhydra.tool.codegen.wizard.CodeGenPage; 30 import org.enhydra.tool.codegen.internal.Enhydra3AppGenerator; 31 import org.enhydra.tool.codegen.internal.WebAppGenerator; 34 import org.enhydra.tool.common.ToolException; 35 import org.enhydra.tool.common.SwingUtil; 36 import org.enhydra.tool.common.ResUtil; 37 38 import java.io.File ; 40 import java.io.FileInputStream ; 41 import java.io.FileOutputStream ; 42 import java.util.Properties ; 43 import java.util.ArrayList ; 44 import java.beans.Beans ; 45 import java.awt.Component ; 46 import java.awt.Dialog ; 47 import java.awt.Frame ; 48 import javax.swing.JOptionPane ; 49 import java.util.ResourceBundle ; 50 51 54 public class CodeGen implements Constants { 55 56 60 static ResourceBundle res = 61 ResourceBundle.getBundle("org.enhydra.tool.codegen.Res"); 62 public static boolean debug = false; 63 64 68 public static boolean verbose = false; 69 70 private static final String GENERATOR = "external.generator"; private static final String CLASS_WEB_GEN = 73 "org.enhydra.tool.codegen.internal.WebAppGenerator"; private static final String CLASS_EN3_GEN = 75 "org.enhydra.tool.codegen.internal.Enhydra3AppGenerator"; 80 private Generator[] generators = new Generator[0]; 82 private Generator selection = null; 83 private Properties properties = new Properties (); 84 private int option = JOptionPane.CANCEL_OPTION; 85 private boolean swing = false; 86 87 91 99 public CodeGen() throws GeneratorException { 100 this(false); 101 } 102 103 117 public CodeGen(boolean useSwing) throws GeneratorException { 118 setSwing(useSwing); 119 readProperties(); 120 initGenerators(); 121 if (generators.length == 0) { 122 throw new GeneratorException(res.getString("No_generators")); 123 } 124 } 125 126 138 public CodeGen(Generator sel) throws GeneratorException { 139 selection = sel; 140 setSwing(true); 141 readProperties(); 142 generators = new Generator[1]; 143 generators[0] = selection; 144 selection.setProperties(properties); 145 } 146 147 public boolean isSwing() { 151 return swing; 152 } 153 154 public void setSwing(boolean b) { 155 swing = b; 156 } 157 158 167 public int getOption() { 168 return option; 169 } 170 171 178 public Generator getSelection() { 179 return selection; 180 } 181 182 public void setSelection(Generator sel) { 183 selection = sel; 184 } 185 186 197 public File [] invokeWizard(Component host) { 198 CodeGenWizard wizard = null; 199 File [] files = new File [0]; 200 201 wizard = new CodeGenWizard(this); 202 if (selection != null) { 203 try { 204 wizard.setSelection(selection); 205 } catch (GeneratorException e) { 206 JOptionPane.showMessageDialog(host, e.getMessage(), 207 res.getString("Unable_to_set"), 208 JOptionPane.ERROR_MESSAGE); 209 if (CodeGen.debug) { 210 e.printStackTrace(); 211 } 212 } 213 } 214 option = wizard.showDialog(host); 215 files = wizard.getGeneratedFiles(); 216 if (option == JOptionPane.CANCEL_OPTION) { 217 218 } else { 220 if (files == null || files.length == 0) { 221 JOptionPane.showMessageDialog(host, 222 res.getString("No_files_generated_"), 223 res.getString("AppWizard_Complete"), 224 JOptionPane.WARNING_MESSAGE); 225 } else { 226 String parentPath = files[0].getParent(); 227 228 for (int i = 0; i < files.length; i++) { 229 if (files[i].getParent().length() < parentPath.length()) { 230 parentPath = files[i].getParent(); 231 } 232 } 233 String mess = 234 ResUtil.format(res.getString("Generated_0_files_in"), 235 (new String ()) + files.length, parentPath); 236 237 JOptionPane.showMessageDialog(host, mess, 238 res.getString("AppWizard_Complete"), 239 JOptionPane.INFORMATION_MESSAGE); 240 } 241 } 242 return files; 243 } 244 245 249 254 private void createProperties() throws GeneratorException { 255 256 Properties prop = new Properties (); 258 File file = null; 259 260 try { 261 file = ToolBoxInfo.storeProperties(prop); 262 System.out.println(ResUtil.format(res.getString("Configuration"), 263 file)); 264 } catch (Exception e) { 265 throw new GeneratorException(e, 266 ResUtil.format(res.getString("Unable_to_write_file"), 267 ToolBoxInfo.getPropertyFilename())); 268 } 269 } 270 271 public void setGenerators(Generator[] g) { 275 generators = g; 276 } 277 278 public Generator[] getGenerators() { 279 return generators; 280 } 281 282 286 public void initGenerators() throws GeneratorException { 287 String className; 288 Object newObject; 289 ArrayList gList = new ArrayList (); 290 int validCount = 0; 291 292 for (int i = 0; i < 100; i++) { 294 className = properties.getProperty(GENERATOR + '.' + i); 295 newObject = null; 296 if (className != null && className.trim().length() > 0) { 297 try { 298 newObject = Beans.instantiate(getClass().getClassLoader(), 299 className); 300 } catch (Exception e) { 301 newObject = null; 302 System.out.println(ResUtil.format(res.getString("AppWizard_Warning"), 303 className)); 304 if (CodeGen.debug) { 305 System.out.println(TAB4 + e.toString()); 306 } 307 } 308 if (newObject != null) { 309 if (newObject instanceof Generator) { 310 gList.add((Generator) newObject); 311 } else { 312 System.out.println(ResUtil.format(res.getString("AppWizard_Warning_Not"), 313 className)); 314 } 315 } 316 } 317 } 318 319 320 gList.add(new Enhydra3AppGenerator()); 323 324 gList.add(new WebAppGenerator()); 326 331 334 for (int i = 0; i < gList.size(); i++) { 335 Generator current = null; 336 337 current = (Generator) gList.get(i); 338 try { 339 current.setProperties(properties); 340 current.setSwing(isSwing()); 341 validCount++; 342 } catch (GeneratorException e) { 343 System.out.println(ResUtil.format(res.getString("Invalid_generator_0_"), 344 current.getCommandName())); 345 System.out.println(e.getMessage()); 346 gList.set(i, new Object ()); 347 } 348 } 349 if (validCount == 0) { 350 throw new GeneratorException(res.getString("No_generators1")); 351 } else { 352 generators = new Generator[validCount]; 353 } 354 validCount = 0; 355 for (int i = 0; i < gList.size(); i++) { 356 if (gList.get(i) instanceof Generator) { 357 generators[validCount] = (Generator) gList.get(i); 358 validCount++; 359 } 360 } 361 gList.clear(); 362 } 363 364 368 protected void list() { 369 int count = generators.length; 370 371 System.out.println(new String ()); 372 if (count < 1) { 373 System.out.println(res.getString("No_generators_to_list")); 374 } else { 375 System.out.println(res.getString("Available_generators_")); 376 for (int i = 0; i < count; i++) { 377 System.out.println(TAB4 + generators[i].getCommandName()); 378 } 379 System.out.println(new String ()); 380 System.out.println(res.getString("Generator")); 381 for (int i = 0; i < count; i++) { 382 StringBuffer buf = new StringBuffer (); 383 384 buf.append(generators[i].getCommandName()); 385 buf.append(TAB4); 386 buf.append(generators[i].getDisplayName()); 387 buf.append('\n'); 388 buf.append(generators[i].getDescription()); 389 buf.append('\n'); 390 System.out.println(buf.toString()); 391 } 392 } 393 System.out.println(new String ()); 394 } 395 396 402 protected void invokeGenerator(String [] args) throws GeneratorException { 403 File [] files = new File [0]; 404 405 initSelection(args[0]); 406 if (selection == null) {} 407 408 else { 410 processGeneratorOptions(args); 411 System.out.println(ResUtil.format(res.getString("Generator_0_"), 412 selection.getCommandName())); 413 GeneratorOption[] options = selection.getOptionSet().toArray(); 414 415 for (int i = 0; i < options.length; i++) { 416 if (options[i].isRequired() || CodeGen.verbose) { 417 System.out.println(options[i].getDisplayName() + ':' 418 + ' ' + options[i].getValue()); 419 } 420 } 421 files = selection.generate(); 422 System.out.println(ResUtil.format(res.getString("File_count_0_"), 423 files.length)); 424 } 425 } 426 427 433 private void processGeneratorOptions(String [] args) 434 throws GeneratorException { 435 436 GeneratorOption[] options = selection.getOptionSet().toArray(); 439 440 for (int i = 0; i < options.length; i++) { 442 if (options[i].isRequired()) { 443 options[i].clearValue(); 444 } 445 } 446 447 for (int i = 1; i < args.length; i++) { 449 if ((args[i].length() > 0) && (args[i].charAt(0) == '-')) { 450 boolean found = false; 451 452 for (int j = 0; j < options.length; j++) { 454 String paramName = '-' + options[j].getName(); 455 456 if (paramName.equalsIgnoreCase(args[i])) { 457 found = true; 458 if (options[j].isBoolean()) { 459 options[j].setValue((new Boolean (true)).toString()); 460 } else if (i + 1 < args.length) { 461 options[j].setValue(args[i + 1]); 462 } 463 } 464 } 465 if (!found) { 466 System.out.println(ResUtil.format(res.getString("Ignoring_unrecognized"), 467 args[i])); 468 } 469 } 470 } 471 StringBuffer buf = new StringBuffer (); 472 boolean missing = false; 473 474 for (int j = 0; j < options.length; j++) { 475 if (options[j].isRequired()) { 476 if (options[j].isEmpty()) { 477 if (!missing) { 478 missing = true; 479 buf.append(res.getString("Required_value")); 480 buf.append('\n'); 481 } 482 buf.append(TAB4); 483 buf.append('-'); 484 buf.append(options[j].getName()); 485 buf.append('\n'); 486 } 487 } 488 } 489 if (buf.length() > 0) { 490 buf.append('\n'); 491 buf.append(res.getString("For_more_information")); 492 buf.append(' '); 493 buf.append(selection.getCommandName()); 494 throw new GeneratorException(buf.toString()); 495 } 496 497 } 499 500 505 private void initSelection(String commandName) throws GeneratorException { 506 selection = null; 507 for (int i = 0; i < generators.length; i++) { 508 if (generators[i].getCommandName().equalsIgnoreCase(commandName)) { 509 selection = generators[i]; 510 break; 511 } 512 } 513 if (selection == null) { 514 System.out.println(ResUtil.format(res.getString("_0_is_not_a_valid"), 515 commandName)); 516 } 517 } 518 519 525 private void readProperties() throws GeneratorException { 526 String fn = ToolBoxInfo.getPropertyFilename(); 527 File file = new File (fn); 528 529 if (!file.exists()) { 530 createProperties(); 531 } 532 try { 533 properties = ToolBoxInfo.loadProperties(); 534 } catch (ToolException e) { 535 throw new GeneratorException(e, res.getString("Unable_to_read")); 536 } 537 } 538 539 544 protected void showGeneratorOptionHelp(String commandName) { 545 try { 546 initSelection(commandName); 547 } catch (GeneratorException e) { 548 e.printStackTrace(); 549 } 550 if (selection == null) { 551 System.out.println(ResUtil.format(res.getString("Unable_to_show"), 552 commandName)); 553 } else { 554 GeneratorOption[] options = selection.getOptionSet().toArray(); 555 556 if (options.length == 0) { 557 System.out.println(ResUtil.format(res.getString("No_options_available"), 558 commandName)); 559 } else { 560 for (int i = 0; i < options.length; i++) { 561 StringBuffer buf = new StringBuffer (); 562 563 buf.append(' '); 564 buf.append('-'); 565 buf.append(options[i].getName()); 566 if (options[i].isBoolean()) { 567 568 } else { 570 buf.append(' '); 571 buf.append(options[i].getDisplayName()); 572 } 573 buf.append(TAB4); 574 buf.append(options[i].getDescription()); 575 System.out.println(buf.toString()); 576 } 577 } 578 } 579 } 580 581 } 582 | Popular Tags |