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.diff;
043:
044: import java.util.Collections;
045: import java.util.List;
046: import org.netbeans.modules.xml.xdm.nodes.Document;
047: import org.netbeans.modules.xml.xdm.nodes.Node;
048:
049: /**
050: * Class that represents diff node info
051: *
052: * @author Ayub Khan
053: */
054: public class NodeInfo {
055:
056: public NodeInfo(Node n, int pos, List<Node> ancestors1,
057: List<Node> ancestors2) {
058: if (!(n instanceof Document)) {
059: assert ancestors1 != null && !ancestors1.isEmpty() : "bad ancestors1";
060: assert ancestors2 != null && !ancestors2.isEmpty() : "bad ancestors1";
061: }
062: this .n = n;
063: this .pos = pos;
064: this .ancestors1 = ancestors1;
065: this .ancestors2 = ancestors2;
066: }
067:
068: public Node getNode() {
069: return n;
070: }
071:
072: /**
073: * Only to update new version of same nodeid.
074: */
075: void setNode(Node node) {
076: if (updated) {
077: assert node.getId() == n.getId() : "expect id=" + n.getId()
078: + " got id=" + node.getId();
079: }
080: updated = true;
081: n = node;
082: }
083:
084: /**
085: * @returns position of removed in the original parent or
086: * position of the added in the final parent.
087: */
088: public int getPosition() {
089: return pos;
090: }
091:
092: public Node getParent() {
093: if (ancestors1 != null && ancestors1.size() > 0) {
094: return ancestors1.get(0);
095: }
096: return null;
097: }
098:
099: /**
100: * @returns document the node is captured in (a node in xdm tree can be in
101: * multiple document roots).
102: */
103: public Document getDocument() {
104: return (Document) ancestors1.get(ancestors1.size() - 1);
105: }
106:
107: /**
108: * @returns original path from parent to root.
109: */
110: public List<Node> getOriginalAncestors() {
111: return Collections.unmodifiableList(ancestors1);
112: }
113:
114: /**
115: * @returns new path to root from parent of added or removed node.
116: * Note that this path need to be updated
117: */
118: public List<Node> getNewAncestors() {
119: if (ancestors2 == null) {
120: assert parent2 != null : "expect parent2 is set";
121: ancestors2 = DiffFinder.getPathToRoot(parent2);
122: }
123: return Collections.unmodifiableList(ancestors2);
124: }
125:
126: public void setNewAncestors(List<Node> ancestors2) {
127: assert ancestors2 != null && !ancestors2.isEmpty();
128: this .ancestors2 = ancestors2;
129: parent2 = ancestors2.get(0);
130: }
131:
132: public void setNewParent(Node parent) {
133: assert parent != null && parent.isInTree() : "new parent should be not null and inTree";
134: ancestors2 = null;
135: parent2 = parent;
136: }
137:
138: public Node getNewParent() {
139: if (parent2 == null && !(getNode() instanceof Document)) {
140: assert ancestors2 != null && ancestors2.size() > 0;
141: return ancestors2.get(0);
142: }
143: return parent2;
144: }
145:
146: public String toString() {
147: int parentId = getParent() == null ? -1 : getParent().getId();
148: return DiffFinder.getNodeType(n) + "." + pos + " ids["
149: + n.getId() + "," + parentId + "]";
150: }
151:
152: ////////////////////////////////////////////////////////////////////////////////
153: // Class variables
154: ////////////////////////////////////////////////////////////////////////////////
155:
156: public static enum NodeType {
157: ELEMENT, ATTRIBUTE, TEXT, WHITE_SPACE
158: };
159:
160: ////////////////////////////////////////////////////////////////////////////////
161: // Member variables
162: ////////////////////////////////////////////////////////////////////////////////
163:
164: private Node n;
165:
166: private boolean updated = false;
167:
168: private final int pos;
169:
170: private final List<Node> ancestors1;
171:
172: private List<Node> ancestors2; // new ancestors or would-have-been ancestors in case of delete
173:
174: private Node parent2;
175: }
|