1 19 20 package org.netbeans.modules.xml.xdm.diff; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 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 ; 28 29 34 public class DefaultElementIdentity implements ElementIdentity { 35 36 39 public DefaultElementIdentity() { 40 } 41 42 public List getIdentifiers() { 43 return identifiers; 44 } 45 46 public void addIdentifier(String identifier) { 47 if(!identifiers.contains(identifier)) 48 identifiers.add(identifier); 49 } 50 51 public boolean compareElement(org.w3c.dom.Element n1, org.w3c.dom.Element n2, org.w3c.dom.Document doc1, org.w3c.dom.Document doc2) { 52 return compareElement(n1, n2, null, doc1, doc2); 53 } 54 55 protected boolean compareElement(org.w3c.dom.Element n1, org.w3c.dom.Element n2, org.w3c.dom.Node parent1, org.w3c.dom.Document doc1, org.w3c.dom.Document doc2) { 56 String qName1 = n1.getLocalName(); 57 String qName2 = n2.getLocalName(); 58 String ns1 = ((Node)n1).getNamespaceURI((Document) doc1); 59 String 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; 69 return compareAttr( n1, n2); 70 } 71 72 protected boolean compareAttr(org.w3c.dom.Element n1, org.w3c.dom.Element n2) { 73 NamedNodeMap attrs1 = n1.getAttributes(); 74 NamedNodeMap attrs2 = n2.getAttributes(); 75 76 List <String > 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 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 if ( matchCount == 1 ) 98 return true; 99 100 if ( unmatchCount == 1 && attrs1.getLength() == attrs2.getLength() ) 102 return false; 103 } 104 105 if ( matchCount == 0 && unmatchCount == 0 ) 107 return true; 108 109 return false; 110 } 111 112 public void clear() { 113 identifiers.clear(); 114 } 115 116 120 private List <String > identifiers = new ArrayList <String >(); 121 } 122 | Popular Tags |