1 2 24 package org.enhydra.tool.codegen; 25 26 import org.enhydra.tool.ToolBoxInfo; 28 import org.enhydra.tool.codegen.wizard.CodeGenPanel; 29 import org.enhydra.tool.common.FileUtil; 30 import org.enhydra.tool.common.PathHandle; 31 import org.enhydra.tool.common.Replacement; 32 import org.enhydra.tool.common.ReplacementSet; 33 import org.enhydra.tool.common.ResUtil; 34 import org.enhydra.tool.common.Template; 35 import org.enhydra.tool.common.TemplateFilter; 36 import org.enhydra.tool.common.TemplateTool; 37 import org.enhydra.tool.common.ToolException; 38 import org.enhydra.tool.common.event.TemplateEvent; 39 import org.enhydra.tool.common.event.TemplateListener; 40 import org.enhydra.tool.configure.ConfigTool; 41 42 import java.io.BufferedReader ; 44 import java.io.IOException ; 45 import java.io.File ; 46 import java.io.FileNotFoundException ; 47 import java.io.FileReader ; 48 import java.io.FileWriter ; 49 import java.io.PrintWriter ; 50 import java.util.ArrayList ; 51 import java.util.Arrays ; 52 import java.util.Properties ; 53 import java.util.ResourceBundle ; 54 import java.util.StringTokenizer ; 55 import java.util.Vector ; 56 57 abstract public class ProjectGenerator extends TemplateDrivenGenerator 59 implements Constants { 60 61 private String javaPath = ToolBoxInfo.getJavaPath(); 63 private String [] copyright = new String [0]; 64 private String [] shellSteps = null; 65 private String [] addinSteps = null; 66 private boolean cli = true; 67 68 public String [] getOutputExcludes() { 69 ArrayList list = new ArrayList (); 70 String [] ex = new String [0]; 71 72 if (cli) { 73 if (ToolBoxInfo.isEnhydra3()) { 74 list.add('.' + TYPE_MK); 76 list.add(File.separator + FILE_MAKEFILE); 77 } else { 80 list.add('.' + TYPE_MK); 81 list.add(File.separator + FILE_MAKEFILE); 82 list.add(File.separator + FILE_BUILD_XML); 83 } 84 } else { 85 list.add('.' + TYPE_MK); 86 list.add(File.separator + FILE_MAKEFILE); 87 list.add(File.separator + FILE_RUN_IN); 88 list.add(File.separator + FILE_RUN_BAT_IN); 89 list.add(File.separator + FILE_BUILD_XML); 90 } 91 list.add(".bak"); 92 list.trimToSize(); 93 ex = new String [list.size()]; 94 ex = (String []) list.toArray(ex); 95 list.clear(); 96 return ex; 97 } 98 99 public ProjectGenerator() { 101 super(); 102 } 103 104 public File [] generate() throws GeneratorException { 105 File [] files = new File [0]; 106 File readme = null; 107 ArrayList list = null; 108 109 setOverwrite(getOptionSet().lookup(ProjectOptionSet.OVERWRITE).isValue()); 110 cli = (!getOptionSet().lookup(ProjectOptionSet.NOCLI).isValue()); 111 files = super.generate(); 112 list = new ArrayList (Arrays.asList(files)); 113 readme = generateReadme(getDestination()); 114 list.add(readme); 115 files = new File [list.size()]; 116 files = (File []) list.toArray(files); 117 list.clear(); 118 return files; 119 } 120 121 public void storeProperties() { 122 try { 123 getOptionSet().lookup(ProjectOptionSet.OVERWRITE).setValue(isOverwrite()); 124 } catch (GeneratorException e) { 125 e.printStackTrace(); 126 } 127 super.storeProperties(); 128 } 129 130 public void setProperties(Properties p) throws GeneratorException { 131 super.setProperties(p); 132 133 getOptionSet().lookup(ProjectOptionSet.NOCLI).setValue(false); 135 136 PathHandle handle = null; 138 File destDir = null; 139 String destPath = new String (); 140 String projectName = new String (); 141 String [] fileList = new String [0]; 142 boolean newName = false; 143 144 handle = 145 PathHandle.createPathHandle(getOptionSet().lookup(ProjectOptionSet.ROOT).getValue()); 146 destPath = handle.getPath(); 147 projectName = 148 getOptionSet().lookup(ProjectOptionSet.PROJECT).getValue(); 149 destDir = handle.getFile(); 150 if (!destDir.exists()) { 151 destDir.mkdirs(); 152 } 153 fileList = destDir.list(); 154 for (int i = 0; i < fileList.length; i++) { 155 if (fileList[i].equalsIgnoreCase(projectName)) { 156 newName = true; 157 char lastChar = projectName.charAt(projectName.length() 158 - 1); 159 String digits = new String (); 160 161 while (Character.isDigit(lastChar)) { 162 projectName = projectName.substring(0, 163 projectName.length() 164 - 1); 165 digits = lastChar + digits; 166 lastChar = projectName.charAt(projectName.length() - 1); 167 } 168 int next = 1; 169 170 try { 171 next = Integer.parseInt(digits); 172 } catch (NumberFormatException e) { 173 next = 1; 174 } 175 next++; 176 projectName = projectName + next; 177 i = 0; } 179 } 180 if (newName) { 181 getOptionSet().lookup(ProjectOptionSet.PROJECT).setValue(projectName); 182 } 183 } 184 185 public TemplateFilter createCopyFilter(TemplateTool tool) 186 throws GeneratorException { 187 ProjectTemplateFilter copyFilter = null; 188 189 copyFilter = new ProjectTemplateFilter(tool, getDestination()); 190 copyFilter.setIncludeTemplate(false); 191 copyFilter.setInputIncludes(getInputIncludes()); 192 copyFilter.setOutputExcludes(getOutputExcludes()); 193 return copyFilter; 194 } 195 196 public TemplateFilter createTemplateFilter(TemplateTool tool) 197 throws GeneratorException { 198 ProjectTemplateFilter templateFilter = null; 199 200 templateFilter = new ProjectTemplateFilter(tool, getDestination()); 201 templateFilter.setIncludeTemplate(true); 202 templateFilter.setInputIncludes(getInputIncludes()); 203 templateFilter.setOutputExcludes(getOutputExcludes()); 204 return templateFilter; 205 } 206 207 public String getDestination() throws GeneratorException { 208 StringBuffer buf = new StringBuffer (); 209 PathHandle path = null; 210 211 buf.append(getOptionSet().lookup(ProjectOptionSet.ROOT).getValue()); 212 buf.append(File.separator); 213 buf.append(getOptionSet().lookup(ProjectOptionSet.PROJECT).getValue()); 214 path = PathHandle.createPathHandle(buf.toString()); 215 return path.getPath(); 216 } 217 218 public void setJavaPath(String p) { 219 javaPath = PathHandle.createPathString(p); 220 } 221 222 public String getJavaPath() { 223 return javaPath; 224 } 225 226 235 public void setShellSteps(String [] steps) { 236 shellSteps = steps; 237 } 238 239 248 public String [] getShellSteps() { 249 String [] steps = shellSteps; 250 251 if (steps == null) { 252 steps = getDefaultShellSteps(); 253 } 254 return steps; 255 } 256 257 265 public String [] getAddinSteps() { 266 String [] steps = addinSteps; 267 268 if (steps == null) { 269 steps = getDefaultAddinSteps(); 270 } 271 return steps; 272 } 273 274 283 public void setAddinSteps(String [] steps) { 284 addinSteps = steps; 285 } 286 287 293 public String getProjectSourcePath() throws GeneratorException { 294 String path = new String (); 295 296 path = findProjectSourcePath(getTemplateRoot()); 297 if (path == null) { 298 throw new GeneratorException(res.getString("Project_source_not")); 299 } 300 return path; 301 } 302 303 309 public void initReplacementSet() throws GeneratorException { 310 try { 311 String packageName = null; 312 String shellPackageDir = null; 313 String systemPackageDir = null; 314 String content = null; 315 int index = 0; 316 317 packageName = 319 getOptionSet().lookup(ProjectOptionSet.PACKAGE).getValue(); 320 shellPackageDir = packageName.replace('.', '/'); 321 systemPackageDir = packageName.replace('.', File.separatorChar); 322 323 readCopyright(); 325 getReplacementSet().lookup(ProjectReplacementSet.COPYRIGHT).setReplaceWith(copyright); 326 getReplacementSet().lookup(ProjectReplacementSet.at_PROJECT_at).setReplaceWith(getOptionSet().lookup(ProjectOptionSet.PROJECT).getValue()); 327 getReplacementSet().lookup(ProjectReplacementSet.at_PACKAGE_DIR_at).setReplaceWith(systemPackageDir); 328 getReplacementSet().lookup(ProjectReplacementSet.SHELL_PACKAGE_DIR).setReplaceWith(shellPackageDir); 329 getReplacementSet().lookup(ProjectReplacementSet.JAVA_PACKAGE).setReplaceWith(packageName); 330 } catch (ToolException e) { 331 e.printStackTrace(); 332 throw new GeneratorException(e, 333 res.getString("Unable_to_init_rep")); 334 } 335 } 336 337 public void onTemplate(TemplateEvent event) { 338 PathHandle phOut = null; 339 ReplacementSet eventSet = null; 340 341 phOut = PathHandle.createPathHandle(event.getTemplate().getOutput()); 342 eventSet = new ReplacementSet(event.getTool().getReplacements()); 343 try { 344 if (copyright.length >= 1) { 345 int last = 0; 346 String [] newCR = new String [0]; 347 348 newCR = new String [copyright.length + 2]; 349 newCR[0] = new String (); 350 if (newCR.length >= 1) { 351 last = (newCR.length - 1); 352 } 353 if (phOut.endsWith('/' + FILE_MAKEFILE) 354 || phOut.hasExtension(TYPE_MK)) { 355 for (int i = 1; i < (newCR.length - 1); i++) { 356 newCR[i] = "# " + copyright[i - 1]; } 358 newCR[last] = "#"; } else if (phOut.hasExtension(TYPE_JAVA)) { 360 for (int i = 1; i < (newCR.length - 1); i++) { 361 newCR[i] = " * " + copyright[i - 1]; } 363 newCR[last] = " *"; } else if (phOut.hasExtension(TYPE_XML)) { 365 for (int i = 1; i < (newCR.length - 1); i++) { 366 newCR[i] = " " + copyright[i - 1]; } 368 newCR[last] = " "; } else { 370 newCR = new String [1]; 371 newCR[0] = new String (); 372 } 373 eventSet.lookup(ProjectReplacementSet.COPYRIGHT).setReplaceWith(newCR); 374 } 375 376 if (phOut.hasExtension(TYPE_JAVA)) { 378 File out = phOut.getFile(); 379 char c0 = out.getName().charAt(0); 380 381 if (Character.isLowerCase(c0)) { 382 StringBuffer buf = new StringBuffer (); 383 384 buf.append(out.getParent()); 385 buf.append(File.separator); 386 buf.append(Character.toUpperCase(c0)); 387 if (out.getName().length() > 1) { 388 buf.append(out.getName().substring(1)); 389 } 390 event.getTemplate().setOutput(new File (buf.toString())); 391 buf.setLength(0); 392 buf.append(eventSet.lookup(ProjectReplacementSet.at_PROJECT_at).getReplaceWith()[0]); 393 buf.setCharAt(0, Character.toUpperCase(c0)); 394 eventSet.lookup(ProjectReplacementSet.at_PROJECT_at).setReplaceWith(buf.toString()); 395 } 396 event.getTool().setReplacements(eventSet.toArray()); 397 } 398 } catch (ToolException e) { 399 e.printStackTrace(); 400 } 401 } 402 403 abstract protected String [] getDefaultShellSteps(); 404 abstract protected String [] getDefaultAddinSteps(); 405 406 private void readCopyright() { 408 String copyText = new String (); 409 String copyPath = new String (); 410 StringTokenizer tokenizer = null; 411 File copyFile = null; 412 BufferedReader reader = null; 413 Vector readVector = new Vector (); 414 415 try { 416 copyText = 417 getOptionSet().lookup(ProjectOptionSet.COPYRIGHT).getValue(); 418 if (copyText.trim().length() > 0) { 419 final String NEWLINE = "\n"; 421 tokenizer = new StringTokenizer (copyText, NEWLINE); 422 copyright = new String [tokenizer.countTokens()]; 423 for (int i = 0; i < copyright.length; i++) { 424 copyright[i] = tokenizer.nextToken(); 425 } 426 } 427 } catch (GeneratorException e) { 428 e.printStackTrace(); 429 } 430 if (copyright.length == 0) { 431 try { 432 copyPath = 433 getOptionSet().lookup(ProjectOptionSet.COPYRIGHTFILE).getValue(); 434 if (FileUtil.isFile(copyPath)) { 435 copyFile = new File (copyPath); 436 reader = new BufferedReader (new FileReader (copyFile)); 437 String line = reader.readLine(); 438 439 while (line != null) { 440 readVector.addElement(new String (line)); 441 line = reader.readLine(); 442 } 443 reader.close(); 444 copyright = new String [readVector.size()]; 445 copyright = (String []) readVector.toArray(copyright); 446 readVector.clear(); 447 } 448 } catch (GeneratorException e) { 449 e.printStackTrace(); 450 copyright = new String [0]; 451 } catch (FileNotFoundException e) { 452 e.printStackTrace(); 453 copyright = new String [0]; 454 } catch (IOException e) { 455 e.printStackTrace(); 456 copyright = new String [0]; 457 } 458 } 459 } 460 461 469 public Replacement[] createReplacementsForDirectory(PathHandle path) 470 throws GeneratorException { 471 Replacement[] pathReps = new Replacement[0]; 472 StringBuffer rootPath = new StringBuffer (); 473 PathHandle destHandle = null; 474 PathHandle target = null; 475 PathHandle cursor = null; 476 477 destHandle = PathHandle.createPathHandle(getDestination()); 478 target = PathHandle.createPathHandle(path.getPath()); 479 cursor = PathHandle.createPathHandle(path.getPath()); 480 if (destHandle.parentOf(cursor)) { 481 rootPath.append('.'); 482 rootPath.append('.'); 483 cursor = 484 PathHandle.createPathHandle(cursor.getFile().getParent()); 485 while (destHandle.parentOf(cursor)) { 486 rootPath.append('/'); 487 rootPath.append('.'); 488 rootPath.append('.'); 489 cursor = 490 PathHandle.createPathHandle(cursor.getFile().getParent()); 491 } 492 } else { 493 rootPath.append('.'); 494 } 495 try { 496 getReplacementSet().lookup(ProjectReplacementSet.SHELL_ROOT_PATH).setReplaceWith(rootPath.toString()); 497 } catch (ToolException e) { 498 throw new GeneratorException(e, 499 res.getString("Unable_to_init_root")); 500 } 501 pathReps = getReplacementSet().toArray(); 502 StringBuffer inputBuf = new StringBuffer (); 503 PathHandle inputPath = null; 504 505 inputBuf.append(getDestination().toLowerCase()); 506 inputBuf.append(File.separator); 507 inputBuf.append(DIR_INPUT); 508 inputBuf.append(File.separator); 509 inputPath = PathHandle.createPathHandle(inputBuf.toString()); 510 if (!inputPath.parentOf(target)) { 511 try { 512 Replacement[] confReps = new Replacement[0]; 513 ArrayList newList = null; 514 ReplacementSet pathSet = null; 515 516 confReps = ConfigTool.createReplacements(getDestination() 517 + File.separator 518 + DIR_OUTPUT); 519 newList = new ArrayList (); 520 newList.addAll(Arrays.asList(pathReps)); 521 newList.addAll(Arrays.asList(confReps)); 522 pathReps = (Replacement[]) newList.toArray(pathReps); 523 newList.clear(); 524 pathSet = new ReplacementSet(pathReps); 525 pathSet.lookup(ConfigTool.JAVA_PATH).setReplaceWith(getJavaPath()); 526 pathReps = pathSet.toArray(); 527 } catch (ToolException e) { 528 throw new GeneratorException(e, 529 res.getString("Unable_to_create_path")); 530 } 531 } 532 return pathReps; 533 } 534 535 private String findProjectSourcePath(Template source) 538 throws GeneratorException { 539 String found = null; 540 StringBuffer buf = new StringBuffer (); 541 Template[] list = new Template[0]; 542 543 list = source.listTemplates(); 544 buf.append(ProjectReplacementSet.at_PACKAGE_DIR_at); 545 buf.append('/'); 546 for (int i = 0; i < list.length; i++) { 547 if (list[i].isDirectory()) { 548 if (list[i].getRelativePath().endsWith(buf.toString())) { 549 String pack = new String (); 550 TemplateTool tool = null; 551 552 tool = new TemplateTool(); 553 tool.setReplacements(getReplacementSet().toArray()); 554 tool.setInitDestination(getDestination()); 555 tool.initOutput(list[i]); 556 found = list[i].getOutput().getAbsolutePath(); 557 pack = 558 tool.lineSearchAndReplace(ProjectReplacementSet.at_PACKAGE_DIR_at); 559 found = found.substring(0, 560 found.length() - pack.length()); 561 found = found.replace('\\', '/'); 562 } else { 563 found = findProjectSourcePath(list[i]); 564 } 565 } 566 if (found != null) { 567 break; 568 } 569 } 570 return found; 571 } 572 573 581 private File generateReadme(String destPath) throws GeneratorException { 582 File readme = null; 583 PrintWriter out = null; 584 Replacement rep = null; 585 String projName = new String (); 586 587 readme = new File (destPath + File.separator + FILE_README_HTML); 588 readme.getParentFile().mkdirs(); 589 try { 590 out = new PrintWriter (new FileWriter (readme)); 591 } catch (IOException e) { 592 throw new GeneratorException(e, 593 res.getString("Unable_to_create_readme")); 594 } 595 try { 596 rep = 597 getReplacementSet().lookup(ProjectReplacementSet.at_PROJECT_at); 598 projName = rep.getReplaceWith()[0]; 599 } catch (ToolException e) { 600 projName = res.getString("The_project"); 601 } 602 out.println("<html>"); out.println("<head>"); out.println(" " 605 + ResUtil.format(res.getString("Title_readme"), 606 projName)); 607 out.println("</head>"); out.println("<body>"); out.println("<br>"); if (getAddinSteps() != null && getAddinSteps().length >= 1) { 611 out.println("<h2>" 612 + ResUtil.format(res.getString("To_build_with_kelp"), projName) 613 + "</h2>"); 614 out.println(getAddinSteps()[0]); 615 out.println("<ol>"); for (int i = 1; i < getAddinSteps().length; i++) { 617 out.println(" <li>"); out.println(" " + getAddinSteps()[i]); out.println(" </li>"); } 621 out.println("</ol>"); } 623 if (!cli) { 624 625 } else if (getShellSteps() != null && getShellSteps().length >= 1) { 628 out.println("<h2>" 629 + ResUtil.format(res.getString("To_build_with_shell"), projName) 630 + "</h2>"); 631 out.println("<ol>"); for (int i = 0; i < getShellSteps().length; i++) { 633 out.println(" <li>"); out.println(" " + getShellSteps()[i]); out.println(" </li>"); } 637 out.println("</ol>"); } 639 out.println("</body>"); out.println("</html>"); out.flush(); 642 out.close(); 643 return readme; 644 } 645 646 } 647 | Popular Tags |