Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/stream/XMLStreamReader.java
48534 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25* Copyright (c) 2009 by Oracle Corporation. All Rights Reserved.26*/2728package javax.xml.stream;2930import java.io.Reader;31import javax.xml.namespace.NamespaceContext;32import javax.xml.namespace.QName;3334/**35* The XMLStreamReader interface allows forward, read-only access to XML.36* It is designed to be the lowest level and most efficient way to37* read XML data.38*39* <p> The XMLStreamReader is designed to iterate over XML using40* next() and hasNext(). The data can be accessed using methods such as getEventType(),41* getNamespaceURI(), getLocalName() and getText();42*43* <p> The <a href="#next()">next()</a> method causes the reader to read the next parse event.44* The next() method returns an integer which identifies the type of event just read.45* <p> The event type can be determined using <a href="#getEventType()">getEventType()</a>.46* <p> Parsing events are defined as the XML Declaration, a DTD,47* start tag, character data, white space, end tag, comment,48* or processing instruction. An attribute or namespace event may be encountered49* at the root level of a document as the result of a query operation.50*51* <p>For XML 1.0 compliance an XML processor must pass the52* identifiers of declared unparsed entities, notation declarations and their53* associated identifiers to the application. This information is54* provided through the property API on this interface.55* The following two properties allow access to this information:56* javax.xml.stream.notations and javax.xml.stream.entities.57* When the current event is a DTD the following call will return a58* list of Notations59* <code>List l = (List) getProperty("javax.xml.stream.notations");</code>60* The following call will return a list of entity declarations:61* <code>List l = (List) getProperty("javax.xml.stream.entities");</code>62* These properties can only be accessed during a DTD event and63* are defined to return null if the information is not available.64*65* <p>The following table describes which methods are valid in what state.66* If a method is called in an invalid state the method will throw a67* java.lang.IllegalStateException.68*69* <table border="2" rules="all" cellpadding="4">70* <thead>71* <tr>72* <th align="center" colspan="2">73* Valid methods for each state74* </th>75* </tr>76* </thead>77* <tbody>78* <tr>79* <th>Event Type</th>80* <th>Valid Methods</th>81* </tr>82* <tr>83* <td> All States </td>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* <td> START_ELEMENT </td>93* <td> next(), getName(), getLocalName(), hasName(), getPrefix(),94* getAttributeXXX(), isAttributeSpecified(),95* getNamespaceXXX(),96* getElementText(), nextTag()97* </td>98* </tr>99* <td> ATTRIBUTE </td>100* <td> next(), nextTag()101* getAttributeXXX(), isAttributeSpecified(),102* </td>103* </tr>104* </tr>105* <td> NAMESPACE </td>106* <td> next(), nextTag()107* getNamespaceXXX()108* </td>109* </tr>110* <tr>111* <td> END_ELEMENT </td>112* <td> next(), getName(), getLocalName(), hasName(), getPrefix(),113* getNamespaceXXX(), nextTag()114* </td>115* </tr>116* <tr>117* <td> CHARACTERS </td>118* <td> next(), getTextXXX(), nextTag() </td>119* </tr>120* <tr>121* <td> CDATA </td>122* <td> next(), getTextXXX(), nextTag() </td>123* </tr>124* <tr>125* <td> COMMENT </td>126* <td> next(), getTextXXX(), nextTag() </td>127* </tr>128* <tr>129* <td> SPACE </td>130* <td> next(), getTextXXX(), nextTag() </td>131* </tr>132* <tr>133* <td> START_DOCUMENT </td>134* <td> next(), getEncoding(), getVersion(), isStandalone(), standaloneSet(),135* getCharacterEncodingScheme(), nextTag()</td>136* </tr>137* <tr>138* <td> END_DOCUMENT </td>139* <td> close()</td>140* </tr>141* <tr>142* <td> PROCESSING_INSTRUCTION </td>143* <td> next(), getPITarget(), getPIData(), nextTag() </td>144* </tr>145* <tr>146* <td> ENTITY_REFERENCE </td>147* <td> next(), getLocalName(), getText(), nextTag() </td>148* </tr>149* <tr>150* <td> DTD </td>151* <td> next(), getText(), nextTag() </td>152* </tr>153* </tbody>154* </table>155*156* @version 1.0157* @author Copyright (c) 2009 by Oracle Corporation. All Rights Reserved.158* @see javax.xml.stream.events.XMLEvent159* @see XMLInputFactory160* @see XMLStreamWriter161* @since 1.6162*/163public interface XMLStreamReader extends XMLStreamConstants {164/**165* Get the value of a feature/property from the underlying implementation166* @param name The name of the property, may not be null167* @return The value of the property168* @throws IllegalArgumentException if name is null169*/170public Object getProperty(java.lang.String name) throws java.lang.IllegalArgumentException;171172/**173* Get next parsing event - a processor may return all contiguous174* character data in a single chunk, or it may split it into several chunks.175* If the property javax.xml.stream.isCoalescing is set to true176* element content must be coalesced and only one CHARACTERS event177* must be returned for contiguous element content or178* CDATA Sections.179*180* By default entity references must be181* expanded and reported transparently to the application.182* An exception will be thrown if an entity reference cannot be expanded.183* If element content is empty (i.e. content is "") then no CHARACTERS event will be reported.184*185* <p>Given the following XML:<br>186* <foo><!--description-->content text<![CDATA[<greeting>Hello</greeting>]]>other content</foo><br>187* The behavior of calling next() when being on foo will be:<br>188* 1- the comment (COMMENT)<br>189* 2- then the characters section (CHARACTERS)<br>190* 3- then the CDATA section (another CHARACTERS)<br>191* 4- then the next characters section (another CHARACTERS)<br>192* 5- then the END_ELEMENT<br>193*194* <p><b>NOTE:</b> empty element (such as <tag/>) will be reported195* with two separate events: START_ELEMENT, END_ELEMENT - This preserves196* parsing equivalency of empty element to <tag></tag>.197*198* This method will throw an IllegalStateException if it is called after hasNext() returns false.199* @see javax.xml.stream.events.XMLEvent200* @return the integer code corresponding to the current parse event201* @throws 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* int eventType = next();232* StringBuffer content = new StringBuffer();233* while(eventType != XMLStreamConstants.END_ELEMENT ) {234* if(eventType == XMLStreamConstants.CHARACTERS235* || eventType == XMLStreamConstants.CDATA236* || eventType == XMLStreamConstants.SPACE237* || eventType == XMLStreamConstants.ENTITY_REFERENCE) {238* buf.append(getText());239* } else if(eventType == XMLStreamConstants.PROCESSING_INSTRUCTION240* || eventType == XMLStreamConstants.COMMENT) {241* // skipping242* } else if(eventType == XMLStreamConstants.END_DOCUMENT) {243* throw new XMLStreamException(244* "unexpected end of document when reading element text content", this);245* } else if(eventType == XMLStreamConstants.START_ELEMENT) {246* throw new XMLStreamException(247* "element text content may not contain START_ELEMENT", getLocation());248* } else {249* throw new XMLStreamException(250* "Unexpected event type "+eventType, getLocation());251* }252* eventType = next();253* }254* return buf.toString();255* </pre>256*257* @throws XMLStreamException if the current event is not a START_ELEMENT258* or if a non text element is encountered259*/260public String getElementText() throws XMLStreamException;261262/**263* Skips any white space (isWhiteSpace() returns true), COMMENT,264* or PROCESSING_INSTRUCTION,265* until a START_ELEMENT or END_ELEMENT is reached.266* If other than white space characters, COMMENT, PROCESSING_INSTRUCTION, START_ELEMENT, END_ELEMENT267* are encountered, an exception is thrown. This method should268* be used when processing element-only content seperated by white space.269*270* <br /> Precondition: none271* <br /> Postcondition: the current event is START_ELEMENT or END_ELEMENT272* and cursor may have moved over any whitespace event.273*274* <br />Essentially it does the following (implementations are free to optimized275* but must do equivalent processing):276* <pre>277* int eventType = next();278* while((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace279* || (eventType == XMLStreamConstants.CDATA && isWhiteSpace())280* // skip whitespace281* || eventType == XMLStreamConstants.SPACE282* || eventType == XMLStreamConstants.PROCESSING_INSTRUCTION283* || eventType == XMLStreamConstants.COMMENT284* ) {285* eventType = next();286* }287* if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {288* throw new String XMLStreamException("expected start or end tag", getLocation());289* }290* return eventType;291* </pre>292*293* @return the event type of the element read (START_ELEMENT or END_ELEMENT)294* @throws XMLStreamException if the current event is not white space, PROCESSING_INSTRUCTION,295* START_ELEMENT or END_ELEMENT296* @throws NoSuchElementException if this is called when hasNext() returns false297*/298public int nextTag() throws XMLStreamException;299300/**301* Returns true if there are more parsing events and false302* if there are no more events. This method will return303* false if the current state of the XMLStreamReader is304* END_DOCUMENT305* @return true if there are more events, false otherwise306* @throws XMLStreamException if there is a fatal error detecting the next state307*/308public boolean hasNext() throws XMLStreamException;309310/**311* Frees any resources associated with this Reader. This method does not close the312* underlying input source.313* @throws XMLStreamException if there are errors freeing associated resources314*/315public void close() throws XMLStreamException;316317/**318* Return the uri for the given prefix.319* The uri returned depends on the current state of the processor.320*321* <p><strong>NOTE:</strong>The 'xml' prefix is bound as defined in322* <a href="http://www.w3.org/TR/REC-xml-names/#ns-using">Namespaces in XML</a>323* specification to "http://www.w3.org/XML/1998/namespace".324*325* <p><strong>NOTE:</strong> The 'xmlns' prefix must be resolved to following namespace326* <a href="http://www.w3.org/2000/xmlns/">http://www.w3.org/2000/xmlns/</a>327* @param prefix The prefix to lookup, may not be null328* @return the uri bound to the given prefix or null if it is not bound329* @throws IllegalArgumentException if the prefix is null330*/331public String getNamespaceURI(String prefix);332333/**334* Returns true if the cursor points to a start tag (otherwise false)335* @return true if the cursor points to a start tag, false otherwise336*/337public boolean isStartElement();338339/**340* Returns true if the cursor points to an end tag (otherwise false)341* @return true if the cursor points to an end tag, false otherwise342*/343public boolean isEndElement();344345/**346* Returns true if the cursor points to a character data event347* @return true if the cursor points to character data, false otherwise348*/349public boolean isCharacters();350351/**352* Returns true if the cursor points to a character data event353* that consists of all whitespace354* @return true if the cursor points to all whitespace, false otherwise355*/356public boolean isWhiteSpace();357358359/**360* Returns the normalized attribute value of the361* attribute with the namespace and localName362* If the namespaceURI is null the namespace363* is not checked for equality364* @param namespaceURI the namespace of the attribute365* @param localName the local name of the attribute, cannot be null366* @return returns the value of the attribute , returns null if not found367* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE368*/369public String getAttributeValue(String namespaceURI,370String localName);371372/**373* Returns the count of attributes on this START_ELEMENT,374* this method is only valid on a START_ELEMENT or ATTRIBUTE. This375* count excludes namespace definitions. Attribute indices are376* zero-based.377* @return returns the number of attributes378* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE379*/380public int getAttributeCount();381382/** Returns the qname of the attribute at the provided index383*384* @param index the position of the attribute385* @return the QName of the attribute386* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE387*/388public QName getAttributeName(int index);389390/**391* Returns the namespace of the attribute at the provided392* index393* @param index the position of the attribute394* @return the namespace URI (can be null)395* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE396*/397public String getAttributeNamespace(int index);398399/**400* Returns the localName of the attribute at the provided401* index402* @param index the position of the attribute403* @return the localName of the attribute404* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE405*/406public String getAttributeLocalName(int index);407408/**409* Returns the prefix of this attribute at the410* provided index411* @param index the position of the attribute412* @return the prefix of the attribute413* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE414*/415public String getAttributePrefix(int index);416417/**418* Returns the XML type of the attribute at the provided419* index420* @param index the position of the attribute421* @return the XML type of the attribute422* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE423*/424public String getAttributeType(int index);425426/**427* Returns the value of the attribute at the428* index429* @param index the position of the attribute430* @return the attribute value431* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE432*/433public String getAttributeValue(int index);434435/**436* Returns a boolean which indicates if this437* attribute was created by default438* @param index the position of the attribute439* @return true if this is a default attribute440* @throws IllegalStateException if this is not a START_ELEMENT or ATTRIBUTE441*/442public boolean isAttributeSpecified(int index);443444/**445* Returns the count of namespaces declared on this START_ELEMENT or END_ELEMENT,446* this method is only valid on a START_ELEMENT, END_ELEMENT or NAMESPACE. On447* an END_ELEMENT the count is of the namespaces that are about to go448* out of scope. This is the equivalent of the information reported449* by SAX callback for an end element event.450* @return returns the number of namespace declarations on this specific element451* @throws IllegalStateException if this is not a START_ELEMENT, END_ELEMENT or NAMESPACE452*/453public int getNamespaceCount();454455/**456* Returns the prefix for the namespace declared at the457* index. Returns null if this is the default namespace458* declaration459*460* @param index the position of the namespace declaration461* @return returns the namespace prefix462* @throws IllegalStateException if this is not a START_ELEMENT, END_ELEMENT or NAMESPACE463*/464public String getNamespacePrefix(int index);465466/**467* Returns the uri for the namespace declared at the468* index.469*470* @param index the position of the namespace declaration471* @return returns the namespace uri472* @throws IllegalStateException if this is not a START_ELEMENT, END_ELEMENT or NAMESPACE473*/474public String getNamespaceURI(int index);475476/**477* Returns a read only namespace context for the current478* position. The context is transient and only valid until479* a call to next() changes the state of the reader.480* @return return a namespace context481*/482public NamespaceContext getNamespaceContext();483484/**485* Returns a reader that points to the current start element486* and all of its contents. Throws an XMLStreamException if the487* cursor does not point to a START_ELEMENT.<p>488* The sub stream is read from it MUST be read before the parent stream is489* moved on, if not any call on the sub stream will cause an XMLStreamException to be490* thrown. The parent stream will always return the same result from next()491* whatever is done to the sub stream.492* @return an XMLStreamReader which points to the next element493*/494// public XMLStreamReader subReader() throws XMLStreamException;495496/**497* Allows the implementation to reset and reuse any underlying tables498*/499// public void recycle() throws XMLStreamException;500501/**502* Returns an integer code that indicates the type503* of the event the cursor is pointing to.504*/505public int getEventType();506507/**508* Returns the current value of the parse event as a string,509* this returns the string value of a CHARACTERS event,510* returns the value of a COMMENT, the replacement value511* for an ENTITY_REFERENCE, the string value of a CDATA section,512* the string value for a SPACE event,513* or the String value of the internal subset of the DTD.514* If an ENTITY_REFERENCE has been resolved, any character data515* will be reported as CHARACTERS events.516* @return the current text or null517* @throws java.lang.IllegalStateException if this state is not518* a valid text state.519*/520public String getText();521522/**523* Returns an array which contains the characters from this event.524* This array should be treated as read-only and transient. I.e. the array will525* contain the text characters until the XMLStreamReader moves on to the next event.526* Attempts to hold onto the character array beyond that time or modify the527* contents of the array are breaches of the contract for this interface.528* @return the current text or an empty array529* @throws java.lang.IllegalStateException if this state is not530* a valid text state.531*/532public char[] getTextCharacters();533534/**535* Gets the the text associated with a CHARACTERS, SPACE or CDATA event.536* Text starting a "sourceStart" is copied into "target" starting at "targetStart".537* Up to "length" characters are copied. The number of characters actually copied is returned.538*539* The "sourceStart" argument must be greater or equal to 0 and less than or equal to540* the number of characters associated with the event. Usually, one requests text starting at a "sourceStart" of 0.541* If the number of characters actually copied is less than the "length", then there is no more text.542* Otherwise, subsequent calls need to be made until all text has been retrieved. For example:543*544*<code>545* int length = 1024;546* char[] myBuffer = new char[ length ];547*548* for ( int sourceStart = 0 ; ; sourceStart += length )549* {550* int nCopied = stream.getTextCharacters( sourceStart, myBuffer, 0, length );551*552* if (nCopied < length)553* break;554* }555* </code>556* XMLStreamException may be thrown if there are any XML errors in the underlying source.557* The "targetStart" argument must be greater than or equal to 0 and less than the length of "target",558* Length must be greater than 0 and "targetStart + length" must be less than or equal to length of "target".559*560* @param sourceStart the index of the first character in the source array to copy561* @param target the destination array562* @param targetStart the start offset in the target array563* @param length the number of characters to copy564* @return the number of characters actually copied565* @throws XMLStreamException if the underlying XML source is not well-formed566* @throws IndexOutOfBoundsException if targetStart < 0 or > than the length of target567* @throws IndexOutOfBoundsException if length < 0 or targetStart + length > length of target568* @throws UnsupportedOperationException if this method is not supported569* @throws NullPointerException is if target is null570*/571public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length)572throws XMLStreamException;573574/**575* Gets the text associated with a CHARACTERS, SPACE or CDATA event. Allows the underlying576* implementation to return the text as a stream of characters. The reference to the577* Reader returned by this method is only valid until next() is called.578*579* All characters must have been checked for well-formedness.580*581* <p> This method is optional and will throw UnsupportedOperationException if it is not supported.582* @throws UnsupportedOperationException if this method is not supported583* @throws IllegalStateException if this is not a valid text state584*/585//public Reader getTextStream();586587/**588* Returns the offset into the text character array where the first589* character (of this text event) is stored.590* @throws java.lang.IllegalStateException if this state is not591* a valid text state.592*/593public int getTextStart();594595/**596* Returns the length of the sequence of characters for this597* Text event within the text character array.598* @throws java.lang.IllegalStateException if this state is not599* a valid text state.600*/601public int getTextLength();602603/**604* Return input encoding if known or null if unknown.605* @return the encoding of this instance or null606*/607public String getEncoding();608609/**610* Return true if the current event has text, false otherwise611* The following events have text:612* CHARACTERS,DTD ,ENTITY_REFERENCE, COMMENT, SPACE613*/614public boolean hasText();615616/**617* Return the current location of the processor.618* If the Location is unknown the processor should return619* an implementation of Location that returns -1 for the620* location and null for the publicId and systemId.621* The location information is only valid until next() is622* called.623*/624public Location getLocation();625626/**627* Returns a QName for the current START_ELEMENT or END_ELEMENT event628* @return the QName for the current START_ELEMENT or END_ELEMENT event629* @throws IllegalStateException if this is not a START_ELEMENT or630* END_ELEMENT631*/632public QName getName();633634/**635* Returns the (local) name of the current event.636* For START_ELEMENT or END_ELEMENT returns the (local) name of the current element.637* For ENTITY_REFERENCE it returns entity name.638* The current event must be START_ELEMENT or END_ELEMENT,639* or ENTITY_REFERENCE640* @return the localName641* @throws IllegalStateException if this not a START_ELEMENT,642* END_ELEMENT or ENTITY_REFERENCE643*/644public String getLocalName();645646/**647* returns true if the current event has a name (is a START_ELEMENT or END_ELEMENT)648* returns false otherwise649*/650public boolean hasName();651652/**653* If the current event is a START_ELEMENT or END_ELEMENT this method654* returns the URI of the prefix or the default namespace.655* Returns null if the event does not have a prefix.656* @return the URI bound to this elements prefix, the default namespace, or null657*/658public String getNamespaceURI();659660/**661* Returns the prefix of the current event or null if the event does not have a prefix662* @return the prefix or null663*/664public String getPrefix();665666/**667* Get the xml version declared on the xml declaration668* Returns null if none was declared669* @return the XML version or null670*/671public String getVersion();672673/**674* Get the standalone declaration from the xml declaration675* @return true if this is standalone, or false otherwise676*/677public boolean isStandalone();678679/**680* Checks if standalone was set in the document681* @return true if standalone was set in the document, or false otherwise682*/683public boolean standaloneSet();684685/**686* Returns the character encoding declared on the xml declaration687* Returns null if none was declared688* @return the encoding declared in the document or null689*/690public String getCharacterEncodingScheme();691692/**693* Get the target of a processing instruction694* @return the target or null695*/696public String getPITarget();697698/**699* Get the data section of a processing instruction700* @return the data or null701*/702public String getPIData();703}704705706