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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.jdom;
13 import java.util.Stack;
15 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
16 import net.sourceforge.phpdt.core.compiler.IProblem;
17 import net.sourceforge.phpdt.core.jdom.IDOMCompilationUnit;
18 import net.sourceforge.phpdt.core.jdom.IDOMNode;
19 import net.sourceforge.phpdt.internal.core.util.ReferenceInfoAdapter;
22 * An abstract DOM builder that contains shared functionality of DOMBuilder and SimpleDOMBuilder.
24 public class AbstractDOMBuilder extends ReferenceInfoAdapter implements ILineStartFinder {
26 * Set to true when an error is encounterd while
29 protected boolean fAbort;
32 * True when a compilation unit is being constructed.
33 * False when any other type of document fragment is
36 protected boolean fBuildingCU = false;
39 * True when a compilation unit or type is being
40 * constructed. False when any other type of document
41 * fragment is being constructed.
43 protected boolean fBuildingType= false;
46 * The String on which the JDOM is being created.
48 protected char[] fDocument= null;
51 * The source positions of all of the line separators in the document.
53 protected int[] fLineStartPositions = new int[] { 0 };
56 * A stack of enclosing scopes used when constructing
57 * a compilation unit or type. The top of the stack
58 * is the document fragment that children are added to.
60 protected Stack fStack = null;
63 * The number of fields constructed in the current
64 * document. This is used when building a single
65 * field document fragment, since the DOMBuilder only
66 * accepts documents with one field declaration.
68 protected int fFieldCount;
71 * The current node being constructed.
73 protected DOMNode fNode;
75 * AbstractDOMBuilder constructor.
77 public AbstractDOMBuilder() {
81 * Accepts the line separator table and converts it into a line start table.
83 * <p>A line separator might corresponds to several characters in the source.
85 * @see IDocumentElementRequestor#acceptLineSeparatorPositions(int[])
87 public void acceptLineSeparatorPositions(int[] positions) {
88 if (positions != null) {
89 int length = positions.length;
91 fLineStartPositions = new int[length + 1];
92 fLineStartPositions[0] = 0;
93 int documentLength = fDocument.length;
94 for (int i = 0; i < length; i++) {
96 int positionPlusOne = positions[i] + 1;
97 if (positionPlusOne < documentLength) {
98 if (iPlusOne < length) {
100 fLineStartPositions[iPlusOne] = positionPlusOne;
102 // no more separators
103 if (fDocument[positionPlusOne] == '\n') {
104 fLineStartPositions[iPlusOne] = positionPlusOne + 1;
106 fLineStartPositions[iPlusOne] = positionPlusOne;
110 fLineStartPositions[iPlusOne] = positionPlusOne;
119 public void acceptProblem(IProblem problem) {} //TODO: (olivier) unused?
121 * Adds the given node to the current enclosing scope, building the JDOM
122 * tree. Nodes are only added to an enclosing scope when a compilation unit or type
123 * is being built (since those are the only nodes that have children).
125 * <p>NOTE: nodes are added to the JDOM via the method #basicAddChild such that
126 * the nodes in the newly created JDOM are not fragmented.
128 protected void addChild(IDOMNode child) {
129 if (fStack.size() > 0) {
130 DOMNode parent = (DOMNode) fStack.peek();
131 if (fBuildingCU || fBuildingType) {
132 parent.basicAddChild(child);
137 * @see IDOMFactory#createCompilationUnit(String, String)
139 public IDOMCompilationUnit createCompilationUnit(char[] contents, char[] name) {
140 return createCompilationUnit(new CompilationUnit(contents, name));
143 * @see IDOMFactory#createCompilationUnit(String, String)
145 public IDOMCompilationUnit createCompilationUnit(ICompilationUnit compilationUnit) {
149 fNode.normalize(this);
150 return (IDOMCompilationUnit)fNode;
153 * @see IDocumentElementRequestor#enterClass(int, int[], int, int, int, char[], int, int, char[], int, int, char[][], int[], int[], int)
155 public void enterCompilationUnit() {
157 IDOMCompilationUnit cu= new DOMCompilationUnit(fDocument, new int[] {0, fDocument.length - 1});
162 * Finishes the configuration of the compilation unit DOM object which
163 * was created by a previous enterCompilationUnit call.
165 * @see IDocumentElementRequestor#exitCompilationUnit(int)
167 public void exitCompilationUnit(int declarationEnd) {
168 DOMCompilationUnit cu = (DOMCompilationUnit) fStack.pop();
169 cu.setSourceRangeEnd(declarationEnd);
173 * Finishes the configuration of the class and interface DOM objects.
175 * @param bodyEnd - a source position corresponding to the closing bracket of the class
176 * @param declarationEnd - a source position corresponding to the end of the class
177 * declaration. This can include whitespace and comments following the closing bracket.
179 protected void exitType(int bodyEnd, int declarationEnd) {
180 DOMType type = (DOMType)fStack.pop();
181 type.setSourceRangeEnd(declarationEnd);
182 type.setCloseBodyRangeStart(bodyEnd);
183 type.setCloseBodyRangeEnd(bodyEnd);
187 * @see ILineStartFinder#getLineStart(int)
189 public int getLineStart(int position) {
190 int lineSeparatorCount = fLineStartPositions.length;
191 // reverse traversal intentional.
192 for(int i = lineSeparatorCount - 1; i >= 0; i--) {
193 if (fLineStartPositions[i] <= position)
194 return fLineStartPositions[i];
199 * Initializes the builder to create a document fragment.
201 * @param sourceCode - the document containing the source code to be analyzed
202 * @param buildingCompilationUnit - true if a the document is being analyzed to
203 * create a compilation unit, otherwise false
204 * @param buildingType - true if the document is being analyzed to create a
205 * type or compilation unit
207 protected void initializeBuild(char[] sourceCode, boolean buildingCompilationUnit, boolean buildingType) {
208 fBuildingCU = buildingCompilationUnit;
209 fBuildingType = buildingType;
210 fStack = new Stack();
211 fDocument = sourceCode;