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.visitor;
043:
044: import java.util.List;
045: import org.netbeans.modules.xml.xdm.nodes.Document;
046: import org.netbeans.modules.xml.xdm.nodes.Node;
047: import org.netbeans.modules.xml.xdm.XDMModel;
048: import org.netbeans.modules.xml.xdm.nodes.NodeImpl;
049: import org.w3c.dom.NamedNodeMap;
050: import org.w3c.dom.NodeList;
051:
052: /**
053: * This class provides a way to merge two trees. A merge is defined as taking a
054: * tree and muting this tree (firing events) to cause the tree to be the same
055: * as the target tree. This allows the original tree to retain nodes which have
056: * not been changed. A change is defined as having both the same syntax and
057: * semantics. The current tree model preserves spacing where appropriate, so
058: * this requires comparing spacing as well as semantics.
059: * @author Chris Webster
060: * @author Vidhya Narayanan
061: */
062: public class MergeVisitor extends DefaultVisitor {
063: /**
064: * This method merges the currentModel and the given newDocument. Events
065: * will be fired on currentModel.
066: * @param model the model to merge the changes required to transform
067: * the current document to something equivalent to newDocument.
068: * @param newDoc to replicate in the current model.
069: */
070: public void merge(XDMModel model, Document newDoc) {
071: xmlModel = model;
072: oldtree = xmlModel.getDocument();
073: target = newDoc;
074: oldtree.accept(this );
075: }
076:
077: protected void visitNode(Node node) {
078: CompareVisitor comparer = new CompareVisitor();
079: boolean result = comparer.compare(node, target);
080: if (!result) {
081: Node newNode = (Node) target.clone(false, false, false);
082: List<Node> path = pathVisitor.findPath(oldtree, node);
083: assert !path.isEmpty();
084: Node oldNode = path.get(0);
085: int offset = 0;
086: NodeList children = ((Node) path.get(1)).getChildNodes();
087: for (; offset < children.getLength(); offset++) {
088: if (oldNode.equals(children.item(offset))) {
089: break;
090: }
091: }
092: xmlModel.delete(oldNode);
093: xmlModel.add((Node) path.get(1), newNode, offset);
094: } else {
095: compareByIndex((Node) target, node);
096: compareAttrsByIndex((Node) target, node);
097: }
098: }
099:
100: private void compareByIndex(Node newNode, Node current) {
101: NodeList newnodes = newNode.getChildNodes();
102: NodeList children = current.getChildNodes();
103: int oldTreesize = children.getLength();
104: int newTreesize = newnodes.getLength();
105:
106: int lastEqualIndex = Math.min(oldTreesize, newTreesize);
107: // these nodes are comparable
108: for (int i = 0; i < lastEqualIndex; i++) {
109: target = (Node) newnodes.item(i);
110: Node n = (Node) children.item(i);
111: n.accept(this );
112: }
113: // reset target as the rest of the tree will need to be walked
114: target = newNode;
115:
116: // delete removed nodes from oldTree
117: for (int i = oldTreesize - 1; i >= lastEqualIndex; i--) {
118: xmlModel.delete((Node) children.item(i));
119: }
120:
121: // add nodes from newTree
122: for (int i = lastEqualIndex; i < newTreesize; i++) {
123: Node n = (Node) newnodes.item(i);
124: xmlModel.add(current, (Node) n.clone(false, false, false),
125: i);
126: }
127: }
128:
129: private void compareAttrsByIndex(Node newNode, Node current) {
130: NamedNodeMap newAttributes = newNode.getAttributes();
131: NamedNodeMap attributes = current.getAttributes();
132: int oldTreesize = attributes.getLength();
133: int newTreesize = newAttributes.getLength();
134:
135: int lastEqualIndex = Math.min(oldTreesize, newTreesize);
136: // these nodes are comparable
137: for (int i = 0; i < lastEqualIndex; i++) {
138: target = (Node) newAttributes.item(i);
139: Node n = (Node) attributes.item(i);
140: // System.out.println(n.getNodeName());
141: n.accept(this );
142: }
143: // reset target as the rest of the tree will need to be walked
144: target = newNode;
145:
146: // delete removed nodes from oldTree
147: for (int i = oldTreesize - 1; i >= lastEqualIndex; i--) {
148: xmlModel.delete((Node) attributes.item(i));
149: }
150:
151: // add nodes from newTree
152: for (int i = lastEqualIndex; i < newTreesize; i++) {
153: Node n = (Node) newAttributes.item(i);
154: xmlModel.add(current, ((NodeImpl) n)
155: .cloneShallowWithModelContext(), i);
156: }
157: }
158:
159: private XDMModel xmlModel;
160: private Document oldtree;
161: private Node target;
162: private PathFromRootVisitor pathVisitor = new PathFromRootVisitor();
163: }
|