KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > visitor > MergeVisitor


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
21 package org.netbeans.modules.xml.xdm.visitor;
22 import java.util.List JavaDoc;
23 import org.netbeans.modules.xml.xdm.nodes.Document;
24 import org.netbeans.modules.xml.xdm.nodes.Node;
25 import org.netbeans.modules.xml.xdm.XDMModel;
26 import org.netbeans.modules.xml.xdm.nodes.NodeImpl;
27 import org.w3c.dom.NamedNodeMap JavaDoc;
28 import org.w3c.dom.NodeList JavaDoc;
29
30 /**
31  * This class provides a way to merge two trees. A merge is defined as taking a
32  * tree and muting this tree (firing events) to cause the tree to be the same
33  * as the target tree. This allows the original tree to retain nodes which have
34  * not been changed. A change is defined as having both the same syntax and
35  * semantics. The current tree model preserves spacing where appropriate, so
36  * this requires comparing spacing as well as semantics.
37  * @author Chris Webster
38  * @author Vidhya Narayanan
39  */

40 public class MergeVisitor extends DefaultVisitor {
41     /**
42      * This method merges the currentModel and the given newDocument. Events
43      * will be fired on currentModel.
44      * @param model the model to merge the changes required to transform
45      * the current document to something equivalent to newDocument.
46      * @param newDoc to replicate in the current model.
47      */

48     public void merge(XDMModel model, Document newDoc) {
49         xmlModel = model;
50         oldtree = xmlModel.getDocument();
51         target = newDoc;
52         oldtree.accept(this);
53     }
54     
55     protected void visitNode(Node node) {
56         CompareVisitor comparer = new CompareVisitor();
57         boolean result = comparer.compare(node, target);
58         if (!result) {
59             Node newNode = (Node)target.clone(false,false,false);
60             List JavaDoc<Node> path = pathVisitor.findPath(oldtree, node);
61             assert !path.isEmpty();
62             Node oldNode = path.get(0);
63             int offset = 0;
64             NodeList JavaDoc children = ((Node)path.get(1)).getChildNodes();
65             for (;offset<children.getLength();offset++) {
66                 if (oldNode.equals(children.item(offset))) {
67                     break;
68                 }
69             }
70             xmlModel.delete(oldNode);
71             xmlModel.add((Node)path.get(1), newNode, offset);
72         } else {
73             compareByIndex((Node)target, node);
74             compareAttrsByIndex((Node)target, node);
75         }
76     }
77     
78     private void compareByIndex(Node newNode, Node current) {
79         NodeList JavaDoc newnodes = newNode.getChildNodes();
80         NodeList JavaDoc children = current.getChildNodes();
81         int oldTreesize = children.getLength();
82         int newTreesize = newnodes.getLength();
83         
84         int lastEqualIndex = Math.min(oldTreesize, newTreesize);
85         // these nodes are comparable
86
for (int i = 0; i < lastEqualIndex; i++) {
87             target = (Node)newnodes.item(i);
88             Node n = (Node)children.item(i);
89             n.accept(this);
90         }
91         // reset target as the rest of the tree will need to be walked
92
target = newNode;
93         
94         // delete removed nodes from oldTree
95
for (int i = oldTreesize-1; i >= lastEqualIndex; i--) {
96             xmlModel.delete((Node)children.item(i));
97         }
98         
99         // add nodes from newTree
100
for (int i = lastEqualIndex; i < newTreesize; i++) {
101             Node n = (Node)newnodes.item(i);
102             xmlModel.add(current, (Node)n.clone(false,false,false), i);
103         }
104     }
105     
106     private void compareAttrsByIndex(Node newNode, Node current) {
107         NamedNodeMap JavaDoc newAttributes = newNode.getAttributes();
108         NamedNodeMap JavaDoc attributes = current.getAttributes();
109         int oldTreesize = attributes.getLength();
110         int newTreesize = newAttributes.getLength();
111         
112         int lastEqualIndex = Math.min(oldTreesize, newTreesize);
113         // these nodes are comparable
114
for (int i = 0; i < lastEqualIndex; i++) {
115             target = (Node)newAttributes.item(i);
116             Node n = (Node)attributes.item(i);
117 // System.out.println(n.getNodeName());
118
n.accept(this);
119         }
120         // reset target as the rest of the tree will need to be walked
121
target = newNode;
122         
123         // delete removed nodes from oldTree
124
for (int i = oldTreesize-1; i >= lastEqualIndex; i--) {
125             xmlModel.delete((Node)attributes.item(i));
126         }
127         
128         // add nodes from newTree
129
for (int i = lastEqualIndex; i < newTreesize; i++) {
130             Node n = (Node)newAttributes.item(i);
131             xmlModel.add(current, ((NodeImpl)n).cloneShallowWithModelContext(), i);
132         }
133     }
134     
135     private XDMModel xmlModel;
136     private Document oldtree;
137     private Node target;
138     private PathFromRootVisitor pathVisitor = new PathFromRootVisitor();
139 }
140
Popular Tags