1 16 17 package xni; 18 19 import java.io.OutputStream ; 20 import java.io.OutputStreamWriter ; 21 import java.io.PrintWriter ; 22 import java.io.UnsupportedEncodingException ; 23 import java.io.Writer ; 24 25 import org.apache.xerces.parsers.XMLDocumentParser; 26 import org.apache.xerces.xni.Augmentations; 27 import org.apache.xerces.xni.NamespaceContext; 28 import org.apache.xerces.xni.QName; 29 import org.apache.xerces.xni.XMLAttributes; 30 import org.apache.xerces.xni.XMLDTDContentModelHandler; 31 import org.apache.xerces.xni.XMLDTDHandler; 32 import org.apache.xerces.xni.XMLLocator; 33 import org.apache.xerces.xni.XMLResourceIdentifier; 34 import org.apache.xerces.xni.XMLString; 35 import org.apache.xerces.xni.XNIException; 36 import org.apache.xerces.xni.parser.XMLConfigurationException; 37 import org.apache.xerces.xni.parser.XMLErrorHandler; 38 import org.apache.xerces.xni.parser.XMLInputSource; 39 import org.apache.xerces.xni.parser.XMLParseException; 40 import org.apache.xerces.xni.parser.XMLParserConfiguration; 41 42 51 public class DocumentTracer 52 extends XMLDocumentParser 53 implements XMLErrorHandler { 54 55 59 61 62 protected static final String NAMESPACES_FEATURE_ID = 63 "http://xml.org/sax/features/namespaces"; 64 65 66 protected static final String VALIDATION_FEATURE_ID = 67 "http://xml.org/sax/features/validation"; 68 69 70 protected static final String SCHEMA_VALIDATION_FEATURE_ID = 71 "http://apache.org/xml/features/validation/schema"; 72 73 74 protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = 75 "http://apache.org/xml/features/validation/schema-full-checking"; 76 77 78 protected static final String NOTIFY_CHAR_REFS_FEATURE_ID = 79 "http://apache.org/xml/features/scanner/notify-char-refs"; 80 81 83 84 protected static final String DEFAULT_PARSER_CONFIG = 85 "org.apache.xerces.parsers.XIncludeAwareParserConfiguration"; 86 87 88 protected static final boolean DEFAULT_NAMESPACES = true; 89 90 91 protected static final boolean DEFAULT_VALIDATION = false; 92 93 94 protected static final boolean DEFAULT_SCHEMA_VALIDATION = false; 95 96 97 protected static final boolean DEFAULT_SCHEMA_FULL_CHECKING = false; 98 99 100 protected static final boolean DEFAULT_NOTIFY_CHAR_REFS = false; 101 102 106 107 private QName fQName = new QName(); 108 109 110 protected PrintWriter fOut; 111 112 113 protected int fIndent; 114 115 protected NamespaceContext fNamespaceContext; 116 117 121 122 public DocumentTracer() { 123 this(null); 124 } 126 127 public DocumentTracer(XMLParserConfiguration config) { 128 super(config); 129 setOutput(new PrintWriter (System.out)); 130 fConfiguration.setErrorHandler(this); 131 } 133 137 138 public void setOutput(OutputStream stream, String encoding) 139 throws UnsupportedEncodingException { 140 141 if (encoding == null) { 142 encoding = "UTF8"; 143 } 144 145 Writer writer = new OutputStreamWriter (stream, encoding); 146 fOut = new PrintWriter (writer); 147 148 } 150 151 public void setOutput(Writer writer) { 152 153 fOut = writer instanceof PrintWriter 154 ? (PrintWriter )writer : new PrintWriter (writer); 155 156 } 158 162 175 public void startDocument(XMLLocator locator, String encoding, 176 NamespaceContext namespaceContext, Augmentations augs) 177 throws XNIException { 178 fNamespaceContext = namespaceContext; 179 fIndent = 0; 180 printIndent(); 181 fOut.print("startDocument("); 182 fOut.print("locator="); 183 if (locator == null) { 184 fOut.print("null"); 185 } 186 else { 187 fOut.print('{'); 188 fOut.print("publicId="); 189 printQuotedString(locator.getPublicId()); 190 fOut.print(','); 191 fOut.print("literal systemId="); 192 printQuotedString(locator.getLiteralSystemId()); 193 fOut.print(','); 194 fOut.print("baseSystemId="); 195 printQuotedString(locator.getBaseSystemId()); 196 fOut.print(','); 197 fOut.print("expanded systemId="); 198 printQuotedString(locator.getExpandedSystemId()); 199 fOut.print(','); 200 fOut.print("lineNumber="); 201 fOut.print(locator.getLineNumber()); 202 fOut.print(','); 203 fOut.print("columnNumber="); 204 fOut.print(locator.getColumnNumber()); 205 fOut.print('}'); 206 } 207 fOut.print(','); 208 fOut.print("encoding="); 209 printQuotedString(encoding); 210 if (augs != null) { 211 fOut.print(','); 212 printAugmentations(augs); 213 } 214 fOut.println(')'); 215 fOut.flush(); 216 fIndent++; 217 218 } 220 221 public void xmlDecl(String version, String encoding, 222 String standalone, Augmentations augs) throws XNIException { 223 224 printIndent(); 225 fOut.print("xmlDecl("); 226 fOut.print("version="); 227 printQuotedString(version); 228 fOut.print(','); 229 fOut.print("encoding="); 230 printQuotedString(encoding); 231 fOut.print(','); 232 fOut.print("standalone="); 233 printQuotedString(standalone); 234 if (augs != null) { 235 fOut.print(','); 236 printAugmentations(augs); 237 } 238 fOut.println(')'); 239 240 } 242 243 public void doctypeDecl(String rootElement, String publicId, 244 String systemId, Augmentations augs) throws XNIException { 245 246 printIndent(); 247 fOut.print("doctypeDecl("); 248 fOut.print("rootElement="); 249 printQuotedString(rootElement); 250 fOut.print(','); 251 fOut.print("publicId="); 252 printQuotedString(publicId); 253 fOut.print(','); 254 fOut.print("systemId="); 255 printQuotedString(systemId); 256 fOut.println(')'); 257 if (augs != null) { 258 fOut.print(','); 259 printAugmentations(augs); 260 } 261 fOut.flush(); 262 263 } 265 266 public void startElement(QName element, XMLAttributes attributes, Augmentations augs) 267 throws XNIException { 268 269 printInScopeNamespaces(); 270 271 printIndent(); 272 fOut.print("startElement("); 273 printElement(element, attributes); 274 if (augs != null) { 275 fOut.print(','); 276 printAugmentations(augs); 277 } 278 fOut.println(')'); 279 fOut.flush(); 280 fIndent++; 281 282 } 284 285 public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs) 286 throws XNIException { 287 printInScopeNamespaces(); 288 printIndent(); 289 fOut.print("emptyElement("); 290 printElement(element, attributes); 291 if (augs != null) { 292 fOut.print(','); 293 printAugmentations(augs); 294 } 295 fOut.println(')'); 296 fOut.flush(); 297 printEndNamespaceMapping(); 298 } 300 301 public void characters(XMLString text, Augmentations augs) throws XNIException { 302 303 printIndent(); 304 fOut.print("characters("); 305 fOut.print("text="); 306 printQuotedString(text.ch, text.offset, text.length); 307 314 if (augs != null) { 315 fOut.print(','); 316 printAugmentations(augs); 317 } 318 fOut.println(')'); 319 fOut.flush(); 320 321 } 323 324 325 326 public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException { 327 328 printIndent(); 329 fOut.print("ignorableWhitespace("); 330 fOut.print("text="); 331 printQuotedString(text.ch, text.offset, text.length); 332 if (augs != null) { 333 fOut.print(','); 334 printAugmentations(augs); 335 } 336 fOut.println(')'); 337 fOut.flush(); 338 339 } 341 342 public void endElement(QName element, Augmentations augs) throws XNIException { 343 344 fIndent--; 345 printIndent(); 346 fOut.print("endElement("); 347 fOut.print("element="); 348 fOut.print('{'); 349 fOut.print("prefix="); 350 printQuotedString(element.prefix); 351 fOut.print(','); 352 fOut.print("localpart="); 353 printQuotedString(element.localpart); 354 fOut.print(','); 355 fOut.print("rawname="); 356 printQuotedString(element.rawname); 357 fOut.print(','); 358 fOut.print("uri="); 359 printQuotedString(element.uri); 360 fOut.print('}'); 361 if (augs != null) { 362 fOut.print(','); 363 printAugmentations(augs); 364 } 365 fOut.println(')'); 366 fOut.flush(); 367 368 printEndNamespaceMapping(); 369 370 } 372 373 374 public void startCDATA(Augmentations augs) throws XNIException { 375 376 printIndent(); 377 fOut.print("startCDATA("); 378 if (augs != null) { 379 printAugmentations(augs); 380 } 381 fOut.println(')'); 382 fOut.flush(); 383 fIndent++; 384 385 } 387 388 public void endCDATA(Augmentations augs) throws XNIException { 389 390 fIndent--; 391 printIndent(); 392 fOut.print("endCDATA("); 393 if (augs != null) { 394 fOut.print(','); 395 printAugmentations(augs); 396 } 397 fOut.println(')'); 398 fOut.flush(); 399 400 } 402 403 public void startGeneralEntity(String name, 404 XMLResourceIdentifier identifier, 405 String encoding, Augmentations augs) 406 throws XNIException { 407 408 printIndent(); 409 fOut.print("startGeneralEntity("); 410 fOut.print("name="); 411 printQuotedString(name); 412 fOut.print(','); 413 fOut.print("identifier="); 414 fOut.print(identifier); 415 fOut.print(','); 416 fOut.print("encoding="); 417 printQuotedString(encoding); 418 if (augs != null) { 419 fOut.print(','); 420 printAugmentations(augs); 421 } 422 fOut.println(')'); 423 fOut.flush(); 424 fIndent++; 425 426 } 428 429 public void textDecl(String version, String encoding, Augmentations augs) throws XNIException { 430 431 printIndent(); 432 fOut.print("textDecl("); 433 fOut.print("version="); 434 printQuotedString(version); 435 fOut.print(','); 436 fOut.print("encoding="); 437 printQuotedString(encoding); 438 if (augs != null) { 439 fOut.print(','); 440 printAugmentations(augs); 441 } 442 fOut.println(')'); 443 fOut.flush(); 444 445 } 447 448 public void comment(XMLString text, Augmentations augs) throws XNIException { 449 450 printIndent(); 451 fOut.print("comment("); 452 fOut.print("text="); 453 printQuotedString(text.ch, text.offset, text.length); 454 if (augs != null) { 455 fOut.print(','); 456 printAugmentations(augs); 457 } 458 fOut.println(')'); 459 fOut.flush(); 460 461 } 463 464 public void processingInstruction(String target, XMLString data, Augmentations augs) 465 throws XNIException { 466 467 printIndent(); 468 fOut.print("processingInstruction("); 469 fOut.print("target="); 470 printQuotedString(target); 471 fOut.print(','); 472 fOut.print("data="); 473 printQuotedString(data.ch, data.offset, data.length); 474 if (augs != null) { 475 fOut.print(','); 476 printAugmentations(augs); 477 } 478 fOut.println(')'); 479 fOut.flush(); 480 481 } 483 484 public void endGeneralEntity(String name, Augmentations augs) throws XNIException { 485 486 fIndent--; 487 printIndent(); 488 fOut.print("endGeneralEntity("); 489 fOut.print("name="); 490 printQuotedString(name); 491 if (augs != null) { 492 fOut.print(','); 493 printAugmentations(augs); 494 } 495 fOut.println(')'); 496 fOut.flush(); 497 498 } 500 501 public void endDocument(Augmentations augs) throws XNIException { 502 503 fIndent--; 504 printIndent(); 505 fOut.print("endDocument("); 506 if (augs != null) { 507 fOut.print(','); 508 printAugmentations(augs); 509 } 510 fOut.println(')'); 511 fOut.flush(); 512 513 } 515 519 520 public void startDTD(XMLLocator locator, Augmentations augs) throws XNIException { 521 522 printIndent(); 523 fOut.print("startDTD("); 524 fOut.print("locator="); 525 if (locator == null) { 526 fOut.print("null"); 527 } 528 else { 529 fOut.print('{'); 530 fOut.print("publicId="); 531 printQuotedString(locator.getPublicId()); 532 fOut.print(','); 533 fOut.print("literal systemId="); 534 printQuotedString(locator.getLiteralSystemId()); 535 fOut.print(','); 536 fOut.print("baseSystemId="); 537 printQuotedString(locator.getBaseSystemId()); 538 fOut.print(','); 539 fOut.print("expanded systemId="); 540 printQuotedString(locator.getExpandedSystemId()); 541 fOut.print(','); 542 fOut.print("lineNumber="); 543 fOut.print(locator.getLineNumber()); 544 fOut.print(','); 545 fOut.print("columnNumber="); 546 fOut.print(locator.getColumnNumber()); 547 fOut.print('}'); 548 } 549 if (augs != null) { 550 fOut.print(','); 551 printAugmentations(augs); 552 } 553 fOut.println(')'); 554 fOut.flush(); 555 fIndent++; 556 557 } 559 560 public void startExternalSubset(XMLResourceIdentifier identifier, 561 Augmentations augs) throws XNIException { 562 563 printIndent(); 564 fOut.print("startExternalSubset("); 565 if (augs != null) { 566 fOut.print(','); 567 printAugmentations(augs); 568 } 569 fOut.println(')'); 570 fOut.flush(); 571 fIndent++; 572 573 } 575 576 public void endExternalSubset(Augmentations augs) throws XNIException { 577 578 fIndent--; 579 printIndent(); 580 fOut.print("endExternalSubset("); 581 if (augs != null) { 582 fOut.print(','); 583 printAugmentations(augs); 584 } 585 fOut.println(')'); 586 fOut.flush(); 587 588 } 590 591 public void ignoredCharacters(XMLString text, Augmentations augs) throws XNIException { 592 593 printIndent(); 594 fOut.print("ignoredCharacters("); 595 fOut.print("text="); 596 printQuotedString(text.ch, text.offset, text.length); 597 if (augs != null) { 598 fOut.print(','); 599 printAugmentations(augs); 600 } 601 fOut.println(')'); 602 fOut.flush(); 603 604 } 606 607 public void startParameterEntity(String name, 608 XMLResourceIdentifier identifier, 609 String encoding, Augmentations augs) 610 throws XNIException { 611 612 printIndent(); 613 fOut.print("startParameterEntity("); 614 fOut.print("name="); 615 printQuotedString(name); 616 fOut.print(','); 617 fOut.print("identifier="); 618 fOut.print(identifier); 619 fOut.print(','); 620 fOut.print("encoding="); 621 printQuotedString(encoding); 622 if (augs != null) { 623 fOut.print(','); 624 printAugmentations(augs); 625 } 626 fOut.println(')'); 627 fOut.flush(); 628 fIndent++; 629 630 } 632 633 public void endParameterEntity(String name, Augmentations augs) throws XNIException { 634 635 fIndent--; 636 printIndent(); 637 fOut.print("endParameterEntity("); 638 fOut.print("name="); 639 printQuotedString(name); 640 if (augs != null) { 641 fOut.print(','); 642 printAugmentations(augs); 643 } 644 fOut.println(')'); 645 fOut.flush(); 646 647 } 649 650 public void elementDecl(String name, String contentModel, Augmentations augs) 651 throws XNIException { 652 653 printIndent(); 654 fOut.print("elementDecl("); 655 fOut.print("name="); 656 printQuotedString(name); 657 fOut.print(','); 658 fOut.print("contentModel="); 659 printQuotedString(contentModel); 660 if (augs != null) { 661 fOut.print(','); 662 printAugmentations(augs); 663 } 664 fOut.println(')'); 665 fOut.flush(); 666 667 } 669 670 public void startAttlist(String elementName, Augmentations augs) throws XNIException { 671 672 printIndent(); 673 fOut.print("startAttlist("); 674 fOut.print("elementName="); 675 printQuotedString(elementName); 676 if (augs != null) { 677 fOut.print(','); 678 printAugmentations(augs); 679 } 680 fOut.println(')'); 681 fOut.flush(); 682 fIndent++; 683 684 } 686 687 public void attributeDecl(String elementName, String attributeName, 688 String type, String [] enumeration, 689 String defaultType, XMLString defaultValue, 690 XMLString nonNormalizedDefaultValue, 691 Augmentations augs) throws XNIException { 692 693 printIndent(); 694 fOut.print("attributeDecl("); 695 fOut.print("elementName="); 696 printQuotedString(elementName); 697 fOut.print(','); 698 fOut.print("attributeName="); 699 printQuotedString(attributeName); 700 fOut.print(','); 701 fOut.print("type="); 702 printQuotedString(type); 703 fOut.print(','); 704 fOut.print("enumeration="); 705 if (enumeration == null) { 706 fOut.print("null"); 707 } 708 else { 709 fOut.print('{'); 710 for (int i = 0; i < enumeration.length; i++) { 711 printQuotedString(enumeration[i]); 712 if (i < enumeration.length - 1) { 713 fOut.print(','); 714 } 715 } 716 fOut.print('}'); 717 } 718 fOut.print(','); 719 fOut.print("defaultType="); 720 printQuotedString(defaultType); 721 fOut.print(','); 722 fOut.print("defaultValue="); 723 if (defaultValue == null) { 724 fOut.print("null"); 725 } 726 else { 727 printQuotedString(defaultValue.ch, defaultValue.offset, 728 defaultValue.length); 729 } 730 fOut.print(','); 731 fOut.print("nonNormalizedDefaultValue="); 732 if (nonNormalizedDefaultValue == null) { 733 fOut.print("null"); 734 } 735 else { 736 printQuotedString(nonNormalizedDefaultValue.ch, nonNormalizedDefaultValue.offset, 737 nonNormalizedDefaultValue.length); 738 } 739 if (augs != null) { 740 fOut.print(','); 741 printAugmentations(augs); 742 } 743 fOut.println(')'); 744 fOut.flush(); 745 746 } 748 749 public void endAttlist(Augmentations augs) throws XNIException { 750 751 fIndent--; 752 printIndent(); 753 fOut.print("endAttlist("); 754 if (augs != null) { 755 fOut.print(','); 756 printAugmentations(augs); 757 } 758 fOut.println(')'); 759 fOut.flush(); 760 761 } 763 764 public void internalEntityDecl(String name, XMLString text, 765 XMLString nonNormalizedText, 766 Augmentations augs) 767 throws XNIException { 768 769 printIndent(); 770 fOut.print("internalEntityDecl("); 771 fOut.print("name="); 772 printQuotedString(name); 773 fOut.print(','); 774 fOut.print("text="); 775 printQuotedString(text.ch, text.offset, text.length); 776 fOut.print(','); 777 fOut.print("nonNormalizedText="); 778 printQuotedString(nonNormalizedText.ch, nonNormalizedText.offset, 779 nonNormalizedText.length); 780 if (augs != null) { 781 fOut.print(','); 782 printAugmentations(augs); 783 } 784 fOut.println(')'); 785 fOut.flush(); 786 787 } 789 790 public void externalEntityDecl(String name,XMLResourceIdentifier identifier, Augmentations augs) 791 throws XNIException { 792 793 printIndent(); 794 fOut.print("externalEntityDecl("); 795 fOut.print("name="); 796 printQuotedString(name); 797 fOut.print(','); 798 fOut.print("publicId="); 799 printQuotedString(identifier.getPublicId()); 800 fOut.print(','); 801 fOut.print("systemId="); 802 printQuotedString(identifier.getLiteralSystemId()); 803 fOut.print(','); 804 fOut.print("baseSystemId="); 805 printQuotedString(identifier.getBaseSystemId()); 806 if (augs != null) { 807 fOut.print(','); 808 printAugmentations(augs); 809 } 810 fOut.println(')'); 811 fOut.flush(); 812 813 } 815 816 public void unparsedEntityDecl(String name, XMLResourceIdentifier identifier, 817 String notation, Augmentations augs) throws XNIException { 818 819 printIndent(); 820 fOut.print("unparsedEntityDecl("); 821 fOut.print("name="); 822 printQuotedString(name); 823 fOut.print(','); 824 fOut.print("publicId="); 825 printQuotedString(identifier.getPublicId()); 826 fOut.print(','); 827 fOut.print("systemId="); 828 printQuotedString(identifier.getLiteralSystemId()); 829 fOut.print(','); 830 fOut.print("baseSystemId="); 831 printQuotedString(identifier.getBaseSystemId()); 832 fOut.print(','); 833 fOut.print("notation="); 834 printQuotedString(notation); 835 if (augs != null) { 836 fOut.print(','); 837 printAugmentations(augs); 838 } 839 fOut.println(')'); 840 fOut.flush(); 841 842 } 844 845 public void notationDecl(String name, XMLResourceIdentifier identifier, 846 Augmentations augs) throws XNIException { 847 848 printIndent(); 849 fOut.print("notationDecl("); 850 fOut.print("name="); 851 printQuotedString(name); 852 fOut.print(','); 853 fOut.print("publicId="); 854 printQuotedString(identifier.getPublicId()); 855 fOut.print(','); 856 fOut.print("systemId="); 857 printQuotedString(identifier.getLiteralSystemId()); 858 fOut.print(','); 859 fOut.print("baseSystemId="); 860 printQuotedString(identifier.getBaseSystemId()); 861 if (augs != null) { 862 fOut.print(','); 863 printAugmentations(augs); 864 } 865 fOut.println(')'); 866 fOut.flush(); 867 868 } 870 871 public void startConditional(short type, Augmentations augs) 872 throws XNIException { 873 874 printIndent(); 875 fOut.print("startConditional("); 876 fOut.print("type="); 877 switch (type) { 878 case XMLDTDHandler.CONDITIONAL_IGNORE: { 879 fOut.print("CONDITIONAL_IGNORE"); 880 break; 881 } 882 case XMLDTDHandler.CONDITIONAL_INCLUDE: { 883 fOut.print("CONDITIONAL_INCLUDE"); 884 break; 885 } 886 default: { 887 fOut.print("??? ("+type+')'); 888 } 889 } 890 if (augs != null) { 891 fOut.print(','); 892 printAugmentations(augs); 893 } 894 fOut.println(')'); 895 fOut.flush(); 896 fIndent++; 897 898 } 900 901 public void endConditional(Augmentations augs) throws XNIException { 902 903 fIndent--; 904 printIndent(); 905 fOut.print("endConditional("); 906 if (augs != null) { 907 fOut.print(','); 908 printAugmentations(augs); 909 } 910 fOut.println(')'); 911 fOut.flush(); 912 913 } 915 916 public void endDTD(Augmentations augs) throws XNIException { 917 918 fIndent--; 919 printIndent(); 920 fOut.print("endDTD("); 921 if (augs != null) { 922 fOut.print(','); 923 printAugmentations(augs); 924 } 925 fOut.println(')'); 926 fOut.flush(); 927 928 } 930 934 935 public void startContentModel(String elementName, Augmentations augs) 936 throws XNIException { 937 938 printIndent(); 939 fOut.print("startContentModel("); 940 fOut.print("elementName="); 941 printQuotedString(elementName); 942 if (augs != null) { 943 fOut.print(','); 944 printAugmentations(augs); 945 } 946 fOut.println(')'); 947 fOut.flush(); 948 fIndent++; 949 950 } 952 953 public void any(Augmentations augs) throws XNIException { 954 955 printIndent(); 956 fOut.print("any("); 957 if (augs != null) { 958 fOut.print(','); 959 printAugmentations(augs); 960 } 961 fOut.println(')'); 962 fOut.flush(); 963 964 } 966 967 public void empty(Augmentations augs) throws XNIException { 968 969 printIndent(); 970 fOut.print("empty("); 971 if (augs != null) { 972 fOut.print(','); 973 printAugmentations(augs); 974 } 975 fOut.println(')'); 976 fOut.flush(); 977 978 } 980 981 public void startGroup(Augmentations augs) throws XNIException { 982 983 printIndent(); 984 fOut.print("startGroup("); 985 if (augs != null) { 986 fOut.print(','); 987 printAugmentations(augs); 988 } 989 fOut.println(')'); 990 fOut.flush(); 991 fIndent++; 992 993 } 995 996 public void pcdata(Augmentations augs) throws XNIException { 997 998 printIndent(); 999 fOut.print("pcdata("); 1000 if (augs != null) { 1001 fOut.print(','); 1002 printAugmentations(augs); 1003 } 1004 fOut.println(')'); 1005 fOut.flush(); 1006 1007 } 1009 1010 public void element(String elementName, Augmentations augs) 1011 throws XNIException { 1012 1013 printIndent(); 1014 fOut.print("element("); 1015 fOut.print("elementName="); 1016 printQuotedString(elementName); 1017 if (augs != null) { 1018 fOut.print(','); 1019 printAugmentations(augs); 1020 } 1021 fOut.println(')'); 1022 fOut.flush(); 1023 1024 } 1026 1027 public void separator(short separator, Augmentations augs) 1028 throws XNIException { 1029 1030 printIndent(); 1031 fOut.print("separator("); 1032 fOut.print("separator="); 1033 switch (separator) { 1034 case XMLDTDContentModelHandler.SEPARATOR_CHOICE: { 1035 fOut.print("SEPARATOR_CHOICE"); 1036 break; 1037 } 1038 case XMLDTDContentModelHandler.SEPARATOR_SEQUENCE: { 1039 fOut.print("SEPARATOR_SEQUENCE"); 1040 break; 1041 } 1042 default: { 1043 fOut.print("??? ("+separator+')'); 1044 } 1045 } 1046 if (augs != null) { 1047 fOut.print(','); 1048 printAugmentations(augs); 1049 } 1050 fOut.println(')'); 1051 fOut.flush(); 1052 1053 } 1055 1056 public void occurrence(short occurrence, Augmentations augs) 1057 throws XNIException { 1058 1059 printIndent(); 1060 fOut.print("occurrence("); 1061 fOut.print("occurrence="); 1062 switch (occurrence) { 1063 case XMLDTDContentModelHandler.OCCURS_ONE_OR_MORE: { 1064 fOut.print("OCCURS_ONE_OR_MORE"); 1065 break; 1066 } 1067 case XMLDTDContentModelHandler.OCCURS_ZERO_OR_MORE: { 1068 fOut.print("OCCURS_ZERO_OR_MORE"); 1069 break; 1070 } 1071 case XMLDTDContentModelHandler.OCCURS_ZERO_OR_ONE: { 1072 fOut.print("OCCURS_ZERO_OR_ONE"); 1073 break; 1074 } 1075 default: { 1076 fOut.print("??? ("+occurrence+')'); 1077 } 1078 } 1079 if (augs != null) { 1080 fOut.print(','); 1081 printAugmentations(augs); 1082 } 1083 fOut.println(')'); 1084 fOut.flush(); 1085 1086 } 1088 1089 public void endGroup(Augmentations augs) throws XNIException { 1090 1091 fIndent--; 1092 printIndent(); 1093 fOut.print("endGroup("); 1094 if (augs != null) { 1095 fOut.print(','); 1096 printAugmentations(augs); 1097 } 1098 fOut.println(')'); 1099 fOut.flush(); 1100 1101 } 1103 1104 public void endContentModel(Augmentations augs) throws XNIException { 1105 1106 fIndent--; 1107 printIndent(); 1108 fOut.print("endContentModel("); 1109 if (augs != null) { 1110 fOut.print(','); 1111 printAugmentations(augs); 1112 } 1113 fOut.println(')'); 1114 fOut.flush(); 1115 1116 } 1118 1122 1123 public void warning(String domain, String key, XMLParseException ex) 1124 throws XNIException { 1125 printError("Warning", ex); 1126 } 1128 1129 public void error(String domain, String key, XMLParseException ex) 1130 throws XNIException { 1131 printError("Error", ex); 1132 } 1134 1135 public void fatalError(String domain, String key, XMLParseException ex) 1136 throws XNIException { 1137 printError("Fatal Error", ex); 1138 throw ex; 1139 } 1141 protected void printInScopeNamespaces(){ 1145 int count = fNamespaceContext.getDeclaredPrefixCount(); 1146 if (count>0){ 1147 for (int i = 0; i < count; i++) { 1148 printIndent(); 1149 fOut.print("declaredPrefix("); 1150 fOut.print("prefix="); 1151 String prefix = fNamespaceContext.getDeclaredPrefixAt(i); 1152 printQuotedString(prefix); 1153 fOut.print(','); 1154 fOut.print("uri="); 1155 printQuotedString(fNamespaceContext.getURI(prefix)); 1156 fOut.println(')'); 1157 fOut.flush(); 1158 } 1159 } 1160 } 1161 1162 protected void printEndNamespaceMapping(){ 1163 int count = fNamespaceContext.getDeclaredPrefixCount(); 1164 if (count > 0) { 1165 for (int i = 0; i < count; i++) { 1166 printIndent(); 1167 fOut.print("endPrefix("); 1168 fOut.print("prefix="); 1169 String prefix = fNamespaceContext.getDeclaredPrefixAt(i); 1170 printQuotedString(prefix); 1171 fOut.println(')'); 1172 fOut.flush(); 1173 } 1174 } 1175 1176 } 1177 1178 protected void printElement(QName element, XMLAttributes attributes) { 1179 1180 fOut.print("element="); 1181 fOut.print('{'); 1182 fOut.print("prefix="); 1183 printQuotedString(element.prefix); 1184 fOut.print(','); 1185 fOut.print("localpart="); 1186 printQuotedString(element.localpart); 1187 fOut.print(','); 1188 fOut.print("rawname="); 1189 printQuotedString(element.rawname); 1190 fOut.print(','); 1191 fOut.print("uri="); 1192 printQuotedString(element.uri); 1193 fOut.print('}'); 1194 fOut.print(','); 1195 fOut.print("attributes="); 1196 if (attributes == null) { 1197 fOut.println("null"); 1198 } 1199 else { 1200 fOut.print('{'); 1201 int length = attributes.getLength(); 1202 for (int i = 0; i < length; i++) { 1203 if (i > 0) { 1204 fOut.print(','); 1205 } 1206 attributes.getName(i, fQName); 1207 String attrType = attributes.getType(i); 1208 String attrValue = attributes.getValue(i); 1209 String attrNonNormalizedValue = attributes.getNonNormalizedValue(i); 1210 Augmentations augs = attributes.getAugmentations(i); 1211 fOut.print("name="); 1212 fOut.print('{'); 1213 fOut.print("prefix="); 1214 printQuotedString(fQName.prefix); 1215 fOut.print(','); 1216 fOut.print("localpart="); 1217 printQuotedString(fQName.localpart); 1218 fOut.print(','); 1219 fOut.print("rawname="); 1220 printQuotedString(fQName.rawname); 1221 fOut.print(','); 1222 fOut.print("uri="); 1223 printQuotedString(fQName.uri); 1224 fOut.print('}'); 1225 fOut.print(','); 1226 fOut.print("type="); 1227 printQuotedString(attrType); 1228 fOut.print(','); 1229 fOut.print("value="); 1230 printQuotedString(attrValue); 1231 fOut.print(','); 1232 fOut.print("nonNormalizedValue="); 1233 printQuotedString(attrNonNormalizedValue); 1234 if (attributes.isSpecified(i) == false ) { 1235 fOut.print("(default)"); 1236 } 1237 if (augs != null) { 1238 fOut.print(','); 1239 printAugmentations(augs); 1240 } 1241 fOut.print('}'); 1242 } 1243 fOut.print('}'); 1244 } 1245 1246 } 1248 1249 protected void printAugmentations(Augmentations augs) { 1250 fOut.print("augs={"); 1251 java.util.Enumeration keys = augs.keys(); 1252 while (keys.hasMoreElements()) { 1253 String key = (String )keys.nextElement(); 1254 Object value = augs.getItem(key); 1255 fOut.print(key); 1256 fOut.print('#'); 1257 fOut.print(String.valueOf(value)); 1258 } 1259 fOut.print('}'); 1260 } 1262 1263 protected void printQuotedString(String s) { 1264 1265 if (s == null) { 1266 fOut.print("null"); 1267 return; 1268 } 1269 1270 fOut.print('"'); 1271 int length = s.length(); 1272 for (int i = 0; i < length; i++) { 1273 char c = s.charAt(i); 1274 normalizeAndPrint(c); 1275 } 1276 fOut.print('"'); 1277 1278 } 1280 1281 protected void printQuotedString(char[] ch, int offset, int length) { 1282 1283 fOut.print('"'); 1284 for (int i = 0; i < length; i++) { 1285 normalizeAndPrint(ch[offset + i]); 1286 } 1287 fOut.print('"'); 1288 1289 } 1291 1292 protected void normalizeAndPrint(char c) { 1293 1294 switch (c) { 1295 case '\n': { 1296 fOut.print("\\n"); 1297 break; 1298 } 1299 case '\r': { 1300 fOut.print("\\r"); 1301 break; 1302 } 1303 case '\t': { 1304 fOut.print("\\t"); 1305 break; 1306 } 1307 case '\\': { 1308 fOut.print("\\\\"); 1309 break; 1310 } 1311 case '"': { 1312 fOut.print("\\\""); 1313 break; 1314 } 1315 default: { 1316 fOut.print(c); 1317 } 1318 } 1319 1320 } 1322 1323 protected void printError(String type, XMLParseException ex) { 1324 1325 System.err.print("["); 1326 System.err.print(type); 1327 System.err.print("] "); 1328 String systemId = ex.getExpandedSystemId(); 1329 if (systemId != null) { 1330 int index = systemId.lastIndexOf('/'); 1331 if (index != -1) 1332 systemId = systemId.substring(index + 1); 1333 System.err.print(systemId); 1334 } 1335 System.err.print(':'); 1336 System.err.print(ex.getLineNumber()); 1337 System.err.print(':'); 1338 System.err.print(ex.getColumnNumber()); 1339 System.err.print(": "); 1340 System.err.print(ex.getMessage()); 1341 System.err.println(); 1342 System.err.flush(); 1343 1344 } 1346 1347 protected void printIndent() { 1348 1349 for (int i = 0; i < fIndent; i++) { 1350 fOut.print(' '); 1351 } 1352 1353 } 1355 1359 1360 public static void main(String [] argv) throws Exception { 1361 1362 if (argv.length == 0) { 1364 printUsage(); 1365 System.exit(1); 1366 } 1367 1368 XMLDocumentParser parser = null; 1370 XMLParserConfiguration parserConfig = null; 1371 boolean namespaces = DEFAULT_NAMESPACES; 1372 boolean validation = DEFAULT_VALIDATION; 1373 boolean schemaValidation = DEFAULT_SCHEMA_VALIDATION; 1374 boolean schemaFullChecking = DEFAULT_SCHEMA_FULL_CHECKING; 1375 boolean notifyCharRefs = DEFAULT_NOTIFY_CHAR_REFS; 1376 1377 for (int i = 0; i < argv.length; i++) { 1379 String arg = argv[i]; 1380 if (arg.startsWith("-")) { 1381 String option = arg.substring(1); 1382 if (option.equals("p")) { 1383 if (++i == argv.length) { 1385 System.err.println("error: Missing argument to -p option."); 1386 continue; 1387 } 1388 String parserName = argv[i]; 1389 1390 try { 1392 parserConfig = (XMLParserConfiguration)ObjectFactory.newInstance(parserName, 1393 ObjectFactory.findClassLoader(), true); 1394 parser = null; 1395 } 1396 catch (Exception e) { 1397 parserConfig = null; 1398 System.err.println("error: Unable to instantiate parser configuration ("+parserName+")"); 1399 } 1400 continue; 1401 } 1402 if (option.equalsIgnoreCase("n")) { 1403 namespaces = option.equals("n"); 1404 continue; 1405 } 1406 if (option.equalsIgnoreCase("v")) { 1407 validation = option.equals("v"); 1408 continue; 1409 } 1410 if (option.equalsIgnoreCase("s")) { 1411 schemaValidation = option.equals("s"); 1412 continue; 1413 } 1414 if (option.equalsIgnoreCase("f")) { 1415 schemaFullChecking = option.equals("f"); 1416 continue; 1417 } 1418 if (option.equalsIgnoreCase("c")) { 1419 notifyCharRefs = option.equals("c"); 1420 continue; 1421 } 1422 if (option.equals("h")) { 1423 printUsage(); 1424 continue; 1425 } 1426 } 1427 1428 if (parserConfig == null) { 1430 1431 try { 1433 parserConfig = (XMLParserConfiguration)ObjectFactory.newInstance(DEFAULT_PARSER_CONFIG, 1434 ObjectFactory.findClassLoader(), true); 1435 } 1436 catch (Exception e) { 1437 System.err.println("error: Unable to instantiate parser configuration ("+DEFAULT_PARSER_CONFIG+")"); 1438 continue; 1439 } 1440 } 1441 1442 if (parser == null) { 1444 parser = new DocumentTracer(parserConfig); 1445 } 1446 try { 1447 parserConfig.setFeature(NAMESPACES_FEATURE_ID, namespaces); 1448 } 1449 catch (XMLConfigurationException e) { 1450 System.err.println("warning: Parser does not support feature ("+NAMESPACES_FEATURE_ID+")"); 1451 } 1452 try { 1453 parserConfig.setFeature(VALIDATION_FEATURE_ID, validation); 1454 } 1455 catch (XMLConfigurationException e) { 1456 System.err.println("warning: Parser does not support feature ("+VALIDATION_FEATURE_ID+")"); 1457 } 1458 try { 1459 parserConfig.setFeature(SCHEMA_VALIDATION_FEATURE_ID, schemaValidation); 1460 } 1461 catch (XMLConfigurationException e) { 1462 if (e.getType() == XMLConfigurationException.NOT_SUPPORTED) { 1463 System.err.println("warning: Parser does not support feature ("+SCHEMA_VALIDATION_FEATURE_ID+")"); 1464 } 1465 } 1466 try { 1467 parserConfig.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking); 1468 } 1469 catch (XMLConfigurationException e) { 1470 if (e.getType() == XMLConfigurationException.NOT_SUPPORTED) { 1471 System.err.println("warning: Parser does not support feature ("+SCHEMA_FULL_CHECKING_FEATURE_ID+")"); 1472 } 1473 } 1474 try { 1475 parserConfig.setFeature(NOTIFY_CHAR_REFS_FEATURE_ID, notifyCharRefs); 1476 } 1477 catch (XMLConfigurationException e) { 1478 if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) { 1479 System.err.println("warning: Parser does not recognize feature ("+NOTIFY_CHAR_REFS_FEATURE_ID+")"); 1481 } 1482 else { 1483 System.err.println("warning: Parser does not support feature ("+NOTIFY_CHAR_REFS_FEATURE_ID+")"); 1484 } 1485 } 1486 1487 try { 1489 parser.parse(new XMLInputSource(null, arg, null)); 1490 } 1491 catch (XMLParseException e) { 1492 } 1494 catch (Exception e) { 1495 System.err.println("error: Parse error occurred - "+e.getMessage()); 1496 if (e instanceof XNIException) { 1497 e = ((XNIException)e).getException(); 1498 } 1499 e.printStackTrace(System.err); 1500 } 1501 } 1502 1503 } 1505 1509 1510 private static void printUsage() { 1511 1512 System.err.println("usage: java xni.DocumentTracer (options) uri ..."); 1513 System.err.println(); 1514 1515 System.err.println("options:"); 1516 System.out.println(" -p name Specify parser configuration by name."); 1517 System.err.println(" -n | -N Turn on/off namespace processing."); 1518 System.err.println(" -v | -V Turn on/off validation."); 1519 System.err.println(" -s | -S Turn on/off Schema validation support."); 1520 System.err.println(" NOTE: Not supported by all parser configurations."); 1521 System.err.println(" -f | -F Turn on/off Schema full checking."); 1522 System.err.println(" NOTE: Requires use of -s and not supported by all parsers."); 1523 System.err.println(" -c | -C Turn on/off character notifications"); 1524 System.err.println(" -h This help screen."); 1525 System.err.println(); 1526 1527 System.err.println("defaults:"); 1528 System.out.println(" Config: "+DEFAULT_PARSER_CONFIG); 1529 System.out.print(" Namespaces: "); 1530 System.err.println(DEFAULT_NAMESPACES ? "on" : "off"); 1531 System.out.print(" Validation: "); 1532 System.err.println(DEFAULT_VALIDATION ? "on" : "off"); 1533 System.out.print(" Schema: "); 1534 System.err.println(DEFAULT_SCHEMA_VALIDATION ? "on" : "off"); 1535 System.err.print(" Schema full checking: "); 1536 System.err.println(DEFAULT_SCHEMA_FULL_CHECKING ? "on" : "off"); 1537 System.out.print(" Char refs: "); 1538 System.err.println(DEFAULT_NOTIFY_CHAR_REFS ? "on" : "off" ); 1539 1540 } 1542} | Popular Tags |