Class 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 an XMLStreamReader 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.

    • 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 at the 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.
      • depth

        protected int depth
        Depth of the current element. The first element gets depth==0. Also used as the index to scopes.
      • _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​(org.w3c.dom.Node node)
    • Method Detail

      • setCurrentNode

        public void setCurrentNode​(org.w3c.dom.Node node)
      • close

        public void close()
                   throws javax.xml.stream.XMLStreamException
        Specified by:
        close in interface javax.xml.stream.XMLStreamReader
        Throws:
        javax.xml.stream.XMLStreamException
      • splitAttributes

        protected void splitAttributes()
        Called when the current node is Element 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 of splitAttributes().

        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.

      • getAttributeCount

        public int getAttributeCount()
        Specified by:
        getAttributeCount in interface javax.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 interface javax.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 interface javax.xml.stream.XMLStreamReader
      • getAttributeNamespace

        public java.lang.String getAttributeNamespace​(int index)
        Specified by:
        getAttributeNamespace in interface javax.xml.stream.XMLStreamReader
      • getAttributePrefix

        public java.lang.String getAttributePrefix​(int index)
        Specified by:
        getAttributePrefix in interface javax.xml.stream.XMLStreamReader
      • getAttributeType

        public java.lang.String getAttributeType​(int index)
        Specified by:
        getAttributeType in interface javax.xml.stream.XMLStreamReader
      • getAttributeValue

        public java.lang.String getAttributeValue​(int index)
        Specified by:
        getAttributeValue in interface javax.xml.stream.XMLStreamReader
      • getAttributeValue

        public java.lang.String getAttributeValue​(java.lang.String namespaceURI,
                                                  java.lang.String localName)
        Specified by:
        getAttributeValue in interface javax.xml.stream.XMLStreamReader
      • getCharacterEncodingScheme

        public java.lang.String getCharacterEncodingScheme()
        Specified by:
        getCharacterEncodingScheme in interface javax.xml.stream.XMLStreamReader
      • getElementText

        public java.lang.String getElementText()
                                        throws javax.xml.stream.XMLStreamException
        Specified by:
        getElementText in interface javax.xml.stream.XMLStreamReader
        Throws:
        javax.xml.stream.XMLStreamException
      • getEncoding

        public java.lang.String getEncoding()
        Specified by:
        getEncoding in interface javax.xml.stream.XMLStreamReader
      • getEventType

        public int getEventType()
        Specified by:
        getEventType in interface javax.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 interface javax.xml.stream.XMLStreamReader
      • getLocation

        public javax.xml.stream.Location getLocation()
        Specified by:
        getLocation in interface javax.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 interface javax.xml.stream.XMLStreamReader
      • getNamespaceContext

        public javax.xml.namespace.NamespaceContext getNamespaceContext()
        Specified by:
        getNamespaceContext in interface javax.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 interface javax.xml.stream.XMLStreamReader
      • getNamespacePrefix

        public java.lang.String getNamespacePrefix​(int index)
        Specified by:
        getNamespacePrefix in interface javax.xml.stream.XMLStreamReader
      • getNamespaceURI

        public java.lang.String getNamespaceURI​(int index)
        Specified by:
        getNamespaceURI in interface javax.xml.stream.XMLStreamReader
      • getNamespaceURI

        public java.lang.String getNamespaceURI()
        Specified by:
        getNamespaceURI in interface javax.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 interface javax.xml.namespace.NamespaceContext
        Specified by:
        getNamespaceURI in interface javax.xml.stream.XMLStreamReader
      • getPrefix

        public java.lang.String getPrefix​(java.lang.String nsUri)
        Specified by:
        getPrefix in interface javax.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 interface javax.xml.namespace.NamespaceContext
      • getPIData

        public java.lang.String getPIData()
        Specified by:
        getPIData in interface javax.xml.stream.XMLStreamReader
      • getPITarget

        public java.lang.String getPITarget()
        Specified by:
        getPITarget in interface javax.xml.stream.XMLStreamReader
      • getPrefix

        public java.lang.String getPrefix()
        Specified by:
        getPrefix in interface javax.xml.stream.XMLStreamReader
      • getProperty

        public java.lang.Object getProperty​(java.lang.String str)
                                     throws java.lang.IllegalArgumentException
        Specified by:
        getProperty in interface javax.xml.stream.XMLStreamReader
        Throws:
        java.lang.IllegalArgumentException
      • getText

        public java.lang.String getText()
        Specified by:
        getText in interface javax.xml.stream.XMLStreamReader
      • getTextCharacters

        public char[] getTextCharacters()
        Specified by:
        getTextCharacters in interface javax.xml.stream.XMLStreamReader
      • getTextCharacters

        public int getTextCharacters​(int sourceStart,
                                     char[] target,
                                     int targetStart,
                                     int targetLength)
                              throws javax.xml.stream.XMLStreamException
        Specified by:
        getTextCharacters in interface javax.xml.stream.XMLStreamReader
        Throws:
        javax.xml.stream.XMLStreamException
      • getTextLength

        public int getTextLength()
        Specified by:
        getTextLength in interface javax.xml.stream.XMLStreamReader
      • getTextStart

        public int getTextStart()
        Specified by:
        getTextStart in interface javax.xml.stream.XMLStreamReader
      • getVersion

        public java.lang.String getVersion()
        Specified by:
        getVersion in interface javax.xml.stream.XMLStreamReader
      • hasName

        public boolean hasName()
        Specified by:
        hasName in interface javax.xml.stream.XMLStreamReader
      • hasNext

        public boolean hasNext()
                        throws javax.xml.stream.XMLStreamException
        Specified by:
        hasNext in interface javax.xml.stream.XMLStreamReader
        Throws:
        javax.xml.stream.XMLStreamException
      • hasText

        public boolean hasText()
        Specified by:
        hasText in interface javax.xml.stream.XMLStreamReader
      • isAttributeSpecified

        public boolean isAttributeSpecified​(int param)
        Specified by:
        isAttributeSpecified in interface javax.xml.stream.XMLStreamReader
      • isCharacters

        public boolean isCharacters()
        Specified by:
        isCharacters in interface javax.xml.stream.XMLStreamReader
      • isEndElement

        public boolean isEndElement()
        Specified by:
        isEndElement in interface javax.xml.stream.XMLStreamReader
      • isStandalone

        public boolean isStandalone()
        Specified by:
        isStandalone in interface javax.xml.stream.XMLStreamReader
      • isStartElement

        public boolean isStartElement()
        Specified by:
        isStartElement in interface javax.xml.stream.XMLStreamReader
      • isWhiteSpace

        public boolean isWhiteSpace()
        Specified by:
        isWhiteSpace in interface javax.xml.stream.XMLStreamReader
      • mapNodeTypeToState

        private static int mapNodeTypeToState​(int nodetype)
      • next

        public int next()
                 throws javax.xml.stream.XMLStreamException
        Specified by:
        next in interface javax.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 interface javax.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 interface javax.xml.stream.XMLStreamReader
        Throws:
        javax.xml.stream.XMLStreamException
      • standaloneSet

        public boolean standaloneSet()
        Specified by:
        standaloneSet in interface javax.xml.stream.XMLStreamReader
      • fixNull

        private static java.lang.String fixNull​(java.lang.String s)