| 1 57 58 package org.enhydra.apache.xerces.parsers; 59 60 import org.enhydra.apache.xerces.framework.XMLAttrList; 61 import org.enhydra.apache.xerces.framework.XMLContentSpec; 62 import org.enhydra.apache.xerces.framework.XMLDocumentHandler; 63 import org.enhydra.apache.xerces.framework.XMLParser; 64 import org.enhydra.apache.xerces.readers.XMLEntityHandler; 65 import org.enhydra.apache.xerces.utils.QName; 66 import org.enhydra.apache.xerces.utils.StringPool; 67 import org.enhydra.apache.xerces.validators.common.XMLAttributeDecl; 68 import org.enhydra.apache.xerces.validators.common.XMLElementDecl; 69 import org.xml.sax.AttributeList ; 70 import org.xml.sax.ContentHandler ; 71 import org.xml.sax.DocumentHandler ; 72 import org.xml.sax.Parser ; 73 import org.xml.sax.SAXException ; 74 import org.xml.sax.SAXNotRecognizedException ; 75 import org.xml.sax.SAXNotSupportedException ; 76 import org.xml.sax.XMLReader ; 77 import org.xml.sax.ext.DeclHandler ; 78 import org.xml.sax.ext.LexicalHandler ; 79 import org.xml.sax.helpers.AttributesImpl ; 80 81 83 89 public class SAXParser 90 extends XMLParser 91 implements XMLDocumentHandler, XMLDocumentHandler.DTDHandler, 92 Parser, XMLReader { 93 94 98 100 101 private static final String RECOGNIZED_FEATURES[] = { 102 104 105 "http://xml.org/sax/features/namespace-prefixes", 106 "http://xml.org/sax/features/string-interning", 107 }; 109 110 111 private static final String RECOGNIZED_PROPERTIES[] = { 112 "http://xml.org/sax/properties/lexical-handler", 114 "http://xml.org/sax/properties/declaration-handler", 115 "http://xml.org/sax/properties/dom-node", 116 }; 118 119 121 122 private static final boolean DEBUG_CALLBACKS = false; 123 124 128 130 131 private DocumentHandler fDocumentHandler; 132 133 135 136 private org.xml.sax.DTDHandler fDTDHandler; 137 138 140 141 private ContentHandler fContentHandler; 142 143 144 private DeclHandler fDeclHandler; 145 146 147 private LexicalHandler fLexicalHandler; 148 149 private boolean fNamespacePrefixes = false; 150 151 153 private transient AttributesImpl fAttributes = new AttributesImpl (); 154 155 159 160 public SAXParser() { 161 initHandlers(true, this, this); 162 } 163 164 protected SAXParser(StringPool stringPool) { 165 super(stringPool); 166 initHandlers(true, this, this); 167 } 168 169 173 175 184 public String [] getFeaturesRecognized() { 185 186 String superRecognized[] = super.getFeaturesRecognized(); 188 String thisRecognized[] = RECOGNIZED_FEATURES; 189 190 int thisLength = thisRecognized.length; 192 if (thisLength == 0) { 193 return superRecognized; 194 } 195 int superLength = superRecognized.length; 196 if (superLength == 0) { 197 return thisRecognized; 198 } 199 200 String recognized[] = new String [superLength + thisLength]; 202 System.arraycopy(superRecognized, 0, recognized, 0, superLength); 203 System.arraycopy(thisRecognized, 0, recognized, superLength, thisLength); 204 return recognized; 205 206 } 208 217 public String [] getPropertiesRecognized() { 218 219 String superRecognized[] = super.getPropertiesRecognized(); 221 String thisRecognized[] = RECOGNIZED_PROPERTIES; 222 223 int thisLength = thisRecognized.length; 225 if (thisLength == 0) { 226 return superRecognized; 227 } 228 int superLength = superRecognized.length; 229 if (superLength == 0) { 230 return thisRecognized; 231 } 232 233 String recognized[] = new String [superLength + thisLength]; 235 System.arraycopy(superRecognized, 0, recognized, 0, superLength); 236 System.arraycopy(thisRecognized, 0, recognized, superLength, thisLength); 237 return recognized; 238 239 } 240 241 245 247 265 273 274 283 289 290 305 313 314 321 327 328 330 343 protected void setDeclHandler(DeclHandler handler) 344 throws SAXNotRecognizedException , SAXNotSupportedException { 345 if (fParseInProgress) { 346 throw new SAXNotSupportedException ( 347 "PAR011 Feature: http://xml.org/sax/properties/declaration-handler" 348 +" is not supported during parse." 349 +"\nhttp://xml.org/sax/properties/declaration-handler"); 350 } 351 fDeclHandler = handler; 352 } 353 354 359 protected DeclHandler getDeclHandler() 360 throws SAXNotRecognizedException , SAXNotSupportedException { 361 return fDeclHandler; 362 } 363 364 377 protected void setLexicalHandler(LexicalHandler handler) 378 throws SAXNotRecognizedException , SAXNotSupportedException { 379 if (fParseInProgress) { 380 throw new SAXNotSupportedException ( 381 "PAR011 Feature: http://xml.org/sax/properties/lexical-handler" 382 +" is not supported during parse." 383 +"\nhttp://xml.org/sax/properties/lexical-handler"); 384 } 385 fLexicalHandler = handler; 386 } 387 388 393 protected LexicalHandler getLexicalHandler() 394 throws SAXNotRecognizedException , SAXNotSupportedException { 395 return fLexicalHandler; 396 } 397 398 402 403 public void setDocumentHandler(DocumentHandler handler) { 404 fDocumentHandler = handler; 405 } 406 407 411 426 public void setDTDHandler(org.xml.sax.DTDHandler handler) { 427 fDTDHandler = handler; 428 } 429 430 437 public org.xml.sax.DTDHandler getDTDHandler() { 438 return fDTDHandler; 439 } 440 441 455 protected void setNamespacePrefixes(boolean process) 456 throws SAXNotRecognizedException , SAXNotSupportedException { 457 if (fParseInProgress) { 458 throw new SAXNotSupportedException ("PAR004 Cannot setFeature(http://xml.org/sax/features/namespace-prefixes): parse is in progress.\n"+ 459 "http://xml.org/sax/features/namespace-prefixes"); 460 } 461 fNamespacePrefixes = process; 462 } 463 464 470 protected boolean getNamespacePrefixes() 471 throws SAXNotRecognizedException , SAXNotSupportedException { 472 return fNamespacePrefixes; 473 } 474 475 476 480 494 public void setFeature(String featureId, boolean state) 495 throws SAXNotRecognizedException , SAXNotSupportedException { 496 497 501 if (featureId.startsWith(SAX2_FEATURES_PREFIX)) { 502 String feature = featureId.substring(SAX2_FEATURES_PREFIX.length()); 503 504 516 527 528 if (feature.equals("namespace-prefixes")) { 535 setNamespacePrefixes(state); 536 return; 537 } 538 if (feature.equals("string-interning")) { 543 if (state) { 544 throw new SAXNotSupportedException ( 545 "PAR018 "+state+" state for feature \""+featureId+"\" is not supported.\n"+ 546 state+'\t'+featureId 547 ); 548 } 549 return; 550 } 551 552 } 556 557 561 569 570 574 super.setFeature(featureId, state); 575 576 } 578 592 public boolean getFeature(String featureId) 593 throws SAXNotRecognizedException , SAXNotSupportedException { 594 595 599 if (featureId.startsWith(SAX2_FEATURES_PREFIX)) { 600 String feature = featureId.substring(SAX2_FEATURES_PREFIX.length()); 601 602 613 623 624 if (feature.equals("namespace-prefixes")) { 631 return getNamespacePrefixes(); 632 } 633 if (feature.equals("string-interning")) { 638 return false; 639 } 640 641 } 645 646 650 657 658 662 return super.getFeature(featureId); 663 664 } 666 681 public void setProperty(String propertyId, Object value) 682 throws SAXNotRecognizedException , SAXNotSupportedException { 683 684 688 if (propertyId.startsWith(SAX2_PROPERTIES_PREFIX)) { 689 String property = propertyId.substring(SAX2_PROPERTIES_PREFIX.length()); 690 if (property.equals("lexical-handler")) { 697 try { 698 setLexicalHandler((LexicalHandler )value); 699 } 700 catch (ClassCastException e) { 701 throw new SAXNotSupportedException ( 702 "PAR012 For propertyID \"" 703 +propertyId+"\", the value \"" 704 +value+"\" cannot be cast to LexicalHandler." 705 +'\n'+propertyId+'\t'+value+"\tLexicalHandler"); 706 } 707 return; 708 } 709 if (property.equals("declaration-handler")) { 716 try { 717 setDeclHandler((DeclHandler )value); 718 } 719 catch (ClassCastException e) { 720 throw new SAXNotSupportedException ( 721 "PAR012 For propertyID \"" 722 +propertyId+"\", the value \"" 723 +value+"\" cannot be cast to DeclHandler." 724 +'\n'+propertyId+'\t'+value+"\tDeclHandler" 725 ); 726 } 727 return; 728 } 729 if (property.equals("dom-node")) { 740 throw new SAXNotSupportedException ( 741 "PAR013 Property \""+propertyId+"\" is read only." 742 +'\n'+propertyId 743 ); } 745 } 749 750 754 761 762 766 super.setProperty(propertyId, value); 767 768 } 770 784 public Object getProperty(String propertyId) 785 throws SAXNotRecognizedException , SAXNotSupportedException { 786 787 791 if (propertyId.startsWith(SAX2_PROPERTIES_PREFIX)) { 792 String property = propertyId.substring(SAX2_PROPERTIES_PREFIX.length()); 793 if (property.equals("lexical-handler")) { 800 return getLexicalHandler(); 801 } 802 if (property.equals("declaration-handler")) { 809 return getDeclHandler(); 810 } 811 if (property.equals("dom-node")) { 822 throw new SAXNotSupportedException ( 823 "PAR014 Cannot getProperty(\""+propertyId 824 +"\". No DOM Tree exists.\n"+propertyId 825 ); } 827 } 831 832 836 843 844 848 return super.getProperty(propertyId); 849 850 } 852 868 public void setContentHandler(ContentHandler handler) { 869 if (handler == null) { 870 throw new NullPointerException (); 871 } 872 fContentHandler = handler; 873 } 874 875 882 public ContentHandler getContentHandler() { 883 return fContentHandler; 884 } 885 886 890 894 public void startDTD(QName rootElement, int publicId, int systemId) throws Exception { 895 if (fLexicalHandler != null || DEBUG_CALLBACKS) { 896 897 String name = fStringPool.toString(rootElement.rawname); 899 String pubid = fStringPool.toString(publicId); 900 String sysid = fStringPool.toString(systemId); 901 902 if (DEBUG_CALLBACKS) { 904 System.err.println("startDTD(" + name + ", " + pubid + ", " + sysid + ")"); 905 } 906 if (fLexicalHandler != null) { 907 fLexicalHandler.startDTD(name, pubid, sysid); 908 } 909 } 910 } 911 912 915 public void endDTD() throws Exception { 916 if (DEBUG_CALLBACKS) { 917 System.err.println("endDTD()"); 918 } 919 if (fLexicalHandler != null) { 920 fLexicalHandler.endDTD(); 921
|