KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
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 JavaDoc;
29
30 /**
31  * This class represents node change between 2 DOM tree
32  *
33  * @author Ayub Khan
34  */

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 JavaDoc name;
45         Type(String JavaDoc name) {
46             this.name = name;
47         }
48     }
49     
50     /** Creates a new instance of DiffEvent */
51     public Change(NodeInfo.NodeType nodeType,
52             List JavaDoc<Node> ancestors1, List JavaDoc<Node> ancestors2,
53             Node n1, Node n2, int n1Pos, int n2Pos, List JavaDoc<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 ) {//find attr changes and add them
63
NamedNodeMap JavaDoc nm1 = n1.getAttributes();
64                 NamedNodeMap JavaDoc nm2 = n2.getAttributes();
65                 HashMap JavaDoc<Node, Integer JavaDoc> posMap = new HashMap JavaDoc<Node, Integer JavaDoc>();
66                 List JavaDoc<String JavaDoc> allAttrNames = new ArrayList JavaDoc<String JavaDoc>();
67                 for ( int i=0; i < nm1.getLength(); i++ ) {
68                     Node oldAttr = (Node) nm1.item(i);
69                     String JavaDoc name = oldAttr.getNodeName();
70                     if ( !allAttrNames.contains( name ) )
71                         allAttrNames.add( name );
72                     posMap.put(oldAttr, new Integer JavaDoc(i));
73                 }
74                 for ( int i=0; i < nm2.getLength(); i++ ) {
75                     Node newAttr = (Node) nm2.item(i);
76                     String JavaDoc name = newAttr.getNodeName();
77                     if ( !allAttrNames.contains( name ) )
78                         allAttrNames.add( name );
79                     posMap.put(newAttr, new Integer JavaDoc(i));
80                 }
81                 for ( int i=0; i < allAttrNames.size(); i++ ) {
82                     String JavaDoc 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 JavaDoc<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     /**
131      * @return true if attribute list changes or attribute tokens change.
132      */

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 JavaDoc<Node> getNewAncestors() {
160         return getNewNodeInfo().getNewAncestors();
161     }
162     
163     public boolean isValid() {
164         return !changes.isEmpty();
165     }
166     
167     public String JavaDoc toString() {
168         return "CHANGE"+changes+"("+ getOldNodeInfo() + "," + getNewNodeInfo() + ")";
169     }
170     
171     ////////////////////////////////////////////////////////////////////////////////
172
// Member variables
173
////////////////////////////////////////////////////////////////////////////////
174

175     private List JavaDoc<Type> changes;
176     private List JavaDoc<AttributeDiff> attrChanges = new ArrayList JavaDoc<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