1 19 20 21 package org.netbeans.modules.xml.xdm.visitor; 22 import java.util.List ; 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 ; 28 import org.w3c.dom.NodeList ; 29 30 40 public class MergeVisitor extends DefaultVisitor { 41 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 <Node> path = pathVisitor.findPath(oldtree, node); 61 assert !path.isEmpty(); 62 Node oldNode = path.get(0); 63 int offset = 0; 64 NodeList 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 newnodes = newNode.getChildNodes(); 80 NodeList children = current.getChildNodes(); 81 int oldTreesize = children.getLength(); 82 int newTreesize = newnodes.getLength(); 83 84 int lastEqualIndex = Math.min(oldTreesize, newTreesize); 85 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 target = newNode; 93 94 for (int i = oldTreesize-1; i >= lastEqualIndex; i--) { 96 xmlModel.delete((Node)children.item(i)); 97 } 98 99 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 newAttributes = newNode.getAttributes(); 108 NamedNodeMap attributes = current.getAttributes(); 109 int oldTreesize = attributes.getLength(); 110 int newTreesize = newAttributes.getLength(); 111 112 int lastEqualIndex = Math.min(oldTreesize, newTreesize); 113 for (int i = 0; i < lastEqualIndex; i++) { 115 target = (Node)newAttributes.item(i); 116 Node n = (Node)attributes.item(i); 117 n.accept(this); 119 } 120 target = newNode; 122 123 for (int i = oldTreesize-1; i >= lastEqualIndex; i--) { 125 xmlModel.delete((Node)attributes.item(i)); 126 } 127 128 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 |