KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > core > hcr > JavaNode


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

11 package org.eclipse.jdt.internal.debug.core.hcr;
12
13
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.jdt.core.ToolFactory;
17 import org.eclipse.jdt.core.compiler.IScanner;
18 import org.eclipse.jdt.core.compiler.ITerminalSymbols;
19 import org.eclipse.jdt.core.compiler.InvalidInputException;
20 import org.eclipse.jdt.internal.core.JavaElement;
21
22 /**
23  * Comparable Java elements are represented as JavaNodes.
24  */

25 class JavaNode {
26     
27     public static final int CU= 0;
28     public static final int PACKAGE= 1;
29     public static final int IMPORT_CONTAINER= 2;
30     public static final int IMPORT= 3;
31     public static final int INTERFACE= 4;
32     public static final int CLASS= 5;
33     public static final int FIELD= 6;
34     public static final int INIT= 7;
35     public static final int CONSTRUCTOR= 8;
36     public static final int METHOD= 9;
37
38     private String JavaDoc fID;
39     private int fTypeCode;
40     private char[] fBuffer;
41     private int fStart;
42     private int fLength;
43     private ArrayList JavaDoc fChildren;
44     private int fInitializerCount= 1;
45
46     /**
47      * Creates a new <code>JavaNode</code> for the given range within the specified
48      * buffer. The <code>typeCode</code> is uninterpreted client data. The ID is used when comparing
49      * two nodes with each other: i.e. the differencing engine performs a content compare
50      * on two nodes if their IDs are equal.
51      *
52      * @param typeCode a type code for this node
53      * @param id an identifier for this node
54      * @param buffer buffer on which this node is based on
55      * @param start start position of range within document
56      * @param length length of range
57      */

58     JavaNode(int typeCode, String JavaDoc id, char[] buffer, int start, int length) {
59         fTypeCode= typeCode;
60         fID= id;
61         fBuffer= buffer;
62         fStart= start;
63         fLength= length;
64     }
65
66     /**
67      * Creates a JavaNode under the given parent.
68      * @param type the Java elements type. Legal values are from the range CU to METHOD of this class.
69      * @param name the name of the Java element
70      * @param start the starting position of the java element in the underlying document
71      * @param length the number of characters of the java element in the underlying document
72      */

73     JavaNode(JavaNode parent, int type, String JavaDoc name, int start, int length) {
74         this(type, buildID(type, name), parent.fBuffer, start, length);
75         if (parent != null) {
76             parent.addChild(this);
77         }
78     }
79     
80     /**
81      * Creates a JavaNode for a CU. It represents the root of a
82      * JavaNode tree, so its parent is null.
83      * @param document the document which contains the Java element
84      */

85     JavaNode(char[] buffer) {
86         this(CU, buildID(CU, "root"), buffer, 0, buffer.length); //$NON-NLS-1$
87
}
88
89     /**
90      * Returns a name which identifies the given typed name.
91      * The type is encoded as a single character at the beginning of the string.
92      */

93     private static String JavaDoc buildID(int type, String JavaDoc name) {
94         StringBuffer JavaDoc sb= new StringBuffer JavaDoc();
95         switch (type) {
96         case JavaNode.CU:
97             sb.append(JavaElement.JEM_COMPILATIONUNIT);
98             break;
99         case JavaNode.CLASS:
100         case JavaNode.INTERFACE:
101             sb.append(JavaElement.JEM_TYPE);
102             sb.append(name);
103             break;
104         case JavaNode.FIELD:
105             sb.append(JavaElement.JEM_FIELD);
106             sb.append(name);
107             break;
108         case JavaNode.CONSTRUCTOR:
109         case JavaNode.METHOD:
110             sb.append(JavaElement.JEM_METHOD);
111             sb.append(name);
112             break;
113         case JavaNode.INIT:
114             sb.append(JavaElement.JEM_INITIALIZER);
115             sb.append(name);
116             break;
117         case JavaNode.PACKAGE:
118             sb.append(JavaElement.JEM_PACKAGEDECLARATION);
119             break;
120         case JavaNode.IMPORT:
121             sb.append(JavaElement.JEM_IMPORTDECLARATION);
122             sb.append(name);
123             break;
124         case JavaNode.IMPORT_CONTAINER:
125             sb.append('<');
126             break;
127         default:
128             //Assert.isTrue(false);
129
break;
130         }
131         return sb.toString();
132     }
133
134     public String JavaDoc getInitializerCount() {
135         return Integer.toString(fInitializerCount++);
136     }
137         
138     public int getStart() {
139         return fStart;
140     }
141     
142     /**
143      * Returns the type code of this node.
144      * The type code is uninterpreted client data which can be set in the constructor.
145      *
146      * @return the type code of this node
147      */

148     public int getTypeCode() {
149         return fTypeCode;
150     }
151     
152     /**
153      * Returns this node's id.
154      * It is used in <code>equals</code> and <code>hashcode</code>.
155      *
156      * @return the node's id
157      */

158     public String JavaDoc getId() {
159         return fID;
160     }
161
162     /**
163      * Sets this node's id.
164      * It is used in <code>equals</code> and <code>hashcode</code>.
165      *
166      * @param id the new id for this node
167      */

168     public void setId(String JavaDoc id) {
169         fID= id;
170     }
171
172     /**
173      * Adds the given node as a child.
174      *
175      * @param node the node to add as a child
176      */

177     public void addChild(JavaNode node) {
178         if (fChildren == null) {
179             fChildren= new ArrayList JavaDoc();
180         }
181         fChildren.add(node);
182     }
183
184     /* (non Javadoc)
185      * see IStructureComparator.getChildren
186      */

187     public Object JavaDoc[] getChildren() {
188         if (fChildren != null) {
189             return fChildren.toArray();
190         }
191         return null;
192     }
193
194     /**
195      * Sets the length of the range of this node.
196      *
197      * @param length the length of the range
198      */

199     public void setLength(int length) {
200         fLength= length;
201     }
202
203     /**
204      * Implementation based on <code>getID</code>.
205      */

206     public boolean equals(Object JavaDoc other) {
207         if (other != null && other.getClass() == getClass()) {
208             JavaNode tn= (JavaNode) other;
209             return fTypeCode == tn.fTypeCode && fID.equals(tn.fID);
210         }
211         return super.equals(other);
212     }
213
214     /**
215      * Implementation based on <code>getID</code>.
216      */

217     public int hashCode() {
218         return fID.hashCode();
219     }
220
221 /* This version of getContents will be affected by whitespace changes.
222     public String getContents() {
223         char[] b= new char[fLength];
224         System.arraycopy(fBuffer, fStart, b, 0, fLength);
225         return new String(b);
226     }
227 */

228     
229      public String JavaDoc getContents() {
230         char[] b= new char[fLength];
231         System.arraycopy(fBuffer, fStart, b, 0, fLength);
232         
233         boolean ignoreWhiteSpace= true;
234         if (ignoreWhiteSpace) {
235             // replace comments and whitespace by a single blank
236
StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
237             
238             // to avoid the trouble when dealing with Unicode
239
// we use the Java scanner to extract non-whitespace and non-comment tokens
240
IScanner scanner= ToolFactory.createScanner(true, true, false, false); // however we request Whitespace and Comments
241
scanner.setSource(b);
242             try {
243                 int token;
244                 while ((token= scanner.getNextToken()) != ITerminalSymbols.TokenNameEOF) {
245                     switch (token) {
246                         case ITerminalSymbols.TokenNameWHITESPACE:
247                         case ITerminalSymbols.TokenNameCOMMENT_BLOCK:
248                         case ITerminalSymbols.TokenNameCOMMENT_JAVADOC:
249                         case ITerminalSymbols.TokenNameCOMMENT_LINE:
250                             int l= buf.length();
251                             if (l > 0 && buf.charAt(l-1) != ' ') {
252                                 buf.append(' ');
253                             }
254                             break;
255                         default:
256                             buf.append(b, scanner.getCurrentTokenStartPosition(), (scanner.getCurrentTokenEndPosition() + 1) - scanner.getCurrentTokenStartPosition());
257                             buf.append(' ');
258                             break;
259                     }
260                 }
261                 return buf.toString(); // success!
262
} catch (InvalidInputException ex) {
263             }
264         }
265         return new String JavaDoc(b); // return original source
266
}
267 }
268
269
Popular Tags