KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > compare > XMLNode


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.compare;
12
13 import org.eclipse.compare.CompareUI;
14 import org.eclipse.compare.ITypedElement;
15 import org.eclipse.compare.structuremergeviewer.DocumentRangeNode;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.swt.graphics.Image;
18
19 /**
20  * Objects that make up the parse tree.
21  */

22 public class XMLNode extends DocumentRangeNode implements ITypedElement {
23
24     private String JavaDoc fValue;
25     private String JavaDoc fName;
26     private String JavaDoc fSignature;
27     private String JavaDoc fOrigId;
28     private XMLNode parent;
29     private String JavaDoc fXMLType;
30     private boolean fUsesIDMAP;
31
32     public int bodies; // counts the number of bodies
33

34     public XMLNode(String JavaDoc XMLType, String JavaDoc id, String JavaDoc value, String JavaDoc signature, IDocument doc, int start, int length) {
35         super(0, id, doc, start, length);
36         fXMLType= XMLType;
37         fValue= value;
38         fSignature= signature;
39         fOrigId= id;
40         if (XMLStructureCreator.DEBUG_MODE)
41             System.out.println("Created XMLNode with XMLType: " + XMLType + ", id: " + id + ", value: " + value + ", signature: " + fSignature); //$NON-NLS-1$ //$NON-NLS-4$ //$NON-NLS-3$ //$NON-NLS-2$
42
bodies= 0;
43         fUsesIDMAP= false;
44     }
45
46     void setValue(String JavaDoc value) {
47         fValue= value;
48     }
49
50     String JavaDoc getValue() {
51         return fValue;
52     }
53
54     /*
55      * @see ITypedElement#getName
56      */

57     public String JavaDoc getName() {
58         if (fName != null)
59             return fName;
60         return this.getId();
61     }
62
63     public void setName(String JavaDoc name) {
64         fName= name;
65     }
66
67     /*
68      * Every xml node is of type "txt" so that the builtin TextMergeViewer is used automatically.
69      * @see ITypedElement#getType
70      */

71     public String JavaDoc getType() {
72         return "xml"; //$NON-NLS-1$
73
}
74
75     
76     /*
77      * @see ITypedElement#getImage
78      */

79     public Image getImage() {
80         return CompareUI.getImage(XMLStructureMapping.getImageKey(getXMLType()));
81     }
82
83     public void setParent(XMLNode parent0) {
84         this.parent= parent0;
85     }
86
87     public XMLNode getParent() {
88         return this.parent;
89     }
90
91     String JavaDoc getXMLType() {
92         return fXMLType;
93     }
94
95     String JavaDoc getSignature() {
96         return fSignature;
97     }
98
99     void setOrigId(String JavaDoc id) {
100         fOrigId= id;
101     }
102
103     public String JavaDoc getOrigId() {
104         return fOrigId;
105     }
106
107     public void setUsesIDMAP(boolean b) {
108         fUsesIDMAP= b;
109     }
110
111     public boolean usesIDMAP() {
112         return fUsesIDMAP;
113     }
114
115     //for tests
116
public boolean testEquals(Object JavaDoc obj) {
117         if (obj instanceof XMLNode) {
118             XMLNode n= (XMLNode) obj;
119             return fValue.equals(n.getValue())
120                 && fSignature.equals(n.getSignature())
121                 && fXMLType.equals(n.getXMLType())
122                 && fUsesIDMAP == n.usesIDMAP();
123         }
124         return false;
125     }
126
127     /*
128      * Returns true if the subtree rooted at this node is equals to the subtree rooted at <code>obj</code>
129      */

130     public boolean subtreeEquals(Object JavaDoc obj) {
131         if (!testEquals(obj))
132             return false;
133         if (obj instanceof XMLNode) {
134             XMLNode n= (XMLNode) obj;
135             if (getXMLType().equals(XMLStructureCreator.TYPE_ATTRIBUTE)
136                 && n.getXMLType().equals(XMLStructureCreator.TYPE_ATTRIBUTE))
137                 return true;
138             Object JavaDoc[] children= getChildren();
139             Object JavaDoc[] n_children= n.getChildren();
140             //if both nodes have no children, return true;
141
if ((children == null || children.length <= 0)
142                 && (n_children == null || n_children.length <= 0))
143                 return true;
144             //now at least one of the two nodes has children;
145
/* so if one of the two nodes has no children, or they don't have the same number of children,
146              * return false;
147              */

148             if ((children == null || children.length <= 0)
149                 || (n_children == null || n_children.length <= 0)
150                 || (children.length != n_children.length))
151                 return false;
152             //now both have children and the same number of children
153
for (int i= 0; i < children.length; i++) {
154                 /* if the subtree rooted at children[i] is not equal to the subtree rooted at n_children[i],
155                  * return false
156                  */

157                 if (!((XMLNode) children[i]).subtreeEquals(n_children[i]))
158                     return false;
159             }
160         }
161         return true;
162     }
163 }
164
Popular Tags