KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > jdom > AbstractDOMBuilder


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.jdt.internal.core.jdom;
12
13 import java.util.Stack JavaDoc;
14
15 import org.eclipse.jdt.core.jdom.*;
16 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
17 import org.eclipse.jdt.internal.core.util.ReferenceInfoAdapter;
18
19 /**
20  * An abstract DOM builder that contains shared functionality of DOMBuilder and SimpleDOMBuilder.
21  * @deprecated The JDOM was made obsolete by the addition in 2.0 of the more
22  * powerful, fine-grained DOM/AST API found in the
23  * org.eclipse.jdt.core.dom package.
24  */

25 public class AbstractDOMBuilder extends ReferenceInfoAdapter implements ILineStartFinder {
26     /**
27      * Set to true when an error is encounterd while
28      * fuzzy parsing
29      */

30     protected boolean fAbort;
31     
32     /**
33      * True when a compilation unit is being constructed.
34      * False when any other type of document fragment is
35      * being constructed.
36      */

37     protected boolean fBuildingCU = false;
38
39     /**
40      * True when a compilation unit or type is being
41      * constructed. False when any other type of document
42      * fragment is being constructed.
43      */

44     protected boolean fBuildingType= false;
45
46     /**
47      * The String on which the JDOM is being created.
48      */

49     protected char[] fDocument= null;
50         
51     /**
52      * The source positions of all of the line separators in the document.
53      */

54     protected int[] fLineStartPositions = new int[] { 0 };
55
56     /**
57      * A stack of enclosing scopes used when constructing
58      * a compilation unit or type. The top of the stack
59      * is the document fragment that children are added to.
60      */

61     protected Stack JavaDoc fStack = null;
62
63     /**
64      * The number of fields constructed in the current
65      * document. This is used when building a single
66      * field document fragment, since the DOMBuilder only
67      * accepts documents with one field declaration.
68      */

69     protected int fFieldCount;
70
71     /**
72      * The current node being constructed.
73      */

74     protected DOMNode fNode;
75 /**
76  * AbstractDOMBuilder constructor.
77  */

78 public AbstractDOMBuilder() {
79     super();
80 }
81 /**
82  * Accepts the line separator table and converts it into a line start table.
83  *
84  * <p>A line separator might corresponds to several characters in the source.
85  *
86  * @see org.eclipse.jdt.internal.compiler.IDocumentElementRequestor#acceptLineSeparatorPositions(int[])
87  */

88 public void acceptLineSeparatorPositions(int[] positions) {
89     if (positions != null) {
90         int length = positions.length;
91         if (length > 0) {
92             fLineStartPositions = new int[length + 1];
93             fLineStartPositions[0] = 0;
94             int documentLength = fDocument.length;
95             for (int i = 0; i < length; i++) {
96                 int iPlusOne = i + 1;
97                 int positionPlusOne = positions[i] + 1;
98                 if (positionPlusOne < documentLength) {
99                     if (iPlusOne < length) {
100                         // more separators
101
fLineStartPositions[iPlusOne] = positionPlusOne;
102                     } else {
103                         // no more separators
104
if (fDocument[positionPlusOne] == '\n') {
105                             fLineStartPositions[iPlusOne] = positionPlusOne + 1;
106                         } else {
107                             fLineStartPositions[iPlusOne] = positionPlusOne;
108                         }
109                     }
110                 } else {
111                     fLineStartPositions[iPlusOne] = positionPlusOne;
112                 }
113             }
114         }
115     }
116 }
117 /**
118  * Adds the given node to the current enclosing scope, building the JDOM
119  * tree. Nodes are only added to an enclosing scope when a compilation unit or type
120  * is being built (since those are the only nodes that have children).
121  *
122  * <p>NOTE: nodes are added to the JDOM via the method #basicAddChild such that
123  * the nodes in the newly created JDOM are not fragmented.
124  */

125 protected void addChild(IDOMNode child) {
126     if (fStack.size() > 0) {
127         DOMNode parent = (DOMNode) fStack.peek();
128         if (fBuildingCU || fBuildingType) {
129             parent.basicAddChild(child);
130         }
131     }
132 }
133 /**
134  * @see IDOMFactory#createCompilationUnit(String, String)
135  */

136 public IDOMCompilationUnit createCompilationUnit(char[] contents, char[] name) {
137     return createCompilationUnit(new CompilationUnit(contents, name));
138 }
139 /**
140  * @see IDOMFactory#createCompilationUnit(String, String)
141  */

142 public IDOMCompilationUnit createCompilationUnit(ICompilationUnit compilationUnit) {
143     if (fAbort) {
144         return null;
145     }
146     fNode.normalize(this);
147     return (IDOMCompilationUnit)fNode;
148 }
149 /**
150  * @see org.eclipse.jdt.internal.compiler.IDocumentElementRequestor#enterClass(int, int[], int, int, int, char[], int, int, char[], int, int, char[][], int[], int[], int)
151  */

152 public void enterCompilationUnit() {
153     if (fBuildingCU) {
154         IDOMCompilationUnit cu= new DOMCompilationUnit(fDocument, new int[] {0, fDocument.length - 1});
155         fStack.push(cu);
156     }
157 }
158 /**
159  * Finishes the configuration of the compilation unit DOM object which
160  * was created by a previous enterCompilationUnit call.
161  *
162  * @see org.eclipse.jdt.internal.compiler.IDocumentElementRequestor#exitCompilationUnit(int)
163  */

164 public void exitCompilationUnit(int declarationEnd) {
165     DOMCompilationUnit cu = (DOMCompilationUnit) fStack.pop();
166     cu.setSourceRangeEnd(declarationEnd);
167     fNode = cu;
168 }
169 /**
170  * Finishes the configuration of the class and interface DOM objects.
171  *
172  * @param bodyEnd - a source position corresponding to the closing bracket of the class
173  * @param declarationEnd - a source position corresponding to the end of the class
174  * declaration. This can include whitespace and comments following the closing bracket.
175  */

176 protected void exitType(int bodyEnd, int declarationEnd) {
177     DOMType type = (DOMType)fStack.pop();
178     type.setSourceRangeEnd(declarationEnd);
179     type.setCloseBodyRangeStart(bodyEnd);
180     type.setCloseBodyRangeEnd(bodyEnd);
181     fNode = type;
182 }
183 /**
184  * @see ILineStartFinder#getLineStart(int)
185  */

186 public int getLineStart(int position) {
187     int lineSeparatorCount = fLineStartPositions.length;
188     // reverse traversal intentional.
189
for(int i = lineSeparatorCount - 1; i >= 0; i--) {
190         if (fLineStartPositions[i] <= position)
191             return fLineStartPositions[i];
192     }
193     return 0;
194 }
195 /**
196  * Initializes the builder to create a document fragment.
197  *
198  * @param sourceCode - the document containing the source code to be analyzed
199  * @param buildingCompilationUnit - true if a the document is being analyzed to
200  * create a compilation unit, otherwise false
201  * @param buildingType - true if the document is being analyzed to create a
202  * type or compilation unit
203  */

204 protected void initializeBuild(char[] sourceCode, boolean buildingCompilationUnit, boolean buildingType) {
205     fBuildingCU = buildingCompilationUnit;
206     fBuildingType = buildingType;
207     fStack = new Stack JavaDoc();
208     fDocument = sourceCode;
209     fFieldCount = 0;
210     fAbort = false;
211 }
212 }
213
Popular Tags