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);
40 public void acceptPackage(int declarationStart, int declarationEnd, char[] name) {
41 int[] sourceRange= new int[] {declarationStart, declarationEnd};
42 fNode= new DOMPackage(fDocument, sourceRange, CharArrayOps.charToString(name));
46 * @see IDOMFactory#createCompilationUnit(String, String)
48 public IDOMCompilationUnit createCompilationUnit(String sourceCode, String name) {
49 return createCompilationUnit(sourceCode.toCharArray(), name.toCharArray());
52 * @see IDOMFactory#createCompilationUnit(String, String)
54 public IDOMCompilationUnit createCompilationUnit(ICompilationUnit compilationUnit) {
55 initializeBuild(compilationUnit.getContents(), true, true);
56 getParser(JavaCore.getOptions()).parseCompilationUnit(compilationUnit, false);
57 return super.createCompilationUnit(compilationUnit);
60 * Creates a new DOMMethod and inizializes.
62 * @param declarationStart - a source position corresponding to the first character
63 * of this constructor declaration
64 * @param modifiers - the modifiers for this constructor converted to a flag
65 * @param returnType - the name of the return type
66 * @param name - the name of this constructor
67 * @param nameStart - a source position corresponding to the first character of the name
68 * @param nameEnd - a source position corresponding to the last character of the name
69 * @param parameterTypes - a list of parameter type names
70 * @param parameterNames - a list of the names of the parameters
71 * @param exceptionTypes - a list of the exception types
73 protected void enterAbstractMethod(int declarationStart, int modifiers,
74 char[] returnType, char[] name, int nameStart, int nameEnd, char[][] parameterTypes,
75 char[][] parameterNames, char[][] exceptionTypes, boolean isConstructor) {
77 int[] sourceRange = {declarationStart, -1}; // will be fixed up on exit
78 int[] nameRange = {nameStart, nameEnd};
79 fNode = new DOMMethod(fDocument, sourceRange, CharArrayOps.charToString(name), nameRange, modifiers,
80 isConstructor, CharArrayOps.charToString(returnType),
81 CharArrayOps.charcharToString(parameterTypes),
82 CharArrayOps.charcharToString(parameterNames),
83 CharArrayOps.charcharToString(exceptionTypes));
89 public void enterClass(int declarationStart, int modifiers, char[] name, int nameStart, int nameEnd, char[] superclass, char[][] superinterfaces) {
90 enterType(declarationStart, modifiers, name, nameStart, nameEnd, superclass,
91 superinterfaces, true);
95 public void enterConstructor(int declarationStart, int modifiers, char[] name, int nameStart, int nameEnd, char[][] parameterTypes, char[][] parameterNames, char[][] exceptionTypes) {
97 String nameString = new String(fDocument, nameStart, nameEnd - nameStart);
98 int openParenPosition = nameString.indexOf('(');
99 if (openParenPosition > -1)
100 nameEnd = nameStart + openParenPosition - 1;
102 enterAbstractMethod(declarationStart, modifiers,
103 null, name, nameStart, nameEnd, parameterTypes,
104 parameterNames, exceptionTypes,true);
108 public void enterField(int declarationStart, int modifiers, char[] type, char[] name, int nameStart, int nameEnd) {
110 int[] sourceRange = {declarationStart, -1};
111 int[] nameRange = {nameStart, nameEnd};
112 boolean isSecondary= false;
113 if (fNode instanceof DOMField) {
114 isSecondary = declarationStart == fNode.fSourceRange[0];
116 fNode = new DOMField(fDocument, sourceRange, CharArrayOps.charToString(name), nameRange,
117 modifiers, CharArrayOps.charToString(type), isSecondary);
124 public void enterInitializer(int declarationSourceStart, int modifiers) {
125 int[] sourceRange = {declarationSourceStart, -1};
126 fNode = new DOMInitializer(fDocument, sourceRange, modifiers);
132 public void enterInterface(int declarationStart, int modifiers, char[] name, int nameStart, int nameEnd, char[][] superinterfaces) {
133 enterType(declarationStart, modifiers, name, nameStart, nameEnd, null,
134 superinterfaces, false);
138 public void enterMethod(int declarationStart, int modifiers, char[] returnType, char[] name, int nameStart, int nameEnd, char[][] parameterTypes, char[][] parameterNames, char[][] exceptionTypes) {
139 enterAbstractMethod(declarationStart, modifiers,
140 returnType, name, nameStart, nameEnd, parameterTypes,
141 parameterNames, exceptionTypes,false);
145 protected void enterType(int declarationStart, int modifiers, char[] name,
146 int nameStart, int nameEnd, char[] superclass, char[][] superinterfaces, boolean isClass) {
148 int[] sourceRange = {declarationStart, -1}; // will be fixed in the exit
149 int[] nameRange = new int[] {nameStart, nameEnd};
150 fNode = new DOMType(fDocument, sourceRange, new String(name), nameRange,
151 modifiers, CharArrayOps.charcharToString(superinterfaces), isClass);
157 * Finishes the configuration of the class DOM object which
158 * was created by a previous enterClass call.
160 * @see ISourceElementRequestor#exitClass(int)
162 public void exitClass(int declarationEnd) {
163 exitType(declarationEnd);
166 * Finishes the configuration of the method DOM object which
167 * was created by a previous enterConstructor call.
169 * @see ISourceElementRequestor#exitConstructor(int)
171 public void exitConstructor(int declarationEnd) {
172 exitMember(declarationEnd);
176 public void exitField(int initializationStart, int declarationEnd, int declarationSourceEnd) {
177 exitMember(declarationEnd);
181 public void exitInitializer(int declarationEnd) {
182 exitMember(declarationEnd);
186 public void exitInterface(int declarationEnd) {
187 exitType(declarationEnd);
190 * Finishes the configuration of the member.
192 * @param declarationEnd - a source position corresponding to the end of the method
193 * declaration. This can include whitespace and comments following the closing bracket.
195 protected void exitMember(int declarationEnd) {
196 DOMMember m= (DOMMember) fStack.pop();
197 m.setSourceRangeEnd(declarationEnd);
202 public void exitMethod(int declarationEnd) {
203 exitMember(declarationEnd);
206 * @see AbstractDOMBuilder#exitType
208 * @param declarationEnd - a source position corresponding to the end of the class
209 * declaration. This can include whitespace and comments following the closing bracket.
211 protected void exitType(int declarationEnd) {
212 exitType(declarationEnd, declarationEnd);
215 * Creates a new parser.
217 protected SourceElementParser getParser(Map settings) {
218 return new SourceElementParser(this, new DefaultProblemFactory(), new CompilerOptions(settings));