1 22 package org.enhydra.kelp.common.node; 23 24 import org.enhydra.tool.common.PathHandle; 26 import org.enhydra.tool.ToolBoxInfo; 27 28 import org.enhydra.xml.xmlc.compiler.Parse; 30 import org.enhydra.xml.xmlc.XMLCException; 31 32 import org.enhydra.kelp.common.Constants; 34 import org.enhydra.kelp.common.PropUtil; 35 import org.enhydra.kelp.common.bridge.XMLCConnectionFactory; 36 import org.enhydra.kelp.common.bridge.MetaDataHandler; 37 import org.enhydra.kelp.common.map.Mapper; 38 import org.enhydra.kelp.common.node.OtterProject; 39 40 import java.io.File ; 42 import java.io.IOException ; 43 import java.io.PrintWriter ; 44 import java.lang.reflect.Constructor ; 45 import java.lang.reflect.Method ; 46 import java.lang.reflect.InvocationTargetException ; 47 import java.util.ArrayList ; 48 import java.util.Arrays ; 49 import java.util.Vector ; 50 import java.util.StringTokenizer ; 51 import java.util.ResourceBundle ; 52 53 59 public class OtterXMLCNode implements OtterDocumentNode { 60 61 static ResourceBundle res = 63 ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); private final String POUND = "#"; private final String DOMFACTORY = "-domfactory"; private final String IMPL = "Impl"; 68 public final static int CLASS_NAME_DEFAULT = 1; 70 public final static int CLASS_NAME_CUSTOM = 2; 71 public final static int CLASS_NAME_MAPPED = 3; 72 73 private MetaDataHandler metaDataHandler; 75 private OtterDocumentNode docNode; 76 77 81 public OtterXMLCNode(OtterDocumentNode d) { 82 docNode = d; 83 initMetaDataHandler(); 84 } 85 86 92 public String getXMLCOptionFilePath() { 93 return docNode.getXMLCOptionFilePath(); 94 } 95 96 102 public void setXMLCOptionFilePath(String n) { 103 docNode.setXMLCOptionFilePath(n); 104 } 105 106 112 public String getXMLCParameters() { 113 String params = new String (); 114 StringBuffer buf = new StringBuffer (); 115 int index = -1; 116 117 params = docNode.getXMLCParameters(); 118 params = PropUtil.removeQuotes(params); 119 return params; 120 } 121 122 128 public void setXMLCParameters(String p) { 129 docNode.setXMLCParameters(p); 130 } 131 132 138 public boolean isSelected() { 139 return docNode.isSelected(); 140 } 141 142 148 public void setSelected(boolean b) { 149 docNode.setSelected(b); 150 if (b) { 151 setStatic(false); 152 } 153 } 154 155 public boolean isStatic() { 156 return docNode.isStatic(); 157 } 158 159 public void setStatic(boolean b) { 160 docNode.setStatic(b); 161 if (b) { 162 setSelected(false); 163 } 164 } 165 166 172 public String getFilePath() { 173 return docNode.getFilePath(); 174 } 175 176 182 public String getCustomClassName() { 183 String custom = getProperty(PropertyKeys.NAME_XMLC_CUSTOM); 184 185 if (custom == null) { 186 custom = new String (); 187 } 188 return custom; 189 } 190 191 197 public void setCustomClassName(String n) { 198 setProperty(PropertyKeys.NAME_XMLC_CUSTOM, n); 199 } 200 201 207 public Object getNativeNode() { 208 return docNode.getNativeNode(); 209 } 210 211 217 public void setNativeNode(Object o) { 218 docNode.setNativeNode(o); 219 } 220 221 227 public OtterProject getProject() { 228 return docNode.getProject(); 229 } 230 231 239 public String getProperty(String n) { 240 return docNode.getProperty(n); 241 } 242 243 250 public void setProperty(String n, String v) { 251 docNode.setProperty(n, v); 252 } 253 254 261 public void setProperty(String n, int v) { 262 docNode.setProperty(n, v); 263 } 264 265 271 public int getClassNameType() { 272 273 int type; 276 int projectScope; 277 String stringType = getProperty(PropertyKeys.NAME_XMLC_TYPE); 278 279 projectScope = getProject().getMapScope(); 280 type = PropUtil.stringToInt(stringType, CLASS_NAME_MAPPED); 281 switch (type) { 282 case CLASS_NAME_DEFAULT: 283 case CLASS_NAME_CUSTOM: 284 if (projectScope == OtterProject.MAP_SCOPE_ALL) { 285 type = CLASS_NAME_MAPPED; 286 setProperty(PropertyKeys.NAME_XMLC_TYPE, type); 287 } 288 break; 289 case CLASS_NAME_MAPPED: 290 if (projectScope == OtterProject.MAP_SCOPE_NONE) { 291 type = CLASS_NAME_DEFAULT; 292 setProperty(PropertyKeys.NAME_XMLC_TYPE, type); 293 } 294 break; 295 } 296 return type; 297 } 298 299 305 public void setClassNameType(int newType) { 306 307 int oldType = getClassNameType(); 310 int projectScope = getProject().getMapScope(); 311 312 setProperty(PropertyKeys.NAME_XMLC_TYPE, newType); 313 syncProjectToType(newType); 314 if ((newType != oldType) 315 || (newType == OtterXMLCNode.CLASS_NAME_MAPPED)) { 316 initClassName(); 317 } 318 } 319 320 328 private String [] getAllParameters(boolean forPrint) { 329 StringBuffer buf = new StringBuffer (); 330 String [] parameters = new String [0]; 331 StringTokenizer tokenizer = null; 332 int count = 0; 333 int index = 0; 334 335 if (getXMLCParameters() != null) { 337 buf.append(getXMLCParameters().trim()); 338 } 339 if (forPrint) { 340 buf.append('#'); 341 } else { 342 buf.append(' '); 343 } 344 345 OtterNode packNode = null; 347 String packParam = new String (); 348 349 packNode = docNode.getParent(); 350 if (packNode == null || packNode instanceof OtterProject) { 351 352 } else { 354 packParam = packNode.getXMLCParameters(); 355 } 356 if ((packParam != null) && (packParam.trim().length() > 0)) { 357 buf.append(packParam); 358 if (forPrint) { 359 buf.append('#'); 360 } else { 361 buf.append(' '); 362 } 363 } 364 365 if (docNode.getProject().getXMLCParameters() != null) { 367 buf.append(docNode.getProject().getXMLCParameters().trim()); 368 } 369 if (forPrint) { 370 tokenizer = new StringTokenizer (buf.toString().trim(), POUND); 371 } else { 372 tokenizer = new StringTokenizer (buf.toString().trim()); 373 } 374 count = tokenizer.countTokens(); 375 parameters = new String [tokenizer.countTokens()]; 376 while (tokenizer.hasMoreTokens()) { 377 parameters[index] = tokenizer.nextToken(); 378 index++; 379 } 380 381 parameters = addDomFactory(parameters); 383 return parameters; 384 } 385 386 private String [] addDomFactory(String [] in) { 387 PathHandle path = null; 388 String [] out = in; 389 ArrayList list = null; 390 boolean found = false; 391 392 path = PathHandle.createPathHandle(docNode.getFilePath()); 393 for (int i = 0; i < in.length; i++) { 394 if (in[i].trim().equalsIgnoreCase(DOMFACTORY)) { 395 found = true; 396 break; 397 } 398 } 399 if (found) { 400 401 } else if (path.hasExtension(Constants.TYPE_HTML) 403 || path.hasExtension(Constants.TYPE_HTM)) { 404 405 } else if (path.hasExtension(Constants.TYPE_WML) 407 && ToolBoxInfo.isClassAvailable(ToolBoxInfo.WML_FACTORY)) { 408 list = new ArrayList (Arrays.asList(in)); 409 list.add(DOMFACTORY); 410 list.add(ToolBoxInfo.WML_FACTORY); 411 out = (String []) list.toArray(out); 412 } else if (path.hasExtension(Constants.TYPE_CHTML) 413 && ToolBoxInfo.isClassAvailable(ToolBoxInfo.CHTML_FACTORY)) { 414 list = new ArrayList (Arrays.asList(in)); 415 list.add(DOMFACTORY); 416 list.add(ToolBoxInfo.CHTML_FACTORY); 417 out = (String []) list.toArray(out); 418 } else if (path.hasExtension(Constants.TYPE_XHTML) 419 && ToolBoxInfo.isClassAvailable(ToolBoxInfo.XHTML_FACTORY)) { 420 list = new ArrayList (Arrays.asList(in)); 421 list.add(DOMFACTORY); 422 list.add(ToolBoxInfo.XHTML_FACTORY); 423 out = (String []) list.toArray(out); 424 } 425 return out; 426 } 427 428 434 private String [] getAllOptionFilenames() { 435 StringBuffer buf = new StringBuffer (); 436 File file = null; 437 PathHandle handle = null; 438 String filename = new String (); 439 String [] filenames = new String [0]; 440 StringTokenizer tokenizer = null; 441 int count = 0; 442 int index = 0; 443 444 handle = 446 PathHandle.createPathHandle(docNode.getProject().getXMLCOptionFilePath()); 447 if (handle.isFile() && handle.hasExtension(Constants.TYPE_XMLC)) { 448 buf.append(handle.getPath()); 449 } 450 451 OtterNode parent = docNode.getParent(); 453 454 if (parent == null || parent instanceof OtterProject) { 455 456 } else { 458 handle = 459 PathHandle.createPathHandle(parent.getXMLCOptionFilePath()); 460 if (handle.isFile() && handle.hasExtension(Constants.TYPE_XMLC)) { 461 buf.append('#'); 462 buf.append(handle.getPath()); 463 } 464 } 465 buf.append('#'); 466 467 handle = PathHandle.createPathHandle(getXMLCOptionFilePath()); 469 if (handle.isFile() && handle.hasExtension(Constants.TYPE_XMLC)) { 470 buf.append(handle.getPath()); 471 } 472 tokenizer = new StringTokenizer (buf.toString().trim(), POUND); 473 count = tokenizer.countTokens(); 474 filenames = new String [tokenizer.countTokens()]; 475 while (tokenizer.hasMoreTokens()) { 476 filenames[index] = tokenizer.nextToken(); 477 index++; 478 } 479 return filenames; 480 } 481 482 488 private void syncProjectToType(int type) { 489 int projectScope = getProject().getMapScope(); 490 491 switch (type) { 492 case CLASS_NAME_CUSTOM: 493 case CLASS_NAME_DEFAULT: 494 if (projectScope == OtterProject.MAP_SCOPE_ALL) { 495 getProject().setMapScope(OtterProject.MAP_SCOPE_SELECTED); 496 } 497 break; 498 case CLASS_NAME_MAPPED: 499 if (projectScope == OtterProject.MAP_SCOPE_NONE) { 500 getProject().setMapScope(OtterProject.MAP_SCOPE_SELECTED); 501 } 502 break; 503 } 504 } 505 506 513 public void setException(Throwable e) { 514 if (e == null) { 515 docNode.setException(null); 516 } else { 517 docNode.setException(new LocalException(e)); 518 } 519 } 520 521 525 public Throwable getException() { 526 return docNode.getException(); 527 } 528 529 public OtterNode getParent() { 530 return docNode.getParent(); 531 } 532 533 public void save() { 534 docNode.save(); 535 } 536 537 540 public MetaDataHandler getMetaDataHandler() { 541 return metaDataHandler; 542 } 543 544 549 public void replaceGeneratedSource(boolean forRecomp) { 550 if ((metaDataHandler.getJavaClassSource().exists()) 551 && (getProject().isOpenBuild())) { 552 int count = 1; 553 554 if (forRecomp) { 555 count = 2; 556 } 557 String [] source = new String [count]; 558 OtterNodeFactory factory = null; 559 560 source[0] = 561 metaDataHandler.getJavaClassSource().getAbsolutePath(); 562 factory = getProject().getNodeFactory(); 563 if (forRecomp) { 564 source[1] = 565 metaDataHandler.getJavaInterfaceSource().getAbsolutePath(); 566 } 567 factory.replaceGeneratedSource(getProject(), docNode, source); 568 } 569 } 570 571 575 public void initProjectOptions() { 576 metaDataHandler.setKeepGeneratedSource(true); metaDataHandler.setCompileSource(false); setXMLCOptionFilePath(docNode.getProject().getSourcePath()+getResourceRelativePath()+"/resources/options.xmlc"); 579 metaDataHandler.setPrintAccessorInfo(getProject().isPrintAccessorInfo()); 581 metaDataHandler.setPrintDocumentInfo(getProject().isPrintDocumentInfo()); 582 metaDataHandler.setPrintDOM(getProject().isPrintDOM()); 583 metaDataHandler.setPrintParseInfo(getProject().isPrintParseInfo()); 584 metaDataHandler.setVerbose(getProject().isVerbose()); 585 586 } 588 589 public String getGenerateToRoot() { 590 String projectGenTo = getProject().getGenerateToDirectory(); 591 StringBuffer buf = new StringBuffer (); 592 593 if (projectGenTo == null) { 594 buf.append(docNode.getProject().getSourcePathOf(docNode)); 595 } else { 596 buf.append(projectGenTo); 597 } 598 return buf.toString(); 599 } 600 601 608 private void initMetaDataHandler() { 609 metaDataHandler = XMLCConnectionFactory.createMetaDataHandler(); 610 initClassName(); 611 initProjectOptions(); 612 metaDataHandler.setInputDocument(getFilePath()); 613 initJavaSourceFile(); 614 } 615 616 623 private void parseOptions() throws XMLCException, IOException { 624 metaDataHandler.parse(getAllOptionFilenames(), 625 getAllParameters(false), (PrintWriter ) null, 626 this); 627 } 628 629 633 private void initJavaSourceFile() { 634 StringBuffer filename = new StringBuffer (); 635 String className = new String (); 636 String packageName = new String (); 637 File source = null; 638 int index = -1; 639 640 filename.append(getGenToDirectory().toString()); 641 filename.append(File.separator); 642 className = metaDataHandler.getClassName(); 643 packageName = metaDataHandler.getPackageName(); 644 if ((packageName == null) || (packageName.trim().length() == 0)) { 645 index = -1; 646 } else { 647 index = className.indexOf(packageName); 648 } 649 if (index > -1) { 650 className = 651 className.substring(metaDataHandler.getPackageName().length() 652 + 1); 653 } 654 index = className.lastIndexOf('.'); 655 if (index > -1) { 656 className = className.substring(index + 1); 657 } 658 filename.append(className); 659 filename.append('.'); 660 filename.append(Constants.TYPE_JAVA); 661 source = new File (filename.toString()); 662 metaDataHandler.setJavaClassSource(source, this); 663 } 664 665 669 private File getGenToDirectory() { 670 String genTo = getGenToPath(); 671 File genDir = new File (genTo); 672 673 if (genDir.exists()) { 674 if (!genDir.isDirectory()) { 675 System.err.println(res.getString("Unable_to_write") + genTo); 676 } 677 } else { 678 genDir.mkdirs(); 679 } 680 return genDir; 681 } 682 683 687 private String getGenToPath() { 688 String packagePath = getPackagePath(); 689 StringBuffer buf = new StringBuffer (); 690 691 buf.append(getGenerateToRoot()); 692 if (packagePath.length() > 0) { 693 buf.append(File.separator); 694 buf.append(packagePath); 695 } 696 return buf.toString(); 697 } 698 699 705 private String getPackagePath() { 706 String packPath = new String (); 707 708 if (metaDataHandler.getPackageName() != null) { 709 packPath = metaDataHandler.getPackageName(); 710 } else if (metaDataHandler.getClassName() != null) { 711 int index = metaDataHandler.getClassName().lastIndexOf('.'); 712 713 if (index > -1) { 714 packPath = metaDataHandler.getClassName().substring(0, index); 715 } 716 } 717 packPath = packPath.replace('.', File.separatorChar); 718 return packPath; 719 } 720 721 726 private void initClassName() { 727 Mapper mapper = new Mapper(getProject()); 728 String className = mapper.getClassName(this); 729 730 metaDataHandler.setClassName(className); 731 } 732 733 private String getResourceRelativePath() { 734 StringBuffer buf = new StringBuffer (); 735 String path = new String (); 736 737 buf.append(File.separator); 738 path = metaDataHandler.getPackageName(); 739 int index = path.lastIndexOf("presentation"); 740 if(index !=-1) { 741 path = path.substring(0,index-1); 742 } 743 path = path.replace('.', File.separatorChar); 744 buf.append(path); 745 return buf.toString(); 746 } 747 748 749 private String baseOut() { 750 StringBuffer buf = new StringBuffer (); 751 String path = new String (); 752 753 buf.append(getProject().getClassOutputPath()); 754 buf.append(File.separator); 755 path = metaDataHandler.getClassName(); 756 path = path.replace('.', File.separatorChar); 757 buf.append(path); 758 return buf.toString(); 759 } 760 761 765 public File getClassFile() { 766 File file = null; 767 StringBuffer buf = new StringBuffer (); 768 769 buf.append(baseOut()); 770 buf.append('.'); 771 buf.append(Constants.TYPE_CLASS); 772 file = new File (buf.toString()); 773 return file; 774 } 775 776 public File getOptionFileForRecomp() { 777 File file = null; 778 StringBuffer buf = new StringBuffer (); 779 780 buf.append(baseOut()); 781 buf.append('.'); 782 buf.append(Constants.TYPE_XMLC); 783 file = new File (buf.toString()); 784 return file; 785 } 786 787 791 public boolean isNewJavaFound() { 792 boolean newFound = false; 793 794 if (metaDataHandler.getKeepGeneratedSource()) { 795 newFound = metaDataHandler.getJavaClassSource().exists(); 796 } 797 return newFound; 798 } 799 800 807 public void preCompile() throws XMLCException, IOException { 808 parseOptions(); 809 810 File implSource = null; 812 StringBuffer implName = new StringBuffer (); 813 int index = -1; 814 815 implName.append(metaDataHandler.getJavaClassSource()); 816 index = implName.toString().lastIndexOf('.'); 817 implName.insert(index, IMPL); 818 implSource = new File (implName.toString()); 819 if (implSource.exists()) { 820 implSource.delete(); 821 } 822 if (metaDataHandler.getJavaClassSource().exists()) { 823 metaDataHandler.getJavaClassSource().delete(); 824 } 825 826 if (metaDataHandler.getRecompilation()) { 828 metaDataHandler.setJavaInterfaceSource(metaDataHandler.getJavaClassSource(), 829 this); 830 metaDataHandler.setJavaClassSource(implSource, this); 831 } 832 docNode.preCompile(); 833 } 834 835 840 class LocalException extends Exception { 841 private Throwable baseException = null; 842 843 849 public LocalException(Throwable e) { 850 baseException = e; 851 } 852 853 859 public String getMessage() { 860 StringBuffer buf = new StringBuffer (); 861 String [] parameters = getAllParameters(true); 862 String [] filenames = getAllOptionFilenames(); 863 864 buf.append(baseException.getMessage()); 865 buf.append('\n'); 866 if (parameters.length >= 1) { 867 buf.append(Constants.TAB2); 868 buf.append(res.getString("Command_line")); 869 buf.append('\n'); 870 for (int i = 0; i < parameters.length; i++) { 871 buf.append(Constants.TAB4); 872 buf.append(parameters[i]); 873 buf.append('\n'); 874 } 875 } 876 if (filenames.length >= 1) { 877 buf.append(Constants.TAB2); 878 if (filenames.length >= 2) { 879 buf.append(res.getString("Option_files")); 880 } else { 881 buf.append(res.getString("Option_file")); 882 } 883 buf.append('\n'); 884 for (int i = 0; i < filenames.length; i++) { 885 buf.append(Constants.TAB4); 886 buf.append(filenames[i]); 887 } 888 } 889 return buf.toString().trim(); 890 } 891 892 } 893 } 894 | Popular Tags |