Class DOM2DTM

  • All Implemented Interfaces:
    DTM

    public class DOM2DTM
    extends DTMDefaultBaseIterators
    The DOM2DTM class serves up a DOM's contents via the DTM API.

    Note that it doesn't necessarily represent a full Document tree. You can wrap a DOM2DTM around a specific node and its subtree and the right things should happen. (I don't _think_ we currently support DocumentFrgment nodes as roots, though that might be worth considering.)

    Note too that we do not currently attempt to track document mutation. If you alter the DOM after wrapping DOM2DTM around it, all bets are off.

    • Field Detail

      • NAMESPACE_DECL_NS

        static final java.lang.String NAMESPACE_DECL_NS
        Manefest constant
        See Also:
        Constant Field Values
      • m_pos

        private transient org.w3c.dom.Node m_pos
        The current position in the DOM tree. Last node examined for possible copying to DTM.
      • m_last_parent

        private int m_last_parent
        The current position in the DTM tree. Who children get appended to.
      • m_last_kid

        private int m_last_kid
        The current position in the DTM tree. Who children reference as their previous sib.
      • m_root

        private final transient org.w3c.dom.Node m_root
        The top of the subtree. %REVIEW%: 'may not be the same as m_context if "//foo" pattern.'
      • m_processedFirstElement

        boolean m_processedFirstElement
        True iff the first element has been processed. This is used to control synthesis of the implied xml: namespace declaration node.
      • m_nodesAreProcessed

        private transient boolean m_nodesAreProcessed
        true if ALL the nodes in the m_root subtree have been processed; false if our incremental build has not yet finished scanning the DOM tree.
      • m_nodes

        protected final java.util.Vector<org.w3c.dom.Node> m_nodes
        The node objects. The instance part of the handle indexes directly into this vector. Each DTM node may actually be composed of several DOM nodes (for example, if logically-adjacent Text/CDATASection nodes in the DOM have been coalesced into a single DTM Text node); this table points only to the first in that sequence.
    • Constructor Detail

      • DOM2DTM

        public DOM2DTM​(DTMManager mgr,
                       javax.xml.transform.dom.DOMSource domSource,
                       int dtmIdentity,
                       boolean doIndexing)
        Construct a DOM2DTM object from a DOM node.
        Parameters:
        mgr - The DTMManager who owns this DTM.
        domSource - the DOM source that this DTM will wrap.
        dtmIdentity - The DTM identity ID for this DTM.
        doIndexing - true if the caller considers it worth it to use indexing schemes.
    • Method Detail

      • addNode

        protected int addNode​(org.w3c.dom.Node node,
                              int parentIndex,
                              int previousSibling,
                              int forceNodeType)
        Construct the node map from the node.
        Parameters:
        node - The node that is to be added to the DTM.
        parentIndex - The current parent index.
        previousSibling - The previous sibling index.
        forceNodeType - If not DTM.NULL, overrides the DOM node type. Used to force nodes to Text rather than CDATASection when their coalesced value includes ordinary Text nodes (current DTM behavior).
        Returns:
        The index identity of the node that was added.
      • nextNode

        protected boolean nextNode()
        This method should try and build one or more nodes in the table.
        Specified by:
        nextNode in class DTMDefaultBase
        Returns:
        The true if a next node is found or false if there are no more nodes.
      • getNode

        public org.w3c.dom.Node getNode​(int nodeHandle)
        Return an DOM node for the given node.
        Specified by:
        getNode in interface DTM
        Overrides:
        getNode in class DTMDefaultBase
        Parameters:
        nodeHandle - The node ID.
        Returns:
        A node representation of the DTM node.
      • lookupNode

        protected org.w3c.dom.Node lookupNode​(int nodeIdentity)
        Get a Node from an identity index.

        NEEDSDOC @param nodeIdentity

        NEEDSDOC ($objectName$) @return

      • getNextNodeIdentity

        protected int getNextNodeIdentity​(int identity)
        Get the next node identity value in the list, and call the iterator if it hasn't been added yet.
        Specified by:
        getNextNodeIdentity in class DTMDefaultBase
        Parameters:
        identity - The node identity (index).
        Returns:
        identity+1, or DTM.NULL.
      • getHandleFromNode

        private int getHandleFromNode​(org.w3c.dom.Node node)
        Get the handle from a Node.

        %OPT% This will be pretty slow.

        %OPT% An XPath-like search (walk up DOM to root, tracking path; walk down DTM reconstructing path) might be considerably faster on later nodes in large documents. That might also imply improving this call to handle nodes which would be in this DTM but have not yet been built, which might or might not be a Good Thing. %REVIEW% This relies on being able to test node-identity via object-identity. DTM2DOM proxying is a great example of a case where that doesn't work. DOM Level 3 will provide the isSameNode() method to fix that, but until then this is going to be flaky.

        Parameters:
        node - A node, which may be null.
        Returns:
        The node handle or DTM.NULL.
      • getHandleOfNode

        public int getHandleOfNode​(org.w3c.dom.Node node)
        Get the handle from a Node. This is a more robust version of getHandleFromNode, intended to be usable by the public.

        %OPT% This will be pretty slow. %REVIEW% This relies on being able to test node-identity via object-identity. DTM2DOM proxying is a great example of a case where that doesn't work. DOM Level 3 will provide the isSameNode() method to fix that, but until then this is going to be flaky.

        Parameters:
        node - A node, which may be null.
        Returns:
        The node handle or DTM.NULL.
      • getAttributeNode

        public int getAttributeNode​(int nodeHandle,
                                    java.lang.String namespaceURI,
                                    java.lang.String name)
        Retrieves an attribute node by local name and namespace URI

        %TBD% Note that we currently have no way to support the DOM's old getAttribute() call, which accesses only the qname.

        Specified by:
        getAttributeNode in interface DTM
        Specified by:
        getAttributeNode in class DTMDefaultBase
        Parameters:
        nodeHandle - Handle of the node upon which to look up this attribute.
        namespaceURI - The namespace URI of the attribute to retrieve, or null.
        name - The local name of the attribute to retrieve.
        Returns:
        The attribute node handle with the specified name ( nodeName) or DTM.NULL if there is no such attribute.
      • getStringValue

        public XString getStringValue​(int nodeHandle)
        Get the string-value of a node as a String object (see ... for the definition of a node's string-value).
        Specified by:
        getStringValue in interface DTM
        Specified by:
        getStringValue in class DTMDefaultBase
        Parameters:
        nodeHandle - The node ID.
        Returns:
        A string object that represents the string-value of the given node.
      • getNodeData

        protected static void getNodeData​(org.w3c.dom.Node node,
                                          java.lang.StringBuilder buf)
        Retrieve the text content of a DOM subtree, appending it into a user-supplied FastStringBuffer object. Note that attributes are not considered part of the content of an element.

        There are open questions regarding whitespace stripping. Currently we make no special effort in that regard, since the standard DOM doesn't yet provide DTD-based information to distinguish whitespace-in-element-context from genuine #PCDATA. Note that we should probably also consider xml:space if/when we address this. DOM Level 3 may solve the problem for us.

        %REVIEW% Actually, since this method operates on the DOM side of the fence rather than the DTM side, it SHOULDN'T do any special handling. The DOM does what the DOM does; if you want DTM-level abstractions, use DTM-level methods.

        Parameters:
        node - Node whose subtree is to be walked, gathering the contents of all Text or CDATASection nodes.
        buf - StringBuilder into which the contents of the text nodes are to be concatenated.
      • getNodeName

        public java.lang.String getNodeName​(int nodeHandle)
        Given a node handle, return its DOM-style node name. This will include names such as #text or #document.
        Specified by:
        getNodeName in interface DTM
        Specified by:
        getNodeName in class DTMDefaultBase
        Parameters:
        nodeHandle - the id of the node.
        Returns:
        String Name of this node, which may be an empty string. %REVIEW% Document when empty string is possible...
      • getNodeNameX

        public java.lang.String getNodeNameX​(int nodeHandle)
        Given a node handle, return the XPath node name. This should be the name as described by the XPath data model, NOT the DOM-style name.
        Specified by:
        getNodeNameX in interface DTM
        Overrides:
        getNodeNameX in class DTMDefaultBase
        Parameters:
        nodeHandle - the id of the node.
        Returns:
        String Name of this node.
      • getLocalPart

        private static java.lang.String getLocalPart​(java.lang.String qname)
        Returns the local name of the given node.
        Parameters:
        qname - Input name
        Returns:
        Local part of the name if prefixed, or the given name if not
      • getLocalName

        public java.lang.String getLocalName​(int nodeHandle)
        Given a node handle, return its DOM-style localname. (As defined in Namespaces, this is the portion of the name after the prefix, if present, or the whole node name if no prefix exists)
        Specified by:
        getLocalName in interface DTM
        Specified by:
        getLocalName in class DTMDefaultBase
        Parameters:
        nodeHandle - the id of the node.
        Returns:
        String Local name of this node.
      • getPrefix

        public java.lang.String getPrefix​(int nodeHandle)
        Given a namespace handle, return the prefix that the namespace decl is mapping. Given a node handle, return the prefix used to map to the namespace. (As defined in Namespaces, this is the portion of the name before any colon character).

        %REVIEW% Are you sure you want "" for no prefix?

        Specified by:
        getPrefix in interface DTM
        Specified by:
        getPrefix in class DTMDefaultBase
        Parameters:
        nodeHandle - the id of the node.
        Returns:
        String prefix of this node's name, or "" if no explicit namespace prefix was given.
      • getNamespaceURI

        public java.lang.String getNamespaceURI​(int nodeHandle)
        Given a node handle, return its DOM-style namespace URI (As defined in Namespaces, this is the declared URI which this node's prefix -- or default in lieu thereof -- was mapped to.)
        Specified by:
        getNamespaceURI in interface DTM
        Specified by:
        getNamespaceURI in class DTMDefaultBase
        Parameters:
        nodeHandle - the id of the node.
        Returns:
        String URI value of this node's namespace, or null if no namespace was resolved.
      • logicalNextDOMTextNode

        private org.w3c.dom.Node logicalNextDOMTextNode​(org.w3c.dom.Node n)
        Utility function: Given a DOM Text node, determine whether it is logically followed by another Text or CDATASection node. This may involve traversing into Entity References.

        %REVIEW% DOM Level 3 is expected to add functionality which may allow us to retire this.

      • getNodeValue

        public java.lang.String getNodeValue​(int nodeHandle)
        Given a node handle, return its node value. This is mostly as defined by the DOM, but may ignore some conveniences.

        Specified by:
        getNodeValue in interface DTM
        Specified by:
        getNodeValue in class DTMDefaultBase
        Parameters:
        nodeHandle - The node id.
        Returns:
        String Value of this node, or null if not meaningful for this node type.
      • getElementById

        public int getElementById​(java.lang.String elementId)
        Returns the Element whose ID is given by elementId. If no such element exists, returns DTM.NULL. Behavior is not defined if more than one element has this ID. Attributes (including those with the name "ID") are not of type ID unless so defined by DTD/Schema information available to the DTM implementation. Implementations that do not know whether attributes are of type ID or not are expected to return DTM.NULL.

        %REVIEW% Presumably IDs are still scoped to a single document, and this operation searches only within a single document, right? Wouldn't want collisions between DTMs in the same process.

        Specified by:
        getElementById in interface DTM
        Specified by:
        getElementById in class DTMDefaultBase
        Parameters:
        elementId - The unique id value for an element.
        Returns:
        The handle of the matching element.