You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by le...@apache.org on 2002/01/19 00:43:16 UTC

cvs commit: xml-xerces/java/src/org/apache/xerces/dom ElementImpl.java NodeImpl.java ParentNode.java

lehors      02/01/18 15:43:16

  Modified:    java/src/org/apache/xerces/dom ElementImpl.java
                        NodeImpl.java ParentNode.java
  Log:
  implemented DOM Level 3 isEqualNode() method
  
  Revision  Changes    Path
  1.40      +46 -1     xml-xerces/java/src/org/apache/xerces/dom/ElementImpl.java
  
  Index: ElementImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/dom/ElementImpl.java,v
  retrieving revision 1.39
  retrieving revision 1.40
  diff -u -r1.39 -r1.40
  --- ElementImpl.java	10 Jan 2002 06:34:20 -0000	1.39
  +++ ElementImpl.java	18 Jan 2002 23:43:16 -0000	1.40
  @@ -65,6 +65,8 @@
   import org.w3c.dom.NodeList;
   import org.w3c.dom.Text;
   
  +import org.apache.xerces.dom3.Node3;
  +
   /**
    * Elements represent most of the "markup" and structure of the
    * document.  They contain both the data for the element itself
  @@ -86,7 +88,7 @@
    * @author Joe Kesselman, IBM
    * @author Andy Clark, IBM
    * @author Ralf Pfeiffer, IBM
  - * @version $Id: ElementImpl.java,v 1.39 2002/01/10 06:34:20 twl Exp $
  + * @version $Id: ElementImpl.java,v 1.40 2002/01/18 23:43:16 lehors Exp $
    * @since  PR-DOM-Level-1-19980818.
    */
   public class ElementImpl
  @@ -762,6 +764,49 @@
       public NodeList getElementsByTagNameNS(String namespaceURI,
                                              String localName) {
       	return new DeepNodeListImpl(this, namespaceURI, localName);
  +    }
  +
  +    /**
  +     * Override inherited behavior from NodeImpl and ParentNode to check on
  +     * attributes
  +     */
  +    public boolean isEqualNode(Node arg, boolean deep) {
  +        if (!super.isEqualNode(arg, deep)) {
  +            return false;
  +        }
  +        boolean hasAttrs = hasAttributes();
  +        if (hasAttrs != ((Element) arg).hasAttributes()) {
  +            return false;
  +        }
  +        if (hasAttrs) {
  +            NamedNodeMap map1 = getAttributes();
  +            NamedNodeMap map2 = ((Element) arg).getAttributes();
  +            int len = map1.getLength();
  +            if (len != map2.getLength()) {
  +                return false;
  +            }
  +            for (int i = 0; i < len; i++) {
  +                Node n1 = map1.item(i);
  +                if (n1.getLocalName() == null) { // DOM Level 1 Node
  +                    Node n2 = map2.getNamedItem(n1.getNodeName());
  +                    // REVISIT: as of 01/18/02 the spec doesn't say whether
  +                    // this should be deep
  +                    if (n2 == null || !((Node3) n1).isEqualNode(n2, deep)) {
  +                        return false;
  +                    }
  +                }
  +                else {
  +                    Node n2 = map2.getNamedItemNS(n1.getNamespaceURI(),
  +                                                  n1.getLocalName());
  +                    // REVISIT: as of 01/18/02 the spec doesn't say whether
  +                    // this should be deep
  +                    if (n2 == null || !((Node3) n1).isEqualNode(n2, deep)) {
  +                        return false;
  +                    }
  +                }
  +            }
  +        }
  +        return true;
       }
   
       //
  
  
  
  1.47      +68 -7     xml-xerces/java/src/org/apache/xerces/dom/NodeImpl.java
  
  Index: NodeImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/dom/NodeImpl.java,v
  retrieving revision 1.46
  retrieving revision 1.47
  diff -u -r1.46 -r1.47
  --- NodeImpl.java	11 Jan 2002 22:07:17 -0000	1.46
  +++ NodeImpl.java	18 Jan 2002 23:43:16 -0000	1.47
  @@ -115,7 +115,7 @@
    *
    * @author Arnaud  Le Hors, IBM
    * @author Joe Kesselman, IBM
  - * @version $Id: NodeImpl.java,v 1.46 2002/01/11 22:07:17 elena Exp $
  + * @version $Id: NodeImpl.java,v 1.47 2002/01/18 23:43:16 lehors Exp $
    * @since  PR-DOM-Level-1-19980818.
    */
   public abstract class NodeImpl
  @@ -728,8 +728,9 @@
        */
       public String getBaseURI() {
           // REVISIT: Implementation needed! :)
  -        throw new DOMException(DOMException.NOT_SUPPORTED_ERR,
  -                               "not implemented yet!");
  +        //        throw new DOMException(DOMException.NOT_SUPPORTED_ERR,
  +        //                               "not implemented yet!");
  +        return null;            // to allow isEqualNode to work
       }
   
       /**
  @@ -945,7 +946,7 @@
        * attribute for <code>Attr</code> nodes, the 
        * <code>isWhitespaceInElementContent</code> attribute for 
        * <code>Text</code> nodes, as well as any user data or event listeners 
  -     * registered on the nodes.Should this be optional?No.
  +     * registered on the nodes.
        * @param arg The node to compare equality with.
        * @param deep If <code>true</code>, recursively compare the subtrees; if 
        *   <code>false</code>, compare only the nodes themselves (and its 
  @@ -955,9 +956,69 @@
        * @since DOM Level 3
        */
       public boolean isEqualNode(Node arg, boolean deep) {
  -        // REVISIT: Implementation needed! :)
  -        throw new DOMException(DOMException.NOT_SUPPORTED_ERR,
  -                               "not implemented yet!");
  +        if (arg == this) {
  +            return true;
  +        }
  +        if (arg.getNodeType() != getNodeType()) {
  +            return false;
  +        }
  +        // in theory nodeName can't be null but better be careful
  +        // who knows what other implementations may be doing?...
  +        if (getNodeName() == null) {
  +            if (arg.getNodeName() != null) {
  +                return false;
  +            }
  +        }
  +        else if (!getNodeName().equals(arg.getNodeName())) {
  +            return false;
  +        }
  +
  +        if (getLocalName() == null) {
  +            if (arg.getLocalName() != null) {
  +                return false;
  +            }
  +        }
  +        else if (!getLocalName().equals(arg.getLocalName())) {
  +            return false;
  +        }
  +
  +        if (getNamespaceURI() == null) {
  +            if (arg.getNamespaceURI() != null) {
  +                return false;
  +            }
  +        }
  +        else if (!getNamespaceURI().equals(arg.getNamespaceURI())) {
  +            return false;
  +        }
  +
  +        if (getPrefix() == null) {
  +            if (arg.getPrefix() != null) {
  +                return false;
  +            }
  +        }
  +        else if (!getPrefix().equals(arg.getPrefix())) {
  +            return false;
  +        }
  +
  +        if (getNodeValue() == null) {
  +            if (arg.getNodeValue() != null) {
  +                return false;
  +            }
  +        }
  +        else if (!getNodeValue().equals(arg.getNodeValue())) {
  +            return false;
  +        }
  +
  +        if (getBaseURI() == null) {
  +            if (((Node3) arg).getBaseURI() != null) {
  +                return false;
  +            }
  +        }
  +        else if (!getBaseURI().equals(((Node3) arg).getBaseURI())) {
  +            return false;
  +        }
  +
  +        return true;
       }
   
       /**
  
  
  
  1.32      +30 -1     xml-xerces/java/src/org/apache/xerces/dom/ParentNode.java
  
  Index: ParentNode.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/dom/ParentNode.java,v
  retrieving revision 1.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- ParentNode.java	17 Dec 2001 19:38:11 -0000	1.31
  +++ ParentNode.java	18 Jan 2002 23:43:16 -0000	1.32
  @@ -1,4 +1,4 @@
  -/* $Id: ParentNode.java,v 1.31 2001/12/17 19:38:11 lehors Exp $ */
  +/* $Id: ParentNode.java,v 1.32 2002/01/18 23:43:16 lehors Exp $ */
   /*
    * The Apache Software License, Version 1.1
    *
  @@ -68,6 +68,8 @@
   import org.w3c.dom.NodeList;
   import org.w3c.dom.Text;
   
  +import org.apache.xerces.dom3.Node3;
  +
   /**
    * ParentNode inherits from ChildNode and adds the capability of having child
    * nodes. Not every node in the DOM can have children, so only nodes that can
  @@ -875,6 +877,33 @@
               kid.normalize();
           }
           isNormalized(true);
  +    }
  +
  +    /**
  +     * Override inherited behavior from NodeImpl to support deep equal.
  +     */
  +    public boolean isEqualNode(Node arg, boolean deep) {
  +        if (!super.isEqualNode(arg, deep)) {
  +            return false;
  +        }
  +        if (deep) {
  +            // there are many ways to do this test, and there isn't any way
  +            // better than another. Performance may vary greatly depending on
  +            // the implementations involved. This one should work fine for us.
  +            Node child1 = getFirstChild();
  +            Node child2 = arg.getFirstChild();
  +            while (child1 != null && child2 != null) {
  +                if (!((Node3) child1).isEqualNode(child2, true)) {
  +                    return false;
  +                }
  +                child1 = child1.getNextSibling();
  +                child2 = child2.getNextSibling();
  +            }
  +            if (child1 != child2) {
  +                return false;
  +            }
  +        }
  +        return true;
       }
   
       //
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org