Class DOMStreamReader
- 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.NamespaceContextCreate anXMLStreamReaderon 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 classDOMStreamReader.ScopeNamespace declarations on one element.
-
Field Summary
Fields Modifier and Type Field Description protected org.w3c.dom.Node_currentCurrent DOM node being traversed.private FinalArrayList<org.w3c.dom.Attr>_currentAttributesList of attributes extracted from_namedNodeMap.private org.w3c.dom.NamedNodeMap_namedNodeMapNamed mapping for attributes and NS decls for the current node.private org.w3c.dom.Node_startStarting node of the subtree being traversed.protected int_stateState of this reader.protected intdepthDepth of the current element.protected DOMStreamReader.Scope[]scopesDOMStreamReader.Scopebuffer.protected java.lang.StringwholeTextIf 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.ScopeallocateScope()Allocate newDOMStreamReader.ScopeforsplitAttributes().voidclose()private voidensureNs(org.w3c.dom.Node n)Sub-routine ofsplitAttributes().private org.w3c.dom.NodefindRootElement()Finds the root element node of the traversal.private static java.lang.StringfixNull(java.lang.String s)intgetAttributeCount()java.lang.StringgetAttributeLocalName(int index)Return an attribute's local name.Handle the case of DOM level 1 nodes.javax.xml.namespace.QNamegetAttributeName(int index)Return an attribute's qname.java.lang.StringgetAttributeNamespace(int index)java.lang.StringgetAttributePrefix(int index)java.lang.StringgetAttributeType(int index)java.lang.StringgetAttributeValue(int index)java.lang.StringgetAttributeValue(java.lang.String namespaceURI, java.lang.String localName)java.lang.StringgetCharacterEncodingScheme()private DOMStreamReader.ScopegetCheckedScope()Verifies the current state to see if we can return the scope, and do so if appropriate.java.lang.StringgetElementText()java.lang.StringgetEncoding()intgetEventType()java.lang.StringgetLocalName()Return an element's local name.Handle the case of DOM level 1 nodes.javax.xml.stream.LocationgetLocation()javax.xml.namespace.QNamegetName()Return an element's qname.javax.xml.namespace.NamespaceContextgetNamespaceContext()intgetNamespaceCount()java.lang.StringgetNamespacePrefix(int index)java.lang.StringgetNamespaceURI()java.lang.StringgetNamespaceURI(int index)java.lang.StringgetNamespaceURI(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.StringgetPIData()java.lang.StringgetPITarget()java.lang.StringgetPrefix()java.lang.StringgetPrefix(java.lang.String nsUri)java.util.Iterator<java.lang.String>getPrefixes(java.lang.String nsUri)private static java.lang.StringgetPrefixForAttr(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.ObjectgetProperty(java.lang.String str)java.lang.StringgetText()char[]getTextCharacters()intgetTextCharacters(int sourceStart, char[] target, int targetStart, int targetLength)intgetTextLength()intgetTextStart()java.lang.StringgetVersion()booleanhasName()booleanhasNext()booleanhasText()booleanisAttributeSpecified(int param)booleanisCharacters()booleanisEndElement()booleanisStandalone()booleanisStartElement()booleanisWhiteSpace()private static intmapNodeTypeToState(int nodetype)intnext()intnextTag()voidrequire(int type, java.lang.String namespaceURI, java.lang.String localName)voidsetCurrentNode(org.w3c.dom.Node node)protected voidsplitAttributes()Called when the current node isElementto look at attribute list (which contains both ns decl and attributes in DOM) and split them to attributes-proper and namespace decls.booleanstandaloneSet()
-
-
-
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.Scopebuffer.
-
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:
closein interfacejavax.xml.stream.XMLStreamReader- Throws:
javax.xml.stream.XMLStreamException
-
splitAttributes
protected void splitAttributes()
Called when the current node isElementto 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.ScopeforsplitAttributes().
-
getAttributeCount
public int getAttributeCount()
- Specified by:
getAttributeCountin 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:
getAttributeLocalNamein 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:
getAttributeNamein interfacejavax.xml.stream.XMLStreamReader
-
getAttributeNamespace
public java.lang.String getAttributeNamespace(int index)
- Specified by:
getAttributeNamespacein interfacejavax.xml.stream.XMLStreamReader
-
getAttributePrefix
public java.lang.String getAttributePrefix(int index)
- Specified by:
getAttributePrefixin interfacejavax.xml.stream.XMLStreamReader
-
getAttributeType
public java.lang.String getAttributeType(int index)
- Specified by:
getAttributeTypein interfacejavax.xml.stream.XMLStreamReader
-
getAttributeValue
public java.lang.String getAttributeValue(int index)
- Specified by:
getAttributeValuein interfacejavax.xml.stream.XMLStreamReader
-
getAttributeValue
public java.lang.String getAttributeValue(java.lang.String namespaceURI, java.lang.String localName)- Specified by:
getAttributeValuein interfacejavax.xml.stream.XMLStreamReader
-
getCharacterEncodingScheme
public java.lang.String getCharacterEncodingScheme()
- Specified by:
getCharacterEncodingSchemein interfacejavax.xml.stream.XMLStreamReader
-
getElementText
public java.lang.String getElementText() throws javax.xml.stream.XMLStreamException- Specified by:
getElementTextin interfacejavax.xml.stream.XMLStreamReader- Throws:
javax.xml.stream.XMLStreamException
-
getEncoding
public java.lang.String getEncoding()
- Specified by:
getEncodingin interfacejavax.xml.stream.XMLStreamReader
-
getEventType
public int getEventType()
- Specified by:
getEventTypein 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:
getLocalNamein interfacejavax.xml.stream.XMLStreamReader
-
getLocation
public javax.xml.stream.Location getLocation()
- Specified by:
getLocationin 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:
getNamein interfacejavax.xml.stream.XMLStreamReader
-
getNamespaceContext
public javax.xml.namespace.NamespaceContext getNamespaceContext()
- Specified by:
getNamespaceContextin 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:
getNamespaceCountin interfacejavax.xml.stream.XMLStreamReader
-
getNamespacePrefix
public java.lang.String getNamespacePrefix(int index)
- Specified by:
getNamespacePrefixin interfacejavax.xml.stream.XMLStreamReader
-
getNamespaceURI
public java.lang.String getNamespaceURI(int index)
- Specified by:
getNamespaceURIin interfacejavax.xml.stream.XMLStreamReader
-
getNamespaceURI
public java.lang.String getNamespaceURI()
- Specified by:
getNamespaceURIin 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:
getNamespaceURIin interfacejavax.xml.namespace.NamespaceContext- Specified by:
getNamespaceURIin interfacejavax.xml.stream.XMLStreamReader
-
getPrefix
public java.lang.String getPrefix(java.lang.String nsUri)
- Specified by:
getPrefixin 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:
getPrefixesin interfacejavax.xml.namespace.NamespaceContext
-
getPIData
public java.lang.String getPIData()
- Specified by:
getPIDatain interfacejavax.xml.stream.XMLStreamReader
-
getPITarget
public java.lang.String getPITarget()
- Specified by:
getPITargetin interfacejavax.xml.stream.XMLStreamReader
-
getPrefix
public java.lang.String getPrefix()
- Specified by:
getPrefixin interfacejavax.xml.stream.XMLStreamReader
-
getProperty
public java.lang.Object getProperty(java.lang.String str) throws java.lang.IllegalArgumentException- Specified by:
getPropertyin interfacejavax.xml.stream.XMLStreamReader- Throws:
java.lang.IllegalArgumentException
-
getText
public java.lang.String getText()
- Specified by:
getTextin interfacejavax.xml.stream.XMLStreamReader
-
getTextCharacters
public char[] getTextCharacters()
- Specified by:
getTextCharactersin interfacejavax.xml.stream.XMLStreamReader
-
getTextCharacters
public int getTextCharacters(int sourceStart, char[] target, int targetStart, int targetLength) throws javax.xml.stream.XMLStreamException- Specified by:
getTextCharactersin interfacejavax.xml.stream.XMLStreamReader- Throws:
javax.xml.stream.XMLStreamException
-
getTextLength
public int getTextLength()
- Specified by:
getTextLengthin interfacejavax.xml.stream.XMLStreamReader
-
getTextStart
public int getTextStart()
- Specified by:
getTextStartin interfacejavax.xml.stream.XMLStreamReader
-
getVersion
public java.lang.String getVersion()
- Specified by:
getVersionin interfacejavax.xml.stream.XMLStreamReader
-
hasName
public boolean hasName()
- Specified by:
hasNamein interfacejavax.xml.stream.XMLStreamReader
-
hasNext
public boolean hasNext() throws javax.xml.stream.XMLStreamException- Specified by:
hasNextin interfacejavax.xml.stream.XMLStreamReader- Throws:
javax.xml.stream.XMLStreamException
-
hasText
public boolean hasText()
- Specified by:
hasTextin interfacejavax.xml.stream.XMLStreamReader
-
isAttributeSpecified
public boolean isAttributeSpecified(int param)
- Specified by:
isAttributeSpecifiedin interfacejavax.xml.stream.XMLStreamReader
-
isCharacters
public boolean isCharacters()
- Specified by:
isCharactersin interfacejavax.xml.stream.XMLStreamReader
-
isEndElement
public boolean isEndElement()
- Specified by:
isEndElementin interfacejavax.xml.stream.XMLStreamReader
-
isStandalone
public boolean isStandalone()
- Specified by:
isStandalonein interfacejavax.xml.stream.XMLStreamReader
-
isStartElement
public boolean isStartElement()
- Specified by:
isStartElementin interfacejavax.xml.stream.XMLStreamReader
-
isWhiteSpace
public boolean isWhiteSpace()
- Specified by:
isWhiteSpacein interfacejavax.xml.stream.XMLStreamReader
-
mapNodeTypeToState
private static int mapNodeTypeToState(int nodetype)
-
next
public int next() throws javax.xml.stream.XMLStreamException- Specified by:
nextin 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:
nextTagin 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:
requirein interfacejavax.xml.stream.XMLStreamReader- Throws:
javax.xml.stream.XMLStreamException
-
standaloneSet
public boolean standaloneSet()
- Specified by:
standaloneSetin interfacejavax.xml.stream.XMLStreamReader
-
fixNull
private static java.lang.String fixNull(java.lang.String s)
-
-