[ 
http://nagoya.apache.org/jira/browse/XALANJ-667?page=comments#action_56802 ]
     
Joe Kesselman commented on XALANJ-667:
--------------------------------------

See past discussion of this issue.

There is *no* portable way to test Level 1 DOM nodes for identity. However, 
most DOM1 implementations do use a single object per node, and thus == works as 
an identity test for those.

.equals(), semanticaly, ought to compare the *value* of two objects, not their 
identity. The concept of node value is not as clear as it should be -- note 
that an Element node officially has no node value -- but if it worked at all, 
node1.equals(node2) ought to mean the two subtrees were equivalent, not 
identitical -- so if anything it's inferior to == for our purposes.

The *right* answer, as discussed in the past, is to test the DOM in question to 
see if it implements the DOM Level 3 isSameNode() method. If so, we should use 
this method to test node identity. If not, see above; there is no good portable 
solution and we either need to find some way of recognizing and handling 
specific implementations or fall back on == as a last resort.

(Note that isSameNode() is *different* from isEqualNode(), also introduced in 
DOM3; that difference tracks the distinction I made above about identity versus 
equality.)

Yes, it's ugly. DOM1, and DOM2, should never have gone out the door without the 
isSameNode() test. All I can offer in our defense is that we honestly didn't 
realize this was an issue until someone (myself, in fact) started creating a 
DOM where node identity was *not* isomorphic with object identity.

That's the downside of developing in web-years; we all get to use the stuff 
sooner, but sometimes that's before it's really finished, and we have to deal 
with the fact that early versions may have gaps. Oh Well.

-- Joe Kesselman, DOM Working Group Alumnus

> using object identity as a substitute for isSameNode is bad idea
> ----------------------------------------------------------------
>
>          Key: XALANJ-667
>          URL: http://nagoya.apache.org/jira/browse/XALANJ-667
>      Project: XalanJ2
>         Type: Improvement
>   Components: DTM
>     Versions: 2.0.0
>  Environment: Operating System: Other
> Platform: Other
>     Reporter: Andrei Tchijov
>     Assignee: Joe Kesselman

>
> In org.apache.xml.dtm.ref.dom2dtm.DOM2DTM.getHandleOfNode "==" used to
> test for two nodes been the same.  In the comment just before this method
> definition it is stated that this is "flaky" solution, and I can not agree 
> more.
>  The question is, what is wrong with using "equals" instead of "=="?  I do
> realize that "equals" is not part of the DOM, but same is true for "==".
> Andrei Tchijov
> PS These are snipets of code I am talking about
>   public int getHandleOfNode(Node node)
>   {
>     if (null != node)
>     {
>       // Is Node actually within the same document? If not, don't search!
>       // This would be easier if m_root was always the Document node, but
>       // we decided to allow wrapping a DTM around a subtree.
>         /*
>          * hyperNOC fix.
>          * used to be
>       if((m_root==node) ||
>          (m_root.getNodeType()==DOCUMENT_NODE &&
>           m_root==node.getOwnerDocument()) ||
>          (m_root.getNodeType()!=DOCUMENT_NODE &&
>           m_root.getOwnerDocument()==node.getOwnerDocument())
>          )
>          */
>       if((m_root==node) ||
>          (m_root.getNodeType()==DOCUMENT_NODE &&
>           m_root.equals( node.getOwnerDocument())) ||
>          (m_root.getNodeType()!=DOCUMENT_NODE &&
>           m_root.getOwnerDocument().equals( node.getOwnerDocument()))
>          )
>         {
>           // If node _is_ in m_root's tree, find its handle
>           //
>           // %OPT% This check may be improved significantly when DOM
>           // Level 3 nodeKey and relative-order tests become
>           // available!
>           for(Node cursor=node;
>               cursor!=null;
>               cursor=
>                 (cursor.getNodeType()!=ATTRIBUTE_NODE)
>                 ? cursor.getParentNode()
>                 : ((org.w3c.dom.Attr)cursor).getOwnerElement())
>             {
>                 /*
>                  * hyperNOC fix.
>                  * used to be
>               if(cursor==m_root)
>                  */
>               if(cursor.equals( m_root ))
>                 // We know this node; find its handle.
>                 return getHandleFromNode(node);
>             } // for ancestors of node
>         } // if node and m_root in same Document
>     } // if node!=null
>     return DTM.NULL;
>   }
> Same is true for getHandleFromNode
>   private int getHandleFromNode(Node node)
>   {
>     if (null != node)
>     {
>       int len = m_nodes.size();       
>       boolean isMore;
>       int i = 0;
>       do
>       {         
>         for (; i < len; i++)
>         {
>             /*
>              * hyperNOC fix
>              * used to be
>           if (m_nodes.elementAt(i) == node)
>              */
>           if (m_nodes.elementAt(i).equals( node ))
>             return i | m_dtmIdent;        
>         }
>         isMore = nextNode();
>  
>         len = m_nodes.size();
>            
>       }
>       while(isMore || i < len);
>     }
>    
>     return DTM.NULL;
>   }

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://nagoya.apache.org/jira/secure/Administrators.jspa
-
If you want more information on JIRA, or have a bug to report see:
   http://www.atlassian.com/software/jira


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

Reply via email to