- java.lang.Object
-
- org.jvnet.staxex.util.DOMStreamReader
-
- All Implemented Interfaces:
javax.xml.namespace.NamespaceContext
,javax.xml.stream.XMLStreamConstants
,javax.xml.stream.XMLStreamReader
public class DOMStreamReader extends java.lang.Object implements javax.xml.stream.XMLStreamReader, javax.xml.namespace.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.
-
-
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 org.w3c.dom.Node
_current
Current DOM node being traversed.private FinalArrayList<org.w3c.dom.Attr>
_currentAttributes
List of attributes extracted from_namedNodeMap
.private org.w3c.dom.NamedNodeMap
_namedNodeMap
Named mapping for attributes and NS decls for the current node.private org.w3c.dom.Node
_start
Starting node of the subtree 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 java.lang.String
wholeText
If the reader points atthe text node
, its whole value.
-
Constructor Summary
Constructors Constructor Description DOMStreamReader()
DOMStreamReader(org.w3c.dom.Node node)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected int
_next()
private DOMStreamReader.Scope
allocateScope()
Allocate newDOMStreamReader.Scope
forsplitAttributes()
.void
close()
private void
ensureNs(org.w3c.dom.Node n)
Sub-routine ofsplitAttributes()
.private org.w3c.dom.Node
findRootElement()
Finds the root element node of the traversal.private static java.lang.String
fixNull(java.lang.String s)
int
getAttributeCount()
java.lang.String
getAttributeLocalName(int index)
Return an attribute's local name.Handle the case of DOM level 1 nodes.javax.xml.namespace.QName
getAttributeName(int index)
Return an attribute's qname.java.lang.String
getAttributeNamespace(int index)
java.lang.String
getAttributePrefix(int index)
java.lang.String
getAttributeType(int index)
java.lang.String
getAttributeValue(int index)
java.lang.String
getAttributeValue(java.lang.String namespaceURI, java.lang.String localName)
java.lang.String
getCharacterEncodingScheme()
private DOMStreamReader.Scope
getCheckedScope()
Verifies the current state to see if we can return the scope, and do so if appropriate.java.lang.String
getElementText()
java.lang.String
getEncoding()
int
getEventType()
java.lang.String
getLocalName()
Return an element's local name.Handle the case of DOM level 1 nodes.javax.xml.stream.Location
getLocation()
javax.xml.namespace.QName
getName()
Return an element's qname.javax.xml.namespace.NamespaceContext
getNamespaceContext()
int
getNamespaceCount()
java.lang.String
getNamespacePrefix(int index)
java.lang.String
getNamespaceURI()
java.lang.String
getNamespaceURI(int index)
java.lang.String
getNamespaceURI(java.lang.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.java.lang.String
getPIData()
java.lang.String
getPITarget()
java.lang.String
getPrefix()
java.lang.String
getPrefix(java.lang.String nsUri)
java.util.Iterator<java.lang.String>
getPrefixes(java.lang.String nsUri)
private static java.lang.String
getPrefixForAttr(org.w3c.dom.Attr attr, java.lang.String nsUri)
If the given attribute is a namespace declaration for the given namespace URI, return its prefix.java.lang.Object
getProperty(java.lang.String str)
java.lang.String
getText()
char[]
getTextCharacters()
int
getTextCharacters(int sourceStart, char[] target, int targetStart, int targetLength)
int
getTextLength()
int
getTextStart()
java.lang.String
getVersion()
boolean
hasName()
boolean
hasNext()
boolean
hasText()
boolean
isAttributeSpecified(int param)
boolean
isCharacters()
boolean
isEndElement()
boolean
isStandalone()
boolean
isStartElement()
boolean
isWhiteSpace()
private static int
mapNodeTypeToState(int nodetype)
int
next()
int
nextTag()
void
require(int type, java.lang.String namespaceURI, java.lang.String localName)
void
setCurrentNode(org.w3c.dom.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 org.w3c.dom.Node _current
Current DOM node being traversed.
-
_start
private org.w3c.dom.Node _start
Starting node of the subtree being traversed.
-
_namedNodeMap
private org.w3c.dom.NamedNodeMap _namedNodeMap
Named mapping for attributes and NS decls for the current node.
-
wholeText
protected java.lang.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.
-
_currentAttributes
private final FinalArrayList<org.w3c.dom.Attr> _currentAttributes
List of attributes extracted from_namedNodeMap
.
-
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.
-
-
Method Detail
-
setCurrentNode
public void setCurrentNode(org.w3c.dom.Node node)
-
close
public void close() throws javax.xml.stream.XMLStreamException
- Specified by:
close
in interfacejavax.xml.stream.XMLStreamReader
- Throws:
javax.xml.stream.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.
-
ensureNs
private void ensureNs(org.w3c.dom.Node n)
Sub-routine ofsplitAttributes()
.Makes sure that the namespace URI/prefix used in the given node is available, and if not, declare it on the current scope to "fix" it. It's often common to create DOM trees without putting namespace declarations, and this makes sure that such DOM tree will be properly marshalled.
-
allocateScope
private DOMStreamReader.Scope allocateScope()
Allocate newDOMStreamReader.Scope
forsplitAttributes()
.
-
getAttributeCount
public int getAttributeCount()
- Specified by:
getAttributeCount
in interfacejavax.xml.stream.XMLStreamReader
-
getAttributeLocalName
public java.lang.String getAttributeLocalName(int index)
Return an attribute's local name.Handle the case of DOM level 1 nodes.- Specified by:
getAttributeLocalName
in interfacejavax.xml.stream.XMLStreamReader
-
getAttributeName
public javax.xml.namespace.QName getAttributeName(int index)
Return an attribute's qname. Handle the case of DOM level 1 nodes.- Specified by:
getAttributeName
in interfacejavax.xml.stream.XMLStreamReader
-
getAttributeNamespace
public java.lang.String getAttributeNamespace(int index)
- Specified by:
getAttributeNamespace
in interfacejavax.xml.stream.XMLStreamReader
-
getAttributePrefix
public java.lang.String getAttributePrefix(int index)
- Specified by:
getAttributePrefix
in interfacejavax.xml.stream.XMLStreamReader
-
getAttributeType
public java.lang.String getAttributeType(int index)
- Specified by:
getAttributeType
in interfacejavax.xml.stream.XMLStreamReader
-
getAttributeValue
public java.lang.String getAttributeValue(int index)
- Specified by:
getAttributeValue
in interfacejavax.xml.stream.XMLStreamReader
-
getAttributeValue
public java.lang.String getAttributeValue(java.lang.String namespaceURI, java.lang.String localName)
- Specified by:
getAttributeValue
in interfacejavax.xml.stream.XMLStreamReader
-
getCharacterEncodingScheme
public java.lang.String getCharacterEncodingScheme()
- Specified by:
getCharacterEncodingScheme
in interfacejavax.xml.stream.XMLStreamReader
-
getElementText
public java.lang.String getElementText() throws javax.xml.stream.XMLStreamException
- Specified by:
getElementText
in interfacejavax.xml.stream.XMLStreamReader
- Throws:
javax.xml.stream.XMLStreamException
-
getEncoding
public java.lang.String getEncoding()
- Specified by:
getEncoding
in interfacejavax.xml.stream.XMLStreamReader
-
getEventType
public int getEventType()
- Specified by:
getEventType
in interfacejavax.xml.stream.XMLStreamReader
-
getLocalName
public java.lang.String getLocalName()
Return an element's local name.Handle the case of DOM level 1 nodes.- Specified by:
getLocalName
in interfacejavax.xml.stream.XMLStreamReader
-
getLocation
public javax.xml.stream.Location getLocation()
- Specified by:
getLocation
in interfacejavax.xml.stream.XMLStreamReader
-
getName
public javax.xml.namespace.QName getName()
Return an element's qname. Handle the case of DOM level 1 nodes.- Specified by:
getName
in interfacejavax.xml.stream.XMLStreamReader
-
getNamespaceContext
public javax.xml.namespace.NamespaceContext getNamespaceContext()
- Specified by:
getNamespaceContext
in interfacejavax.xml.stream.XMLStreamReader
-
getCheckedScope
private DOMStreamReader.Scope getCheckedScope()
Verifies the current state to see if we can return the scope, and do so if appropriate. Used to implement a bunch of StAX API methods that have the same usage restriction.
-
getNamespaceCount
public int getNamespaceCount()
- Specified by:
getNamespaceCount
in interfacejavax.xml.stream.XMLStreamReader
-
getNamespacePrefix
public java.lang.String getNamespacePrefix(int index)
- Specified by:
getNamespacePrefix
in interfacejavax.xml.stream.XMLStreamReader
-
getNamespaceURI
public java.lang.String getNamespaceURI(int index)
- Specified by:
getNamespaceURI
in interfacejavax.xml.stream.XMLStreamReader
-
getNamespaceURI
public java.lang.String getNamespaceURI()
- Specified by:
getNamespaceURI
in interfacejavax.xml.stream.XMLStreamReader
-
getNamespaceURI
public java.lang.String getNamespaceURI(java.lang.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 interfacejavax.xml.namespace.NamespaceContext
- Specified by:
getNamespaceURI
in interfacejavax.xml.stream.XMLStreamReader
-
getPrefix
public java.lang.String getPrefix(java.lang.String nsUri)
- Specified by:
getPrefix
in interfacejavax.xml.namespace.NamespaceContext
-
findRootElement
private org.w3c.dom.Node findRootElement()
Finds the root element node of the traversal.
-
getPrefixForAttr
private static java.lang.String getPrefixForAttr(org.w3c.dom.Attr attr, java.lang.String nsUri)
If the given attribute is a namespace declaration for the given namespace URI, return its prefix. Otherwise null.
-
getPrefixes
public java.util.Iterator<java.lang.String> getPrefixes(java.lang.String nsUri)
- Specified by:
getPrefixes
in interfacejavax.xml.namespace.NamespaceContext
-
getPIData
public java.lang.String getPIData()
- Specified by:
getPIData
in interfacejavax.xml.stream.XMLStreamReader
-
getPITarget
public java.lang.String getPITarget()
- Specified by:
getPITarget
in interfacejavax.xml.stream.XMLStreamReader
-
getPrefix
public java.lang.String getPrefix()
- Specified by:
getPrefix
in interfacejavax.xml.stream.XMLStreamReader
-
getProperty
public java.lang.Object getProperty(java.lang.String str) throws java.lang.IllegalArgumentException
- Specified by:
getProperty
in interfacejavax.xml.stream.XMLStreamReader
- Throws:
java.lang.IllegalArgumentException
-
getText
public java.lang.String getText()
- Specified by:
getText
in interfacejavax.xml.stream.XMLStreamReader
-
getTextCharacters
public char[] getTextCharacters()
- Specified by:
getTextCharacters
in interfacejavax.xml.stream.XMLStreamReader
-
getTextCharacters
public int getTextCharacters(int sourceStart, char[] target, int targetStart, int targetLength) throws javax.xml.stream.XMLStreamException
- Specified by:
getTextCharacters
in interfacejavax.xml.stream.XMLStreamReader
- Throws:
javax.xml.stream.XMLStreamException
-
getTextLength
public int getTextLength()
- Specified by:
getTextLength
in interfacejavax.xml.stream.XMLStreamReader
-
getTextStart
public int getTextStart()
- Specified by:
getTextStart
in interfacejavax.xml.stream.XMLStreamReader
-
getVersion
public java.lang.String getVersion()
- Specified by:
getVersion
in interfacejavax.xml.stream.XMLStreamReader
-
hasName
public boolean hasName()
- Specified by:
hasName
in interfacejavax.xml.stream.XMLStreamReader
-
hasNext
public boolean hasNext() throws javax.xml.stream.XMLStreamException
- Specified by:
hasNext
in interfacejavax.xml.stream.XMLStreamReader
- Throws:
javax.xml.stream.XMLStreamException
-
hasText
public boolean hasText()
- Specified by:
hasText
in interfacejavax.xml.stream.XMLStreamReader
-
isAttributeSpecified
public boolean isAttributeSpecified(int param)
- Specified by:
isAttributeSpecified
in interfacejavax.xml.stream.XMLStreamReader
-
isCharacters
public boolean isCharacters()
- Specified by:
isCharacters
in interfacejavax.xml.stream.XMLStreamReader
-
isEndElement
public boolean isEndElement()
- Specified by:
isEndElement
in interfacejavax.xml.stream.XMLStreamReader
-
isStandalone
public boolean isStandalone()
- Specified by:
isStandalone
in interfacejavax.xml.stream.XMLStreamReader
-
isStartElement
public boolean isStartElement()
- Specified by:
isStartElement
in interfacejavax.xml.stream.XMLStreamReader
-
isWhiteSpace
public boolean isWhiteSpace()
- Specified by:
isWhiteSpace
in interfacejavax.xml.stream.XMLStreamReader
-
mapNodeTypeToState
private static int mapNodeTypeToState(int nodetype)
-
next
public int next() throws javax.xml.stream.XMLStreamException
- Specified by:
next
in interfacejavax.xml.stream.XMLStreamReader
- Throws:
javax.xml.stream.XMLStreamException
-
_next
protected int _next() throws javax.xml.stream.XMLStreamException
- Throws:
javax.xml.stream.XMLStreamException
-
nextTag
public int nextTag() throws javax.xml.stream.XMLStreamException
- Specified by:
nextTag
in interfacejavax.xml.stream.XMLStreamReader
- Throws:
javax.xml.stream.XMLStreamException
-
require
public void require(int type, java.lang.String namespaceURI, java.lang.String localName) throws javax.xml.stream.XMLStreamException
- Specified by:
require
in interfacejavax.xml.stream.XMLStreamReader
- Throws:
javax.xml.stream.XMLStreamException
-
standaloneSet
public boolean standaloneSet()
- Specified by:
standaloneSet
in interfacejavax.xml.stream.XMLStreamReader
-
fixNull
private static java.lang.String fixNull(java.lang.String s)
-
-