1 19 20 package org.netbeans.modules.xml.xdm.diff; 21 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import org.netbeans.modules.xml.xdm.nodes.Attribute; 26 import org.netbeans.modules.xml.xdm.nodes.Element; 27 import org.netbeans.modules.xml.xdm.nodes.Node; 28 import org.w3c.dom.NamedNodeMap ; 29 30 35 public class Change extends Difference { 36 37 public enum Type { 38 TOKEN("token"), 39 ATTRIBUTE("attribute"), 40 POSITION("position"), 41 UNKNOWN("unknown"), 42 NO_CHANGE("noChange"); 43 44 String name; 45 Type(String name) { 46 this.name = name; 47 } 48 } 49 50 51 public Change(NodeInfo.NodeType nodeType, 52 List <Node> ancestors1, List <Node> ancestors2, 53 Node n1, Node n2, int n1Pos, int n2Pos, List <Type> changes) { 54 super(nodeType, ancestors1, ancestors2, n1, n2, n1Pos, n2Pos); 55 if (ancestors2.size() > 0) { 56 assert ancestors2.get(0).getIndexOfChild(n2) > -1; 57 } 58 assert changes != null && !changes.isEmpty(); 59 this.changes = changes; 60 61 if(changes.contains(Type.ATTRIBUTE)) { 62 if ( n1 instanceof Element ) { NamedNodeMap nm1 = n1.getAttributes(); 64 NamedNodeMap nm2 = n2.getAttributes(); 65 HashMap <Node, Integer > posMap = new HashMap <Node, Integer >(); 66 List <String > allAttrNames = new ArrayList <String >(); 67 for ( int i=0; i < nm1.getLength(); i++ ) { 68 Node oldAttr = (Node) nm1.item(i); 69 String name = oldAttr.getNodeName(); 70 if ( !allAttrNames.contains( name ) ) 71 allAttrNames.add( name ); 72 posMap.put(oldAttr, new Integer (i)); 73 } 74 for ( int i=0; i < nm2.getLength(); i++ ) { 75 Node newAttr = (Node) nm2.item(i); 76 String name = newAttr.getNodeName(); 77 if ( !allAttrNames.contains( name ) ) 78 allAttrNames.add( name ); 79 posMap.put(newAttr, new Integer (i)); 80 } 81 for ( int i=0; i < allAttrNames.size(); i++ ) { 82 String attrName = allAttrNames.get( i ); 83 Node oldAttr = (Node) nm1.getNamedItem(attrName); 84 Node currAttr = (Node) nm2.getNamedItem(attrName); 85 int oldAttrPos = oldAttr!=null?posMap.get(oldAttr).intValue():-1; 86 int newAttrPos = currAttr!=null?posMap.get(currAttr).intValue():-1; 87 if ( oldAttr != null ) { 88 if ( currAttr == null ) { 89 AttributeDelete delete = 90 new AttributeDelete((Attribute) oldAttr, oldAttrPos); 91 addAttrChanges(delete); 92 } else { 93 boolean tokenChange = new DiffFinder().checkChange( 94 oldAttr, currAttr).size() > 0; 95 boolean posChange = oldAttrPos != newAttrPos; 96 if(tokenChange || posChange) { 97 AttributeChange change = 98 new AttributeChange((Attribute) oldAttr, 99 (Attribute) currAttr, oldAttrPos, newAttrPos, 100 tokenChange, posChange); 101 addAttrChanges(change); 102 } 103 } 104 } else if ( currAttr != null ) { 105 AttributeAdd add = 106 new AttributeAdd((Attribute) currAttr, newAttrPos); 107 addAttrChanges(add); 108 } 109 } 110 } 111 } 112 } 113 114 public List <AttributeDiff> getAttrChanges() { 115 return attrChanges; 116 } 117 118 public void addAttrChanges(AttributeDiff attrDif) { 119 attrChanges.add(attrDif); 120 } 121 122 public void removeAttrChanges(AttributeDiff attrDif) { 123 attrChanges.remove(attrDif); 124 } 125 126 public boolean isTokenChanged() { 127 return changes.contains(Type.TOKEN); 128 } 129 130 133 public boolean isAttributeChanged() { 134 return changes.contains(Type.ATTRIBUTE); 135 } 136 137 public boolean isPositionChanged() { 138 return changes.contains(Type.POSITION); 139 } 140 141 void setPositionChanged(boolean posChange) { 142 if(posChange && !changes.contains(Type.POSITION)) 143 changes.add(Type.POSITION); 144 else if(!posChange && changes.contains(Type.POSITION)) 145 changes.remove(Type.POSITION); 146 } 147 148 public void setNewParent(Node parent2) { 149 if (parent2 == null) return; 150 assert parent2.getId() == getOldNodeInfo().getParent().getId(); 151 getOldNodeInfo().setNewParent(parent2); 152 getNewNodeInfo().setNewParent(parent2); 153 } 154 155 public Node getNewParent() { 156 return getNewNodeInfo().getNewParent(); 157 } 158 159 public List <Node> getNewAncestors() { 160 return getNewNodeInfo().getNewAncestors(); 161 } 162 163 public boolean isValid() { 164 return !changes.isEmpty(); 165 } 166 167 public String toString() { 168 return "CHANGE"+changes+"("+ getOldNodeInfo() + "," + getNewNodeInfo() + ")"; 169 } 170 171 175 private List <Type> changes; 176 private List <AttributeDiff> attrChanges = new ArrayList <AttributeDiff>(); 177 178 public class AttributeAdd extends AttributeDiff { 179 180 public AttributeAdd(Attribute newAttr, int newAttrPos) { 181 super(null, newAttr, -1, newAttrPos); 182 } 183 } 184 185 public class AttributeDelete extends AttributeDiff { 186 187 public AttributeDelete(Attribute oldAttr, int oldAttrPos) { 188 super(oldAttr, null, oldAttrPos, -1); 189 } 190 } 191 192 public class AttributeChange extends AttributeDiff { 193 194 private boolean posChanged; 195 196 private boolean tokenChanged; 197 198 public AttributeChange(Attribute oldAttr, Attribute newAttr, 199 int oldAttrPos, int newAttrPos, boolean tokenChanged, 200 boolean posChanged) { 201 super(oldAttr, newAttr, oldAttrPos, newAttrPos); 202 this.tokenChanged = tokenChanged; 203 this.posChanged = posChanged; 204 } 205 206 public boolean isTokenChanged() { 207 return tokenChanged; 208 } 209 210 public boolean isPositionChanged() { 211 return posChanged; 212 } 213 } 214 215 public class AttributeDiff { 216 217 private Attribute oldAttr; 218 219 private Attribute newAttr; 220 221 private int oldAttrPos; 222 223 private int newAttrPos; 224 225 public AttributeDiff(Attribute oldAttr, Attribute newAttr, 226 int oldAttrPos, int newAttrPos) { 227 this.oldAttr = oldAttr; 228 this.newAttr = newAttr; 229 this.oldAttrPos = oldAttrPos; 230 this.newAttrPos = newAttrPos; 231 } 232 233 public Attribute getOldAttribute() { 234 return oldAttr; 235 } 236 237 public Attribute getNewAttribute() { 238 return newAttr; 239 } 240 241 public int getOldAttributePosition() { 242 return oldAttrPos; 243 } 244 245 public int getNewAttributePosition() { 246 return newAttrPos; 247 } 248 } 249 250 } 251 | Popular Tags |