KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > diff > DefaultElementIdentity


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xdm.diff;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.modules.xml.xam.dom.ElementIdentity;
25 import org.netbeans.modules.xml.xdm.nodes.Document;
26 import org.netbeans.modules.xml.xdm.nodes.Node;
27 import org.w3c.dom.NamedNodeMap JavaDoc;
28
29 /*
30  * This class is used by DiffFinder to compare 2 elements by identifying attributes
31  *
32  * @author Ayub Khan
33  */

34 public class DefaultElementIdentity implements ElementIdentity {
35     
36     /**
37      * Creates a new instance of DefaultElementIdentity
38      */

39     public DefaultElementIdentity() {
40     }
41     
42     public List JavaDoc getIdentifiers() {
43         return identifiers;
44     }
45     
46     public void addIdentifier(String JavaDoc identifier) {
47         if(!identifiers.contains(identifier))
48             identifiers.add(identifier);
49     }
50     
51     public boolean compareElement(org.w3c.dom.Element JavaDoc n1, org.w3c.dom.Element JavaDoc n2, org.w3c.dom.Document JavaDoc doc1, org.w3c.dom.Document JavaDoc doc2) {
52         return compareElement(n1, n2, null, doc1, doc2);
53     }
54     
55     protected boolean compareElement(org.w3c.dom.Element JavaDoc n1, org.w3c.dom.Element JavaDoc n2, org.w3c.dom.Node JavaDoc parent1, org.w3c.dom.Document JavaDoc doc1, org.w3c.dom.Document JavaDoc doc2) {
56         String JavaDoc qName1 = n1.getLocalName();
57         String JavaDoc qName2 = n2.getLocalName();
58         String JavaDoc ns1 = ((Node)n1).getNamespaceURI((Document) doc1);
59         String JavaDoc ns2 = ((Node)n2).getNamespaceURI((Document) doc2);
60         
61         if ( qName1.intern() != qName2.intern() )
62             return false;
63         if ( !(ns1 == null && ns2 == null) &&
64                 !(ns1 != null && ns2 != null && ns1.intern() == ns2.intern() ) )
65             return false;
66         
67         if(parent1 == doc1) return true; //if root no need to compare other identifiers
68

69         return compareAttr( n1, n2);
70     }
71     
72     protected boolean compareAttr(org.w3c.dom.Element JavaDoc n1, org.w3c.dom.Element JavaDoc n2) {
73         NamedNodeMap JavaDoc attrs1 = n1.getAttributes();
74         NamedNodeMap JavaDoc attrs2 = n2.getAttributes();
75         
76         List JavaDoc<String JavaDoc> nameSet = getIdentifiers();
77         if( nameSet.isEmpty() )
78             return true;
79         else if ( attrs1.getLength() == 0 && attrs2.getLength() == 0 )
80             return true;
81         
82         int matchCount = 0;
83         int unmatchCount = 0;
84         for ( String JavaDoc name:nameSet ) {
85             Node attr1 = (Node) attrs1.getNamedItem( name );
86             Node attr2 = (Node) attrs2.getNamedItem( name );
87             if ( attr1 == null && attr2 == null )
88                 continue;
89             else if ( attr1 != null && attr2 != null ){
90                 if ( attr2.getNodeValue().intern() != attr1.getNodeValue().intern() )
91                     unmatchCount++;
92                 else
93                     matchCount++;
94             } else
95                 unmatchCount++;
96             //check for exact match
97
if ( matchCount == 1 )
98                 return true;
99             
100             //check for rename
101
if ( unmatchCount == 1 && attrs1.getLength() == attrs2.getLength() )
102                 return false;
103         }
104         
105         //no attributes in attrs1 and attrs2 that match nameSet
106
if ( matchCount == 0 && unmatchCount == 0 )
107             return true;
108         
109         return false;
110     }
111     
112     public void clear() {
113         identifiers.clear();
114     }
115     
116     ////////////////////////////////////////////////////////////////////////////////
117
// Member variables
118
////////////////////////////////////////////////////////////////////////////////
119

120     private List JavaDoc<String JavaDoc> identifiers = new ArrayList JavaDoc<String JavaDoc>();
121 }
122
Popular Tags