code: public int addNodeInDocOrder(int node, boolean test, XPathContext support)
    {
  
      if (!m_mutable)
        throw new RuntimeException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_NODESETDTM_NOT_MUTABLE, null)); //"This NodeSetDTM is not mutable!");
  
      int insertIndex = -1;
  
      if (test)
      {
  
        // This needs to do a binary search, but a binary search 
        // is somewhat tough because the sequence test involves 
        // two nodes.
        int size = size(), i;
  
        for (i = size - 1; i >= 0; i--)
        {
          int child = elementAt(i);
  
          if (child == node)
          {
            i = -2;  // Duplicate, suppress insert
  
            break;
          }
  
          DTM dtm = support.getDTM(node);
          if (!dtm.isNodeAfter(node, child))
          {
            break;
          }
        }
  
        if (i != -2)
        {
          insertIndex = i + 1;
  
          insertElementAt(node, insertIndex);
        }
      }
      else
      {
        insertIndex = this.size();
  
        boolean foundit = false;
  
        for (int i = 0; i < insertIndex; i++)
        {
          if (i == node)
          {
            foundit = true;
  
            break;
          }
        }
  
        if (!foundit)
          addElement(node);
      }
  
      // checkDups();
      return insertIndex;
    }
docs: Add the node into a vector of nodes where it should occur in
  document order.
  @param node The node to be added.
  @param test true if we should test for doc order
  @param support The XPath runtime context.
  @return insertIndex.
  @throws RuntimeException thrown if this NodeSetDTM is not of
  a mutable type.