Class IDIndexer

java.lang.Object
org.exolab.adaptx.xml.IDIndexer

public class IDIndexer extends Object
A utility class which helps overcome some DOM 1.0 deficiencies.
Version:
$Revision: 3633 $ $Date: 2003-03-01 08:38:44 +0100 (Sat, 01 Mar 2003) $
Author:
Keith Visco
  • Constructor Details

    • IDIndexer

      public IDIndexer()
      Creates a new DOMHelper
  • Method Details

    • addIdAttribute

      public void addIdAttribute(String attrName, String appliesTo)
      Adds the given attribute name as an ID attribute.
      Parameters:
      attrName - the name of the attribute to treat as an Id.
      appliesTo - the element that this ID attribute appliesTo, "*" may be used to indicate all Elements.
    • addIdReference

      public void addIdReference(String id, XPathNode node)
      Associates the given Id with the given Element
      Parameters:
      id - the Id to associate with the given Element
      element - the element which the Id maps to
    • getElementById

      public XPathNode getElementById(XPathNode root, String id)
      Determines the document order of a given node. Document Order is defined by the document order of the parent of the given node and the childNumber of the given Node. The document order for a Document node is 0.
      Parameters:
      root - the "root" XPathNode to search within
      id - the Id of the element to return
      Returns:
      the element XPathNode that is associated with the given Id, or null if no XPathNode was found
      See Also:
      • invalid reference
        public int[] getDocumentOrder(Node node) {
        
               int[] order = null;
        
               if (node == null) {
                   order = new int[1];
                   order[0] = -1;
                   return order;
               }
        
               //-- check cache
               //-- * due to bugs in XML4J 1.1.x (2.x works fine) 
               //-- * we need to use the System.identityHash to
               //-- * create a unique key. The problem is Attr nodes
               //-- * with the same name, generate the same hash code.
               Object key = createKey(node);
        
               order = (int[]) documentOrders.get(key);
               if (order != null) return order;
        
        
               Node parent = null;
               //-- calculate document order
               if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
                   // Use parent's document order for attributes
                   parent = getParentNode((Attr)node);
                   if (parent == null) {
                       // int[3]  {0 = document, 0 = att-list, att-number}
                       order = new int[3];
                       order[0] = 0;
                       order[1] = 0;
                       order[2] = childNumber(node);
                   }
                   else { 
                       int[] porder = getDocumentOrder(parent);
                       order = new int[porder.length+2];
                       for (int i = 0; i invalid input: '<' porder.length; i++) 
                           order[i] = porder[i];
                       order[order.length-2] = 0;  //-- signal att-list
                       order[order.length-1] = childNumber(node);
                   }
               }
               else if (node.getNodeType() == Node.DOCUMENT_NODE) {
                   order = new int[1];
                   order[0] = 0;
               }
               else {
        
                   //-- get parent's document order
                   parent = getParentNode(node);
                   int[] porder = getDocumentOrder(getParentNode(node));
                   order = new int[porder.length+1];
                   for (int i = 0; i invalid input: '<' porder.length; i++) 
                       order[i] = porder[i];
                   order[order.length-1] = childNumber(node);
               }
        
               //-- add to cache
               documentOrders.put(key,order);
               return order;
           } //-- getDocumentOrder
        
           /**
        Returns the element XPathNode that is associated with the given Id.