| 1 57 58 package org.enhydra.apache.xerces.parsers; 59 60 import java.util.Enumeration ; 61 import java.util.StringTokenizer ; 62 63 import org.enhydra.apache.xerces.dom.AttrImpl; 64 import org.enhydra.apache.xerces.dom.DeferredDocumentImpl; 65 import org.enhydra.apache.xerces.dom.DocumentImpl; 66 import org.enhydra.apache.xerces.dom.DocumentTypeImpl; 67 import org.enhydra.apache.xerces.dom.ElementDefinitionImpl; 68 import org.enhydra.apache.xerces.dom.EntityImpl; 69 import org.enhydra.apache.xerces.dom.NotationImpl; 70 import org.enhydra.apache.xerces.dom.TextImpl; 71 import org.enhydra.apache.xerces.framework.XMLAttrList; 72 import org.enhydra.apache.xerces.framework.XMLContentSpec; 73 import org.enhydra.apache.xerces.framework.XMLDocumentHandler; 74 import org.enhydra.apache.xerces.framework.XMLParser; 75 import org.enhydra.apache.xerces.readers.XMLEntityHandler; 76 import org.enhydra.apache.xerces.utils.QName; 77 import org.enhydra.apache.xerces.utils.StringPool; 78 import org.enhydra.apache.xerces.validators.common.XMLAttributeDecl; 79 import org.enhydra.apache.xerces.validators.common.XMLElementDecl; 80 import org.enhydra.apache.xerces.validators.schema.SchemaSymbols; 81 import org.enhydra.apache.xerces.validators.schema.XUtil; 82 import org.w3c.dom.Attr ; 83 import org.w3c.dom.Comment ; 84 import org.w3c.dom.Document ; 85 import org.w3c.dom.DocumentType ; 86 import org.w3c.dom.Element ; 87 import org.w3c.dom.Entity ; 88 import org.w3c.dom.EntityReference ; 89 import org.w3c.dom.NamedNodeMap ; 90 import org.w3c.dom.Node ; 91 import org.w3c.dom.ProcessingInstruction ; 92 import org.w3c.dom.Text ; 93 import org.xml.sax.SAXException ; 94 import org.xml.sax.SAXNotRecognizedException ; 95 import org.xml.sax.SAXNotSupportedException ; 96 97 103 public class DOMParser 104 extends XMLParser 105 implements XMLDocumentHandler 106 { 107 108 112 114 115 public static final String DEFAULT_DOCUMENT_CLASS_NAME = "org.enhydra.apache.xerces.dom.DocumentImpl"; 116 117 118 public static final String DEFAULT_DEFERRED_DOCUMENT_CLASS_NAME = "org.enhydra.apache.xerces.dom.DeferredDocumentImpl"; 119 120 122 123 private static final boolean DEBUG_ATTLIST_DECL = false; 124 125 127 128 private static final String RECOGNIZED_FEATURES[] = { 129 "http://apache.org/xml/features/dom/defer-node-expansion", 132 "http://apache.org/xml/features/dom/create-entity-ref-nodes", 133 "http://apache.org/xml/features/dom/include-ignorable-whitespace", 134 "http://apache.org/xml/features/domx/grammar-access", 136 }; 137 138 139 private static final String RECOGNIZED_PROPERTIES[] = { 140 "http://apache.org/xml/properties/dom/document-class-name", 143 "http://apache.org/xml/properties/dom/current-element-node", 144 }; 145 146 150 152 protected Document fDocument; 153 154 156 protected DeferredDocumentImpl fDeferredDocumentImpl; 157 protected int fDocumentIndex; 158 protected int fDocumentTypeIndex; 159 protected int fCurrentNodeIndex; 160 161 163 protected int fCurrentEntityName; protected int fCurrentEntityNode; 166 168 protected DocumentImpl fDocumentImpl; 169 protected DocumentType fDocumentType; 170 protected Node fCurrentElementNode; 171 172 174 protected boolean fInDTD; 175 protected boolean fWithinElement; 176 protected boolean fInCDATA; 177 178 private boolean fGrammarAccess; 180 181 183 private String fDocumentClassName; 186 private boolean fDeferNodeExpansion; 187 private boolean fCreateEntityReferenceNodes; 188 private boolean fIncludeIgnorableWhitespace; 189 190 192 protected int fAmpIndex; 193 protected int fLtIndex; 194 protected int fGtIndex; 195 protected int fAposIndex; 196 protected int fQuotIndex; 197 198 private boolean fSeenRootElement; 199 200 private boolean fStringPoolInUse; 201 202 private XMLAttrList fAttrList; 203 204 208 209 public DOMParser() { 210 211 initHandlers(false, this, this); 212 213 init(); 215 216 try { 218 setDocumentClassName(DEFAULT_DOCUMENT_CLASS_NAME); 219 setCreateEntityReferenceNodes(true); 220 setDeferNodeExpansion(true); 221 setIncludeIgnorableWhitespace(true); 222 } catch (SAXException e) { 223 throw new RuntimeException ("PAR001 Fatal error constructing DOMParser."); 224 } 225 226 } 228 232 234 235 public Document getDocument() { 236 return fDocument; 237 } 238 239 241 250 public String [] getFeaturesRecognized() { 251 252 String superRecognized[] = super.getFeaturesRecognized(); 254 String thisRecognized[] = RECOGNIZED_FEATURES; 255 256 int thisLength = thisRecognized.length; 258 if (thisLength == 0) { 259 return superRecognized; 260 } 261 int superLength = superRecognized.length; 262 if (superLength == 0) { 263 return thisRecognized; 264 } 265 266 String recognized[] = new String [superLength + thisLength]; 268 System.arraycopy(superRecognized, 0, recognized, 0, superLength); 269 System.arraycopy(thisRecognized, 0, recognized, superLength, thisLength); 270 return recognized; 271 272 } 274 283 public String [] getPropertiesRecognized() { 284 285 String superRecognized[] = super.getPropertiesRecognized(); 287 String thisRecognized[] = RECOGNIZED_PROPERTIES; 288 289 int thisLength = thisRecognized.length; 291 if (thisLength == 0) { 292 return superRecognized; 293 } 294 int superLength = superRecognized.length; 295 if (superLength == 0) { 296 return thisRecognized; 297 } 298 299 String recognized[] = new String [superLength + thisLength]; 301 System.arraycopy(superRecognized, 0, recognized, 0, superLength); 302 System.arraycopy(thisRecognized, 0, recognized, superLength, thisLength); 303 return recognized; 304 305 } 306 307 309 310 public void reset() throws Exception { 311 if (fStringPoolInUse) { 312 fStringPool = new StringPool(); 314 fStringPoolInUse = false; 315 } 316 super.reset(); 317 init(); 318 } 319 320 321 public void resetOrCopy() throws Exception { 322 super.resetOrCopy(); 323 init(); 324 } 325 326 330 332 336 protected void init() { 337 338 fDocument = null; 340 341 fDeferredDocumentImpl = null; 343 fDocumentIndex = -1; 344 fDocumentTypeIndex = -1; 345 fCurrentNodeIndex = -1; 346 347 fCurrentEntityNode = -1; 349 fCurrentEntityName = -1; 350 351 fDocumentImpl = null; 353 fDocumentType = null; 354 fCurrentElementNode = null; 355 356 fInDTD = false; 358 fWithinElement = false; 359 fInCDATA = false; 360 361 fAmpIndex = fStringPool.addSymbol("amp"); 363 fLtIndex = fStringPool.addSymbol("lt"); 364 fGtIndex = fStringPool.addSymbol("gt"); 365 fAposIndex = fStringPool.addSymbol("apos"); 366 fQuotIndex = fStringPool.addSymbol("quot"); 367 368 fSeenRootElement = false; 369 fStringPoolInUse = false; 370 371 fAttrList = new XMLAttrList(fStringPool); 372 373 } 375 377 384 protected void setDeferNodeExpansion(boolean deferNodeExpansion) 385 throws SAXNotRecognizedException , SAXNotSupportedException { 386 fDeferNodeExpansion = deferNodeExpansion; 387 } 388 389 395 protected boolean getDeferNodeExpansion() 396 throws SAXNotRecognizedException , SAXNotSupportedException { 397 return fDeferNodeExpansion; 398 } 399 400 415 protected void setCreateEntityReferenceNodes(boolean create) 416 throws SAXNotRecognizedException , SAXNotSupportedException { 417 fCreateEntityReferenceNodes = create; 418 } 419 420 426 public boolean getCreateEntityReferenceNodes() 427 throws SAXNotRecognizedException , SAXNotSupportedException { 428 return fCreateEntityReferenceNodes; 429 } 430 431 450 public void setIncludeIgnorableWhitespace(boolean include) 451 throws SAXNotRecognizedException , SAXNotSupportedException { 452 fIncludeIgnorableWhitespace = include; 453 } 454 455 461 public boolean getIncludeIgnorableWhitespace() 462 throws SAXNotRecognizedException , SAXNotSupportedException { 463 return fIncludeIgnorableWhitespace; 464 } 465 466 468 483 protected void setDocumentClassName(String documentClassName) 484 throws SAXNotRecognizedException , SAXNotSupportedException { 485 486 if (documentClassName == null) { 488 documentClassName = DEFAULT_DOCUMENT_CLASS_NAME; 489 } 490 491 try { 493 Class _class = Class.forName(documentClassName, true, getClass().getClassLoader()); 496 if (!Document .class.isAssignableFrom(_class)) { 499 throw new IllegalArgumentException ("PAR002 Class, \""+documentClassName+"\", is not of type org.w3c.dom.Document."+"\n"+documentClassName); 500 } 501 } 502 catch (ClassNotFoundException e) { 503 throw new IllegalArgumentException ("PAR003 Class, \""+documentClassName+"\", not found."+"\n"+documentClassName); 504 } 505 506 fDocumentClassName = documentClassName; 508 if (!documentClassName.equals(DEFAULT_DOCUMENT_CLASS_NAME)) { 509 setDeferNodeExpansion(false); 510 } 511 512 } 514 520 protected String getDocumentClassName() 521 throws SAXNotRecognizedException , SAXNotSupportedException { 522 return fDocumentClassName; 523 } 524 525 532 protected Element getCurrentElementNode() 533 throws SAXNotRecognizedException , SAXNotSupportedException { 534 535 if (fCurrentElementNode != null && 536 fCurrentElementNode.getNodeType() == Node.ELEMENT_NODE) { 537 return (Element )fCurrentElementNode; 538 } 539 return null; 540 541 } 543 547 561 public void setFeature(String featureId, boolean state) 562 throws SAXNotRecognizedException , SAXNotSupportedException { 563 564 568 if (featureId.startsWith(SAX2_FEATURES_PREFIX)) { 569 } 574 575 579 else if (featureId.startsWith(XERCES_FEATURES_PREFIX)) { 580 String feature = featureId.substring(XERCES_FEATURES_PREFIX.length()); 581 if (feature.equals("dom/defer-node-expansion")) { 592 if (fParseInProgress) { 593 throw new SAXNotSupportedException ("PAR004 Cannot setFeature("+featureId + "): parse is in progress."+"\n"+featureId); 594 } 595 setDeferNodeExpansion(state); 596 return; 597 } 598 if (feature.equals("dom/create-entity-ref-nodes")) { 609 setCreateEntityReferenceNodes(state); 610 return; 611 } 612 613 if (feature.equals("dom/include-ignorable-whitespace")) { 627 setIncludeIgnorableWhitespace(state); 628 return; 629 } 630 631 635 if (feature.equals("domx/grammar-access")) { 642 fGrammarAccess = state; 643 return; 644 } 645 646 } 650 651 super.setFeature(featureId, state); 655 656 } 658 670 public boolean getFeature(String featureId) 671 throws SAXNotRecognizedException , SAXNotSupportedException { 672 673 677 if (featureId.startsWith(SAX2_FEATURES_PREFIX)) { 678 } 683 684 688 else if (featureId.startsWith(XERCES_FEATURES_PREFIX)) { 689 String feature = featureId.substring(XERCES_FEATURES_PREFIX.length()); 690 if (feature.equals("dom/defer-node-expansion")) { 701 return getDeferNodeExpansion(); 702 } 703 else if (feature.equals("dom/create-entity-ref-nodes")) { 714 return getCreateEntityReferenceNodes(); 715 } 716 717 if (feature.equals("dom/include-ignorable-whitespace")) { 731 return getIncludeIgnorableWhitespace(); 732 } 733 734 738 if (feature.equals("domx/grammar-access")) { 745 return fGrammarAccess; 746 } 747 748 } 752 753 return super.getFeature(featureId); 757 758 } 760 775 public void setProperty(String propertyId, Object value) 776 throws SAXNotRecognizedException , SAXNotSupportedException { 777 778 782 if (propertyId.startsWith(XERCES_PROPERTIES_PREFIX)) { 783 String property = propertyId.substring(XERCES_PROPERTIES_PREFIX.length()); 784 if (property.equals("dom/current-element-node")) { 794 throw new SAXNotSupportedException ("PAR005 Property, \""+propertyId+"\" is read-only.\n"+propertyId); 795 } 796 else if (property.equals("dom/document-class-name")) { 802 if (value != null && !(value instanceof String )) { 803 throw new SAXNotSupportedException ("PAR006 Property value must be of type java.lang.String."); 804 } 805 setDocumentClassName((String )value); 806 return; 807 } 808 } 809 810 super.setProperty(propertyId, value); 814 815 } 817 831 public Object getProperty(String propertyId) 832 throws SAXNotRecognizedException , SAXNotSupportedException { 833 834 838 if (propertyId.startsWith(XERCES_PROPERTIES_PREFIX)) { 839 String property = propertyId.substring(XERCES_PROPERTIES_PREFIX.length()); 840 if (property.equals("dom/current-element-node")) { 850 boolean throwException = false; 851 try { 852 throwException = getFeature(XERCES_FEATURES_PREFIX+"dom/defer-node-expansion"); 853 } 854 catch (SAXNotSupportedException e) { 855 } 857 catch (SAXNotRecognizedException e) { 858 } 860 if (throwException) { 861 throw new SAXNotSupportedException ("PAR007 Current element node cannot be queried when node expansion is deferred."); 862 } 863 return getCurrentElementNode(); 864 } 865 else if (property.equals("dom/document-class-name")) { 871 return getDocumentClassName(); 872 } 873 } 874 875 return super.getProperty(propertyId); 879 880 } 882 886 887 public void startDocument() { 888 889 String documentClassName = null; 891 try { 892 documentClassName = getDocumentClassName(); 893 } catch (SAXException e) {
|