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.XMLStreamException
public void setInput(java.io.InputStream in, java.lang.String encoding) throws XMLStreamException
in
- the input source.encoding
- the associated encoding.XMLStreamException
public void setInput(java.io.Reader reader) throws XMLStreamException
reader
- the input source reader.XMLStreamException
UTF8StreamReader
,
UTF8ByteBufferReader
,
CharSequenceReader
public 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.Object
public 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 XMLStreamReader
XMLStreamException
- if there is an error processing the
underlying XML sourcepublic void reset()
Reusable
public void require(int type, java.lang.CharSequence namespaceURI, java.lang.CharSequence localName) throws XMLStreamException
XMLStreamReader
require
in interface XMLStreamReader
type
- 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 XMLStreamReader
XMLStreamException
- 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
XMLStreamReader
getProperty
in interface XMLStreamReader
name
- the name of the property.java.lang.IllegalArgumentException
public void close() throws XMLStreamException
XMLStreamReader
close
in interface XMLStreamReader
XMLStreamException
- if there are errors freeing associated
resourcespublic int getAttributeCount()
XMLStreamReader
getAttributeCount
in interface XMLStreamReader
public CharArray getAttributeLocalName(int index)
XMLStreamReader
getAttributeLocalName
in interface XMLStreamReader
index
- the position of the attribute.public CharArray getAttributeNamespace(int index)
XMLStreamReader
getAttributeNamespace
in interface XMLStreamReader
index
- the position of the attribute.null
if no prefix.public CharArray getAttributePrefix(int index)
XMLStreamReader
getAttributePrefix
in interface XMLStreamReader
index
- the position of the attribute.null
if no prefix.public CharArray getAttributeType(int index)
XMLStreamReader
getAttributeType
in interface XMLStreamReader
index
- the position of the attributepublic CharArray getAttributeValue(java.lang.CharSequence uri, java.lang.CharSequence localName)
XMLStreamReader
getAttributeValue
in interface XMLStreamReader
uri
- the namespace of the attribute or null
.localName
- the local name of the attribute.null
.public CharArray getAttributeValue(int index)
XMLStreamReader
getAttributeValue
in interface XMLStreamReader
index
- the position of the attribute.public CharArray getCharacterEncodingScheme()
XMLStreamReader
getCharacterEncodingScheme
in interface XMLStreamReader
null
public java.lang.String getEncoding()
XMLStreamReader
null
if unknown.getEncoding
in interface XMLStreamReader
public int getEventType()
XMLStreamReader
getEventType
in interface XMLStreamReader
public CharArray getLocalName()
XMLStreamReader
getLocalName
in interface XMLStreamReader
public Location getLocation()
XMLStreamReader
getLocation
in interface XMLStreamReader
public int getNamespaceCount()
XMLStreamReader
getNamespaceCount
in interface XMLStreamReader
public CharArray getNamespacePrefix(int index)
XMLStreamReader
getNamespacePrefix
in interface XMLStreamReader
index
- the position of the namespace declaration.null
if no prefix.public CharArray getNamespaceURI(java.lang.CharSequence prefix)
XMLStreamReader
NOTE: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 XMLStreamReader
prefix
- the prefix to lookup.null
if it is
not boundpublic CharArray getNamespaceURI(int index)
XMLStreamReader
getNamespaceURI
in interface XMLStreamReader
index
- the position of the namespace declaration.null
if no prefix.public NamespaceContext getNamespaceContext()
XMLStreamReader
getNamespaceContext
in interface XMLStreamReader
public CharArray getNamespaceURI()
XMLStreamReader
null
).getNamespaceURI
in interface XMLStreamReader
null
.public CharArray getPrefix()
XMLStreamReader
getPrefix
in interface XMLStreamReader
null
public CharArray getPIData()
XMLStreamReader
getPIData
in interface XMLStreamReader
null
if the processing instruction only has target.public CharArray getPITarget()
XMLStreamReader
getPITarget
in interface XMLStreamReader
public CharArray getText()
XMLStreamReader
getText
in interface XMLStreamReader
null
public char[] getTextCharacters()
XMLStreamReader
getTextCharacters
in interface XMLStreamReader
public 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 XMLStreamReader
sourceStart
- 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()
XMLStreamReader
getTextLength
in interface XMLStreamReader
public int getTextStart()
XMLStreamReader
getTextStart
in interface XMLStreamReader
public CharArray getVersion()
XMLStreamReader
getVersion
in interface XMLStreamReader
null
public boolean isStandalone()
XMLStreamReader
isStandalone
in interface XMLStreamReader
true
if this is standalone;
false
otherwise.public boolean standaloneSet()
XMLStreamReader
standaloneSet
in interface XMLStreamReader
true
if standalone was set;
false
otherwise.public boolean hasName()
XMLStreamReader
hasName
in interface XMLStreamReader
true
if the current event has a name;
false
otherwise.public boolean hasNext() throws XMLStreamException
XMLStreamReader
hasNext
in interface XMLStreamReader
XMLStreamException
- if there is a fatal error detecting the next
state.public boolean hasText()
XMLStreamReader
hasText
in interface XMLStreamReader
true
if the current event as text;
false
otherwise.public boolean isAttributeSpecified(int index)
XMLStreamReader
isAttributeSpecified
in interface XMLStreamReader
index
- the position of the attribute.true
if this is a default attribute;
false
otherwise.public boolean isCharacters()
XMLStreamReader
isCharacters
in interface XMLStreamReader
true
if the cursor points to character data;
false
otherwise.public boolean isEndElement()
XMLStreamReader
isEndElement
in interface XMLStreamReader
true
if the cursor points to a end tag;
false
otherwise.public boolean isStartElement()
XMLStreamReader
isStartElement
in interface XMLStreamReader
true
if the cursor points to a start tag;
false
otherwise.public boolean isWhiteSpace()
XMLStreamReader
isWhiteSpace
in interface XMLStreamReader
true
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 XMLStreamReader
XMLStreamException
- if the current event is not white space,
PROCESSING_INSTRUCTION, START_ELEMENT or END_ELEMENTCopyright © 2005 - 2007 Javolution.