KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
23 import org.netbeans.modules.xml.xam.dom.ElementIdentity;
24 import org.netbeans.modules.xml.xdm.XDMModel;
25 import org.netbeans.modules.xml.xdm.nodes.Document;
26 import org.netbeans.modules.xml.xdm.nodes.Element;
27 import org.netbeans.modules.xml.xdm.nodes.Node;
28 import org.w3c.dom.NodeList JavaDoc;
29
30 /*
31  * This class is the Facade to find diff between 2 XML documents as well
32  * as merge the changes detected back to the original XDM model
33  *
34  * @author Ayub Khan
35  */

36 public class XDMTreeDiff {
37     
38     private ElementIdentity eID;
39     
40     /** Creates a new instance of XDMTreeDiff */
41     public XDMTreeDiff(ElementIdentity eID) {
42         this.eID = eID;
43     }
44     
45     public List JavaDoc<Difference> performDiff(Document doc1, Document doc2) {
46 // System.out.println("doc1==>");
47
// printDocument(doc1);
48
// System.out.println("doc2==>");
49
// printDocument(doc2);
50
List JavaDoc<Difference> deList = new DiffFinder(eID).findDiff(doc1, doc2);
51         return deList;
52     }
53     
54     public List JavaDoc<Difference> performDiff(XDMModel model, Document doc2) {
55         return performDiff(model.getDocument(), doc2);
56     }
57     
58     public List JavaDoc<Difference> performDiffAndMutate(XDMModel model, Document doc2) {
59         List JavaDoc<Difference> deList = performDiff(model.getDocument(), doc2);
60         model.mergeDiff(deList);
61         return deList;
62     }
63     
64     public static void printDocument(Node node) {
65         String JavaDoc name = node.getNodeName();
66         if(node instanceof Element && node.getAttributes().getLength() > 0)
67             name+=node.getAttributes().item(0).getNodeValue();
68         System.out.println("node: "+name+" id:"+node.getId());
69         NodeList JavaDoc childs = node.getChildNodes();
70         for(int i=0;i<childs.getLength();i++)
71             printDocument((Node) childs.item(i));
72     }
73 }
74
Popular Tags