KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collections JavaDoc;
23 import java.util.List JavaDoc;
24 import org.netbeans.modules.xml.xdm.nodes.Document;
25 import org.netbeans.modules.xml.xdm.nodes.Node;
26
27 /**
28  * Class that represents diff node info
29  *
30  * @author Ayub Khan
31  */

32 public class NodeInfo {
33     
34     public NodeInfo(Node n, int pos, List JavaDoc<Node> ancestors1, List JavaDoc<Node> ancestors2) {
35         if (! (n instanceof Document)) {
36             assert ancestors1 != null && ! ancestors1.isEmpty() : "bad ancestors1";
37             assert ancestors2 != null && ! ancestors2.isEmpty() : "bad ancestors1";
38         }
39         this.n = n;
40         this.pos = pos;
41         this.ancestors1 = ancestors1;
42         this.ancestors2 = ancestors2;
43     }
44     
45     public Node getNode() {
46         return n;
47     }
48     
49     /**
50      * Only to update new version of same nodeid.
51      */

52     void setNode(Node node) {
53         if (updated) {
54             assert node.getId() == n.getId() : "expect id="+n.getId()+" got id="+node.getId();
55         }
56         updated = true;
57         n = node;
58     }
59     
60     /**
61      * @returns position of removed in the original parent or
62      * position of the added in the final parent.
63      */

64     public int getPosition() {
65         return pos;
66     }
67     
68     public Node getParent() {
69         if (ancestors1 != null && ancestors1.size() > 0) {
70             return ancestors1.get(0);
71         }
72         return null;
73     }
74     
75     /**
76      * @returns document the node is captured in (a node in xdm tree can be in
77      * multiple document roots).
78      */

79     public Document getDocument() {
80         return (Document) ancestors1.get(ancestors1.size()-1);
81     }
82     
83     /**
84      * @returns original path from parent to root.
85      */

86     public List JavaDoc<Node> getOriginalAncestors() {
87         return Collections.unmodifiableList(ancestors1);
88     }
89     
90     /**
91      * @returns new path to root from parent of added or removed node.
92      * Note that this path need to be updated
93      */

94     public List JavaDoc<Node> getNewAncestors() {
95         if (ancestors2 == null) {
96             assert parent2 != null : "expect parent2 is set";
97             ancestors2 = DiffFinder.getPathToRoot(parent2);
98         }
99         return Collections.unmodifiableList(ancestors2);
100     }
101     
102     public void setNewAncestors(List JavaDoc<Node> ancestors2) {
103         assert ancestors2 != null && ! ancestors2.isEmpty();
104         this.ancestors2 = ancestors2;
105         parent2 = ancestors2.get(0);
106     }
107     
108     public void setNewParent(Node parent) {
109         assert parent != null && parent.isInTree() : "new parent should be not null and inTree";
110         ancestors2 = null;
111         parent2 = parent;
112     }
113     
114     public Node getNewParent() {
115         if (parent2 == null && ! (getNode() instanceof Document)) {
116             assert ancestors2 != null && ancestors2.size() > 0;
117             return ancestors2.get(0);
118         }
119         return parent2;
120     }
121     
122     public String JavaDoc toString() {
123         int parentId = getParent() == null ? -1 : getParent().getId();
124         return DiffFinder.getNodeType(n) + "." + pos + " ids[" + n.getId() + "," +
125                 parentId + "]";
126     }
127     
128     ////////////////////////////////////////////////////////////////////////////////
129
// Class variables
130
////////////////////////////////////////////////////////////////////////////////
131

132     public static enum NodeType { ELEMENT, ATTRIBUTE, TEXT, WHITE_SPACE };
133     
134     ////////////////////////////////////////////////////////////////////////////////
135
// Member variables
136
////////////////////////////////////////////////////////////////////////////////
137

138     private Node n;
139     
140     private boolean updated = false;
141     
142     private final int pos;
143     
144     private final List JavaDoc<Node> ancestors1;
145     
146     private List JavaDoc<Node> ancestors2; // new ancestors or would-have-been ancestors in case of delete
147

148     private Node parent2;
149 }
150
Popular Tags