1 16 17 package org.apache.xerces.parsers; 18 19 import java.util.Locale ; 20 import java.util.Stack ; 21 22 import org.apache.xerces.dom.AttrImpl; 23 import org.apache.xerces.dom.CoreDocumentImpl; 24 import org.apache.xerces.dom.DOMErrorImpl; 25 import org.apache.xerces.dom.DOMMessageFormatter; 26 import org.apache.xerces.dom.DeferredDocumentImpl; 27 import org.apache.xerces.dom.DocumentImpl; 28 import org.apache.xerces.dom.DocumentTypeImpl; 29 import org.apache.xerces.dom.ElementDefinitionImpl; 30 import org.apache.xerces.dom.ElementImpl; 31 import org.apache.xerces.dom.ElementNSImpl; 32 import org.apache.xerces.dom.EntityImpl; 33 import org.apache.xerces.dom.EntityReferenceImpl; 34 import org.apache.xerces.dom.NodeImpl; 35 import org.apache.xerces.dom.NotationImpl; 36 import org.apache.xerces.dom.PSVIAttrNSImpl; 37 import org.apache.xerces.dom.PSVIDocumentImpl; 38 import org.apache.xerces.dom.PSVIElementNSImpl; 39 import org.apache.xerces.dom.TextImpl; 40 import org.apache.xerces.impl.Constants; 41 import org.apache.xerces.impl.dv.XSSimpleType; 42 import org.apache.xerces.util.DOMErrorHandlerWrapper; 43 import org.apache.xerces.xni.Augmentations; 44 import org.apache.xerces.xni.NamespaceContext; 45 import org.apache.xerces.xni.QName; 46 import org.apache.xerces.xni.XMLAttributes; 47 import org.apache.xerces.xni.XMLLocator; 48 import org.apache.xerces.xni.XMLResourceIdentifier; 49 import org.apache.xerces.xni.XMLString; 50 import org.apache.xerces.xni.XNIException; 51 import org.apache.xerces.xni.parser.XMLParserConfiguration; 52 import org.apache.xerces.xs.AttributePSVI; 53 import org.apache.xerces.xs.ElementPSVI; 54 import org.apache.xerces.xs.XSTypeDefinition; 55 import org.w3c.dom.Attr ; 56 import org.w3c.dom.CDATASection ; 57 import org.w3c.dom.Comment ; 58 import org.w3c.dom.Document ; 59 import org.w3c.dom.DocumentType ; 60 import org.w3c.dom.DOMError ; 61 import org.w3c.dom.Element ; 62 import org.w3c.dom.EntityReference ; 63 import org.w3c.dom.NamedNodeMap ; 64 import org.w3c.dom.Node ; 65 import org.w3c.dom.NodeList ; 66 import org.w3c.dom.ProcessingInstruction ; 67 import org.w3c.dom.Text ; 68 import org.w3c.dom.ls.LSParserFilter ; 69 import org.w3c.dom.traversal.NodeFilter; 70 import org.xml.sax.SAXException ; 71 72 85 public class AbstractDOMParser extends AbstractXMLDocumentParser { 86 87 91 93 94 protected static final String NAMESPACES = 95 Constants.SAX_FEATURE_PREFIX+Constants.NAMESPACES_FEATURE; 96 97 98 protected static final String CREATE_ENTITY_REF_NODES = 99 Constants.XERCES_FEATURE_PREFIX + Constants.CREATE_ENTITY_REF_NODES_FEATURE; 100 101 102 protected static final String INCLUDE_COMMENTS_FEATURE = 103 Constants.XERCES_FEATURE_PREFIX + Constants.INCLUDE_COMMENTS_FEATURE; 104 105 106 protected static final String CREATE_CDATA_NODES_FEATURE = 107 Constants.XERCES_FEATURE_PREFIX + Constants.CREATE_CDATA_NODES_FEATURE; 108 109 110 protected static final String INCLUDE_IGNORABLE_WHITESPACE = 111 Constants.XERCES_FEATURE_PREFIX + Constants.INCLUDE_IGNORABLE_WHITESPACE; 112 113 114 protected static final String DEFER_NODE_EXPANSION = 115 Constants.XERCES_FEATURE_PREFIX + Constants.DEFER_NODE_EXPANSION_FEATURE; 116 117 118 119 private static final String [] RECOGNIZED_FEATURES = { 120 NAMESPACES, 121 CREATE_ENTITY_REF_NODES, 122 INCLUDE_COMMENTS_FEATURE, 123 CREATE_CDATA_NODES_FEATURE, 124 INCLUDE_IGNORABLE_WHITESPACE, 125 DEFER_NODE_EXPANSION 126 }; 127 128 130 131 protected static final String DOCUMENT_CLASS_NAME = 132 Constants.XERCES_PROPERTY_PREFIX + Constants.DOCUMENT_CLASS_NAME_PROPERTY; 133 134 protected static final String CURRENT_ELEMENT_NODE= 135 Constants.XERCES_PROPERTY_PREFIX + Constants.CURRENT_ELEMENT_NODE_PROPERTY; 136 137 140 141 private static final String [] RECOGNIZED_PROPERTIES = { 142 DOCUMENT_CLASS_NAME, 143 CURRENT_ELEMENT_NODE, 144 }; 145 146 148 149 protected static final String DEFAULT_DOCUMENT_CLASS_NAME = 150 "org.apache.xerces.dom.DocumentImpl"; 151 152 protected static final String CORE_DOCUMENT_CLASS_NAME = 153 "org.apache.xerces.dom.CoreDocumentImpl"; 154 155 protected static final String PSVI_DOCUMENT_CLASS_NAME = 156 "org.apache.xerces.dom.PSVIDocumentImpl"; 157 158 161 public static final RuntimeException abort = new RuntimeException (); 162 163 165 private static final boolean DEBUG_EVENTS = false; 166 private static final boolean DEBUG_BASEURI = false; 167 168 172 173 protected DOMErrorHandlerWrapper fErrorHandler = null; 174 175 176 protected boolean fInDTD; 177 178 180 181 protected boolean fCreateEntityRefNodes; 182 183 184 protected boolean fIncludeIgnorableWhitespace; 185 186 187 protected boolean fIncludeComments; 188 189 190 protected boolean fCreateCDATANodes; 191 192 194 195 protected Document fDocument; 196 197 198 protected CoreDocumentImpl fDocumentImpl; 199 200 201 protected boolean fStorePSVI; 202 203 204 protected String fDocumentClassName; 205 206 207 protected DocumentType fDocumentType; 208 209 210 protected Node fCurrentNode; 211 protected CDATASection fCurrentCDATASection; 212 protected EntityImpl fCurrentEntityDecl; 213 protected int fDeferredEntityDecl; 214 215 216 protected final StringBuffer fStringBuffer = new StringBuffer (50); 217 218 220 221 protected StringBuffer fInternalSubset; 222 223 225 protected boolean fDeferNodeExpansion; 226 protected boolean fNamespaceAware; 227 protected DeferredDocumentImpl fDeferredDocumentImpl; 228 protected int fDocumentIndex; 229 protected int fDocumentTypeIndex; 230 protected int fCurrentNodeIndex; 231 protected int fCurrentCDATASectionIndex; 232 233 235 236 protected boolean fInDTDExternalSubset; 237 238 239 protected QName fRoot = new QName(); 240 241 242 protected boolean fInCDATASection; 243 244 245 protected boolean fFirstChunk = false; 246 247 248 250 protected boolean fFilterReject = false; 251 252 254 255 protected Stack fBaseURIStack = new Stack (); 256 257 258 259 protected final QName fRejectedElement = new QName (); 260 261 262 protected Stack fSkippedElemStack = null; 263 264 265 protected boolean fInEntityRef = false; 266 267 268 private QName fAttrQName = new QName (); 269 270 272 protected LSParserFilter fDOMFilter = null; 273 274 278 279 protected AbstractDOMParser (XMLParserConfiguration config) { 280 281 super (config); 282 283 284 fConfiguration.addRecognizedFeatures (RECOGNIZED_FEATURES); 286 287 fConfiguration.setFeature (CREATE_ENTITY_REF_NODES, true); 289 fConfiguration.setFeature (INCLUDE_IGNORABLE_WHITESPACE, true); 290 fConfiguration.setFeature (DEFER_NODE_EXPANSION, true); 291 fConfiguration.setFeature (INCLUDE_COMMENTS_FEATURE, true); 292 fConfiguration.setFeature (CREATE_CDATA_NODES_FEATURE, true); 293 294 fConfiguration.addRecognizedProperties (RECOGNIZED_PROPERTIES); 296 297 fConfiguration.setProperty (DOCUMENT_CLASS_NAME, 299 DEFAULT_DOCUMENT_CLASS_NAME); 300 301 } 303 306 protected String getDocumentClassName () { 307 return fDocumentClassName; 308 } 309 310 324 protected void setDocumentClassName (String documentClassName) { 325 326 if (documentClassName == null) { 328 documentClassName = DEFAULT_DOCUMENT_CLASS_NAME; 329 } 330 331 if (!documentClassName.equals(DEFAULT_DOCUMENT_CLASS_NAME) && 332 !documentClassName.equals(PSVI_DOCUMENT_CLASS_NAME)) { 333 try { 335 Class _class = ObjectFactory.findProviderClass (documentClassName, 336 ObjectFactory.findClassLoader (), true); 337 if (!Document .class.isAssignableFrom (_class)) { 339 throw new IllegalArgumentException ( 340 DOMMessageFormatter.formatMessage( 341 DOMMessageFormatter.DOM_DOMAIN, 342 "InvalidDocumentClassName", new Object [] {documentClassName})); 343 } 344 } 345 catch (ClassNotFoundException e) { 346 throw new IllegalArgumentException ( 347 DOMMessageFormatter.formatMessage( 348 DOMMessageFormatter.DOM_DOMAIN, 349 "MissingDocumentClassName", new Object [] {documentClassName})); 350 } 351 } 352 353 fDocumentClassName = documentClassName; 355 if (!documentClassName.equals (DEFAULT_DOCUMENT_CLASS_NAME)) { 356 fDeferNodeExpansion = false; 357 } 358 359 } 361 365 366 public Document getDocument () { 367 return fDocument; 368 } 370 374 379 public void reset () throws XNIException { 380 super.reset (); 381 382 383 fCreateEntityRefNodes = 385 fConfiguration.getFeature (CREATE_ENTITY_REF_NODES); 386 387 fIncludeIgnorableWhitespace = 388 fConfiguration.getFeature (INCLUDE_IGNORABLE_WHITESPACE); 389 390 fDeferNodeExpansion = 391 fConfiguration.getFeature (DEFER_NODE_EXPANSION); 392 393 fNamespaceAware = fConfiguration.getFeature (NAMESPACES); 394 395 fIncludeComments = fConfiguration.getFeature (INCLUDE_COMMENTS_FEATURE); 396 397 fCreateCDATANodes = fConfiguration.getFeature (CREATE_CDATA_NODES_FEATURE); 398 399 setDocumentClassName ((String ) 401 fConfiguration.getProperty (DOCUMENT_CLASS_NAME)); 402 403 fDocument = null; 405 fDocumentImpl = null; 406 fStorePSVI = false; 407 fDocumentType = null; 408 fDocumentTypeIndex = -1; 409 fDeferredDocumentImpl = null; 410 fCurrentNode = null; 411 412 fStringBuffer.setLength (0); 414 415 fRoot.clear(); 417 fInDTD = false; 418 fInDTDExternalSubset = false; 419 fInCDATASection = false; 420 fFirstChunk = false; 421 fCurrentCDATASection = null; 422 fCurrentCDATASectionIndex = -1; 423 424 fBaseURIStack.removeAllElements (); 425 426 427 } 429 435 public void setLocale (Locale locale) { 436 fConfiguration.setLocale (locale); 437 438 } 440 444 461 public void startGeneralEntity (String name, 462 XMLResourceIdentifier identifier, 463 String encoding, Augmentations augs) 464 throws XNIException { 465 if (DEBUG_EVENTS) { 466 System.out.println ("==>startGeneralEntity ("+name+")"); 467 if (DEBUG_BASEURI) { 468 System.out.println (" expandedSystemId( **baseURI): "+identifier.getExpandedSystemId ()); 469 System.out.println (" baseURI:"+ identifier.getBaseSystemId ()); 470 } 471 } 472 473 if (!fDeferNodeExpansion) { 476 if (fFilterReject) { 477 return; 478 } 479 setCharacterData (true); 480 EntityReference er = fDocument.createEntityReference (name); 481 if (fDocumentImpl != null) { 482 486 EntityReferenceImpl erImpl =(EntityReferenceImpl)er; 487 488 erImpl.setBaseURI (identifier.getExpandedSystemId ()); 490 if (fDocumentType != null) { 491 NamedNodeMap entities = fDocumentType.getEntities (); 493 fCurrentEntityDecl = (EntityImpl) entities.getNamedItem (name); 494 if (fCurrentEntityDecl != null) { 495 fCurrentEntityDecl.setInputEncoding (encoding); 496 } 497 498 } 499 erImpl.needsSyncChildren (false); 502 } 503 fInEntityRef = true; 504 fCurrentNode.appendChild (er); 505 fCurrentNode = er; 506 } 507 else { 508 509 int er = 510 fDeferredDocumentImpl.createDeferredEntityReference (name, identifier.getExpandedSystemId ()); 511 if (fDocumentTypeIndex != -1) { 512 int node = fDeferredDocumentImpl.getLastChild (fDocumentTypeIndex, false); 514 while (node != -1) { 515 short nodeType = fDeferredDocumentImpl.getNodeType (node, false); 516 if (nodeType == Node.ENTITY_NODE) { 517 String nodeName = 518 fDeferredDocumentImpl.getNodeName (node, false); 519 if (nodeName.equals (name)) { 520 fDeferredEntityDecl = node; 521 fDeferredDocumentImpl.setInputEncoding (node, encoding); 522 break; 523 } 524 } 525 node = fDeferredDocumentImpl.getRealPrevSibling (node, false); 526 } 527 } 528 fDeferredDocumentImpl.appendChild (fCurrentNodeIndex, er); 529 fCurrentNodeIndex = er; 530 } 531 532 } 534 551 public void textDecl (String version, String encoding, Augmentations augs) throws XNIException { 552 if (fInDTD){ 553 return; 554 } 555 if (!fDeferNodeExpansion) { 556 if (fCurrentEntityDecl != null && !fFilterReject) { 557 fCurrentEntityDecl.setXmlEncoding (encoding); 558 if (version != null) 559 fCurrentEntityDecl.setXmlVersion (version); 560 } 561 } 562 else { 563 if (fDeferredEntityDecl !=-1) { 564 fDeferredDocumentImpl.setEntityInfo (fDeferredEntityDecl, version, encoding); 565 } 566 } 567 } 569 577 public void comment (XMLString text, Augmentations augs) throws XNIException { 578 if (fInDTD) { 579 if (fInternalSubset != null && !fInDTDExternalSubset) { 580 fInternalSubset.append ("<!-- "); 581 fInternalSubset.append (text.toString ()); 582 fInternalSubset.append (" -->"); 583 } 584 return; 585 } 586 if (!fIncludeComments || fFilterReject) { 587 return; 588 } 589 if (!fDeferNodeExpansion) { 590 Comment comment = fDocument.createComment (text.toString ()); 591 592 setCharacterData (false); 593 fCurrentNode.appendChild (comment); 594 if (fDOMFilter !=null && !fInEntityRef && 595 (fDOMFilter.getWhatToShow () & NodeFilter.SHOW_COMMENT)!= 0) { 596 short code = fDOMFilter.acceptNode (comment); 597 switch (code) { 598 case LSParserFilter.FILTER_INTERRUPT:{ 599 throw abort; 600 } 601 case LSParserFilter.FILTER_REJECT:{ 602 605 } 607 case LSParserFilter.FILTER_SKIP: { 608 fCurrentNode.removeChild (comment); 611 fFirstChunk = true; 613 return; 614 } 615 616 default: { 617 } 619 } 620 } 621 622 } 623 else { 624 int comment = 625 fDeferredDocumentImpl.createDeferredComment (text.toString ()); 626 fDeferredDocumentImpl.appendChild (fCurrentNodeIndex, comment); 627 } 628 629 } 631 648 public void processingInstruction (String target, XMLString data, Augmentations augs) 649 throws XNIException { 650 651 if (fInDTD) { 652 if (fInternalSubset != null && !fInDTDExternalSubset) { 653 fInternalSubset.append ("<?"); 654 fInternalSubset.append (target); 655 fInternalSubset.append (' '); 656 fInternalSubset.append (data.toString ()); 657 fInternalSubset.append ("?>"); 658 659 } 660 return; 661 } 662 663 if (DEBUG_EVENTS) { 664 System.out.println ("==>processingInstruction ("+target+")"); 665 } 666 if (!fDeferNodeExpansion) { 667 if (fFilterReject) { 668 return; 669 } 670 ProcessingInstruction pi = 671 fDocument.createProcessingInstruction (target, data.toString ()); 672 673 674 setCharacterData (false); 675 fCurrentNode.appendChild (pi); 676 if (fDOMFilter !=null && !fInEntityRef && 677 (fDOMFilter.getWhatToShow () & NodeFilter.SHOW_PROCESSING_INSTRUCTION)!= 0) { 678 short code = fDOMFilter.acceptNode (pi); 679 switch (code) { 680 case LSParserFilter.FILTER_INTERRUPT:{ 681 throw abort; 682 } 683 case LSParserFilter.FILTER_REJECT:{ 684 } 686 case LSParserFilter.FILTER_SKIP: { 687 fCurrentNode.removeChild (pi); 688 fFirstChunk = true; 692 return; 693 } 694 default: { 695 } 696 } 697 } 698 } 699 else { 700 int pi = fDeferredDocumentImpl. 701 createDeferredProcessingInstruction (target, data.toString ()); 702 fDeferredDocumentImpl.appendChild (fCurrentNodeIndex, pi); 703 } 704 705 } 707 729 public void startDocument (XMLLocator locator, String encoding, 730 NamespaceContext namespaceContext, Augmentations augs) 731 throws XNIException { 732 733 if (!fDeferNodeExpansion) { 734 if (fDocumentClassName.equals (DEFAULT_DOCUMENT_CLASS_NAME)) { 735 fDocument = new DocumentImpl (); 736 fDocumentImpl = (CoreDocumentImpl)fDocument; 737 fDocumentImpl.setStrictErrorChecking (false); 741 fDocumentImpl.setInputEncoding (encoding); 743 fDocumentImpl.setDocumentURI (locator.getExpandedSystemId ()); 745 } 746 else if (fDocumentClassName.equals (PSVI_DOCUMENT_CLASS_NAME)) { 747 fDocument = new PSVIDocumentImpl(); 748 fDocumentImpl = (CoreDocumentImpl)fDocument; 749 fStorePSVI = true; 750 fDocumentImpl.setStrictErrorChecking (false); 754 fDocumentImpl.setInputEncoding (encoding); 756 fDocumentImpl.setDocumentURI (locator.getExpandedSystemId ()); 758 } 759 else { 760 try { 762 ClassLoader cl = ObjectFactory.findClassLoader(); 763 Class documentClass = ObjectFactory.findProviderClass (fDocumentClassName, 764 cl, true); 765 fDocument = (Document )documentClass.newInstance (); 766 767 Class defaultDocClass = 769 ObjectFactory.findProviderClass (CORE_DOCUMENT_CLASS_NAME, 770 cl, true); 771 if (defaultDocClass.isAssignableFrom (documentClass)) { 772 fDocumentImpl = (CoreDocumentImpl)fDocument; 773 774 Class psviDocClass = ObjectFactory.findProviderClass (PSVI_DOCUMENT_CLASS_NAME, 775 cl, true); 776 if (psviDocClass.isAssignableFrom (documentClass)) { 777 fStorePSVI = true; 778 } 779 780 fDocumentImpl.setStrictErrorChecking (false); 784 fDocumentImpl.setInputEncoding (encoding); 786 if (locator != null) { 788 fDocumentImpl.setDocumentURI (locator.getExpandedSystemId ()); 789 } 790 } 791 } 792 catch (ClassNotFoundException e) { 793 } 795 catch (Exception e) { 796 throw new RuntimeException ( 797 DOMMessageFormatter.formatMessage( 798 DOMMessageFormatter.DOM_DOMAIN, 799 "CannotCreateDocumentClass", 800 new Object [] {fDocumentClassName})); 801 } 802 } 803 fCurrentNode = fDocument; 804 } 805 else { 806 fDeferredDocumentImpl = new DeferredDocumentImpl (fNamespaceAware); 807 fDocument = fDeferredDocumentImpl; 808 fDocumentIndex = fDeferredDocumentImpl.createDeferredDocument (); 809 812 fDeferredDocumentImpl.setInputEncoding (encoding); 814 fDeferredDocumentImpl.setDocumentURI (locator.getExpandedSystemId ()); 816 fCurrentNodeIndex = fDocumentIndex; 817 818 } 819 820 } 822 835 public void xmlDecl (String version, String encoding, String standalone, 836 Augmentations augs) 837 throws XNIException { 838 if (!fDeferNodeExpansion) { 839 if (fDocumentImpl != null) { 842 if (version != null) 843 fDocumentImpl.setXmlVersion (version); 844 fDocumentImpl.setXmlEncoding (encoding); 845 fDocumentImpl.setXmlStandalone ("yes".equals (standalone)); 846 } 847 } 848 else { 849 if (version != null) 850 fDeferredDocumentImpl.setXmlVersion (version); 851 fDeferredDocumentImpl.setXmlEncoding (encoding); 852 fDeferredDocumentImpl.setXmlStandalone ("yes".equals (standalone)); 853 } 854 } 856 868 public void doctypeDecl (String rootElement, 869 String publicId, String systemId, Augmentations augs) 870 throws XNIException { 871 872 if (!fDeferNodeExpansion) { 873 if (fDocumentImpl != null) { 874 fDocumentType = fDocumentImpl.createDocumentType ( 875 rootElement, publicId, systemId); 876 fCurrentNode.appendChild (fDocumentType); 877 } 878 } 879 else { 880 fDocumentTypeIndex = fDeferredDocumentImpl. 881 createDeferredDocumentType (rootElement, publicId, systemId); 882 fDeferredDocumentImpl.appendChild (fCurrentNodeIndex, fDocumentTypeIndex); 883 } 884 885 } 887 898 public void startElement (QName element, XMLAttributes attributes, Augmentations augs) 899 throws XNIException { 900 if (DEBUG_EVENTS) { 901 System.out.println ("==>startElement ("+element.rawname+")"); 902 } 903 if (!fDeferNodeExpansion) { 904 if (fFilterReject) { 905 return; 906 } 907 Element el = createElementNode (element); 908 int attrCount = attributes.getLength (); 909 for (int i = 0; i < attrCount; i++) { 910 attributes.getName (i, fAttrQName); 911 Attr attr = createAttrNode (fAttrQName); 912 913 String attrValue = attributes.getValue (i); 914 915 AttributePSVI attrPSVI =(AttributePSVI) attributes.getAugmentations (i).getItem (Constants.ATTRIBUTE_PSVI); 916 if (fStorePSVI && attrPSVI != null){ 917 ((PSVIAttrNSImpl) attr).setPSVI (attrPSVI); 918 } 919 920 921 attr.setValue (attrValue); 922 el.setAttributeNode (attr); 923 if (fDocumentImpl != null) { 928 AttrImpl attrImpl = (AttrImpl) attr; 929 Object type = null; 930 boolean id = false; 931 932 if (attrPSVI != null && fNamespaceAware) { 938 type = attrPSVI.getMemberTypeDefinition (); 940 if (type == null) { 941 type = attrPSVI.getTypeDefinition (); 942 if (type != null) { 943 id = ((XSSimpleType) type).isIDType (); 944 attrImpl.setType (type); 945 } 946 } 947 else { 948 id = ((XSSimpleType) type).isIDType (); 949 attrImpl.setType (type); 950 } 951 } 952 else { 953 boolean isDeclared = Boolean.TRUE.equals (attributes.getAugmentations (i).getItem (Constants.ATTRIBUTE_DECLARED)); 955 if (isDeclared) { 959 type = attributes.getType (i); 960 id = "ID".equals (type); 961 } 962 attrImpl.setType (type); 963 } 964 965 if (id) { 966 ((ElementImpl) el).setIdAttributeNode (attr, true); 967 } 968 969 attrImpl.setSpecified (attributes.isSpecified (i)); 970 } 972 } 973 setCharacterData (false); 974 975 if (augs != null) { 976 ElementPSVI elementPSVI = (ElementPSVI)augs.getItem (Constants.ELEMENT_PSVI); 977 if (elementPSVI != null && fNamespaceAware) { 978 XSTypeDefinition type = elementPSVI.getMemberTypeDefinition (); 979 if (type == null) { 980 type = elementPSVI.getTypeDefinition (); 981 } 982 ((ElementNSImpl)el).setType (type); 983 } 984 } 985 986 987 if (fDOMFilter != null && !fInEntityRef) { 989 if (fRoot.rawname == null) { 990 fRoot.setValues(element); 992 } else { 993 short code = fDOMFilter.startElement(el); 994 switch (code) { 995 case LSParserFilter.FILTER_INTERRUPT : 996 { 997 throw abort; 998 } 999 case LSParserFilter.FILTER_REJECT : 1000 { 1001 fFilterReject = true; 1002 fRejectedElement.setValues(element); 1003 return; 1004 } 1005 case LSParserFilter.FILTER_SKIP : 1006 { 1007 fSkippedElemStack.push(element.clone()); 1008 return; 1009 } 1010 default : {} 1011 } 1012 } 1013 } 1014 fCurrentNode.appendChild (el); 1015 fCurrentNode = el; 1016 } 1017 else { 1018 Object type = null; 1019 if (augs != null) { 1020 ElementPSVI elementPSVI = (ElementPSVI)augs.getItem (Constants.ELEMENT_PSVI); 1021 if (elementPSVI != null) { 1022 type = elementPSVI.getMemberTypeDefinition (); 1023 if (type == null) { 1024 type = elementPSVI.getTypeDefinition (); 1025 } 1026 } 1027 } 1028 1029 int el = 1030 fDeferredDocumentImpl.createDeferredElement (fNamespaceAware ? 1031 element.uri : null, 1032 element.rawname, 1033 type); 1034 int attrCount = attributes.getLength (); 1035 for (int i = 0; i < attrCount; i++) { 1036 AttributePSVI attrPSVI = (AttributePSVI)attributes.getAugmentations (i).getItem (Constants.ATTRIBUTE_PSVI); 1038 boolean id = false; 1039 1040 if (attrPSVI != null && fNamespaceAware) { 1046 type = attrPSVI.getMemberTypeDefinition (); 1048 if (type == null) { 1049 type = attrPSVI.getTypeDefinition (); 1050 if (type != null){ 1051 id = ((XSSimpleType) type).isIDType (); 1052 } 1053 } 1054 else { 1055 id = ((XSSimpleType) type).isIDType (); 1056 } 1057 } 1058 else { 1059 boolean isDeclared = Boolean.TRUE.equals (attributes.getAugmentations (i).getItem (Constants.ATTRIBUTE_DECLARED)); 1061 if (isDeclared) { 1065 type = attributes.getType (i); 1066 id = "ID".equals (type); 1067 } 1068 } 1069 1070 fDeferredDocumentImpl.setDeferredAttribute ( 1072 el, 1073 attributes.getQName (i), 1074 attributes.getURI (i), 1075 attributes.getValue (i), 1076 attributes.isSpecified (i), 1077 id, 1078 type); 1079 } 1080 1081 fDeferredDocumentImpl.appendChild (fCurrentNodeIndex, el); 1082 fCurrentNodeIndex = el; 1083 } 1084 } 1086 1087 1096 public void emptyElement (QName element, XMLAttributes attributes, Augmentations augs) 1097 throws XNIException { 1098 1099 startElement (element, attributes, augs); 1100 endElement (element, augs); 1101 1102 } 1104 1112 public void characters (XMLString text, Augmentations augs) throws XNIException { 1113 1114 if (DEBUG_EVENTS) { 1115 System.out.println ("==>characters(): "+text.toString ()); 1116 } 1117 1118 if (!fDeferNodeExpansion) { 1119 1120 if (fFilterReject) { 1121 return; 1122 } 1123 if (fInCDATASection && fCreateCDATANodes) { 1124 if (fCurrentCDATASection == null) { 1125 fCurrentCDATASection = 1126 fDocument.createCDATASection (text.toString ()); 1127 fCurrentNode.appendChild (fCurrentCDATASection); 1128 fCurrentNode = fCurrentCDATASection; 1129 } 1130 else { 1131 fCurrentCDATASection.appendData (text.toString ()); 1132 } 1133 } 1134 else if (!fInDTD) { 1135 if (text.length == 0) { 1138 return; 1139 } 1140 1141 String value = text.toString (); 1142 Node child = fCurrentNode.getLastChild (); 1143 if (child != null && child.getNodeType () == Node.TEXT_NODE) { 1144 if (fFirstChunk) { 1146 if (fDocumentImpl != null) { 1147 fStringBuffer.append (((TextImpl)child).removeData ()); 1148 } else { 1149 fStringBuffer.append (((Text )child).getData ()); 1150 ((Text )child).setNodeValue (null); 1151 } 1152 fFirstChunk = false; 1153 } 1154 fStringBuffer.append (value); 1155 } 1156 else { 1157 fFirstChunk = true; 1158 Text textNode = fDocument.createTextNode (value); 1159 fCurrentNode.appendChild (textNode); 1160 } 1161 1162 } 1163 } 1164 else { 1165 if (fInCDATASection && fCreateCDATANodes) { 1168 if (fCurrentCDATASectionIndex == -1) { 1169 int cs = fDeferredDocumentImpl. 1170 createDeferredCDATASection (text.toString ()); 1171 1172 fDeferredDocumentImpl.appendChild (fCurrentNodeIndex, cs); 1173 fCurrentCDATASectionIndex = cs; 1174 fCurrentNodeIndex = cs; 1175 } 1176 else { 1177 int txt = fDeferredDocumentImpl. 1178 createDeferredTextNode (text.toString (), false); 1179 fDeferredDocumentImpl.appendChild (fCurrentNodeIndex, txt); 1180 } 1181 } else if (!fInDTD) { 1182 if (text.length == 0) { 1185 return; 1186 } 1187 1188 String value = text.toString (); 1189 int txt = fDeferredDocumentImpl. 1190 createDeferredTextNode (value, false); 1191 fDeferredDocumentImpl.appendChild (fCurrentNodeIndex, txt); 1192 1193 } 1194 } 1195 } 1197 1210 public void ignorableWhitespace (XMLString text, Augmentations augs) throws XNIException { 1211 1212 if (!fIncludeIgnorableWhitespace || fFilterReject) { 1213 return; 1214 } 1215 if (!fDeferNodeExpansion) { 1216 Node child = fCurrentNode.getLastChild (); 1217 if (child != null && child.getNodeType () == Node.TEXT_NODE) { 1218 Text textNode = (Text )child; 1219 textNode.appendData (text.toString ()); 1220 } 1221 else { 1222 Text textNode = fDocument.createTextNode (text.toString ()); 1223 if (fDocumentImpl != null) { 1224 TextImpl textNodeImpl = (TextImpl)textNode; 1225 textNodeImpl.setIgnorableWhitespace (true); 1226 } 1227 fCurrentNode.appendChild (textNode); 1228 } 1229 } 1230 else { 1231 int txt = fDeferredDocumentImpl. 1234 createDeferredTextNode (text.toString (), true); 1235 fDeferredDocumentImpl.appendChild (fCurrentNodeIndex, txt); 1236 } 1237 1238 } 1240 1248 public void endElement (QName element, Augmentations augs) throws XNIException { 1249 if (DEBUG_EVENTS) { 1250 System.out.println ("==>endElement ("+element.rawname+")"); 1251 } 1252 if (!fDeferNodeExpansion) { 1253 1254 if (augs != null && fDocumentImpl != null && (fNamespaceAware || fStorePSVI)) { 1256 ElementPSVI elementPSVI = (ElementPSVI) augs.getItem(Constants.ELEMENT_PSVI); 1257 if (elementPSVI != null) { 1258 if (fNamespaceAware) { 1262 XSTypeDefinition type = elementPSVI.getMemberTypeDefinition(); 1263 if (type == null) { 1264 type = elementPSVI.getTypeDefinition(); 1265 } 1266 ((ElementNSImpl)fCurrentNode).setType(type); 1267 } 1268 if (fStorePSVI) { 1269 ((PSVIElementNSImpl)fCurrentNode).setPSVI (elementPSVI); 1270 } 1271 } 1272 } 1273 1274 if (fDOMFilter != null) { 1275 if (fFilterReject) { 1276 if (element.equals (fRejectedElement)) { 1277 fFilterReject = false; 1278 } 1279 return; 1280 } 1281 if (!fSkippedElemStack.isEmpty ()) { 1282 if (fSkippedElemStack.peek ().equals (element)) { 1283 fSkippedElemStack.pop (); 1284 return; 1285 } 1286 } 1287 setCharacterData (false); 1288 if (!fRoot.equals(element) && !fInEntityRef && (fDOMFilter.getWhatToShow () & NodeFilter.SHOW_ELEMENT)!=0) { 1289 short code = fDOMFilter.acceptNode (fCurrentNode); 1290 switch (code) { 1291 case LSParserFilter.FILTER_INTERRUPT:{ 1292 throw abort; 1293 } 1294 case LSParserFilter.FILTER_REJECT:{ 1295 Node parent = fCurrentNode.getParentNode (); 1296 parent.removeChild (fCurrentNode); 1297 fCurrentNode = parent; 1298 return; 1299 } 1300 case LSParserFilter.FILTER_SKIP: { 1301 fFirstChunk = true; 1307 1308 Node parent = fCurrentNode.getParentNode (); 1310 NodeList ls = fCurrentNode.getChildNodes (); 1311 int length = ls.getLength (); 1312 1313 for (int i=0;i<length;i++) { 1314 parent.appendChild (ls.item (0)); 1315 } 1316 parent.removeChild (fCurrentNode); 1317 fCurrentNode = parent; 1318 1319 return; 1320 } 1321 1322 default: { } 1323 } 1324 } 1325 fCurrentNode = fCurrentNode.getParentNode (); 1326 1327 } else { 1329 setCharacterData (false); 1330 fCurrentNode = fCurrentNode.getParentNode (); 1331 } 1332 1333 } 1334 else { 1335 fCurrentNodeIndex = 1336 fDeferredDocumentImpl.getParentNode (fCurrentNodeIndex, false); 1337 } 1338 1339 1340 } 1342 1343 1349 public void startCDATA (Augmentations augs) throws XNIException { 1350 1351 fInCDATASection = true; 1352 if (!fDeferNodeExpansion) { 1353 if (fFilterReject) { 1354 return; 1355 } 1356 if (fCreateCDATANodes) { 1357 setCharacterData (false); 1358 } 1359 } 1360 } 1362 1368 public void endCDATA (Augmentations augs) throws XNIException { 1369 1370 fInCDATASection = false; 1371 if (!fDeferNodeExpansion) { 1372 1373 if (fFilterReject) { 1374 return; 1375 } 1376 1377 if (fCurrentCDATASection !=null) { 1378 1379 if (fDOMFilter !=null && !fInEntityRef && 1380 (fDOMFilter.getWhatToShow () & NodeFilter.SHOW_CDATA_SECTION)!= 0) { 1381 short code = fDOMFilter.acceptNode (fCurrentCDATASection); 1382 switch (code) { 1383 case LSParserFilter.FILTER_INTERRUPT:{ 1384 throw abort; 1385 } 1386 case LSParserFilter.FILTER_REJECT:{ 1387 } 1389 case LSParserFilter.FILTER_SKIP: { 1390 Node parent = fCurrentNode.getParentNode (); 1391 parent.removeChild (fCurrentCDATASection); 1392 fCurrentNode = parent; 1393 return; 1394 } 1395 1396 default: { 1397 } 1399 } 1400 } 1401 1402 fCurrentNode = fCurrentNode.getParentNode (); 1403 fCurrentCDATASection = null; 1404 } 1405 } 1406 else { 1407 if (fCurrentCDATASectionIndex !=-1) { 1408 fCurrentNodeIndex = 1409 fDeferredDocumentImpl.getParentNode (fCurrentNodeIndex, false); 1410 fCurrentCDATASectionIndex = -1; 1411 } 1412 } 1413 1414 } 1416 1422 public void endDocument (Augmentations augs) throws XNIException { 1423 1424 if (!fDeferNodeExpansion) { 1425 if (fDocumentImpl != null) { 1429 fDocumentImpl.setStrictErrorChecking (true); 1430 } 1431 fCurrentNode = null; 1432 } 1433 else { 1434 fCurrentNodeIndex = -1; 1435 } 1436 1437 } 1439 1451 public void endGeneralEntity (String name, Augmentations augs) throws XNIException { 1452 if (DEBUG_EVENTS) { 1453 System.out.println ("==>endGeneralEntity: ("+name+")"); 1454 } 1455 if (!fDeferNodeExpansion) { 1456 1457 if (fFilterReject) { 1458 return; 1459 } 1460 setCharacterData (true); 1461 1462 if (fDocumentType != null) { 1463 NamedNodeMap entities = fDocumentType.getEntities (); 1465 fCurrentEntityDecl = (EntityImpl) entities.getNamedItem (name); 1466 if (fCurrentEntityDecl != null) { 1467 if (fCurrentEntityDecl != null && fCurrentEntityDecl.getFirstChild () == null) { 1468 fCurrentEntityDecl.setReadOnly (false, true); 1469 Node child = fCurrentNode.getFirstChild (); 1470 while (child != null) { 1471 Node copy = child.cloneNode (true); 1472 fCurrentEntityDecl.appendChild (copy); 1473 child = child.getNextSibling (); 1474 } 1475 fCurrentEntityDecl.setReadOnly (true, true); 1476 1477 } 1479 fCurrentEntityDecl = null; 1480 } 1481 1482 } 1483 fInEntityRef = false; 1484 boolean removeEntityRef = false; 1485 if (fCreateEntityRefNodes) { 1486 if (fDocumentImpl != null) { 1487 ((NodeImpl)fCurrentNode).setReadOnly (true, true); 1489 } 1490 1491 if (fDOMFilter !=null && 1492 (fDOMFilter.getWhatToShow () & NodeFilter.SHOW_ENTITY_REFERENCE)!= 0) { 1493 short code = fDOMFilter.acceptNode (fCurrentNode); 1494 switch (code) { 1495 case LSParserFilter.FILTER_INTERRUPT:{ 1496 throw abort; 1497 } 1498 case LSParserFilter.FILTER_REJECT:{ 1499 Node parent = fCurrentNode.getParentNode (); 1500 parent.removeChild (fCurrentNode); 1501 fCurrentNode = parent; 1502 return; 1503 1504 } 1505 case LSParserFilter.FILTER_SKIP: { 1506 fFirstChunk = true; 1508 removeEntityRef = true; 1509 break; 1510 } 1511 1512 default: { 1513 fCurrentNode = fCurrentNode.getParentNode (); 1514 } 1515 } 1516 } else { 1517 fCurrentNode = fCurrentNode.getParentNode (); 1518 } 1519 } 1520 1521 if (!fCreateEntityRefNodes || removeEntityRef) { 1522 NodeList children = fCurrentNode.getChildNodes (); 1525 Node parent = fCurrentNode.getParentNode (); 1526 int length = children.getLength (); 1527 if (length > 0) { 1528 1529 Node node = fCurrentNode.getPreviousSibling (); 1531 Node child = children.item (0); 1533 if (node != null && node.getNodeType () == Node.TEXT_NODE && 1534 child.getNodeType () == Node.TEXT_NODE) { 1535 ((Text )node).appendData (child.getNodeValue ()); 1536 fCurrentNode.removeChild (child); 1537 1538 } else { 1539 node = parent.insertBefore (child, fCurrentNode); 1540 handleBaseURI (node); 1541 } 1542 1543 for (int i=1;i <length;i++) { 1544 node = parent.insertBefore (children.item (0), fCurrentNode); 1545 handleBaseURI (node); 1546 } 1547 } parent.removeChild (fCurrentNode); 1549 fCurrentNode = parent; 1550 } 1551 } 1552 else { 1553 1554 if (fDocumentTypeIndex != -1) { 1555 int node = fDeferredDocumentImpl.getLastChild (fDocumentTypeIndex, false); 1557 while (node != -1) { 1558 short nodeType = fDeferredDocumentImpl.getNodeType (node, false); 1559 if (nodeType == Node.ENTITY_NODE) { 1560 String nodeName = 1561 fDeferredDocumentImpl.getNodeName (node, false); 1562 if (nodeName.equals (name)) { 1563 fDeferredEntityDecl = node; 1564 break; 1565 } 1566 } 1567 node = fDeferredDocumentImpl.getRealPrevSibling (node, false); 1568 } 1569 } 1570 1571 if (fDeferredEntityDecl != -1 && 1572 fDeferredDocumentImpl.getLastChild (fDeferredEntityDecl, false) == -1) { 1573 int prevIndex = -1; 1575 int childIndex = fDeferredDocumentImpl.getLastChild (fCurrentNodeIndex, false); 1576 while (childIndex != -1) { 1577 int cloneIndex = fDeferredDocumentImpl.cloneNode (childIndex, true); 1578 fDeferredDocumentImpl.insertBefore (fDeferredEntityDecl, cloneIndex, prevIndex); 1579 prevIndex = cloneIndex; 1580 childIndex = fDeferredDocumentImpl.getRealPrevSibling (childIndex, false); 1581 } 1582 } 1583 if (fCreateEntityRefNodes) { 1584 fCurrentNodeIndex = 1585 fDeferredDocumentImpl.getParentNode (fCurrentNodeIndex, 1586 false); 1587 } else { 1591 int childIndex = fDeferredDocumentImpl.getLastChild (fCurrentNodeIndex, false); 1593 int parentIndex = 1594 fDeferredDocumentImpl.getParentNode (fCurrentNodeIndex, 1595 false); 1596 1597 int prevIndex = fCurrentNodeIndex; 1598 int lastChild = childIndex; 1599 int sibling = -1; 1600 while (childIndex != -1) { 1601 handleBaseURI (childIndex); 1602 sibling = fDeferredDocumentImpl.getRealPrevSibling (childIndex, false); 1603 fDeferredDocumentImpl.insertBefore (parentIndex, childIndex, prevIndex); 1604 prevIndex = childIndex; 1605 childIndex = sibling; 1606 } 1607 if(lastChild != -1) 1608 fDeferredDocumentImpl.setAsLastChild (parentIndex, lastChild); 1609 else{ 1610 sibling = fDeferredDocumentImpl.getRealPrevSibling (prevIndex, false); 1611 fDeferredDocumentImpl.setAsLastChild (parentIndex, sibling); 1612 } 1613 fCurrentNodeIndex = parentIndex; 1614 } 1615 fDeferredEntityDecl = -1; 1616 } 1617 1618 1619 } 1621 1622 1629 protected final void handleBaseURI (Node node){ 1630 if (fDocumentImpl != null) { 1631 1634 String baseURI = null; 1635 short nodeType = node.getNodeType (); 1636 1637 if (nodeType == Node.ELEMENT_NODE) { 1638 if (fNamespaceAware) { 1641 if (((Element )node).getAttributeNodeNS ("http://www.w3.org/XML/1998/namespace","base")!=null) { 1642 return; 1643 } 1644 } else if (((Element )node).getAttributeNode ("xml:base") != null) { 1645 return; 1646 } 1647 baseURI = ((EntityReferenceImpl)fCurrentNode).getBaseURI (); 1649 if (baseURI !=null && !baseURI.equals (fDocumentImpl.getDocumentURI ())) { 1650 if (fNamespaceAware) { 1651 ((Element )node).setAttributeNS ("http://www.w3.org/XML/1998/namespace","base", baseURI); 1652 } else { 1653 ((Element )node).setAttribute ("xml:base", baseURI); 1654 } 1655 } 1656 } 1657 else if (nodeType == Node.PROCESSING_INSTRUCTION_NODE) { 1658 1659 baseURI = ((EntityReferenceImpl)fCurrentNode).getBaseURI (); 1660 if (baseURI !=null && fErrorHandler != null) { 1661 DOMErrorImpl error = new DOMErrorImpl (); 1662 error.fType = "pi-base-uri-not-preserved"; 1663 error.fRelatedData = baseURI; 1664 error.fSeverity = DOMError.SEVERITY_WARNING; 1665 fErrorHandler.getErrorHandler ().handleError (error); 1666 } 1667 } 1668 } 1669 } 1670 1671 1679 protected final void handleBaseURI (int node){ 1680 short nodeType = fDeferredDocumentImpl.getNodeType (node, false); 1681 1682 if (nodeType == Node.ELEMENT_NODE) { 1683 String baseURI = fDeferredDocumentImpl.getNodeValueString (fCurrentNodeIndex, false); 1684 if (baseURI == null) { 1685 baseURI = fDeferredDocumentImpl.getDeferredEntityBaseURI (fDeferredEntityDecl); 1686 } 1687 if (baseURI !=null && !baseURI.equals (fDeferredDocumentImpl.getDocumentURI ())) { 1688 fDeferredDocumentImpl.setDeferredAttribute (node, 1689 "xml:base", 1690 "http://www.w3.org/XML/1998/namespace", 1691 baseURI, 1692 true); 1693 } 1694 } 1695 else if (nodeType == Node.PROCESSING_INSTRUCTION_NODE) { 1696 1697 1698 String baseURI = fDeferredDocumentImpl.getNodeValueString (fCurrentNodeIndex, false); 1700 1701 if (baseURI == null) { 1702 baseURI = fDeferredDocumentImpl.getDeferredEntityBaseURI (fDeferredEntityDecl); 1704 } 1705 1706 if (baseURI != null && fErrorHandler != null) { 1707 DOMErrorImpl error = new DOMErrorImpl (); 1708 error.fType = "pi-base-uri-not-preserved"; 1709 error.fRelatedData = baseURI; 1710 error.fSeverity = DOMError.SEVERITY_WARNING; 1711 fErrorHandler.getErrorHandler ().handleError (error); 1712 } 1713 } 1714 } 1715 1716 1717 1721 1735 public void startDTD (XMLLocator locator, Augmentations augs) throws XNIException { 1736 if (DEBUG_EVENTS) { 1737 System.out.println ("==>startDTD"); 1738 if (DEBUG_BASEURI) { 1739 System.out.println (" expandedSystemId: "+locator.getExpandedSystemId ()); 1740 System.out.println (" baseURI:"+ locator.getBaseSystemId ()); 1741 } 1742 } 1743 1744 fInDTD = true; 1745 if (locator != null) { 1746 fBaseURIStack.push (locator.getBaseSystemId ()); 1747 } 1748 if (fDeferNodeExpansion || fDocumentImpl != null) { 1749 fInternalSubset = new StringBuffer (1024); 1750 } 1751 } 1753 1754 1762 public void endDTD (Augmentations augs) throws XNIException { 1763 if (DEBUG_EVENTS) { 1764 System.out.println ("==>endDTD()"); 1765 } 1766 fInDTD = false; 1767 if (!fBaseURIStack.isEmpty ()) { 1768 fBaseURIStack.pop (); 1769 } 1770 String internalSubset = fInternalSubset != null && fInternalSubset.length () > 0 1771 ? fInternalSubset.toString () : null; 1772 if (fDeferNodeExpansion) { 1773 if (internalSubset != null) { 1774 fDeferredDocumentImpl.setInternalSubset (fDocumentTypeIndex, internalSubset); 1775 } 1776 } 1777 else if (fDocumentImpl != null) { 1778 if (internalSubset != null) { 1779 ((DocumentTypeImpl)fDocumentType).setInternalSubset (internalSubset); 1780 } 1781 } 1782 } 1784 1797 public void startConditional (short type, Augmentations augs) throws XNIException { 1798 } 1800 1808 public void endConditional (Augmentations augs) throws XNIException { 1809 } 1811 1812 1820 public void startExternalSubset (XMLResourceIdentifier identifier, 1821 Augmentations augs) throws XNIException { 1822 if (DEBUG_EVENTS) { 1823 System.out.println ("==>startExternalSubset"); 1824 if (DEBUG_BASEURI) { 1825 System.out.println (" expandedSystemId: "+identifier.getExpandedSystemId ()); 1826 System.out.println (" baseURI:"+ identifier.getBaseSystemId ()); 1827 } 1828 } 1829 fBaseURIStack.push (identifier.getBaseSystemId ()); 1830 fInDTDExternalSubset = true; 1831 } 1833 1841 public void endExternalSubset (Augmentations augs) throws XNIException { 1842 fInDTDExternalSubset = false; 1843 fBaseURIStack.pop (); 1844 } 1846 1862 public void internalEntityDecl (String name, XMLString text, 1863 XMLString nonNormalizedText, 1864 Augmentations augs) throws XNIException { 1865 1866 if (DEBUG_EVENTS) { 1867 System.out.println ("==>internalEntityDecl: "+name); 1868 if (DEBUG_BASEURI) { 1869 System.out.println (" baseURI:"+ (String )fBaseURIStack.peek ()); 1870 } 1871 } 1872 if (fInternalSubset != null && !fInDTDExternalSubset) { 1874 fInternalSubset.append ("<!ENTITY "); 1875 if (name.startsWith ("%")) { 1876 fInternalSubset.append ("% "); 1877 fInternalSubset.append (name.substring (1)); 1878 } 1879 else { 1880 fInternalSubset.append (name); 1881 } 1882 fInternalSubset.append (' '); 1883 String value = nonNormalizedText.toString (); 1884 boolean singleQuote = value.indexOf ('\'') == -1; 1885 fInternalSubset.append (singleQuote ? '\'' : '"'); 1886 fInternalSubset.append (value); 1887 fInternalSubset.append (singleQuote ? '\'' : '"'); 1888 fInternalSubset.append (">\n"); 1889 } 1890 1891 1895 if(name.startsWith ("%")) 1898 return; 1899 if (fDocumentType != null) { 1900 NamedNodeMap entities = fDocumentType.getEntities (); 1901 EntityImpl entity = (EntityImpl)entities.getNamedItem (name); 1902 if (entity == null) { 1903 entity = (EntityImpl)fDocumentImpl.createEntity (name); 1904 entity.setBaseURI ((String )fBaseURIStack.peek ()); 1905 entities.setNamedItem (entity); 1906 } 1907 } 1908 1909 if (fDocumentTypeIndex != -1) { 1911 boolean found = false; 1912 int node = fDeferredDocumentImpl.getLastChild (fDocumentTypeIndex, false); 1913 while (node != -1) { 1914 short nodeType = fDeferredDocumentImpl.getNodeType (node, false); 1915 if (nodeType == Node.ENTITY_NODE) { 1916 String nodeName = fDeferredDocumentImpl.getNodeName (node, false); 1917 if (nodeName.equals (name)) { 1918 found = true; 1919 break; 1920 } 1921 } 1922 node = fDeferredDocumentImpl.getRealPrevSibling (node, false); 1923 } 1924 if (!found) { 1925 int entityIndex = 1926 fDeferredDocumentImpl.createDeferredEntity (name, null, null, null, (String )fBaseURIStack.peek ()); 1927 fDeferredDocumentImpl.appendChild (fDocumentTypeIndex, entityIndex); 1928 } 1929 } 1930 1931 } 1933 1946 public void externalEntityDecl (String name, XMLResourceIdentifier identifier, 1947 Augmentations augs) throws XNIException { 1948 1949 1950 if (DEBUG_EVENTS) { 1951 System.out.println ("==>externalEntityDecl: "+name); 1952 if (DEBUG_BASEURI) { 1953 System.out.println (" expandedSystemId:"+ identifier.getExpandedSystemId ()); 1954 System.out.println (" baseURI:"+ identifier.getBaseSystemId ()); 1955 } 1956 } 1957 String publicId = identifier.getPublicId (); 1959 String literalSystemId = identifier.getLiteralSystemId (); 1960 if (fInternalSubset != null && !fInDTDExternalSubset) { 1961 fInternalSubset.append ("<!ENTITY "); 1962 if (name.startsWith ("%")) { 1963 fInternalSubset.append ("% "); 1964 fInternalSubset.append (name.substring (1)); 1965 } 1966 else { 1967 fInternalSubset.append (name); 1968 } 1969 fInternalSubset.append (' '); 1970 if (publicId != null) { 1971 fInternalSubset.append ("PUBLIC '"); 1972 fInternalSubset.append (publicId); 1973 fInternalSubset.append ("' '"); 1974 } 1975 else { 1976 fInternalSubset.append ("SYSTEM '"); 1977 } 1978 fInternalSubset.append (literalSystemId); 1979 fInternalSubset.append ("'>\n"); 1980 } 1981 1982 1986 if(name.startsWith ("%")) 1989 return; 1990 if (fDocumentType != null) { 1991 NamedNodeMap entities = fDocumentType.getEntities (); 1992 EntityImpl entity = (EntityImpl)entities.getNamedItem (name); 1993 if (entity == null) { 1994 entity = (EntityImpl)fDocumentImpl.createEntity (name); 1995 entity.setPublicId (publicId); 1996 entity.setSystemId (literalSystemId); 1997 entity.setBaseURI (identifier.getBaseSystemId ()); 1998 entities.setNamedItem (entity); 1999 } 2000 } 2001 2002 if (fDocumentTypeIndex != -1) { 2004 boolean found = false; 2005 int nodeIndex = fDeferredDocumentImpl.getLastChild (fDocumentTypeIndex, false); 2006 while (nodeIndex != -1) { 2007 short nodeType = fDeferredDocumentImpl.getNodeType (nodeIndex, false); 2008 if (nodeType == Node.ENTITY_NODE) { 2009 String nodeName = fDeferredDocumentImpl.getNodeName (nodeIndex, false); 2010 if (nodeName.equals (name)) { 2011 found = true; 2012 break; 2013 } 2014 } 2015 nodeIndex = fDeferredDocumentImpl.getRealPrevSibling (nodeIndex, false); 2016 } 2017 if (!found) { 2018 int entityIndex = fDeferredDocumentImpl.createDeferredEntity ( 2019 name, publicId, literalSystemId, null, identifier.getBaseSystemId ()); 2020 fDeferredDocumentImpl.appendChild (fDocumentTypeIndex, entityIndex); 2021 } 2022 } 2023 2024 } 2026 2027 2042 public void startParameterEntity (String name, 2043 XMLResourceIdentifier identifier, 2044 String encoding, 2045 Augmentations augs) throws XNIException { 2046 if (DEBUG_EVENTS) { 2047 System.out.println ("==>startParameterEntity: "+name); 2048 if (DEBUG_BASEURI) { 2049 System.out.println (" expandedSystemId: "+identifier.getExpandedSystemId ()); 2050 System.out.println (" baseURI:"+ identifier.getBaseSystemId ()); 2051 } 2052 } 2053 fBaseURIStack.push (identifier.getExpandedSystemId ()); 2054 } 2055 2056 2057 2067 public void endParameterEntity (String name, Augmentations augs) throws XNIException { 2068 2069 if (DEBUG_EVENTS) { 2070 System.out.println ("==>endParameterEntity: "+name); 2071 } 2072 fBaseURIStack.pop (); 2073 } 2074 2075 2087 public void unparsedEntityDecl (String name, XMLResourceIdentifier identifier, 2088 String notation, Augmentations augs) 2089 throws XNIException { 2090 2091 if (DEBUG_EVENTS) { 2092 System.out.println ("==>unparsedEntityDecl: "+name); 2093 if (DEBUG_BASEURI) { 2094 System.out.println (" expandedSystemId:"+ identifier.getExpandedSystemId ()); 2095 System.out.println (" baseURI:"+ identifier.getBaseSystemId ()); 2096 } 2097 } 2098 String publicId = identifier.getPublicId (); 2100 String literalSystemId = identifier.getLiteralSystemId (); 2101 if (fInternalSubset != null && !fInDTDExternalSubset) { 2102 fInternalSubset.append ("<!ENTITY "); 2103 fInternalSubset.append (name); 2104 fInternalSubset.append (' '); 2105 if (publicId != null) { 2106 fInternalSubset.append ("PUBLIC '"); 2107 fInternalSubset.append (publicId); 2108 if (literalSystemId != null) { 2109 fInternalSubset.append ("' '"); 2110 fInternalSubset.append (literalSystemId); 2111 } 2112 } 2113 else { 2114 fInternalSubset.append ("SYSTEM '"); 2115 fInternalSubset.append (literalSystemId); 2116 } 2117 fInternalSubset.append ("' NDATA "); 2118 fInternalSubset.append (notation); 2119 fInternalSubset.append (">\n"); 2120 } 2121 2122 2126 if (fDocumentType != null) { 2128 NamedNodeMap entities = fDocumentType.getEntities (); 2129 EntityImpl entity = (EntityImpl)entities.getNamedItem (name); 2130 if (entity == null) { 2131 entity = (EntityImpl)fDocumentImpl.createEntity (name); 2132 entity.setPublicId (publicId); 2133 entity.setSystemId (literalSystemId); 2134 entity.setNotationName (notation); 2135 entity.setBaseURI (identifier.getBaseSystemId ()); 2136 entities.setNamedItem (entity); 2137 } 2138 } 2139 2140 if (fDocumentTypeIndex != -1) { 2142 boolean found = false; 2143 int nodeIndex = fDeferredDocumentImpl.getLastChild (fDocumentTypeIndex, false); 2144 while (nodeIndex != -1) { 2145 short nodeType = fDeferredDocumentImpl.getNodeType (nodeIndex, false); 2146 if (nodeType == Node.ENTITY_NODE) { 2147 String nodeName = fDeferredDocumentImpl.getNodeName (nodeIndex, false); 2148 if (nodeName.equals (name)) { 2149 found = true; 2150 break; 2151 } 2152 } 2153 nodeIndex = fDeferredDocumentImpl.getRealPrevSibling (nodeIndex, false); 2154 } 2155 if (!found) { 2156 int entityIndex = fDeferredDocumentImpl.createDeferredEntity ( 2157 name, publicId, literalSystemId, notation, identifier.getBaseSystemId ()); 2158 fDeferredDocumentImpl.appendChild (fDocumentTypeIndex, entityIndex); 2159 } 2160 } 2161 2162 } 2164 2175 public void notationDecl (String name, XMLResourceIdentifier identifier, 2176 Augmentations augs) throws XNIException { 2177 2178 String publicId = identifier.getPublicId (); 2180 String literalSystemId = identifier.getLiteralSystemId (); 2181 if (fInternalSubset != null && !fInDTDExternalSubset) { 2182 fInternalSubset.append ("<!NOTATION "); 2183 fInternalSubset.append (name); 2184 if (publicId != null) { 2185 fInternalSubset.append (" PUBLIC '"); 2186 fInternalSubset.append (publicId); 2187 if (literalSystemId != null) { 2188 fInternalSubset.append ("' '"); 2189 fInternalSubset.append (literalSystemId); 2190 } 2191 } 2192 else { 2193 fInternalSubset.append (" SYSTEM '"); 2194 fInternalSubset.append (literalSystemId); 2195 } 2196 fInternalSubset.append ("'>\n"); 2197 } 2198 2199 2203 if (fDocumentImpl !=null && fDocumentType != null) { 2205 NamedNodeMap notations = fDocumentType.getNotations (); 2206 if (notations.getNamedItem (name) == null) { 2207 NotationImpl notation = (NotationImpl)fDocumentImpl.createNotation (name); 2208 notation.setPublicId (publicId); 2209 notation.setSystemId (literalSystemId); 2210 notation.setBaseURI (identifier.getBaseSystemId ()); 2211 notations.setNamedItem (notation); 2212 } 2213 } 2214 2215 if (fDocumentTypeIndex != -1) { 2217 boolean found = false; 2218 int nodeIndex = fDeferredDocumentImpl.getLastChild (fDocumentTypeIndex, false); 2219 while (nodeIndex != -1) { 2220 short nodeType = fDeferredDocumentImpl.getNodeType (nodeIndex, false); 2221 if (nodeType == Node.NOTATION_NODE) { 2222 String nodeName = fDeferredDocumentImpl.getNodeName (nodeIndex, false); 2223 if (nodeName.equals (name)) { 2224 found = true; 2225 break; 2226 } 2227 } 2228 nodeIndex = fDeferredDocumentImpl.getPrevSibling (nodeIndex, false); 2229 } 2230 if (!found) { 2231 int notationIndex = fDeferredDocumentImpl.createDeferredNotation ( 2232 name, publicId, literalSystemId, identifier.getBaseSystemId ()); 2233 fDeferredDocumentImpl.appendChild (fDocumentTypeIndex, notationIndex); 2234 } 2235 } 2236 2237 } 2239 2248 public void ignoredCharacters (XMLString text, Augmentations augs) throws XNIException { 2249 } 2251 2252 2262 public void elementDecl (String name, String contentModel, Augmentations augs) 2263 throws XNIException { 2264 2265 if (fInternalSubset != null && !fInDTDExternalSubset) { 2267 fInternalSubset.append ("<!ELEMENT "); 2268 fInternalSubset.append (name); 2269 fInternalSubset.append (' '); 2270 fInternalSubset.append (contentModel); 2271 fInternalSubset.append (">\n"); 2272 } 2273 2274 } 2276 2301 public void attributeDecl (String elementName, String attributeName, 2302 String type, String [] enumeration, 2303 String defaultType, XMLString defaultValue, 2304 XMLString nonNormalizedDefaultValue, Augmentations augs) throws XNIException { 2305 2306 if (fInternalSubset != null && !fInDTDExternalSubset) { 2308 fInternalSubset.append ("<!ATTLIST "); 2309 fInternalSubset.append (elementName); 2310 fInternalSubset.append (' '); 2311 fInternalSubset.append (attributeName); 2312 fInternalSubset.append (' '); 2313 if (type.equals ("ENUMERATION")) { 2314 fInternalSubset.append ('('); 2315 for (int i = 0; i < enumeration.length; i++) { 2316 if (i > 0) { 2317 fInternalSubset.append ('|'); 2318 } 2319 fInternalSubset.append (enumeration[i]); 2320 } 2321 fInternalSubset.append (')'); 2322 } 2323 else { 2324 fInternalSubset.append (type); 2325 } 2326 if (defaultType != null) { 2327 fInternalSubset.append (' '); 2328 fInternalSubset.append (defaultType); 2329 } 2330 if (defaultValue != null) { 2331 fInternalSubset.append (" '"); 2332 for (int i = 0; i < defaultValue.length; i++) { 2333 char c = defaultValue.ch[defaultValue.offset + i]; 2334 if (c == '\'') { 2335 fInternalSubset.append ("'"); 2336 } 2337 else { 2338 fInternalSubset.append (c); 2339 } 2340 } 2341 fInternalSubset.append ('\''); 2342 } 2343 fInternalSubset.append (">\n"); 2344 } 2345 2348 if (fDeferredDocumentImpl != null) { 2350 2351 if (defaultValue != null) { 2353 2354 int elementDefIndex = fDeferredDocumentImpl.lookupElementDefinition (elementName); 2356 2357 if (elementDefIndex == -1) { 2359 elementDefIndex = fDeferredDocumentImpl.createDeferredElementDefinition (elementName); 2360 fDeferredDocumentImpl.appendChild (fDocumentTypeIndex, elementDefIndex); 2361 } 2362 int attrIndex = fDeferredDocumentImpl.createDeferredAttribute ( 2364 attributeName, defaultValue.toString (), false); 2365 if ("ID".equals (type)) { 2366 fDeferredDocumentImpl.setIdAttribute (attrIndex); 2367 } 2368 fDeferredDocumentImpl.appendChild (elementDefIndex, attrIndex); 2370 } 2371 2372 } 2374 else if (fDocumentImpl != null) { 2376 2377 if (defaultValue != null) { 2379 2380 NamedNodeMap elements = ((DocumentTypeImpl)fDocumentType).getElements (); 2382 ElementDefinitionImpl elementDef = (ElementDefinitionImpl)elements.getNamedItem (elementName); 2383 if (elementDef == null) { 2384 elementDef = fDocumentImpl.createElementDefinition (elementName); 2385 ((DocumentTypeImpl)fDocumentType).getElements ().setNamedItem (elementDef); 2386 } 2387 2388 2390 boolean nsEnabled = fNamespaceAware; 2392 AttrImpl attr; 2393 if (nsEnabled) { 2394 String namespaceURI = null; 2395 if (attributeName.startsWith ("xmlns:") || 2400 attributeName.equals ("xmlns")) { 2401 namespaceURI = NamespaceContext.XMLNS_URI; 2402 } 2403 attr = (AttrImpl)fDocumentImpl.createAttributeNS (namespaceURI, 2404 attributeName); 2405 } 2406 else { 2407 attr = (AttrImpl)fDocumentImpl.createAttribute (attributeName); 2408 } 2409 attr.setValue (defaultValue.toString ()); 2410 attr.setSpecified (false); 2411 attr.setIdAttribute ("ID".equals (type)); 2412 2413 if (nsEnabled){ 2415 elementDef.getAttributes ().setNamedItemNS (attr); 2416 } 2417 else { 2418 elementDef.getAttributes ().setNamedItem (attr); 2419 } 2420 } 2421 2422 } 2424 } 2426 2427 2437 public void startAttlist (String elementName, Augmentations augs) throws XNIException { 2438 } 2440 2441 2449 public void endAttlist (Augmentations augs) throws XNIException { 2450 } 2452 2453 protected Element createElementNode (QName element) { 2456 Element el = null; 2457 2458 if (fNamespaceAware) { 2459 if (fDocumentImpl != null) { 2462 el = fDocumentImpl.createElementNS (element.uri, element.rawname, 2463 element.localpart); 2464 } 2465 else { 2466 el = fDocument.createElementNS (element.uri, element.rawname); 2467 } 2468 } 2469 else { 2470 el = fDocument.createElement (element.rawname); 2471 } 2472 2473 return el; 2474 } 2475 2476 protected Attr createAttrNode (QName attrQName) { 2479 Attr attr = null; 2480 2481 if (fNamespaceAware) { 2482 if (fDocumentImpl != null) { 2483 attr = fDocumentImpl.createAttributeNS (attrQName.uri, 2486 attrQName.rawname, 2487 attrQName.localpart); 2488 } 2489 else { 2490 attr = fDocument.createAttributeNS (attrQName.uri, 2491 attrQName.rawname); 2492 } 2493 } 2494 else { 2495 attr = fDocument.createAttribute (attrQName.rawname); 2496 } 2497 2498 return attr; 2499 } 2500 2501 2514 protected void setCharacterData (boolean sawChars){ 2515 2516 fFirstChunk = sawChars; 2518 2519 2520 2523 Node child = fCurrentNode.getLastChild (); 2524 if (child != null) { 2525 if (fStringBuffer.length () > 0) { 2526 if (child.getNodeType () == Node.TEXT_NODE) { 2528 if (fDocumentImpl != null) { 2529 ((TextImpl)child).replaceData (fStringBuffer.toString ()); 2530 } 2531 else { 2532 ((Text )child).setData (fStringBuffer.toString ()); 2533 } 2534 } 2535 fStringBuffer.setLength (0); 2537 } 2538 2539 if (fDOMFilter !=null && !fInEntityRef) { 2540 if ( (child.getNodeType () == Node.TEXT_NODE ) && 2541 ((fDOMFilter.getWhatToShow () & NodeFilter.SHOW_TEXT)!= 0) ) { 2542 short code = fDOMFilter.acceptNode (child); 2543 switch (code) { 2544 case LSParserFilter.FILTER_INTERRUPT:{ 2545 throw abort; 2546 } 2547 case LSParserFilter.FILTER_REJECT:{ 2548 } 2550 case LSParserFilter.FILTER_SKIP: { 2551 fCurrentNode.removeChild (child); 2552 return; 2553 } 2554 default: { 2555 } 2557 } 2558 } 2559 } 2561 } } 2563 2564 2565 2568 public void abort () { 2569 throw abort; 2570 } 2571 2572 2573} | Popular Tags |