A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / jdom / SimpleDOMBuilder.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.jdom;
12
13 import java.util.Map;
14
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;
24
25 /**
26  * A DOM builder that uses the SourceElementParser
27  */
28 public class SimpleDOMBuilder extends AbstractDOMBuilder implements
29                 ISourceElementRequestor {
30
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 '*' */
36                 if (onDemand) {
37                         importName += ".*"; //$NON-NLS-1$
38                 }
39                 fNode = new DOMImport(fDocument, sourceRange, importName, onDemand);
40                 addChild(fNode);
41         }
42
43         public void acceptPackage(int declarationStart, int declarationEnd,
44                         char[] name) {
45                 int[] sourceRange = new int[] { declarationStart, declarationEnd };
46                 fNode = new DOMPackage(fDocument, sourceRange, CharArrayOps
47                                 .charToString(name));
48                 addChild(fNode);
49         }
50
51         /**
52          * @see IDOMFactory#createCompilationUnit(String, String)
53          */
54         public IDOMCompilationUnit createCompilationUnit(String sourceCode,
55                         String name) {
56                 return createCompilationUnit(sourceCode.toCharArray(), name
57                                 .toCharArray());
58         }
59
60         /**
61          * @see IDOMFactory#createCompilationUnit(String, String)
62          */
63         public IDOMCompilationUnit createCompilationUnit(
64                         ICompilationUnit compilationUnit) {
65                 initializeBuild(compilationUnit.getContents(), true, true);
66                 getParser(JavaCore.getOptions()).parseCompilationUnit(compilationUnit,
67                                 false);
68                 return super.createCompilationUnit(compilationUnit);
69         }
70
71         /**
72          * Creates a new DOMMethod and inizializes.
73          * 
74          * @param declarationStart -
75          *            a source position corresponding to the first character of this
76          *            constructor declaration
77          * @param modifiers -
78          *            the modifiers for this constructor converted to a flag
79          * @param returnType -
80          *            the name of the return type
81          * @param name -
82          *            the name of this constructor
83          * @param nameStart -
84          *            a source position corresponding to the first character of the
85          *            name
86          * @param nameEnd -
87          *            a source position corresponding to the last character of the
88          *            name
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
95          */
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) {
100
101                 int[] sourceRange = { declarationStart, -1 }; // will be fixed up on
102                                                                                                                 // exit
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));
110                 addChild(fNode);
111                 fStack.push(fNode);
112         }
113
114         /**
115          */
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);
121         }
122
123         /**
124          */
125         public void enterConstructor(int declarationStart, int modifiers,
126                         char[] name, int nameStart, int nameEnd, char[][] parameterTypes,
127                         char[][] parameterNames, char[][] exceptionTypes) {
128                 /* see 1FVIIQZ */
129                 String nameString = new String(fDocument, nameStart, nameEnd
130                                 - nameStart);
131                 int openParenPosition = nameString.indexOf('(');
132                 if (openParenPosition > -1)
133                         nameEnd = nameStart + openParenPosition - 1;
134
135                 enterAbstractMethod(declarationStart, modifiers, null, name, nameStart,
136                                 nameEnd, parameterTypes, parameterNames, exceptionTypes, true);
137         }
138
139         /**
140          */
141         public void enterField(int declarationStart, int modifiers, char[] type,
142                         char[] name, int nameStart, int nameEnd) {
143
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];
149                 }
150                 fNode = new DOMField(fDocument, sourceRange, CharArrayOps
151                                 .charToString(name), nameRange, modifiers, CharArrayOps
152                                 .charToString(type), isSecondary);
153                 addChild(fNode);
154                 fStack.push(fNode);
155         }
156
157         /**
158          * 
159          */
160         public void enterInitializer(int declarationSourceStart, int modifiers) {
161                 int[] sourceRange = { declarationSourceStart, -1 };
162                 fNode = new DOMInitializer(fDocument, sourceRange, modifiers);
163                 addChild(fNode);
164                 fStack.push(fNode);
165         }
166
167         /**
168          */
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);
173         }
174
175         /**
176          */
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);
184         }
185
186         /**
187          */
188         protected void enterType(int declarationStart, int modifiers, char[] name,
189                         int nameStart, int nameEnd, char[] superclass,
190                         char[][] superinterfaces, boolean isClass) {
191                 if (fBuildingType) {
192                         int[] sourceRange = { declarationStart, -1 }; // will be fixed in
193                                                                                                                         // the exit
194                         int[] nameRange = new int[] { nameStart, nameEnd };
195                         fNode = new DOMType(fDocument, sourceRange, new String(name),
196                                         nameRange, modifiers, CharArrayOps
197                                                         .charcharToString(superinterfaces), isClass);
198                         addChild(fNode);
199                         fStack.push(fNode);
200                 }
201         }
202
203         /**
204          * Finishes the configuration of the class DOM object which was created by a
205          * previous enterClass call.
206          * 
207          * @see ISourceElementRequestor#exitClass(int)
208          */
209         public void exitClass(int declarationEnd) {
210                 exitType(declarationEnd);
211         }
212
213         /**
214          * Finishes the configuration of the method DOM object which was created by
215          * a previous enterConstructor call.
216          * 
217          * @see ISourceElementRequestor#exitConstructor(int)
218          */
219         public void exitConstructor(int declarationEnd) {
220                 exitMember(declarationEnd);
221         }
222
223         /**
224          */
225         public void exitField(int initializationStart, int declarationEnd,
226                         int declarationSourceEnd) {
227                 exitMember(declarationEnd);
228         }
229
230         /**
231          */
232         public void exitInitializer(int declarationEnd) {
233                 exitMember(declarationEnd);
234         }
235
236         /**
237          */
238         public void exitInterface(int declarationEnd) {
239                 exitType(declarationEnd);
240         }
241
242         /**
243          * Finishes the configuration of the member.
244          * 
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.
249          */
250         protected void exitMember(int declarationEnd) {
251                 DOMMember m = (DOMMember) fStack.pop();
252                 m.setSourceRangeEnd(declarationEnd);
253                 fNode = m;
254         }
255
256         /**
257          */
258         public void exitMethod(int declarationEnd) {
259                 exitMember(declarationEnd);
260         }
261
262         /**
263          * @see AbstractDOMBuilder#exitType
264          * 
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.
269          */
270         protected void exitType(int declarationEnd) {
271                 exitType(declarationEnd, declarationEnd);
272         }
273
274         /**
275          * Creates a new parser.
276          */
277         protected SourceElementParser getParser(Map settings) {
278                 return new SourceElementParser(this, new DefaultProblemFactory(),
279                                 new CompilerOptions(settings));
280         }
281 }