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 ISourceElementRequestor {
30 public void acceptImport(int declarationStart, int declarationEnd, char[] name, boolean onDemand) {
31 int[] sourceRange = { declarationStart, declarationEnd };
32 String importName = new String(name);
33 /** name is set to contain the '*' */
35 importName += ".*"; //$NON-NLS-1$
37 fNode = new DOMImport(fDocument, sourceRange, importName, onDemand);
41 public void acceptPackage(int declarationStart, int declarationEnd, char[] name) {
42 int[] sourceRange = new int[] { declarationStart, declarationEnd };
43 fNode = new DOMPackage(fDocument, sourceRange, CharArrayOps.charToString(name));
48 * @see IDOMFactory#createCompilationUnit(String, String)
50 public IDOMCompilationUnit createCompilationUnit(String sourceCode, String name) {
51 return createCompilationUnit(sourceCode.toCharArray(), name.toCharArray());
55 * @see IDOMFactory#createCompilationUnit(String, String)
57 public IDOMCompilationUnit createCompilationUnit(ICompilationUnit compilationUnit) {
58 initializeBuild(compilationUnit.getContents(), true, true);
59 getParser(JavaCore.getOptions()).parseCompilationUnit(compilationUnit, false);
60 return super.createCompilationUnit(compilationUnit);
64 * Creates a new DOMMethod and inizializes.
66 * @param declarationStart -
67 * a source position corresponding to the first character of this constructor declaration
69 * the modifiers for this constructor converted to a flag
71 * the name of the return type
73 * the name of this constructor
75 * a source position corresponding to the first character of the name
77 * a source position corresponding to the last character of the name
78 * @param parameterTypes -
79 * a list of parameter type names
80 * @param parameterNames -
81 * a list of the names of the parameters
82 * @param exceptionTypes -
83 * a list of the exception types
85 protected void enterAbstractMethod(int declarationStart, int modifiers, char[] returnType, char[] name, int nameStart,
86 int nameEnd, char[][] parameterTypes, char[][] parameterNames, char[][] exceptionTypes, boolean isConstructor) {
88 int[] sourceRange = { declarationStart, -1 }; // will be fixed up on exit
89 int[] nameRange = { nameStart, nameEnd };
90 fNode = new DOMMethod(fDocument, sourceRange, CharArrayOps.charToString(name), nameRange, modifiers, isConstructor,
91 CharArrayOps.charToString(returnType), CharArrayOps.charcharToString(parameterTypes), CharArrayOps
92 .charcharToString(parameterNames), CharArrayOps.charcharToString(exceptionTypes));
99 public void enterClass(int declarationStart, int modifiers, char[] name, int nameStart, int nameEnd, char[] superclass,
100 char[][] superinterfaces) {
101 enterType(declarationStart, modifiers, name, nameStart, nameEnd, superclass, superinterfaces, true);
106 public void enterConstructor(int declarationStart, int modifiers, char[] name, int nameStart, int nameEnd,
107 char[][] parameterTypes, char[][] parameterNames, char[][] exceptionTypes) {
109 String nameString = new String(fDocument, nameStart, nameEnd - nameStart);
110 int openParenPosition = nameString.indexOf('(');
111 if (openParenPosition > -1)
112 nameEnd = nameStart + openParenPosition - 1;
114 enterAbstractMethod(declarationStart, modifiers, null, name, nameStart, nameEnd, parameterTypes, parameterNames,
115 exceptionTypes, true);
120 public void enterField(int declarationStart, int modifiers, char[] type, char[] name, int nameStart, int nameEnd) {
122 int[] sourceRange = { declarationStart, -1 };
123 int[] nameRange = { nameStart, nameEnd };
124 boolean isSecondary = false;
125 if (fNode instanceof DOMField) {
126 isSecondary = declarationStart == fNode.fSourceRange[0];
128 fNode = new DOMField(fDocument, sourceRange, CharArrayOps.charToString(name), nameRange, modifiers, CharArrayOps
129 .charToString(type), isSecondary);
137 public void enterInitializer(int declarationSourceStart, int modifiers) {
138 int[] sourceRange = { declarationSourceStart, -1 };
139 fNode = new DOMInitializer(fDocument, sourceRange, modifiers);
146 public void enterInterface(int declarationStart, int modifiers, char[] name, int nameStart, int nameEnd, char[][] superinterfaces) {
147 enterType(declarationStart, modifiers, name, nameStart, nameEnd, null, superinterfaces, false);
152 public void enterMethod(int declarationStart, int modifiers, char[] returnType, char[] name, int nameStart, int nameEnd,
153 char[][] parameterTypes, char[][] parameterNames, char[][] exceptionTypes) {
154 enterAbstractMethod(declarationStart, modifiers, returnType, name, nameStart, nameEnd, parameterTypes, parameterNames,
155 exceptionTypes, false);
160 protected void enterType(int declarationStart, int modifiers, char[] name, int nameStart, int nameEnd, char[] superclass,
161 char[][] superinterfaces, boolean isClass) {
163 int[] sourceRange = { declarationStart, -1 }; // will be fixed in the exit
164 int[] nameRange = new int[] { nameStart, nameEnd };
165 fNode = new DOMType(fDocument, sourceRange, new String(name), nameRange, modifiers, CharArrayOps
166 .charcharToString(superinterfaces), isClass);
173 * Finishes the configuration of the class DOM object which was created by a previous enterClass call.
175 * @see ISourceElementRequestor#exitClass(int)
177 public void exitClass(int declarationEnd) {
178 exitType(declarationEnd);
182 * Finishes the configuration of the method DOM object which was created by a previous enterConstructor call.
184 * @see ISourceElementRequestor#exitConstructor(int)
186 public void exitConstructor(int declarationEnd) {
187 exitMember(declarationEnd);
192 public void exitField(int initializationStart, int declarationEnd, int declarationSourceEnd) {
193 exitMember(declarationEnd);
198 public void exitInitializer(int declarationEnd) {
199 exitMember(declarationEnd);
204 public void exitInterface(int declarationEnd) {
205 exitType(declarationEnd);
209 * Finishes the configuration of the member.
211 * @param declarationEnd -
212 * a source position corresponding to the end of the method declaration. This can include whitespace and comments
213 * following the closing bracket.
215 protected void exitMember(int declarationEnd) {
216 DOMMember m = (DOMMember) fStack.pop();
217 m.setSourceRangeEnd(declarationEnd);
223 public void exitMethod(int declarationEnd) {
224 exitMember(declarationEnd);
228 * @see AbstractDOMBuilder#exitType
230 * @param declarationEnd -
231 * a source position corresponding to the end of the class declaration. This can include whitespace and comments
232 * following the closing bracket.
234 protected void exitType(int declarationEnd) {
235 exitType(declarationEnd, declarationEnd);
239 * Creates a new parser.
241 protected SourceElementParser getParser(Map settings) {
242 return new SourceElementParser(this, new DefaultProblemFactory(), new CompilerOptions(settings));