public final class XMLStreamReaderImpl extends java.lang.Object implements XMLStreamReader, Reusable
This class represents a reusable
implementation of XMLStreamWriter.
Except for the types being used (CharArray/
CharSequence instead of String) the
parsing behavior is about the same as for the standard
javax.xml.stream.XMLStreamReader (although several times
faster).
The CharArray instances returned by this reader
supports fast primitive conversions as illustrated below:
// Creates reader for an input sream with unknown encoding.
XMLStreamReaderImpl xmlReader = new XMLStreamReaderImpl().setInput(inputStream);
// Parses.
for (int e=xmlReader.next(); e != XMLStreamConstants.END_DOCUMENT; e = xmlReader.next()) {
switch (e) { // Event.
case XMLStreamConstants.START_ELEMENT:
if (xmlReader.getLocalName().equals("Time")) {
// Reads primitive types (int) attributes directly.
int hour = xmlReader.getAttributeValue("hour").toInt();
int minute = xmlReader.getAttributeValue("minute").toInt();
int second = xmlReader.getAttributeValue("second").toInt();
...
}
...
break;
}
}
// Closes reader, it is automatically reset() and can be reused!
xmlReader.close();
This reader returns all contiguous character data in a single
chunk (always coalescing). It is non-validating (DTD is returned
unparsed). Although, users may define custom entities mapping using
the setEntities(java.util.Map) method (e.g. after parsing/resolving
external entities).
ATTRIBUTE, CDATA, CHARACTERS, COMMENT, DTD, END_DOCUMENT, END_ELEMENT, ENTITY_DECLARATION, ENTITY_REFERENCE, NAMESPACE, NOTATION_DECLARATION, PROCESSING_INSTRUCTION, SPACE, START_DOCUMENT, START_ELEMENT| Constructor and Description |
|---|
XMLStreamReaderImpl()
Default constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Frees any resources associated with this Reader.
|
int |
getAttributeCount()
Returns the count of attributes on this START_ELEMENT, this method is
only valid on a START_ELEMENT or ATTRIBUTE.
|
CharArray |
getAttributeLocalName(int index)
Returns the localName of the attribute at the provided index.
|
CharArray |
getAttributeNamespace(int index)
Returns the namespace of the attribute at the provided index
|
CharArray |
getAttributePrefix(int index)
Returns the prefix of this attribute at the provided index
|
Attributes |
getAttributes()
Returns the current attributes (SAX2-Like).
|
CharArray |
getAttributeType(int index)
Returns the XML type of the attribute at the provided index.
|
CharArray |
getAttributeValue(java.lang.CharSequence uri,
java.lang.CharSequence localName)
Returns the normalized attribute value of the attribute with the
namespace and localName.
|
CharArray |
getAttributeValue(int index)
Returns the value of the attribute at the index.
|
CharArray |
getCharacterEncodingScheme()
Returns the character encoding declared on the xml declaration.
|
int |
getDepth()
Returns the current depth of the element.
|
CharArray |
getElementText()
Reads the content of a text-only element, an exception is thrown if this
is not a text-only element.
|
java.lang.String |
getEncoding()
Returns the input encoding if known or
null if unknown. |
int |
getEventType()
Returns an integer code that indicates the type of the event the cursor
is pointing to.
|
CharArray |
getLocalName()
Returns the (local) name of the current event.
|
Location |
getLocation()
Return the current location of the processor.
|
NamespaceContext |
getNamespaceContext()
Returns a read only namespace context for the current position.
|
int |
getNamespaceCount()
Returns the count of namespaces declared on this START_ELEMENT or
END_ELEMENT.
|
CharArray |
getNamespacePrefix(int index)
Returns the prefix for the namespace declared at the index.
|
CharArray |
getNamespaceURI()
If the current event is a START_ELEMENT or END_ELEMENT this method
returns the URI of the current element (URI mapping to the prefix
element/attribute has; or if no prefix
null). |
CharArray |
getNamespaceURI(java.lang.CharSequence prefix)
Returns the uri for the given prefix.
|
CharArray |
getNamespaceURI(int index)
Returns the URI for the namespace declared at the index.
|
CharArray |
getPIData()
Get the data section of a processing instruction.
|
CharArray |
getPITarget()
Returns the target of a processing instruction.
|
CharArray |
getPrefix()
Returns the prefix of the current event or null if the event does not
have a prefix.
|
java.lang.Object |
getProperty(java.lang.String name)
Gets the value of a feature/property from the underlying implementation
|
CharArray |
getQName()
Returns the qualified name of the current event.
|
CharArray |
getQName(int depth)
Returns the qualified name of the element at the specified level.
|
CharArray |
getText()
Returns the current value of the parse event as a string, this returns
the string value of a CHARACTERS event, returns the value of a COMMENT,
the replacement value for an ENTITY_REFERENCE, the string value of a
CDATA section, the string value for a SPACE event, or the String value of
the internal subset of the DTD.
|
char[] |
getTextCharacters()
Returns an array which contains the characters from this event.
|
int |
getTextCharacters(int sourceStart,
char[] target,
int targetStart,
int length)
Gets the the text associated with a CHARACTERS, SPACE or CDATA event.
|
int |
getTextLength()
Returns the length of the sequence of characters for this Text event
within the text character array.
|
int |
getTextStart()
Returns the offset into the text character array where the first
character (of this text event) is stored.
|
CharArray |
getVersion()
Gets the xml version declared on the xml declaration.
|
boolean |
hasName()
Indicates if the current event has a name (is a START_ELEMENT or
END_ELEMENT).
|
boolean |
hasNext()
Returns true if there are more parsing events and false if there are no
more events.
|
boolean |
hasText()
Indicates if the current event has text.
|
boolean |
isAttributeSpecified(int index)
Indicates if this attribute was created by default.
|
boolean |
isCharacters()
Indicates if the cursor points to character data.
|
boolean |
isEndElement()
Indicates if the cursor points to an end tag.
|
boolean |
isStandalone()
Gets the standalone declaration from the xml declaration.
|
boolean |
isStartElement()
Indicates if the cursor points to a start tag.
|
boolean |
isWhiteSpace()
Indicates if the cursor points to character data that consists
of all whitespace.
|
int |
next()
Gets next parsing event - contiguous character data is returned into a
single chunk.
|
int |
nextTag()
Skips any white space (isWhiteSpace() returns true), COMMENT, or
PROCESSING_INSTRUCTION, until a START_ELEMENT or END_ELEMENT is reached.
|
void |
require(int type,
java.lang.CharSequence namespaceURI,
java.lang.CharSequence localName)
Tests if the current event is of the given type and if the namespace and
name match the current namespace and name of the current event.
|
void |
reset()
Resets the internal state of this object to its default values.
|
void |
setEntities(java.util.Map entities)
Defines a custom entities to replacement text mapping for this reader.
|
void |
setInput(java.io.InputStream in)
Sets the input stream source for this XML stream reader
(encoding retrieved from XML prolog if any).
|
void |
setInput(java.io.InputStream in,
java.lang.String encoding)
Sets the input stream source and encoding for this XML stream reader.
|
void |
setInput(java.io.Reader reader)
Sets the reader input source for this XML stream reader.
|
boolean |
standaloneSet()
Checks if standalone was set in the document.
|
java.lang.String |
toString()
Returns the textual representation of this reader current state.
|
public void setInput(java.io.InputStream in)
throws XMLStreamException
in - the input source with unknown encoding.XMLStreamExceptionpublic void setInput(java.io.InputStream in,
java.lang.String encoding)
throws XMLStreamException
in - the input source.encoding - the associated encoding.XMLStreamExceptionpublic void setInput(java.io.Reader reader)
throws XMLStreamException
reader - the input source reader.XMLStreamExceptionUTF8StreamReader,
UTF8ByteBufferReader,
CharSequenceReaderpublic int getDepth()
<!-- outside --> 0
<root> 1
sometext 1
<foobar> 2
</foobar> 2
</root> 1
<!-- outside --> 0 public CharArray getQName()
java.lang.IllegalStateException - if this not a START_ELEMENT or END_ELEMENT.public CharArray getQName(int depth)
java.lang.IllegalArgumentException - if depth > getDepth()public Attributes getAttributes()
java.lang.IllegalStateException - if not a START_ELEMENT.public void setEntities(java.util.Map entities)
FastMap<String, String> HTML_ENTITIES = new FastMap<String, String>();
HTML_ENTITIES.put("nbsp", " ");
HTML_ENTITIES.put("copy", "©");
HTML_ENTITIES.put("eacute", "é");
...
XMLStreamReaderImpl reader = new XMLStreamReaderImpl();
reader.setEntities(HTML_ENTITIES);
The entities mapping may be changed dynamically (e.g.
after reading the DTD and all external entities references are resolved).entities - the entities to replacement texts mapping
(both must be CharSequence instances).public java.lang.String toString()
toString in class java.lang.Objectpublic int next()
throws XMLStreamException
XMLStreamReader
Given the following XML:
<foo><!--description-->content
text<![CDATA[<greeting>Hello</greeting>]]>other content</foo>
The behavior of calling next() when being on foo will be:
1- the comment (COMMENT)
2- then the characters section (CHARACTERS)
3- then the CDATA section (another CHARACTERS)
4- then the next characters section (another CHARACTERS)
5- then the END_ELEMENT
NOTE: empty element (such as <tag/>) will be reported with two separate events: START_ELEMENT, END_ELEMENT - This preserves parsing equivalency of empty element to <tag></tag>. This method will throw an IllegalStateException if it is called after hasNext() returns false.
next in interface XMLStreamReaderXMLStreamException - if there is an error processing the
underlying XML sourcepublic void reset()
Reusablepublic void require(int type,
java.lang.CharSequence namespaceURI,
java.lang.CharSequence localName)
throws XMLStreamException
XMLStreamReaderrequire in interface XMLStreamReadertype - the event type.namespaceURI - the uri of the event, may be null.localName - the localName of the event, may be null.XMLStreamException - if the required values are not matched.public CharArray getElementText() throws XMLStreamException
XMLStreamReader
if (getEventType() != XMLStreamConstants.START_ELEMENT) {
throw new XMLStreamException(
"parser must be on START_ELEMENT to read next text", getLocation());
}
int eventType = next();
StringBuffer content = new StringBuffer();
while (eventType != XMLStreamConstants.END_ELEMENT) {
if (eventType == XMLStreamConstants.CHARACTERS
|| eventType == XMLStreamConstants.CDATA
|| eventType == XMLStreamConstants.SPACE
|| eventType == XMLStreamConstants.ENTITY_REFERENCE) {
buf.append(getText());
} else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
|| eventType == XMLStreamConstants.COMMENT) {
// skipping
} else if (eventType == XMLStreamConstants.END_DOCUMENT) {
throw new XMLStreamException(
"unexpected end of document when reading element text content",
this);
} else if (eventType == XMLStreamConstants.START_ELEMENT) {
throw new XMLStreamException(
"element text content may not contain START_ELEMENT",
getLocation());
} else {
throw new XMLStreamException("Unexpected event type " + eventType,
getLocation());
}
eventType = next();
}
return buf.toString();
getElementText in interface XMLStreamReaderXMLStreamException - if the current event is not a START_ELEMENT
or if a non text element is encountered.public java.lang.Object getProperty(java.lang.String name)
throws java.lang.IllegalArgumentException
XMLStreamReadergetProperty in interface XMLStreamReadername - the name of the property.java.lang.IllegalArgumentExceptionpublic void close()
throws XMLStreamException
XMLStreamReaderclose in interface XMLStreamReaderXMLStreamException - if there are errors freeing associated
resourcespublic int getAttributeCount()
XMLStreamReadergetAttributeCount in interface XMLStreamReaderpublic CharArray getAttributeLocalName(int index)
XMLStreamReadergetAttributeLocalName in interface XMLStreamReaderindex - the position of the attribute.public CharArray getAttributeNamespace(int index)
XMLStreamReadergetAttributeNamespace in interface XMLStreamReaderindex - the position of the attribute.null if no prefix.public CharArray getAttributePrefix(int index)
XMLStreamReadergetAttributePrefix in interface XMLStreamReaderindex - the position of the attribute.null if no prefix.public CharArray getAttributeType(int index)
XMLStreamReadergetAttributeType in interface XMLStreamReaderindex - the position of the attributepublic CharArray getAttributeValue(java.lang.CharSequence uri, java.lang.CharSequence localName)
XMLStreamReadergetAttributeValue in interface XMLStreamReaderuri - the namespace of the attribute or null.localName - the local name of the attribute.null.public CharArray getAttributeValue(int index)
XMLStreamReadergetAttributeValue in interface XMLStreamReaderindex - the position of the attribute.public CharArray getCharacterEncodingScheme()
XMLStreamReadergetCharacterEncodingScheme in interface XMLStreamReadernullpublic java.lang.String getEncoding()
XMLStreamReadernull if unknown.getEncoding in interface XMLStreamReaderpublic int getEventType()
XMLStreamReadergetEventType in interface XMLStreamReaderpublic CharArray getLocalName()
XMLStreamReadergetLocalName in interface XMLStreamReaderpublic Location getLocation()
XMLStreamReadergetLocation in interface XMLStreamReaderpublic int getNamespaceCount()
XMLStreamReadergetNamespaceCount in interface XMLStreamReaderpublic CharArray getNamespacePrefix(int index)
XMLStreamReadergetNamespacePrefix in interface XMLStreamReaderindex - the position of the namespace declaration.null if no prefix.public CharArray getNamespaceURI(java.lang.CharSequence prefix)
XMLStreamReaderNOTE:The 'xml' prefix is bound as defined in Namespaces in XML specification to "http://www.w3.org/XML/1998/namespace".
NOTE: The 'xmlns' prefix must be resolved to following namespace http://www.w3.org/2000/xmlns/
getNamespaceURI in interface XMLStreamReaderprefix - the prefix to lookup.null if it is
not boundpublic CharArray getNamespaceURI(int index)
XMLStreamReadergetNamespaceURI in interface XMLStreamReaderindex - the position of the namespace declaration.null if no prefix.public NamespaceContext getNamespaceContext()
XMLStreamReadergetNamespaceContext in interface XMLStreamReaderpublic CharArray getNamespaceURI()
XMLStreamReadernull).getNamespaceURI in interface XMLStreamReadernull.public CharArray getPrefix()
XMLStreamReadergetPrefix in interface XMLStreamReadernullpublic CharArray getPIData()
XMLStreamReadergetPIData in interface XMLStreamReadernull if the processing instruction only has target.public CharArray getPITarget()
XMLStreamReadergetPITarget in interface XMLStreamReaderpublic CharArray getText()
XMLStreamReadergetText in interface XMLStreamReadernullpublic char[] getTextCharacters()
XMLStreamReadergetTextCharacters in interface XMLStreamReaderpublic int getTextCharacters(int sourceStart,
char[] target,
int targetStart,
int length)
throws XMLStreamException
XMLStreamReader
int length = 1024;
char[] myBuffer = new char[ length ];
for ( int sourceStart = 0 ; ; sourceStart += length )
{
int nCopied = stream.getTextCharacters( sourceStart, myBuffer, 0, length );
if (nCopied < length)
break;
}
XMLStreamException may be thrown
if there are any XML errors in the underlying source. The "targetStart"
argument must be greater than or equal to 0 and less than the length of
"target", Length must be greater than 0 and "targetStart + length" must
be less than or equal to length of "target".getTextCharacters in interface XMLStreamReadersourceStart - the index of te first character in the source array
to copytarget - the destination arraytargetStart - the start offset in the target arraylength - the number of characters to copyXMLStreamException - if the XML source is not well-formed.public int getTextLength()
XMLStreamReadergetTextLength in interface XMLStreamReaderpublic int getTextStart()
XMLStreamReadergetTextStart in interface XMLStreamReaderpublic CharArray getVersion()
XMLStreamReadergetVersion in interface XMLStreamReadernullpublic boolean isStandalone()
XMLStreamReaderisStandalone in interface XMLStreamReadertrue if this is standalone;
false otherwise.public boolean standaloneSet()
XMLStreamReaderstandaloneSet in interface XMLStreamReadertrue if standalone was set;
false otherwise.public boolean hasName()
XMLStreamReaderhasName in interface XMLStreamReadertrue if the current event has a name;
false otherwise.public boolean hasNext()
throws XMLStreamException
XMLStreamReaderhasNext in interface XMLStreamReaderXMLStreamException - if there is a fatal error detecting the next
state.public boolean hasText()
XMLStreamReaderhasText in interface XMLStreamReadertrue if the current event as text;
false otherwise.public boolean isAttributeSpecified(int index)
XMLStreamReaderisAttributeSpecified in interface XMLStreamReaderindex - the position of the attribute.true if this is a default attribute;
false otherwise.public boolean isCharacters()
XMLStreamReaderisCharacters in interface XMLStreamReadertrue if the cursor points to character data;
false otherwise.public boolean isEndElement()
XMLStreamReaderisEndElement in interface XMLStreamReadertrue if the cursor points to a end tag;
false otherwise.public boolean isStartElement()
XMLStreamReaderisStartElement in interface XMLStreamReadertrue if the cursor points to a start tag;
false otherwise.public boolean isWhiteSpace()
XMLStreamReaderisWhiteSpace in interface XMLStreamReadertrue if the cursor points to whitespaces;
false otherwise.public int nextTag()
throws XMLStreamException
XMLStreamReader
int eventType = next();
while((eventType == XMLStreamConstants.CHARACTERS && isWhiteSpace()) // skip whitespace
|| (eventType == XMLStreamConstants.CDATA && isWhiteSpace())
// skip whitespace
|| eventType == XMLStreamConstants.SPACE
|| eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
|| eventType == XMLStreamConstants.COMMENT
) {
eventType = next();
}
if (eventType != XMLStreamConstants.START_ELEMENT && eventType != XMLStreamConstants.END_ELEMENT) {
throw new String XMLStreamException("expected start or end tag", getLocation());
}
return eventType;
nextTag in interface XMLStreamReaderXMLStreamException - if the current event is not white space,
PROCESSING_INSTRUCTION, START_ELEMENT or END_ELEMENTCopyright © 2005 - 2007 Javolution.