001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.xdm.diff;
043:
044: import java.util.ArrayList;
045: import java.util.List;
046: import org.netbeans.modules.xml.xam.dom.ElementIdentity;
047: import org.netbeans.modules.xml.xdm.nodes.Document;
048: import org.netbeans.modules.xml.xdm.nodes.Node;
049: import org.w3c.dom.NamedNodeMap;
050:
051: /*
052: * This class is used by DiffFinder to compare 2 elements by identifying attributes
053: *
054: * @author Ayub Khan
055: */
056: public class DefaultElementIdentity implements ElementIdentity {
057:
058: /**
059: * Creates a new instance of DefaultElementIdentity
060: */
061: public DefaultElementIdentity() {
062: }
063:
064: public List getIdentifiers() {
065: return identifiers;
066: }
067:
068: public void addIdentifier(String identifier) {
069: if (!identifiers.contains(identifier))
070: identifiers.add(identifier);
071: }
072:
073: public boolean compareElement(org.w3c.dom.Element n1,
074: org.w3c.dom.Element n2, org.w3c.dom.Document doc1,
075: org.w3c.dom.Document doc2) {
076: return compareElement(n1, n2, null, doc1, doc2);
077: }
078:
079: protected boolean compareElement(org.w3c.dom.Element n1,
080: org.w3c.dom.Element n2, org.w3c.dom.Node parent1,
081: org.w3c.dom.Document doc1, org.w3c.dom.Document doc2) {
082: String qName1 = n1.getLocalName();
083: String qName2 = n2.getLocalName();
084: String ns1 = ((Node) n1).getNamespaceURI((Document) doc1);
085: String ns2 = ((Node) n2).getNamespaceURI((Document) doc2);
086:
087: if (qName1.intern() != qName2.intern())
088: return false;
089: if (!(ns1 == null && ns2 == null)
090: && !(ns1 != null && ns2 != null && ns1.intern() == ns2
091: .intern()))
092: return false;
093:
094: if (parent1 == doc1)
095: return true; //if root no need to compare other identifiers
096:
097: return compareAttr(n1, n2);
098: }
099:
100: protected boolean compareAttr(org.w3c.dom.Element n1,
101: org.w3c.dom.Element n2) {
102: NamedNodeMap attrs1 = n1.getAttributes();
103: NamedNodeMap attrs2 = n2.getAttributes();
104:
105: List<String> nameSet = getIdentifiers();
106: if (nameSet.isEmpty())
107: return true;
108: else if (attrs1.getLength() == 0 && attrs2.getLength() == 0)
109: return true;
110:
111: int matchCount = 0;
112: int unmatchCount = 0;
113: for (String name : nameSet) {
114: Node attr1 = (Node) attrs1.getNamedItem(name);
115: Node attr2 = (Node) attrs2.getNamedItem(name);
116: if (attr1 == null && attr2 == null)
117: continue;
118: else if (attr1 != null && attr2 != null) {
119: if (attr2.getNodeValue().intern() != attr1
120: .getNodeValue().intern())
121: unmatchCount++;
122: else
123: matchCount++;
124: } else
125: unmatchCount++;
126: //check for exact match
127: if (matchCount == 1)
128: return true;
129:
130: //check for rename
131: if (unmatchCount == 1
132: && attrs1.getLength() == attrs2.getLength())
133: return false;
134: }
135:
136: //no attributes in attrs1 and attrs2 that match nameSet
137: if (matchCount == 0 && unmatchCount == 0)
138: return true;
139:
140: return false;
141: }
142:
143: public void clear() {
144: identifiers.clear();
145: }
146:
147: ////////////////////////////////////////////////////////////////////////////////
148: // Member variables
149: ////////////////////////////////////////////////////////////////////////////////
150:
151: private List<String> identifiers = new ArrayList<String>();
152: }
|