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;
15 import net.sourceforge.phpdt.core.JavaCore;
16 import net.sourceforge.phpdt.core.jdom.IDOMCompilationUnit;
17 import net.sourceforge.phpdt.core.jdom.IDOMFactory;
18 import net.sourceforge.phpdt.internal.compiler.ISourceElementRequestor;
19 import net.sourceforge.phpdt.internal.compiler.SourceElementParser;
20 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
21 import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
22 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
23 import net.sourceforge.phpdt.internal.core.util.CharArrayOps;
26 * A DOM builder that uses the SourceElementParser
28 public class SimpleDOMBuilder extends AbstractDOMBuilder implements
29 ISourceElementRequestor {
31 public void acceptImport(int declarationStart, int declarationEnd,
32 char[] name, boolean onDemand) {
33 int[] sourceRange = { declarationStart, declarationEnd };
34 String importName = new String(name);
35 /** name is set to contain the '*' */
37 importName += ".*"; //$NON-NLS-1$
39 fNode = new DOMImport(fDocument, sourceRange, importName, onDemand);
43 public void acceptPackage(int declarationStart, int declarationEnd,
45 int[] sourceRange = new int[] { declarationStart, declarationEnd };
46 fNode = new DOMPackage(fDocument, sourceRange, CharArrayOps
52 * @see IDOMFactory#createCompilationUnit(String, String)
54 public IDOMCompilationUnit createCompilationUnit(String sourceCode,
56 return createCompilationUnit(sourceCode.toCharArray(), name
61 * @see IDOMFactory#createCompilationUnit(String, String)
63 public IDOMCompilationUnit createCompilationUnit(
64 ICompilationUnit compilationUnit) {
65 initializeBuild(compilationUnit.getContents(), true, true);
66 getParser(JavaCore.getOptions()).parseCompilationUnit(compilationUnit,
68 return super.createCompilationUnit(compilationUnit);
72 * Creates a new DOMMethod and inizializes.
74 * @param declarationStart -
75 * a source position corresponding to the first character of this
76 * constructor declaration
78 * the modifiers for this constructor converted to a flag
80 * the name of the return type
82 * the name of this constructor
84 * a source position corresponding to the first character of the
87 * a source position corresponding to the last character of the
89 * @param parameterTypes -
90 * a list of parameter type names
91 * @param parameterNames -
92 * a list of the names of the parameters
93 * @param exceptionTypes -
94 * a list of the exception types
96 protected void enterAbstractMethod(int declarationStart, int modifiers,
97 char[] returnType, char[] name, int nameStart, int nameEnd,
98 char[][] parameterTypes, char[][] parameterNames,
99 char[][] exceptionTypes, boolean isConstructor) {
101 int[] sourceRange = { declarationStart, -1 }; // will be fixed up on
103 int[] nameRange = { nameStart, nameEnd };
104 fNode = new DOMMethod(fDocument, sourceRange, CharArrayOps
105 .charToString(name), nameRange, modifiers, isConstructor,
106 CharArrayOps.charToString(returnType), CharArrayOps
107 .charcharToString(parameterTypes), CharArrayOps
108 .charcharToString(parameterNames), CharArrayOps
109 .charcharToString(exceptionTypes));
116 public void enterClass(int declarationStart, int modifiers, char[] name,
117 int nameStart, int nameEnd, char[] superclass,
118 char[][] superinterfaces) {
119 enterType(declarationStart, modifiers, name, nameStart, nameEnd,
120 superclass, superinterfaces, true);
125 public void enterConstructor(int declarationStart, int modifiers,
126 char[] name, int nameStart, int nameEnd, char[][] parameterTypes,
127 char[][] parameterNames, char[][] exceptionTypes) {
129 String nameString = new String(fDocument, nameStart, nameEnd
131 int openParenPosition = nameString.indexOf('(');
132 if (openParenPosition > -1)
133 nameEnd = nameStart + openParenPosition - 1;
135 enterAbstractMethod(declarationStart, modifiers, null, name, nameStart,
136 nameEnd, parameterTypes, parameterNames, exceptionTypes, true);
141 public void enterField(int declarationStart, int modifiers, char[] type,
142 char[] name, int nameStart, int nameEnd) {
144 int[] sourceRange = { declarationStart, -1 };
145 int[] nameRange = { nameStart, nameEnd };
146 boolean isSecondary = false;
147 if (fNode instanceof DOMField) {
148 isSecondary = declarationStart == fNode.fSourceRange[0];
150 fNode = new DOMField(fDocument, sourceRange, CharArrayOps
151 .charToString(name), nameRange, modifiers, CharArrayOps
152 .charToString(type), isSecondary);
160 public void enterInitializer(int declarationSourceStart, int modifiers) {
161 int[] sourceRange = { declarationSourceStart, -1 };
162 fNode = new DOMInitializer(fDocument, sourceRange, modifiers);
169 public void enterInterface(int declarationStart, int modifiers,
170 char[] name, int nameStart, int nameEnd, char[][] superinterfaces) {
171 enterType(declarationStart, modifiers, name, nameStart, nameEnd, null,
172 superinterfaces, false);
177 public void enterMethod(int declarationStart, int modifiers,
178 char[] returnType, char[] name, int nameStart, int nameEnd,
179 char[][] parameterTypes, char[][] parameterNames,
180 char[][] exceptionTypes) {
181 enterAbstractMethod(declarationStart, modifiers, returnType, name,
182 nameStart, nameEnd, parameterTypes, parameterNames,
183 exceptionTypes, false);
188 protected void enterType(int declarationStart, int modifiers, char[] name,
189 int nameStart, int nameEnd, char[] superclass,
190 char[][] superinterfaces, boolean isClass) {
192 int[] sourceRange = { declarationStart, -1 }; // will be fixed in
194 int[] nameRange = new int[] { nameStart, nameEnd };
195 fNode = new DOMType(fDocument, sourceRange, new String(name),
196 nameRange, modifiers, CharArrayOps
197 .charcharToString(superinterfaces), isClass);
204 * Finishes the configuration of the class DOM object which was created by a
205 * previous enterClass call.
207 * @see ISourceElementRequestor#exitClass(int)
209 public void exitClass(int declarationEnd) {
210 exitType(declarationEnd);
214 * Finishes the configuration of the method DOM object which was created by
215 * a previous enterConstructor call.
217 * @see ISourceElementRequestor#exitConstructor(int)
219 public void exitConstructor(int declarationEnd) {
220 exitMember(declarationEnd);
225 public void exitField(int initializationStart, int declarationEnd,
226 int declarationSourceEnd) {
227 exitMember(declarationEnd);
232 public void exitInitializer(int declarationEnd) {
233 exitMember(declarationEnd);
238 public void exitInterface(int declarationEnd) {
239 exitType(declarationEnd);
243 * Finishes the configuration of the member.
245 * @param declarationEnd -
246 * a source position corresponding to the end of the method
247 * declaration. This can include whitespace and comments
248 * following the closing bracket.
250 protected void exitMember(int declarationEnd) {
251 DOMMember m = (DOMMember) fStack.pop();
252 m.setSourceRangeEnd(declarationEnd);
258 public void exitMethod(int declarationEnd) {
259 exitMember(declarationEnd);
263 * @see AbstractDOMBuilder#exitType
265 * @param declarationEnd -
266 * a source position corresponding to the end of the class
267 * declaration. This can include whitespace and comments
268 * following the closing bracket.
270 protected void exitType(int declarationEnd) {
271 exitType(declarationEnd, declarationEnd);
275 * Creates a new parser.
277 protected SourceElementParser getParser(Map settings) {
278 return new SourceElementParser(this, new DefaultProblemFactory(),
279 new CompilerOptions(settings));