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 to 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 Details

    • m_nodes

      protected final List<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 Details

    • DOM2DTM

      public DOM2DTM(DTMManager mgr, 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 Details

    • addNode

      protected int addNode(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 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 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.
    • getHandleOfNode

      public int getHandleOfNode(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, String nsURI, 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.
      nsURI - 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(Node node, 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 - FastStringBuffer into which the contents of the text nodes are to be concatenated.
    • getNodeName

      public 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 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.
    • getLocalName

      public 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 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 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.
    • getNodeValue

      public 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(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.