Path: blob/master/src/java.xml/share/classes/javax/xml/stream/XMLStreamReader.java
40948 views
/*1* Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.stream;2627import javax.xml.namespace.NamespaceContext;28import javax.xml.namespace.QName;2930/**31* The XMLStreamReader interface allows forward, read-only access to XML.32* It is designed to be the lowest level and most efficient way to33* read XML data.34*35* <p>36* The XMLStreamReader is designed to iterate over XML using37* next() and hasNext(). The data can be accessed using methods such as getEventType(),38* getNamespaceURI(), getLocalName() and getText();39*40* <p>41* An XMLStreamReader instance is created with an initial event type START_DOCUMENT.42* At any moment in time, it has a current event that the methods of the interface43* access and may load the next event through the {@link #next() next()} method.44* The current event type can be determined by {@link #getEventType getEventType()}, and45* the next returned by the {@link #next() next()} method.46*47* <p>48* Parsing events are defined as the XML Declaration, a DTD,49* start tag, character data, white space, end tag, comment,50* or processing instruction. An attribute or namespace event may be encountered51* at the root level of a document as the result of a query operation.52*53* <p>54* For XML 1.0 compliance an XML processor must pass the55* identifiers of declared unparsed entities, notation declarations and their56* associated identifiers to the application. This information is57* provided through the property API on this interface.58* The following two properties allow access to this information:59* javax.xml.stream.notations and javax.xml.stream.entities.60* When the current event is a DTD the following call will return a61* list of Notations62* {@code List l = (List) getProperty("javax.xml.stream.notations");}63* The following call will return a list of entity declarations:64* {@code List l = (List) getProperty("javax.xml.stream.entities");}65* These properties can only be accessed during a DTD event and66* are defined to return null if the information is not available.67*68* <p>69* The following table describes which methods are valid in what state.70* If a method is called in an invalid state the method will throw a71* java.lang.IllegalStateException.72*73* <table class="striped">74* <caption>Valid methods for each state</caption>75* <thead>76* <tr>77* <th scope="col">Event Type</th>78* <th scope="col">Valid Methods</th>79* </tr>80* </thead>81* <tbody>82* <tr>83* <th scope="row"> All States </th>84* <td> getProperty(), hasNext(), require(), close(),85* getNamespaceURI(), isStartElement(),86* isEndElement(), isCharacters(), isWhiteSpace(),87* getNamespaceContext(), getEventType(),getLocation(),88* hasText(), hasName()89* </td>90* </tr>91* <tr>92* <th scope="row"> START_ELEMENT </th>93* <td> next(), getName(), getLocalName(), hasName(), getPrefix(),94* getAttributeXXX(), isAttributeSpecified(),95* getNamespaceXXX(),96* getElementText(), nextTag()97* </td>98* </tr>99* <tr>100* <th scope="row"> ATTRIBUTE </th>101* <td> next(), nextTag()102* getAttributeXXX(), isAttributeSpecified(),103* </td>104* </tr>105* <tr>106* <th scope="row"> NAMESPACE </th>107* <td> next(), nextTag()108* getNamespaceXXX()109* </td>110* </tr>111* <tr>112* <th scope="row"> END_ELEMENT </th>113* <td> next(), getName(), getLocalName(), hasName(), getPrefix(),114* getNamespaceXXX(), nextTag()115* </td>116* </tr>117* <tr>118* <th scope="row"> CHARACTERS </th>119* <td> next(), getTextXXX(), nextTag() </td>120* </tr>121* <tr>122* <th scope="row"> CDATA </th>123* <td> next(), getTextXXX(), nextTag() </td>124* </tr>125* <tr>126* <th scope="row"> COMMENT </th>127* <td> next(), getTextXXX(), nextTag() </td>128* </tr>129* <tr>130* <th scope="row"> SPACE </th>131* <td> next(), getTextXXX(), nextTag() </td>132* </tr>133* <tr>134* <th scope="row"> START_DOCUMENT </th>135* <td> next(), getEncoding(), getVersion(), isStandalone(), standaloneSet(),136* getCharacterEncodingScheme(), nextTag()</td>137* </tr>138* <tr>139* <th scope="row"> END_DOCUMENT </th>140* <td> close()</td>141* </tr>142* <tr>143* <th scope="row"> PROCESSING_INSTRUCTION </th>144* <td> next(), getPITarget(), getPIData(), nextTag() </td>145* </tr>146* <tr>147* <th scope="row"> ENTITY_REFERENCE </th>148* <td> next(), getLocalName(), getText(), nextTag() </td>149* </tr>150* <tr>151* <th scope="row"> DTD </th>152* <td> next(), getText(), nextTag() </td>153* </tr>154* </tbody>155* </table>156*157* @version 1.0158* @author Copyright (c) 2009 by Oracle Corporation. All Rights Reserved.159* @see javax.xml.stream.events.XMLEvent160* @see XMLInputFactory161* @see XMLStreamWriter162* @since 1.6163*/164public interface XMLStreamReader extends XMLStreamConstants {165/**166* Get the value of a feature/property from the underlying implementation167* @param name The name of the property, may not be null168* @return The value of the property169* @throws IllegalArgumentException if name is null170*/171public Object getProperty(java.lang.String name) throws java.lang.IllegalArgumentException;172173/**174* Get next parsing event - a processor may return all contiguous175* character data in a single chunk, or it may split it into several chunks.176* If the property javax.xml.stream.isCoalescing is set to true177* element content must be coalesced and only one CHARACTERS event178* must be returned for contiguous element content or179* CDATA Sections.180*181* By default entity references must be182* expanded and reported transparently to the application.183* An exception will be thrown if an entity reference cannot be expanded.184* If element content is empty (i.e. content is "") then no CHARACTERS event will be reported.185*186* <p>Given the following XML:<br>187* {@code <foo><!--description-->content text<![CDATA[<greeting>Hello>/greeting>]]>other content>/foo>}<br>188* The behavior of calling next() when being on foo will be:<br>189* 1- the comment (COMMENT)<br>190* 2- then the characters section (CHARACTERS)<br>191* 3- then the CDATA section (another CHARACTERS)<br>192* 4- then the next characters section (another CHARACTERS)<br>193* 5- then the END_ELEMENT<br>194*195* <p><b>NOTE:</b> empty element (such as {@code <tag/>}) will be reported196* with two separate events: START_ELEMENT, END_ELEMENT - This preserves197* parsing equivalency of empty element to {@code <tag></tag>}.198*199* @see javax.xml.stream.events.XMLEvent200* @return the integer code corresponding to the current parse event201* @throws java.util.NoSuchElementException if this is called when hasNext() returns false202* @throws XMLStreamException if there is an error processing the underlying XML source203*/204public int next() throws XMLStreamException;205206/**207* Test if the current event is of the given type and if the namespace and name match the current208* namespace and name of the current event. If the namespaceURI is null it is not checked for equality,209* if the localName is null it is not checked for equality.210* @param type the event type211* @param namespaceURI the uri of the event, may be null212* @param localName the localName of the event, may be null213* @throws XMLStreamException if the required values are not matched.214*/215public void require(int type, String namespaceURI, String localName) throws XMLStreamException;216217/**218* Reads the content of a text-only element, an exception is thrown if this is219* not a text-only element.220* Regardless of value of javax.xml.stream.isCoalescing this method always returns coalesced content.221* <br> Precondition: the current event is START_ELEMENT.222* <br> Postcondition: the current event is the corresponding END_ELEMENT.223*224* <br>The method does the following (implementations are free to optimized225* but must do equivalent processing):226* <pre>227* if(getEventType() != XMLStreamConstants.START_ELEMENT) {228* throw new XMLStreamException(229* "parser must be on START_ELEMENT to read next text", getLocation());230* }231*232* int eventType = next();233* StringBuffer content = new StringBuffer();234* while(eventType != XMLStreamConstants.END_ELEMENT) {235* if(eventType == XMLStreamConstants.CHARACTERS236* || eventType == XMLStreamConstants.CDATA237* || eventType == XMLStreamConstants.SPACE238* || eventType == XMLStreamConstants.ENTITY_REFERENCE) {239* buf.append(getText());240* } else if(eventType == XMLStreamConstants.PROCESSING_INSTRUCTION241* || eventType == XMLStreamConstants.COMMENT) {242* // skipping243* } else if(eventType == XMLStreamConstants.END_DOCUMENT) {244* throw new XMLStreamException(245* "unexpected end of document when reading element text content", this);246* } else if(eventType == XMLStreamConstants.START_ELEMENT) {247* throw new XMLStreamException(248* "element text content may not contain START_ELEMENT", getLocation());249* } else {250* throw new XMLStreamException(251* "Unexpected event type "+eventType, getLocation());252* }253* eventType = next();254* }255* return buf.toString();256* </pre>257*258* @return the content of a text-only element259* @throws XMLStreamException if the current event is not a START_ELEMENT260* or if a non text element is encountered261*/262public String getElementText() throws XMLStreamException;263264/**265* Skips any white space (isWhiteSpace() returns true), COMMENT,266* or PROCESSING_INSTRUCTION,267* until a START_ELEMENT or END_ELEMENT is reached.268* If other than white space characters, COMMENT, PROCESSING_INSTRUCTION, START_ELEMENT, END_ELEMENT269* are encountered, an exception is thrown. This method should270* be used when processing element-only content seperated by white space.271*272* <br> Precondition: none273* <br> Postcondition: the current event is START_ELEMENT or END_ELEMENT274* and cursor may have moved over any whitespace event.275*276* <br>Essentially it does the following (implementations are free to optimized277* but must do equivalent processing):278* <pre> {@code279* int eventType = next();280* while((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace281* || (eventType == XMLStreamConstants.CDATA && isWhiteSpace())282* // skip whitespace283* || eventType == XMLStreamConstants.SPACE284* || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION285* || eventType == XMLStreamConstants.COMMENT286* ) {287* eventType = next();288* }289* if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {290* throw new String XMLStreamException("expected start or end tag", getLocation());291* }292* return eventType; }293* </pre>294*295* @return the event type of the element read (START_ELEMENT or END_ELEMENT)296* @throws XMLStreamException if the current event is not white space, PROCESSING_INSTRUCTION,297* START_ELEMENT or END_ELEMENT298* @throws java.util.NoSuchElementException if this is called when hasNext() returns false299*/300public int nextTag() throws XMLStreamException;301302/**303* Returns true if there are more parsing events and false304* if there are no more events. This method will return305* false if the current state of the XMLStreamReader is306* END_DOCUMENT307* @return true if there are more events, false otherwise308* @throws XMLStreamException if there is a fatal error detecting the next state309*/310public boolean hasNext() throws XMLStreamException;311312/**313* Frees any resources associated with this Reader. This method does not close the314* underlying input source.315* @throws XMLStreamException if there are errors freeing associated resources316*/317public void close() throws XMLStreamException;318319/**320* Return the uri for the given prefix.321* The uri returned depends on the current state of the processor.322*323* <p><strong>NOTE:</strong>The 'xml' prefix is bound as defined in324* <a href="http://www.w3.org/TR/REC-xml-names/#ns-using">Namespaces in XML</a>325* specification to "http://www.w3.org/XML/1998/namespace".326*327* <p><strong>NOTE:</strong> The 'xmlns' prefix must be resolved to following namespace328* <a href="http://www.w3.org/2000/xmlns/">http://www.w3.org/2000/xmlns/</a>329* @param prefix The prefix to lookup, may not be null330* @return the uri bound to the given prefix or null if it is not bound331* @throws IllegalArgumentException if the prefix is null332*/333public String getNamespaceURI(String prefix);334335/**336* Returns true if the cursor points to a start tag (otherwise false)337* @return true if the cursor points to a start tag, false otherwise338*/339public boolean isStartElement();340341/**342* Returns true if the cursor points to an end tag (otherwise false)343* @return true if the cursor points to an end tag, false otherwise344*/345public boolean isEndElement();346347/**348* Returns true if the cursor points to a character data event349* @return true if the cursor points to character data, false otherwise350*/351public boolean isCharacters();352353/**354* Returns true if the cursor points to a character data event355* that consists of all whitespace356* @return true if the cursor points to all whitespace, false otherwise357*/358public boolean isWhiteSpace();359360361/**362* Returns the normalized attribute value of the363* attribute with the namespace and localName364* If the namespaceURI is null the namespace365* is not checked for equality366* @param namespaceURI the namespace of the attribute367* @param localName the local name of the attribute, cannot be null368* @return returns the value of the attribute , returns null if not found369* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE370*/371public String getAttributeValue(String namespaceURI,372String localName);373374/**375* Returns the count of attributes on this START_ELEMENT,376* this method is only valid on a START_ELEMENT or ATTRIBUTE. This377* count excludes namespace definitions. Attribute indices are378* zero-based.379* @return returns the number of attributes380* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE381*/382public int getAttributeCount();383384/** Returns the qname of the attribute at the provided index385*386* @param index the position of the attribute387* @return the QName of the attribute388* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE389*/390public QName getAttributeName(int index);391392/**393* Returns the namespace of the attribute at the provided394* index395* @param index the position of the attribute396* @return the namespace URI (can be null)397* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE398*/399public String getAttributeNamespace(int index);400401/**402* Returns the localName of the attribute at the provided403* index404* @param index the position of the attribute405* @return the localName of the attribute406* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE407*/408public String getAttributeLocalName(int index);409410/**411* Returns the prefix of this attribute at the412* provided index413* @param index the position of the attribute414* @return the prefix of the attribute415* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE416*/417public String getAttributePrefix(int index);418419/**420* Returns the XML type of the attribute at the provided421* index422* @param index the position of the attribute423* @return the XML type of the attribute424* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE425*/426public String getAttributeType(int index);427428/**429* Returns the value of the attribute at the430* index431* @param index the position of the attribute432* @return the attribute value433* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE434*/435public String getAttributeValue(int index);436437/**438* Returns a boolean which indicates if this439* attribute was created by default440* @param index the position of the attribute441* @return true if this is a default attribute442* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE443*/444public boolean isAttributeSpecified(int index);445446/**447* Returns the count of namespaces declared on this START_ELEMENT or END_ELEMENT,448* this method is only valid on a START_ELEMENT, END_ELEMENT or NAMESPACE. On449* an END_ELEMENT the count is of the namespaces that are about to go450* out of scope. This is the equivalent of the information reported451* by SAX callback for an end element event.452* @return returns the number of namespace declarations on this specific element453* @throws IllegalStateException if this is not a START_ELEMENT, END_ELEMENT or NAMESPACE454*/455public int getNamespaceCount();456457/**458* Returns the prefix for the namespace declared at the459* index. Returns null if this is the default namespace460* declaration461*462* @param index the position of the namespace declaration463* @return returns the namespace prefix464* @throws IllegalStateException if this is not a START_ELEMENT, END_ELEMENT or NAMESPACE465*/466public String getNamespacePrefix(int index);467468/**469* Returns the uri for the namespace declared at the470* index.471*472* @param index the position of the namespace declaration473* @return returns the namespace uri474* @throws IllegalStateException if this is not a START_ELEMENT, END_ELEMENT or NAMESPACE475*/476public String getNamespaceURI(int index);477478/**479* Returns a read only namespace context for the current480* position. The context is transient and only valid until481* a call to next() changes the state of the reader.482* @return return a namespace context483*/484public NamespaceContext getNamespaceContext();485486/**487* Returns a reader that points to the current start element488* and all of its contents. Throws an XMLStreamException if the489* cursor does not point to a START_ELEMENT.<p>490* The sub stream is read from it MUST be read before the parent stream is491* moved on, if not any call on the sub stream will cause an XMLStreamException to be492* thrown. The parent stream will always return the same result from next()493* whatever is done to the sub stream.494* @return an XMLStreamReader which points to the next element495*/496// public XMLStreamReader subReader() throws XMLStreamException;497498/**499* Allows the implementation to reset and reuse any underlying tables500*/501// public void recycle() throws XMLStreamException;502503/**504* Returns an integer code that indicates the type of the event the cursor is505* pointing to. The initial event type is {@link #START_DOCUMENT}.506*507* @return the type of the current event508*/509public int getEventType();510511/**512* Returns the current value of the parse event as a string,513* this returns the string value of a CHARACTERS event,514* returns the value of a COMMENT, the replacement value515* for an ENTITY_REFERENCE, the string value of a CDATA section,516* the string value for a SPACE event,517* or the String value of the internal subset of the DTD.518* If an ENTITY_REFERENCE has been resolved, any character data519* will be reported as CHARACTERS events.520* @return the current text or null521* @throws java.lang.IllegalStateException if this state is not522* a valid text state.523*/524public String getText();525526/**527* Returns an array which contains the characters from this event.528* This array should be treated as read-only and transient. I.e. the array will529* contain the text characters until the XMLStreamReader moves on to the next event.530* Attempts to hold onto the character array beyond that time or modify the531* contents of the array are breaches of the contract for this interface.532* @return the current text or an empty array533* @throws java.lang.IllegalStateException if this state is not534* a valid text state.535*/536public char[] getTextCharacters();537538/**539* Gets the the text associated with a CHARACTERS, SPACE or CDATA event.540* Text starting a "sourceStart" is copied into "target" starting at "targetStart".541* Up to "length" characters are copied. The number of characters actually copied is returned.542*543* The "sourceStart" argument must be greater or equal to 0 and less than or equal to544* the number of characters associated with the event. Usually, one requests text starting at a "sourceStart" of 0.545* If the number of characters actually copied is less than the "length", then there is no more text.546* Otherwise, subsequent calls need to be made until all text has been retrieved. For example:547*548* <pre>{@code549* int length = 1024;550* char[] myBuffer = new char[ length ];551*552* for ( int sourceStart = 0 ; ; sourceStart += length )553* {554* int nCopied = stream.getTextCharacters( sourceStart, myBuffer, 0, length );555*556* if (nCopied < length)557* break;558* }559* } </pre>560* XMLStreamException may be thrown if there are any XML errors in the underlying source.561* The "targetStart" argument must be greater than or equal to 0 and less than the length of "target",562* Length must be greater than 0 and "targetStart + length" must be less than or equal to length of "target".563*564* @param sourceStart the index of the first character in the source array to copy565* @param target the destination array566* @param targetStart the start offset in the target array567* @param length the number of characters to copy568* @return the number of characters actually copied569* @throws XMLStreamException if the underlying XML source is not well-formed570* @throws IndexOutOfBoundsException if targetStart {@literal <} 0 or {@literal >} than the length of target571* @throws IndexOutOfBoundsException if length {@literal <} 0 or targetStart + length {@literal >} length of target572* @throws UnsupportedOperationException if this method is not supported573* @throws NullPointerException is if target is null574*/575public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length)576throws XMLStreamException;577578/**579* Gets the text associated with a CHARACTERS, SPACE or CDATA event. Allows the underlying580* implementation to return the text as a stream of characters. The reference to the581* Reader returned by this method is only valid until next() is called.582*583* All characters must have been checked for well-formedness.584*585* <p> This method is optional and will throw UnsupportedOperationException if it is not supported.586* @throws UnsupportedOperationException if this method is not supported587* @throws IllegalStateException if this is not a valid text state588*/589//public Reader getTextStream();590591/**592* Returns the offset into the text character array where the first593* character (of this text event) is stored.594*595* @return the starting position of the text in the character array596* @throws java.lang.IllegalStateException if this state is not597* a valid text state.598*/599public int getTextStart();600601/**602* Returns the length of the sequence of characters for this603* Text event within the text character array.604*605* @return the length of the text606* @throws java.lang.IllegalStateException if this state is not607* a valid text state.608*/609public int getTextLength();610611/**612* Return input encoding if known or null if unknown.613* @return the encoding of this instance or null614*/615public String getEncoding();616617/**618* Return a boolean indicating whether the current event has text.619* The following events have text:620* CHARACTERS,DTD ,ENTITY_REFERENCE, COMMENT, SPACE621*622* @return true if the event has text, false otherwise623*/624public boolean hasText();625626/**627* Return the current location of the processor.628* If the Location is unknown the processor should return629* an implementation of Location that returns -1 for the630* location and null for the publicId and systemId.631* The location information is only valid until next() is632* called.633* @return the location of the cursor634*/635public Location getLocation();636637/**638* Returns a QName for the current START_ELEMENT or END_ELEMENT event639* @return the QName for the current START_ELEMENT or END_ELEMENT event640* @throws IllegalStateException if this is not a START_ELEMENT or641* END_ELEMENT642*/643public QName getName();644645/**646* Returns the (local) name of the current event.647* For START_ELEMENT or END_ELEMENT returns the (local) name of the current element.648* For ENTITY_REFERENCE it returns entity name.649* The current event must be START_ELEMENT or END_ELEMENT,650* or ENTITY_REFERENCE651* @return the localName652* @throws IllegalStateException if this not a START_ELEMENT,653* END_ELEMENT or ENTITY_REFERENCE654*/655public String getLocalName();656657/**658* returns a boolean indicating whether the current event has a name659* (is a START_ELEMENT or END_ELEMENT).660*661* @return true if the event has a name, false otherwise662*/663public boolean hasName();664665/**666* If the current event is a START_ELEMENT or END_ELEMENT this method667* returns the URI of the prefix or the default namespace.668* Returns null if the event does not have a prefix.669* @return the URI bound to this elements prefix, the default namespace, or null670*/671public String getNamespaceURI();672673/**674* Returns the prefix of the current event or null if the event does not have a prefix675* @return the prefix or null676*/677public String getPrefix();678679/**680* Get the xml version declared on the xml declaration681* Returns null if none was declared682* @return the XML version or null683*/684public String getVersion();685686/**687* Get the standalone declaration from the xml declaration688* @return true if this is standalone, or false otherwise689*/690public boolean isStandalone();691692/**693* Checks if standalone was set in the document694* @return true if standalone was set in the document, or false otherwise695*/696public boolean standaloneSet();697698/**699* Returns the character encoding declared on the xml declaration700* Returns null if none was declared701* @return the encoding declared in the document or null702*/703public String getCharacterEncodingScheme();704705/**706* Get the target of a processing instruction707* @return the target or null708*/709public String getPITarget();710711/**712* Get the data section of a processing instruction713* @return the data or null714*/715public String getPIData();716}717718719