1 20 21 27 28 package com.sun.org.apache.xerces.internal.impl; 29 30 import com.sun.xml.internal.stream.Entity; 31 import com.sun.xml.internal.stream.StaxErrorReporter; 32 import com.sun.xml.internal.stream.XMLEntityStorage; 33 import com.sun.xml.internal.stream.events.EntityDeclarationImpl; 34 import com.sun.xml.internal.stream.events.NotationDeclarationImpl; 35 import javax.xml.namespace.NamespaceContext ; 36 import com.sun.org.apache.xerces.internal.xni.XNIException; 37 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 38 import javax.xml.namespace.QName ; 39 import javax.xml.stream.Location; 40 import javax.xml.stream.events.XMLEvent; 41 import com.sun.org.apache.xerces.internal.util.NamespaceContextWrapper; 42 import com.sun.org.apache.xerces.internal.util.SymbolTable; 43 import com.sun.xml.internal.stream.dtd.nonvalidating.XMLNotationDecl; 44 import com.sun.xml.internal.stream.dtd.nonvalidating.DTDGrammar; 45 import java.io.BufferedInputStream ; 46 import java.io.BufferedReader ; 47 import java.io.IOException ; 48 import java.io.InputStream ; 49 import java.io.Reader ; 50 import java.util.ArrayList ; 51 import java.util.Enumeration ; 52 import java.util.Hashtable ; 53 import java.util.Iterator ; 54 import java.util.List ; 55 import javax.xml.stream.XMLInputFactory; 56 import javax.xml.stream.XMLStreamConstants; 57 import javax.xml.stream.XMLStreamException; 58 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 59 import com.sun.org.apache.xerces.internal.util.XMLChar; 60 import com.sun.org.apache.xerces.internal.util.XMLStringBuffer; 61 import com.sun.org.apache.xerces.internal.util.NamespaceSupport; 62 import com.sun.org.apache.xerces.internal.impl.Constants; 63 import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler; 64 import com.sun.xml.internal.stream.dtd.DTDGrammarUtil; 65 66 74 public class XMLStreamReaderImpl implements javax.xml.stream.XMLStreamReader { 75 76 77 protected static final String ENTITY_MANAGER = 78 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY; 79 80 81 protected static final String ERROR_REPORTER = 82 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 83 84 85 protected static final String SYMBOL_TABLE = 86 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 87 88 protected static final String READER_IN_DEFINED_STATE = 89 Constants.READER_IN_DEFINED_STATE; 90 91 private SymbolTable fSymbolTable = new SymbolTable(); 92 93 94 protected XMLDocumentScannerImpl fScanner = new XMLNSDocumentScannerImpl(); 95 96 protected NamespaceContextWrapper fNamespaceContextWrapper = new NamespaceContextWrapper((NamespaceSupport)fScanner.getNamespaceContext()) ; 99 protected XMLEntityManager fEntityManager = new XMLEntityManager(); 100 protected StaxErrorReporter fErrorReporter = new StaxErrorReporter(); 101 102 103 104 protected XMLEntityScanner fEntityScanner = null; 105 106 107 protected XMLInputSource fInputSource = null; 108 109 protected PropertyManager fPropertyManager = null ; 110 111 112 private int fEventType ; 113 114 static final boolean DEBUG = false ; 115 116 private boolean fReuse = true; 117 private boolean fReaderInDefinedState = true ; 118 private boolean fBindNamespaces = true; 119 private String fDTDDecl = null; 120 private String versionStr = null; 121 122 127 public XMLStreamReaderImpl(InputStream inputStream, PropertyManager props) throws XMLStreamException { 128 init(props); 129 XMLInputSource inputSource = new XMLInputSource(null,null,null,inputStream,null); 131 setInputSource(inputSource); 133 } 134 135 public XMLDocumentScannerImpl getScanner(){ 136 System.out.println("returning scanner"); 137 return fScanner; 138 } 139 144 public XMLStreamReaderImpl(String systemid, PropertyManager props) throws XMLStreamException { 145 init(props); 146 XMLInputSource inputSource = new XMLInputSource(null,systemid,null); 148 setInputSource(inputSource); 150 } 151 152 153 159 public XMLStreamReaderImpl(InputStream inputStream, String encoding, PropertyManager props ) throws XMLStreamException { 160 init(props); 161 XMLInputSource inputSource = new XMLInputSource(null,null,null, new BufferedInputStream (inputStream),encoding ); 163 setInputSource(inputSource); 165 } 166 167 172 public XMLStreamReaderImpl(Reader reader, PropertyManager props) throws XMLStreamException { 173 init(props); 174 XMLInputSource inputSource = new XMLInputSource(null,null,null,new BufferedReader (reader),null); 177 setInputSource(inputSource); 179 } 180 181 186 public XMLStreamReaderImpl(XMLInputSource inputSource, PropertyManager props) throws XMLStreamException { 187 init(props); 188 setInputSource(inputSource); 190 } 191 192 196 public void setInputSource(XMLInputSource inputSource ) throws XMLStreamException { 197 fReuse = false; 201 202 try{ 203 204 fScanner.setInputSource(inputSource) ; 205 if(fReaderInDefinedState){ 207 fEventType = fScanner.next(); 208 if (versionStr == null) 209 versionStr = getVersion(); 210 211 if (fEventType == XMLStreamConstants.START_DOCUMENT && versionStr != null && versionStr.equals("1.1")){ 212 switchToXML11Scanner(); 213 } 214 215 } 216 }catch(java.io.IOException ex){ 217 throw new XMLStreamException(ex); 218 } 219 } 221 void init(PropertyManager propertyManager) throws XMLStreamException { 222 fPropertyManager = propertyManager; 223 propertyManager.setProperty(SYMBOL_TABLE, fSymbolTable ) ; 230 propertyManager.setProperty(ERROR_REPORTER, fErrorReporter ) ; 232 propertyManager.setProperty(ENTITY_MANAGER, fEntityManager); 234 reset(); 236 } 237 238 242 public boolean canReuse(){ 243 if(DEBUG){ 244 System.out.println("fReuse = " + fReuse); 245 System.out.println("fEventType = " + getEventTypeString(fEventType) ); 246 } 247 return fReuse; 250 } 251 252 255 public void reset(){ 256 fReuse = true; 257 fEventType = 0 ; 258 fEntityManager.reset(fPropertyManager); 260 fScanner.reset(fPropertyManager); 262 fDTDDecl = null; 265 fEntityScanner = (XMLEntityScanner)fEntityManager.getEntityScanner() ; 266 fReaderInDefinedState = ((Boolean )fPropertyManager.getProperty(READER_IN_DEFINED_STATE)).booleanValue(); 269 fBindNamespaces = ((Boolean )fPropertyManager.getProperty(XMLInputFactory.IS_NAMESPACE_AWARE)).booleanValue(); 270 versionStr = null; 271 } 272 273 274 277 public void close() throws XMLStreamException { 278 fReuse = true ; 281 } 282 283 284 287 public String getCharacterEncodingScheme() { 288 return fScanner.getCharacterEncodingScheme(); 289 290 } 291 292 293 296 public int getColumnNumber() { 297 return fEntityScanner.getColumnNumber(); 298 } 300 303 public String getEncoding() { 304 return fEntityScanner.getEncoding(); 305 } 307 310 public int getEventType() { 311 return fEventType ; 312 } 314 317 public int getLineNumber() { 318 return fEntityScanner.getLineNumber() ; 319 } 321 326 public String getLocalName() { 327 if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT){ 328 return fScanner.getElementQName().localpart ; 330 } 331 else if(fEventType == XMLEvent.PROCESSING_INSTRUCTION){ 332 return fScanner.getPITarget(); 333 } 334 else if(fEventType == XMLEvent.ENTITY_REFERENCE){ 335 return fScanner.getEntityName(); 336 } 337 return null; 338 } 340 343 public String getNamespaceURI() { 344 if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT){ 346 return fScanner.getElementQName().uri ; 347 } 348 return null ; 349 } 351 354 355 public String getPIData() { 356 if( fEventType == XMLEvent.PROCESSING_INSTRUCTION){ 357 return fScanner.getPIData().toString(); 358 } 359 else throw new java.lang.IllegalStateException ("Current state of the parser is " + getEventTypeString(fEventType) + 360 " But Expected state is " + XMLEvent.PROCESSING_INSTRUCTION ) ; 361 } 363 364 367 public String getPITarget() { 368 if( fEventType == XMLEvent.PROCESSING_INSTRUCTION){ 369 return fScanner.getPITarget(); 370 } 371 else throw new java.lang.IllegalStateException ("Current state of the parser is " + getEventTypeString(fEventType) + 372 " But Expected state is " + XMLEvent.PROCESSING_INSTRUCTION ) ; 373 374 } 376 377 380 public String getPrefix() { 381 if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT){ 383 return fScanner.getElementQName().prefix ; 384 } 385 return null ; 386 } 388 389 390 393 public char[] getTextCharacters() { 394 if( fEventType == XMLEvent.CHARACTERS || fEventType == XMLEvent.COMMENT 395 || fEventType == XMLEvent.CDATA || fEventType == XMLEvent.SPACE){ 396 return fScanner.getCharacterData().ch; 397 } else{ 398 throw new IllegalStateException ("Current state = " + getEventTypeString(fEventType) 399 + " is not among the states " + getEventTypeString(XMLEvent.CHARACTERS) + " , " 400 + getEventTypeString(XMLEvent.COMMENT) + " , " + getEventTypeString(XMLEvent.CDATA) 401 + " , " + getEventTypeString(XMLEvent.SPACE) +" valid for getTextCharacters() " ) ; 402 } 403 } 404 405 408 public int getTextLength() { 409 if( fEventType == XMLEvent.CHARACTERS || fEventType == XMLEvent.COMMENT 410 || fEventType == XMLEvent.CDATA || fEventType == XMLEvent.SPACE){ 411 return fScanner.getCharacterData().length; 412 } else{ 413 throw new IllegalStateException ("Current state = " + getEventTypeString(fEventType) 414 + " is not among the states " + getEventTypeString(XMLEvent.CHARACTERS) + " , " 415 + getEventTypeString(XMLEvent.COMMENT) + " , " + getEventTypeString(XMLEvent.CDATA) 416 + " , " + getEventTypeString(XMLEvent.SPACE) +" valid for getTextLength() " ) ; 417 } 418 419 } 420 421 424 public int getTextStart() { 425 if( fEventType == XMLEvent.CHARACTERS || fEventType == XMLEvent.COMMENT || fEventType == XMLEvent.CDATA || fEventType == XMLEvent.SPACE){ 426 return fScanner.getCharacterData().offset; 427 } else{ 428 throw new IllegalStateException ("Current state = " + getEventTypeString(fEventType) 429 + " is not among the states " + getEventTypeString(XMLEvent.CHARACTERS) + " , " 430 + getEventTypeString(XMLEvent.COMMENT) + " , " + getEventTypeString(XMLEvent.CDATA) 431 + " , " + getEventTypeString(XMLEvent.SPACE) +" valid for getTextStart() " ) ; 432 } 433 } 434 435 438 public String getValue() { 439 if(fEventType == XMLEvent.PROCESSING_INSTRUCTION){ 440 return fScanner.getPIData().toString(); 441 } else if(fEventType == XMLEvent.COMMENT){ 442 return fScanner.getComment(); 443 } else if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT){ 444 return fScanner.getElementQName().localpart ; 445 } else if(fEventType == XMLEvent.CHARACTERS){ 446 return fScanner.getCharacterData().toString(); 447 } 448 return null; 449 } 451 452 public String getVersion() { 453 return fEntityScanner.getXMLVersion(); 454 } 455 456 459 public boolean hasAttributes() { 460 return fScanner.getAttributeIterator().getLength() > 0 ? true : false ; 461 } 462 463 464 public boolean hasName() { 465 if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT 466 || fEventType == XMLEvent.ENTITY_REFERENCE || fEventType == XMLEvent.PROCESSING_INSTRUCTION) { 467 return true; 468 } else { 469 return false; 470 } 471 } 473 477 public boolean hasNext() throws XMLStreamException { 478 return fEventType != XMLEvent.END_DOCUMENT; 481 } 482 483 486 public boolean hasValue() { 487 if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT 488 || fEventType == XMLEvent.ENTITY_REFERENCE || fEventType == XMLEvent.PROCESSING_INSTRUCTION 489 || fEventType == XMLEvent.COMMENT || fEventType == XMLEvent.CHARACTERS) { 490 return true; 491 } else { 492 return false; 493 } 494 495 } 496 497 500 public boolean isEndElement() { 501 return fEventType == XMLEvent.END_ELEMENT; 502 } 503 504 507 public boolean isStandalone() { 508 return fScanner.isStandAlone(); 509 } 510 511 514 public boolean isStartElement() { 515 return fEventType == XMLEvent.START_ELEMENT; 516 } 517 518 524 public boolean isWhiteSpace() { 525 if(isCharacters() || (fEventType == XMLStreamConstants.CDATA)){ 526 char [] ch = this.getTextCharacters(); 527 final int start = this.getTextStart(); 528 final int end = start + this.getTextLength(); 529 for (int i = start; i < end; i++){ 530 if(!XMLChar.isSpace(ch[i])){ 531 return false; 532 } 533 } 534 return true; 535 } 536 return false; 537 } 538 539 540 541 545 public int next() throws XMLStreamException { 546 547 try { 548 fEventType = fScanner.next(); 549 550 if (versionStr == null) { 551 versionStr = getVersion(); 552 } 553 554 if (fEventType == XMLStreamConstants.START_DOCUMENT 555 && versionStr != null 556 && versionStr.equals("1.1")) { 557 switchToXML11Scanner(); 558 } 559 560 return fEventType; 561 } catch (IOException ex) { 562 if (fScanner.fScannerState == fScanner.SCANNER_STATE_DTD_EXTERNAL) { 565 Boolean isValidating = (Boolean ) fPropertyManager.getProperty( 566 XMLInputFactory.IS_VALIDATING); 567 if (isValidating != null 568 && !isValidating.booleanValue()) { 569 fEventType = XMLEvent.DTD; 571 fScanner.setScannerState(fScanner.SCANNER_STATE_PROLOG); 572 fScanner.setDriver(fScanner.fPrologDriver); 573 if (fDTDDecl == null 574 || fDTDDecl.length() == 0) { 575 fDTDDecl = "<!-- " 576 + "Exception scanning External DTD Subset. " 577 + "True contents of DTD cannot be determined. " 578 + "Processing will continue as XMLInputFactory.IS_VALIDATING == false." 579 + " -->"; 580 } 581 return XMLEvent.DTD; 582 } 583 } 584 585 throw new XMLStreamException(ex.getMessage(), getLocation(), ex); 587 } catch (XNIException ex) { 588 throw new XMLStreamException( 589 ex.getMessage(), 590 getLocation(), 591 ex.getException()); 592 } 593 } 595 private void switchToXML11Scanner() throws IOException { 596 597 int oldEntityDepth = fScanner.fEntityDepth; 598 com.sun.org.apache.xerces.internal.xni.NamespaceContext oldNamespaceContext = fScanner.fNamespaceContext; 599 600 fScanner = new XML11NSDocumentScannerImpl(); 601 602 fScanner.reset(fPropertyManager); 604 fScanner.setPropertyManager(fPropertyManager); 605 fEntityScanner = (XMLEntityScanner)fEntityManager.getEntityScanner() ; 606 fEntityManager.fCurrentEntity.mayReadChunks = true; 607 fScanner.setScannerState(XMLEvent.START_DOCUMENT); 608 609 fScanner.fEntityDepth = oldEntityDepth; 610 fScanner.fNamespaceContext = oldNamespaceContext; 611 fEventType = fScanner.next(); 612 } 613 614 615 616 final static String getEventTypeString(int eventType) { 617 switch (eventType){ 618 case XMLEvent.START_ELEMENT: 619 return "START_ELEMENT"; 620 case XMLEvent.END_ELEMENT: 621 return "END_ELEMENT"; 622 case XMLEvent.PROCESSING_INSTRUCTION: 623 return "PROCESSING_INSTRUCTION"; 624 case XMLEvent.CHARACTERS: 625 return "CHARACTERS"; 626 case XMLEvent.COMMENT: 627 return "COMMENT"; 628 case XMLEvent.START_DOCUMENT: 629 return "START_DOCUMENT"; 630 case XMLEvent.END_DOCUMENT: 631 return "END_DOCUMENT"; 632 case XMLEvent.ENTITY_REFERENCE: 633 return "ENTITY_REFERENCE"; 634 case XMLEvent.ATTRIBUTE: 635 return "ATTRIBUTE"; 636 case XMLEvent.DTD: 637 return "DTD"; 638 case XMLEvent.CDATA: 639 return "CDATA"; 640 case XMLEvent.SPACE: 641 return "SPACE"; 642 } 643 return "UNKNOWN_EVENT_TYPE, " + String.valueOf(eventType); 644 } 645 646 653 public int getAttributeCount() { 654 657 if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) { 659 return fScanner.getAttributeIterator().getLength() ; 660 } else{ 661 throw new java.lang.IllegalStateException ( "Current state is not among the states " 662 + getEventTypeString(XMLEvent.START_ELEMENT) + " , " 663 + getEventTypeString(XMLEvent.ATTRIBUTE) 664 + "valid for getAttributeCount()") ; 665 } 666 } 668 674 public QName getAttributeName(int index) { 675 if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) { 677 return convertXNIQNametoJavaxQName(fScanner.getAttributeIterator().getQualifiedName(index)) ; 678 } else{ 679 throw new java.lang.IllegalStateException ("Current state is not among the states " 680 + getEventTypeString(XMLEvent.START_ELEMENT) + " , " 681 + getEventTypeString(XMLEvent.ATTRIBUTE) 682 + "valid for getAttributeName()") ; 683 } 684 } 686 690 public String getAttributeLocalName(int index) { 691 if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) { 693 return fScanner.getAttributeIterator().getLocalName(index) ; 694 } else{ 695 throw new java.lang.IllegalStateException () ; 696 } 697 } 699 705 public String getAttributeNamespace(int index) { 706 if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) { 708 return fScanner.getAttributeIterator().getURI(index); 709 } else{ 710 throw new java.lang.IllegalStateException ("Current state is not among the states " 711 + getEventTypeString(XMLEvent.START_ELEMENT) + " , " 712 + getEventTypeString(XMLEvent.ATTRIBUTE) 713 + "valid for getAttributeNamespace()") ; 714 } 715 716 } 718 724 public String getAttributePrefix(int index) { 725 if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) { 727 return fScanner.getAttributeIterator().getPrefix(index); 728 } else{ 729 throw new java.lang.IllegalStateException ("Current state is not among the states " 730 + getEventTypeString(XMLEvent.START_ELEMENT) + " , " 731 + getEventTypeString(XMLEvent.ATTRIBUTE) 732 + "valid for getAttributePrefix()") ; 733 } 734 } 736 742 public javax.xml.namespace.QName getAttributeQName(int index) { 743 if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) { 745 String localName = fScanner.getAttributeIterator().getLocalName(index) ; 747 String uri = fScanner.getAttributeIterator().getURI(index) ; 748 return new javax.xml.namespace.QName (uri, localName) ; 749 } else{ 750 throw new java.lang.IllegalStateException ("Current state is not among the states " 751 + getEventTypeString(XMLEvent.START_ELEMENT) + " , " 752 + getEventTypeString(XMLEvent.ATTRIBUTE) 753 + "valid for getAttributeQName()") ; 754 } 755 } 757 763 public String getAttributeType(int index) { 764 if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) { 766 return fScanner.getAttributeIterator().getType(index) ; 767 } else{ 768 throw new java.lang.IllegalStateException ("Current state is not among the states " 769 + getEventTypeString(XMLEvent.START_ELEMENT) + " , " 770 + getEventTypeString(XMLEvent.ATTRIBUTE) 771 + "valid for getAttributeType()") ; 772 } 773 774 } 776 782 public String getAttributeValue(int index) { 783 if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) { 785 return fScanner.getAttributeIterator().getValue(index) ; 786 } else{ 787 throw new java.lang.IllegalStateException ("Current state is not among the states " 788 + getEventTypeString(XMLEvent.START_ELEMENT) + " , " 789 + getEventTypeString(XMLEvent.ATTRIBUTE) 790 + "valid for getAttributeValue()") ; 791 } 792 793 } 795 800 public String getAttributeValue(String namespaceURI, String localName) { 801 if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) { 803 return fScanner.getAttributeIterator().getValue(namespaceURI, localName) ; 804 } else{ 805 throw new java.lang.IllegalStateException ("Current state is not among the states " 806 + getEventTypeString(XMLEvent.START_ELEMENT) + " , " 807 + getEventTypeString(XMLEvent.ATTRIBUTE) 808 + "valid for getAttributeValue()") ; 809 } 810 811 } 812 813 819 public String getElementText() throws XMLStreamException { 820 821 if(getEventType() != XMLStreamConstants.START_ELEMENT) { 822 throw new XMLStreamException( 823 "parser must be on START_ELEMENT to read next text", getLocation()); 824 } 825 int eventType = next(); 826 StringBuffer content = new StringBuffer (); 827 while(eventType != XMLStreamConstants.END_ELEMENT ) { 828 if(eventType == XMLStreamConstants.CHARACTERS 829 || eventType == XMLStreamConstants.CDATA 830 || eventType == XMLStreamConstants.SPACE 831 || eventType == XMLStreamConstants.ENTITY_REFERENCE) { 832 content.append(getText()); 833 } else if(eventType == XMLStreamConstants.PROCESSING_INSTRUCTION 834 || eventType == XMLStreamConstants.COMMENT) { 835 } else if(eventType == XMLStreamConstants.END_DOCUMENT) { 837 throw new XMLStreamException("unexpected end of document when reading element text content"); 838 } else if(eventType == XMLStreamConstants.START_ELEMENT) { 839 throw new XMLStreamException( 840 "elementGetText() function expects text only elment but START_ELEMENT was encountered.", getLocation()); 841 } else { 842 throw new XMLStreamException( 843 "Unexpected event type "+ eventType, getLocation()); 844 } 845 eventType = next(); 846 } 847 return content.toString(); 848 } 849 850 857 public Location getLocation() { 858 return new Location() { 859 860 public String getLocationURI(){ 861 return fEntityScanner.getExpandedSystemId(); 862 } 863 864 public int getCharacterOffset(){ 865 return fEntityScanner.getCharacterOffset(); 866 } 867 868 public int getColumnNumber() { 869 return fEntityScanner.getColumnNumber(); 870 } 871 872 public int getLineNumber(){ 873 return fEntityScanner.getLineNumber(); 874 } 875 876 public String getPublicId(){ 877 return fEntityScanner.getPublicId(); 878 } 879 880 public String getSystemId(){ 881 return fEntityScanner.getExpandedSystemId(); 882 } 883 884 public String toString(){ 885 StringBuffer sbuffer = new StringBuffer () ; 886 sbuffer.append("Line number = " + getLineNumber()); 887 sbuffer.append("\n") ; 888 sbuffer.append("Column number = " + getColumnNumber()); 889 sbuffer.append("\n") ; 890 sbuffer.append("System Id = " + getSystemId()); 891 sbuffer.append("\n") ; 892 sbuffer.append("Public Id = " + getPublicId()); 893 sbuffer.append("\n") ; 894 sbuffer.append("Location Uri= " + getLocationURI()); 895 sbuffer.append("\n") ; 896 sbuffer.append("CharacterOffset = " + getCharacterOffset()); 897 sbuffer.append("\n") ; 898 return sbuffer.toString(); 899 } 900 } ; 901 902 } 903 904 907 public javax.xml.namespace.QName getName() { 908 if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT) 909 return convertXNIQNametoJavaxQName(fScanner.getElementQName()); 910 else 911 throw new java.lang.IllegalArgumentException ("Illegal to call getName() "+ 912 "when event type is "+ getEventTypeString(fEventType) + "." 913 + " Valid states are " + getEventTypeString(XMLEvent.START_ELEMENT) + ", " 914 + getEventTypeString(XMLEvent.END_ELEMENT)); 915 } 916 917 922 public NamespaceContext getNamespaceContext() { 923 return fNamespaceContextWrapper ; 924 } 925 926 934 public int getNamespaceCount() { 935 if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT || fEventType == XMLEvent.NAMESPACE){ 938 return fScanner.getNamespaceContext().getDeclaredPrefixCount() ; 939 } else{ 940 throw new IllegalStateException ("Current event state is " + getEventTypeString(fEventType) 941 + " is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT) 942 + ", " + getEventTypeString(XMLEvent.END_ELEMENT) + ", " 943 + getEventTypeString(XMLEvent.NAMESPACE) 944 + " valid for getNamespaceCount()." ); 945 } 946 } 947 948 956 public String getNamespacePrefix(int index) { 957 if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT || fEventType == XMLEvent.NAMESPACE){ 958 String prefix = fScanner.getNamespaceContext().getDeclaredPrefixAt(index) ; 960 return prefix.equals("") ? null : prefix ; 961 } 962 else{ 963 throw new IllegalStateException ("Current state " + getEventTypeString(fEventType) 964 + " is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT) 965 + ", " + getEventTypeString(XMLEvent.END_ELEMENT) + ", " 966 + getEventTypeString(XMLEvent.NAMESPACE) 967 + " valid for getNamespacePrefix()." ); 968 } 969 } 970 971 978 public String getNamespaceURI(int index) { 979 if(fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.END_ELEMENT || fEventType == XMLEvent.NAMESPACE){ 980 return fScanner.getNamespaceContext().getURI(fScanner.getNamespaceContext().getDeclaredPrefixAt(index)); 982 } 983 else{ 984 throw new IllegalStateException ("Current state " + getEventTypeString(fEventType) 985 + " is not among the states " + getEventTypeString(XMLEvent.START_ELEMENT) 986 + ", " + getEventTypeString(XMLEvent.END_ELEMENT) + ", " 987 + getEventTypeString(XMLEvent.NAMESPACE) 988 + " valid for getNamespaceURI()." ); 989 } 990 991 } 992 993 998 public Object getProperty(java.lang.String name) throws java.lang.IllegalArgumentException { 999 if(name == null) throw new java.lang.IllegalArgumentException () ; 1000 if (fPropertyManager != null ){ 1001 if(name.equals(fPropertyManager.STAX_NOTATIONS)){ 1002 return getNotationDecls(); 1003 }else if(name.equals(fPropertyManager.STAX_ENTITIES)){ 1004 return getEntityDecls(); 1005 }else 1006 return fPropertyManager.getProperty(name); 1007 } 1008 return null; 1009 } 1010 1011 1020 public String getText() { 1021 if( fEventType == XMLEvent.CHARACTERS || fEventType == XMLEvent.COMMENT 1022 || fEventType == XMLEvent.CDATA || fEventType == XMLEvent.SPACE){ 1023 return fScanner.getCharacterData().toString() ; 1026 } else if(fEventType == XMLEvent.ENTITY_REFERENCE){ 1027 String name = fScanner.getEntityName(); 1028 if(name != null){ 1029 if(fScanner.foundBuiltInRefs) 1030 return fScanner.getCharacterData().toString(); 1031 1032 XMLEntityStorage entityStore = fEntityManager.getEntityStore(); 1033 Hashtable ht = entityStore.getDeclaredEntities(); 1034 Entity en = (Entity)ht.get(name); 1035 if(en == null) 1036 return null; 1037 if(en.isExternal()) 1038 return ((Entity.ExternalEntity)en).entityLocation.getExpandedSystemId(); 1039 else 1040 return ((Entity.InternalEntity)en).text; 1041 }else 1042 return null; 1043 } 1044 else if(fEventType == XMLEvent.DTD){ 1045 if(fDTDDecl != null){ 1046 return fDTDDecl; 1047 } 1048 XMLStringBuffer tmpBuffer = fScanner.getDTDDecl(); 1049 fDTDDecl = tmpBuffer.toString(); 1050 return fDTDDecl; 1051 } else{ 1052 throw new IllegalStateException ("Current state " + getEventTypeString(fEventType) 1053 + " is not among the states" + getEventTypeString(XMLEvent.CHARACTERS) + ", " 1054 + getEventTypeString(XMLEvent.COMMENT) + ", " 1055 + getEventTypeString(XMLEvent.CDATA) + ", " 1056 + getEventTypeString(XMLEvent.SPACE) + ", " 1057 + getEventTypeString(XMLEvent.ENTITY_REFERENCE) + ", " 1058 + getEventTypeString(XMLEvent.DTD) + " valid for getText() " ) ; 1059 } 1060 } 1062 1063 1070 public void require(int type, String namespaceURI, String localName) throws XMLStreamException { 1071 if( type != fEventType) 1072 throw new XMLStreamException("Event type " + getEventTypeString(type) + " specified did " + 1073 "not match with current parser event " + getEventTypeString(fEventType)); 1074 if( namespaceURI != null && !namespaceURI.equals(getNamespaceURI()) ) 1075 throw new XMLStreamException("Namespace URI " + namespaceURI +" specified did not match " + 1076 "with current namespace URI"); 1077 if(localName != null && !localName.equals(getLocalName())) 1078 throw new XMLStreamException("LocalName " + localName +" specified did not match with " + 1079 "current local name"); 1080 return; 1081 } 1082 1083 1119 public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length) throws XMLStreamException { 1120 1121 if(target == null){ 1122 throw new NullPointerException ("target char array can't be null") ; 1123 } 1124 1125 if(targetStart < 0 || length < 0 || sourceStart < 0 || targetStart >= target.length || 1126 (targetStart + length ) > target.length) { 1127 throw new IndexOutOfBoundsException (); 1128 } 1129 1130 int copiedLength = 0; 1133 int available = getTextLength() - sourceStart; 1135 if(available < 0){ 1136 throw new IndexOutOfBoundsException ("sourceStart is greater than" + 1137 "number of characters associated with this event"); 1138 } 1139 if(available < length){ 1140 copiedLength = available; 1141 } else{ 1142 copiedLength = length; 1143 } 1144 1145 System.arraycopy(getTextCharacters(), getTextStart() + sourceStart , target, targetStart, copiedLength); 1146 return copiedLength; 1147 } 1148 1149 1153 public boolean hasText() { 1154 if(DEBUG) pr("XMLReaderImpl#EVENT TYPE = " + fEventType ) ; 1155 if( fEventType == XMLEvent.CHARACTERS || fEventType == XMLEvent.COMMENT || fEventType == XMLEvent.CDATA) { 1156 return fScanner.getCharacterData().length > 0 ? true : false; 1157 } else if(fEventType == XMLEvent.ENTITY_REFERENCE) { 1158 String name = fScanner.getEntityName(); 1159 if(name != null){ 1160 if(fScanner.foundBuiltInRefs) 1161 return true; 1162 1163 XMLEntityStorage entityStore = fEntityManager.getEntityStore(); 1164 Hashtable ht = entityStore.getDeclaredEntities(); 1165 Entity en =(Entity)ht.get(name); 1166 if(en == null) 1167 return false; 1168 if(en.isExternal()){ 1169 return ((Entity.ExternalEntity)en).entityLocation.getExpandedSystemId() != null ? true : false; 1170 } else{ 1171 return ((Entity.InternalEntity)en).text != null ? true : false ; 1172 } 1173 }else 1174 return false; 1175 } else { 1176 if(fEventType == XMLEvent.DTD) 1177 return fScanner.fSeenDoctypeDecl; 1178 } 1179 return false; 1180 } 1181 1182 1188 public boolean isAttributeSpecified(int index) { 1189 if( (fEventType == XMLEvent.START_ELEMENT) || (fEventType == XMLEvent.ATTRIBUTE)){ 1191 return fScanner.getAttributeIterator().isSpecified(index) ; 1192 } else{ 1193 throw new IllegalStateException ("Current state is not among the states " 1194 + getEventTypeString(XMLEvent.START_ELEMENT) + " , " 1195 + getEventTypeString(XMLEvent.ATTRIBUTE) 1196 + "valid for isAttributeSpecified()") ; 1197 } 1198 } 1199 1200 1203 public boolean isCharacters() { 1204 return fEventType == XMLEvent.CHARACTERS ; 1205 } 1206 1207 1217 public int nextTag() throws XMLStreamException { 1218 1219 int eventType = next(); 1220 while((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) || (eventType == XMLStreamConstants.CDATA && isWhiteSpace()) 1222 || eventType == XMLStreamConstants.SPACE 1224 || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION 1225 || eventType == XMLStreamConstants.COMMENT 1226 ) { 1227 eventType = next(); 1228 } 1229 1230 if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) { 1231 throw new XMLStreamException( 1232 "found: " + getEventTypeString(eventType) 1233 + ", expected " + getEventTypeString(XMLStreamConstants.START_ELEMENT) 1234 + " or " + getEventTypeString(XMLStreamConstants.END_ELEMENT), 1235 getLocation()); 1236 } 1237 1238 return eventType; 1239 } 1240 1241 1244 public boolean standaloneSet() { 1245 return fScanner.isStandAlone() ; 1248 } 1249 1250 1254 public javax.xml.namespace.QName convertXNIQNametoJavaxQName(com.sun.org.apache.xerces.internal.xni.QName qname){ 1255 if(qname.prefix == null){ 1257 return new javax.xml.namespace.QName (qname.uri, qname.localpart) ; 1258 } else{ 1259 return new javax.xml.namespace.QName (qname.uri, qname.localpart, qname.prefix) ; 1260 } 1261 } 1262 1263 1275 public String getNamespaceURI(String prefix) { 1276 return fScanner.getNamespaceContext().getURI(fSymbolTable.addSymbol(prefix)) ; 1278 } 1279 1280 protected void setPropertyManager(PropertyManager propertyManager){ 1282 fPropertyManager = propertyManager ; 1283 fScanner.setProperty("stax-properties",propertyManager); 1285 fScanner.setPropertyManager(propertyManager) ; 1286 } 1287 1288 1291 protected PropertyManager getPropertyManager(){ 1292 return fPropertyManager ; 1293 } 1294 1295 static void pr(String str) { 1296 System.out.println(str) ; 1297 } 1298 1299 protected List getEntityDecls(){ 1300 if(fEventType == XMLStreamConstants.DTD){ 1301 XMLEntityStorage entityStore = fEntityManager.getEntityStore(); 1302 Hashtable ht = entityStore.getDeclaredEntities(); 1303 ArrayList list = null; 1304 if(ht != null){ 1305 EntityDeclarationImpl decl = null; 1306 list = new ArrayList (ht.size()); 1307 Enumeration enu = ht.keys(); 1308 while(enu.hasMoreElements()){ 1309 String key = (String )enu.nextElement(); 1310 Entity en = (Entity)ht.get(key); 1311 decl = new EntityDeclarationImpl(); 1312 decl.setEntityName(key); 1313 if(en.isExternal()){ 1314 decl.setXMLResourceIdentifier(((Entity.ExternalEntity)en).entityLocation); 1315 decl.setNotationName(((Entity.ExternalEntity)en).notation); 1316 } 1317 else 1318 decl.setEntityReplacementText(((Entity.InternalEntity)en).text); 1319 list.add(decl); 1320 } 1321 } 1322 return list; 1323 } 1324 return null; 1325 } 1326 1327 protected List getNotationDecls(){ 1328 if(fEventType == XMLStreamConstants.DTD){ 1329 if(fScanner.fDTDScanner == null) return null; 1330 DTDGrammar grammar = ((XMLDTDScannerImpl)(fScanner.fDTDScanner)).getGrammar(); 1331 if(grammar == null) return null; 1332 List notations = grammar.getNotationDecls(); 1333 1334 Iterator it = notations.iterator(); 1335 ArrayList list = new ArrayList (); 1336 while(it.hasNext()){ 1337 XMLNotationDecl ni = (XMLNotationDecl)it.next(); 1338 if(ni!= null){ 1339 list.add(new NotationDeclarationImpl(ni)); 1340 } 1341 } 1342 return list; 1343 } 1344 return null; 1345 } 1346 1347 1348 1349} | Popular Tags |