Author: mrglavas
Date: Mon Jan 23 08:42:23 2006
New Revision: 371588

URL: http://svn.apache.org/viewcvs?rev=371588&view=rev
Log:
Fixing JIRA Bug #1127:
http://issues.apache.org/jira/browse/XERCESJ-1127

TreeWalker can be used with DOM implementations other than Xerces. If the nodes
in the DOM implementation are proxies they are only be comparable using 
isSameNode().
If the DOM implemenation supports DOM Level 3 and isn't the Xerces 
implementation
we now call isSameNode() to support such a scenario.

Modified:
    xerces/java/trunk/src/org/apache/xerces/dom/TreeWalkerImpl.java

Modified: xerces/java/trunk/src/org/apache/xerces/dom/TreeWalkerImpl.java
URL: 
http://svn.apache.org/viewcvs/xerces/java/trunk/src/org/apache/xerces/dom/TreeWalkerImpl.java?rev=371588&r1=371587&r2=371588&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/dom/TreeWalkerImpl.java (original)
+++ xerces/java/trunk/src/org/apache/xerces/dom/TreeWalkerImpl.java Mon Jan 23 
08:42:23 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 1999-2002,2004 The Apache Software Foundation.
+ * Copyright 1999-2002,2004,2006 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,17 +17,18 @@
 package org.apache.xerces.dom;
 
 import org.w3c.dom.DOMException;
+import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 import org.w3c.dom.traversal.NodeFilter;
 import org.w3c.dom.traversal.TreeWalker;
 
-/** This class implements the TreeWalker interface. 
+/** 
+ * This class implements the TreeWalker interface. 
  *
  * @xerces.internal
  * 
  * @version $Id$
  */
-
 public class TreeWalkerImpl implements TreeWalker {
     
     //
@@ -44,6 +45,8 @@
     Node fCurrentNode;
     /** The root Node. */
     Node fRoot;
+    /** Use Node.isSameNode() to check if one node is the same as another. */
+    private boolean fUseIsSameNode;
     
     //
     // Implementation Note: No state is kept except the data above
@@ -63,6 +66,7 @@
                           boolean entityReferenceExpansion) {
         fCurrentNode = root;
         fRoot = root;
+        fUseIsSameNode = useIsSameNode(root);
         fWhatToShow = whatToShow;
         fNodeFilter = nodeFilter;
         fEntityReferenceExpansion = entityReferenceExpansion;
@@ -270,7 +274,7 @@
      */
     Node getParentNode(Node node) {
         
-        if (node == null || node == fRoot) return null;
+        if (node == null || isSameNode(node, fRoot)) return null;
         
         Node newNode = node.getParentNode();
         if (newNode == null)  return null; 
@@ -305,14 +309,14 @@
      */
     Node getNextSibling(Node node, Node root) {
         
-        if (node == null || node == root) return null;
+        if (node == null || isSameNode(node, root)) return null;
         
         Node newNode = node.getNextSibling();
         if (newNode == null) {
                 
             newNode = node.getParentNode();
                 
-            if (newNode == null || newNode == root)  return null; 
+            if (newNode == null || isSameNode(newNode, root)) return null; 
                 
             int parentAccept = acceptNode(newNode);
                 
@@ -360,13 +364,13 @@
      */
     Node getPreviousSibling(Node node, Node root) {
         
-        if (node == null || node == root) return null;
+        if (node == null || isSameNode(node, root)) return null;
         
         Node newNode = node.getPreviousSibling();
         if (newNode == null) {
                 
             newNode = node.getParentNode();
-            if (newNode == null || newNode == root)  return null; 
+            if (newNode == null || isSameNode(newNode, root)) return null; 
                 
             int parentAccept = acceptNode(newNode);
                 
@@ -500,5 +504,25 @@
                 return NodeFilter.FILTER_SKIP;
             }
         }
-    } 
+    }
+    
+    /**
+     * Use isSameNode() for testing node equality if the DOM implementation
+     * supports DOM Level 3 core and it isn't the Xerces implementation.
+     */
+    private boolean useIsSameNode(Node node) {
+        if (node instanceof NodeImpl) {
+            return false;
+        }
+        Document doc = node.getNodeType() == Node.DOCUMENT_NODE 
+            ? (Document) node : node.getOwnerDocument();
+        return (doc != null && doc.getImplementation().hasFeature("Core", 
"3.0"));
+    }
+    
+    /**
+     * Returns true if <code>m</code> is the same node <code>n</code>.
+     */
+    private boolean isSameNode(Node m, Node n) {
+        return (fUseIsSameNode) ? m.isSameNode(n) : m == n;
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to