Organized imports
[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 ISourceElementRequestor {
29
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 '*' */
34     if (onDemand) {
35       importName += ".*"; //$NON-NLS-1$
36     }
37     fNode = new DOMImport(fDocument, sourceRange, importName, onDemand);
38     addChild(fNode);
39   }
40
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));
44     addChild(fNode);
45   }
46
47   /**
48    * @see IDOMFactory#createCompilationUnit(String, String)
49    */
50   public IDOMCompilationUnit createCompilationUnit(String sourceCode, String name) {
51     return createCompilationUnit(sourceCode.toCharArray(), name.toCharArray());
52   }
53
54   /**
55    * @see IDOMFactory#createCompilationUnit(String, String)
56    */
57   public IDOMCompilationUnit createCompilationUnit(ICompilationUnit compilationUnit) {
58     initializeBuild(compilationUnit.getContents(), true, true);
59     getParser(JavaCore.getOptions()).parseCompilationUnit(compilationUnit, false);
60     return super.createCompilationUnit(compilationUnit);
61   }
62
63   /**
64    * Creates a new DOMMethod and inizializes.
65    * 
66    * @param declarationStart -
67    *          a source position corresponding to the first character of this constructor declaration
68    * @param modifiers -
69    *          the modifiers for this constructor converted to a flag
70    * @param returnType -
71    *          the name of the return type
72    * @param name -
73    *          the name of this constructor
74    * @param nameStart -
75    *          a source position corresponding to the first character of the name
76    * @param nameEnd -
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
84    */
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) {
87
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));
93     addChild(fNode);
94     fStack.push(fNode);
95   }
96
97   /**
98    */
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);
102   }
103
104   /**
105    */
106   public void enterConstructor(int declarationStart, int modifiers, char[] name, int nameStart, int nameEnd,
107       char[][] parameterTypes, char[][] parameterNames, char[][] exceptionTypes) {
108     /* see 1FVIIQZ */
109     String nameString = new String(fDocument, nameStart, nameEnd - nameStart);
110     int openParenPosition = nameString.indexOf('(');
111     if (openParenPosition > -1)
112       nameEnd = nameStart + openParenPosition - 1;
113
114     enterAbstractMethod(declarationStart, modifiers, null, name, nameStart, nameEnd, parameterTypes, parameterNames,
115         exceptionTypes, true);
116   }
117
118   /**
119    */
120   public void enterField(int declarationStart, int modifiers, char[] type, char[] name, int nameStart, int nameEnd) {
121
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];
127     }
128     fNode = new DOMField(fDocument, sourceRange, CharArrayOps.charToString(name), nameRange, modifiers, CharArrayOps
129         .charToString(type), isSecondary);
130     addChild(fNode);
131     fStack.push(fNode);
132   }
133
134   /**
135    *  
136    */
137   public void enterInitializer(int declarationSourceStart, int modifiers) {
138     int[] sourceRange = { declarationSourceStart, -1 };
139     fNode = new DOMInitializer(fDocument, sourceRange, modifiers);
140     addChild(fNode);
141     fStack.push(fNode);
142   }
143
144   /**
145    */
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);
148   }
149
150   /**
151    */
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);
156   }
157
158   /**
159    */
160   protected void enterType(int declarationStart, int modifiers, char[] name, int nameStart, int nameEnd, char[] superclass,
161       char[][] superinterfaces, boolean isClass) {
162     if (fBuildingType) {
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);
167       addChild(fNode);
168       fStack.push(fNode);
169     }
170   }
171
172   /**
173    * Finishes the configuration of the class DOM object which was created by a previous enterClass call.
174    * 
175    * @see ISourceElementRequestor#exitClass(int)
176    */
177   public void exitClass(int declarationEnd) {
178     exitType(declarationEnd);
179   }
180
181   /**
182    * Finishes the configuration of the method DOM object which was created by a previous enterConstructor call.
183    * 
184    * @see ISourceElementRequestor#exitConstructor(int)
185    */
186   public void exitConstructor(int declarationEnd) {
187     exitMember(declarationEnd);
188   }
189
190   /**
191    */
192   public void exitField(int initializationStart, int declarationEnd, int declarationSourceEnd) {
193     exitMember(declarationEnd);
194   }
195
196   /**
197    */
198   public void exitInitializer(int declarationEnd) {
199     exitMember(declarationEnd);
200   }
201
202   /**
203    */
204   public void exitInterface(int declarationEnd) {
205     exitType(declarationEnd);
206   }
207
208   /**
209    * Finishes the configuration of the member.
210    * 
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.
214    */
215   protected void exitMember(int declarationEnd) {
216     DOMMember m = (DOMMember) fStack.pop();
217     m.setSourceRangeEnd(declarationEnd);
218     fNode = m;
219   }
220
221   /**
222    */
223   public void exitMethod(int declarationEnd) {
224     exitMember(declarationEnd);
225   }
226
227   /**
228    * @see AbstractDOMBuilder#exitType
229    * 
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.
233    */
234   protected void exitType(int declarationEnd) {
235     exitType(declarationEnd, declarationEnd);
236   }
237
238   /**
239    * Creates a new parser.
240    */
241   protected SourceElementParser getParser(Map settings) {
242     return new SourceElementParser(this, new DefaultProblemFactory(), new CompilerOptions(settings));
243   }
244 }