- java.lang.Object
-
- org.jvnet.staxex.util.DOMStreamReader
-
- All Implemented Interfaces:
NamespaceContext
,XMLStreamConstants
,XMLStreamReader
public class DOMStreamReader extends Object implements XMLStreamReader, NamespaceContext
Create anXMLStreamReader
on top of a DOM tree.Since various libraries as well as users often create "incorrect" DOM node, this class spends a lot of efforts making sure that broken DOM trees are nevertheless interpreted correctly.
For example, if a DOM level 1 tree is passed, each method will attempt to return the correct value by using
Node.getNodeName()
.Similarly, if DOM is missing explicit namespace declarations, this class attempts to emulate necessary declarations.
- Author:
- [email protected], Kohsuke Kawaguchi
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
DOMStreamReader.Scope
Namespace declarations on one element.
-
Field Summary
Fields Modifier and Type Field Description protected Node
_current
Current DOM node being traversed.protected int
_state
State of this reader.protected int
depth
Depth of the current element.protected DOMStreamReader.Scope[]
scopes
DOMStreamReader.Scope
buffer.protected String
wholeText
If the reader points atthe text node
, its whole value.-
Fields inherited from interface javax.xml.stream.XMLStreamConstants
ATTRIBUTE, CDATA, CHARACTERS, COMMENT, DTD, END_DOCUMENT, END_ELEMENT, ENTITY_DECLARATION, ENTITY_REFERENCE, NAMESPACE, NOTATION_DECLARATION, PROCESSING_INSTRUCTION, SPACE, START_DOCUMENT, START_ELEMENT
-
-
Constructor Summary
Constructors Constructor Description DOMStreamReader()
DOMStreamReader(Node node)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected int
_next()
void
close()
int
getAttributeCount()
String
getAttributeLocalName(int index)
Return an attribute's local name.QName
getAttributeName(int index)
Return an attribute's qname.String
getAttributeNamespace(int index)
String
getAttributePrefix(int index)
String
getAttributeType(int index)
String
getAttributeValue(int index)
String
getAttributeValue(String namespaceURI, String localName)
String
getCharacterEncodingScheme()
String
getElementText()
String
getEncoding()
int
getEventType()
String
getLocalName()
Return an element's local name.Location
getLocation()
QName
getName()
Return an element's qname.NamespaceContext
getNamespaceContext()
int
getNamespaceCount()
String
getNamespacePrefix(int index)
String
getNamespaceURI()
String
getNamespaceURI(int index)
String
getNamespaceURI(String prefix)
This method is not particularly fast, but shouldn't be called very often.String
getPIData()
String
getPITarget()
String
getPrefix()
String
getPrefix(String nsUri)
Iterator
getPrefixes(String nsUri)
Object
getProperty(String str)
String
getText()
char[]
getTextCharacters()
int
getTextCharacters(int sourceStart, char[] target, int targetStart, int targetLength)
int
getTextLength()
int
getTextStart()
String
getVersion()
boolean
hasName()
boolean
hasNext()
boolean
hasText()
boolean
isAttributeSpecified(int param)
boolean
isCharacters()
boolean
isEndElement()
boolean
isStandalone()
boolean
isStartElement()
boolean
isWhiteSpace()
int
next()
int
nextTag()
void
require(int type, String namespaceURI, String localName)
void
setCurrentNode(Node node)
protected void
splitAttributes()
Called when the current node isElement
to look at attribute list (which contains both ns decl and attributes in DOM) and split them to attributes-proper and namespace decls.boolean
standaloneSet()
-
-
-
Field Detail
-
_current
protected Node _current
Current DOM node being traversed.
-
wholeText
protected String wholeText
If the reader points atthe text node
, its whole value.This is simply a cache of
Text.getWholeText()
of_current
, but when a large binary data sent as base64 text, this could get very much non-trivial.
-
scopes
protected DOMStreamReader.Scope[] scopes
DOMStreamReader.Scope
buffer.
-
depth
protected int depth
Depth of the current element. The first element gets depth==0. Also used as the index toscopes
.
-
_state
protected int _state
State of this reader. Any of the valid states defined in StAX' XMLStreamConstants class.
-
-
Constructor Detail
-
DOMStreamReader
public DOMStreamReader()
-
DOMStreamReader
public DOMStreamReader(Node node)
-
-
Method Detail
-
setCurrentNode
public void setCurrentNode(Node node)
-
close
public void close() throws XMLStreamException
- Specified by:
close
in interfaceXMLStreamReader
- Throws:
XMLStreamException
-
splitAttributes
protected void splitAttributes()
Called when the current node isElement
to look at attribute list (which contains both ns decl and attributes in DOM) and split them to attributes-proper and namespace decls.
-
getAttributeCount
public int getAttributeCount()
- Specified by:
getAttributeCount
in interfaceXMLStreamReader
-
getAttributeLocalName
public String getAttributeLocalName(int index)
Return an attribute's local name. Handle the case of DOM level 1 nodes.- Specified by:
getAttributeLocalName
in interfaceXMLStreamReader
-
getAttributeName
public QName getAttributeName(int index)
Return an attribute's qname. Handle the case of DOM level 1 nodes.- Specified by:
getAttributeName
in interfaceXMLStreamReader
-
getAttributeNamespace
public String getAttributeNamespace(int index)
- Specified by:
getAttributeNamespace
in interfaceXMLStreamReader
-
getAttributePrefix
public String getAttributePrefix(int index)
- Specified by:
getAttributePrefix
in interfaceXMLStreamReader
-
getAttributeType
public String getAttributeType(int index)
- Specified by:
getAttributeType
in interfaceXMLStreamReader
-
getAttributeValue
public String getAttributeValue(int index)
- Specified by:
getAttributeValue
in interfaceXMLStreamReader
-
getAttributeValue
public String getAttributeValue(String namespaceURI, String localName)
- Specified by:
getAttributeValue
in interfaceXMLStreamReader
-
getCharacterEncodingScheme
public String getCharacterEncodingScheme()
- Specified by:
getCharacterEncodingScheme
in interfaceXMLStreamReader
-
getElementText
public String getElementText() throws XMLStreamException
- Specified by:
getElementText
in interfaceXMLStreamReader
- Throws:
XMLStreamException
-
getEncoding
public String getEncoding()
- Specified by:
getEncoding
in interfaceXMLStreamReader
-
getEventType
public int getEventType()
- Specified by:
getEventType
in interfaceXMLStreamReader
-
getLocalName
public String getLocalName()
Return an element's local name. Handle the case of DOM level 1 nodes.- Specified by:
getLocalName
in interfaceXMLStreamReader
-
getLocation
public Location getLocation()
- Specified by:
getLocation
in interfaceXMLStreamReader
-
getName
public QName getName()
Return an element's qname. Handle the case of DOM level 1 nodes.- Specified by:
getName
in interfaceXMLStreamReader
-
getNamespaceContext
public NamespaceContext getNamespaceContext()
- Specified by:
getNamespaceContext
in interfaceXMLStreamReader
-
getNamespaceCount
public int getNamespaceCount()
- Specified by:
getNamespaceCount
in interfaceXMLStreamReader
-
getNamespacePrefix
public String getNamespacePrefix(int index)
- Specified by:
getNamespacePrefix
in interfaceXMLStreamReader
-
getNamespaceURI
public String getNamespaceURI(int index)
- Specified by:
getNamespaceURI
in interfaceXMLStreamReader
-
getNamespaceURI
public String getNamespaceURI()
- Specified by:
getNamespaceURI
in interfaceXMLStreamReader
-
getNamespaceURI
public String getNamespaceURI(String prefix)
This method is not particularly fast, but shouldn't be called very often. If we start to use it more, we should keep track of the NS declarations using a NamespaceContext implementation instead.- Specified by:
getNamespaceURI
in interfaceNamespaceContext
- Specified by:
getNamespaceURI
in interfaceXMLStreamReader
-
getPrefix
public String getPrefix(String nsUri)
- Specified by:
getPrefix
in interfaceNamespaceContext
-
getPrefixes
public Iterator getPrefixes(String nsUri)
- Specified by:
getPrefixes
in interfaceNamespaceContext
-
getPIData
public String getPIData()
- Specified by:
getPIData
in interfaceXMLStreamReader
-
getPITarget
public String getPITarget()
- Specified by:
getPITarget
in interfaceXMLStreamReader
-
getPrefix
public String getPrefix()
- Specified by:
getPrefix
in interfaceXMLStreamReader
-
getProperty
public Object getProperty(String str) throws IllegalArgumentException
- Specified by:
getProperty
in interfaceXMLStreamReader
- Throws:
IllegalArgumentException
-
getText
public String getText()
- Specified by:
getText
in interfaceXMLStreamReader
-
getTextCharacters
public char[] getTextCharacters()
- Specified by:
getTextCharacters
in interfaceXMLStreamReader
-
getTextCharacters
public int getTextCharacters(int sourceStart, char[] target, int targetStart, int targetLength) throws XMLStreamException
- Specified by:
getTextCharacters
in interfaceXMLStreamReader
- Throws:
XMLStreamException
-
getTextLength
public int getTextLength()
- Specified by:
getTextLength
in interfaceXMLStreamReader
-
getTextStart
public int getTextStart()
- Specified by:
getTextStart
in interfaceXMLStreamReader
-
getVersion
public String getVersion()
- Specified by:
getVersion
in interfaceXMLStreamReader
-
hasName
public boolean hasName()
- Specified by:
hasName
in interfaceXMLStreamReader
-
hasNext
public boolean hasNext() throws XMLStreamException
- Specified by:
hasNext
in interfaceXMLStreamReader
- Throws:
XMLStreamException
-
hasText
public boolean hasText()
- Specified by:
hasText
in interfaceXMLStreamReader
-
isAttributeSpecified
public boolean isAttributeSpecified(int param)
- Specified by:
isAttributeSpecified
in interfaceXMLStreamReader
-
isCharacters
public boolean isCharacters()
- Specified by:
isCharacters
in interfaceXMLStreamReader
-
isEndElement
public boolean isEndElement()
- Specified by:
isEndElement
in interfaceXMLStreamReader
-
isStandalone
public boolean isStandalone()
- Specified by:
isStandalone
in interfaceXMLStreamReader
-
isStartElement
public boolean isStartElement()
- Specified by:
isStartElement
in interfaceXMLStreamReader
-
isWhiteSpace
public boolean isWhiteSpace()
- Specified by:
isWhiteSpace
in interfaceXMLStreamReader
-
next
public int next() throws XMLStreamException
- Specified by:
next
in interfaceXMLStreamReader
- Throws:
XMLStreamException
-
_next
protected int _next() throws XMLStreamException
- Throws:
XMLStreamException
-
nextTag
public int nextTag() throws XMLStreamException
- Specified by:
nextTag
in interfaceXMLStreamReader
- Throws:
XMLStreamException
-
require
public void require(int type, String namespaceURI, String localName) throws XMLStreamException
- Specified by:
require
in interfaceXMLStreamReader
- Throws:
XMLStreamException
-
standaloneSet
public boolean standaloneSet()
- Specified by:
standaloneSet
in interfaceXMLStreamReader
-
-