new version with WorkingCopy Management
[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.jdom.IDOMCompilationUnit;
16 import net.sourceforge.phpdt.internal.compiler.ISourceElementRequestor;
17 import net.sourceforge.phpdt.internal.compiler.SourceElementParser;
18 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
19 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
20 import net.sourceforge.phpdt.internal.core.util.CharArrayOps;
21 import net.sourceforge.phpeclipse.PHPCore;
22
23 /**
24  * A DOM builder that uses the SourceElementParser
25  */
26 public class SimpleDOMBuilder extends AbstractDOMBuilder implements ISourceElementRequestor {
27
28 public void acceptImport(int declarationStart, int declarationEnd, char[] name, boolean onDemand) {
29         int[] sourceRange = {declarationStart, declarationEnd};
30         String importName = new String(name);
31         /** name is set to contain the '*' */
32         if (onDemand) {
33                 importName+=".*"; //$NON-NLS-1$
34         }
35         fNode= new DOMImport(fDocument, sourceRange, importName, onDemand);
36         addChild(fNode);        
37 }
38 public void acceptPackage(int declarationStart, int declarationEnd, char[] name) {
39         int[] sourceRange= new int[] {declarationStart, declarationEnd};
40         fNode= new DOMPackage(fDocument, sourceRange, CharArrayOps.charToString(name));
41         addChild(fNode);        
42 }
43 /**
44  * @see IDOMFactory#createCompilationUnit(String, String)
45  */
46 public IDOMCompilationUnit createCompilationUnit(String sourceCode, String name) {
47         return createCompilationUnit(sourceCode.toCharArray(), name.toCharArray());
48 }
49 /**
50  * @see IDOMFactory#createCompilationUnit(String, String)
51  */
52 public IDOMCompilationUnit createCompilationUnit(ICompilationUnit compilationUnit) {
53         initializeBuild(compilationUnit.getContents(), true, true);
54         getParser(PHPCore.getOptions()).parseCompilationUnit(compilationUnit, false);
55         return super.createCompilationUnit(compilationUnit);
56 }
57 /**
58  * Creates a new DOMMethod and inizializes.
59  *
60  * @param declarationStart - a source position corresponding to the first character 
61  *              of this constructor declaration
62  * @param modifiers - the modifiers for this constructor converted to a flag
63  * @param returnType - the name of the return type
64  * @param name - the name of this constructor
65  * @param nameStart - a source position corresponding to the first character of the name
66  * @param nameEnd - a source position corresponding to the last character of the name
67  * @param parameterTypes - a list of parameter type names
68  * @param parameterNames - a list of the names of the parameters
69  * @param exceptionTypes - a list of the exception types
70  */
71 protected void enterAbstractMethod(int declarationStart, int modifiers,
72         char[] returnType, char[] name, int nameStart, int nameEnd, char[][] parameterTypes,
73         char[][] parameterNames, char[][] exceptionTypes, boolean isConstructor) {
74                 
75         int[] sourceRange = {declarationStart, -1}; // will be fixed up on exit
76         int[] nameRange = {nameStart, nameEnd};
77         fNode = new DOMMethod(fDocument, sourceRange, CharArrayOps.charToString(name), nameRange, modifiers, 
78                 isConstructor, CharArrayOps.charToString(returnType),
79                 CharArrayOps.charcharToString(parameterTypes),
80                 CharArrayOps.charcharToString(parameterNames), 
81                 CharArrayOps.charcharToString(exceptionTypes));
82         addChild(fNode);
83         fStack.push(fNode);
84 }
85 /**
86  */
87 public void enterClass(int declarationStart, int modifiers, char[] name, int nameStart, int nameEnd, char[] superclass, char[][] superinterfaces) {
88         enterType(declarationStart, modifiers, name, nameStart, nameEnd, superclass,
89                 superinterfaces, true);
90 }
91 /**
92  */
93 public void enterConstructor(int declarationStart, int modifiers, char[] name, int nameStart, int nameEnd, char[][] parameterTypes, char[][] parameterNames, char[][] exceptionTypes) {
94         /* see 1FVIIQZ */
95         String nameString = new String(fDocument, nameStart, nameEnd - nameStart);
96         int openParenPosition = nameString.indexOf('(');
97         if (openParenPosition > -1)
98                 nameEnd = nameStart + openParenPosition - 1;
99
100         enterAbstractMethod(declarationStart, modifiers, 
101                 null, name, nameStart, nameEnd, parameterTypes,
102                 parameterNames, exceptionTypes,true);
103 }
104 /**
105  */
106 public void enterField(int declarationStart, int modifiers, char[] type, char[] name, int nameStart, int nameEnd) {
107
108         int[] sourceRange = {declarationStart, -1};
109         int[] nameRange = {nameStart, nameEnd};
110         boolean isSecondary= false;
111         if (fNode instanceof DOMField) {
112                 isSecondary = declarationStart == fNode.fSourceRange[0];
113         }
114         fNode = new DOMField(fDocument, sourceRange, CharArrayOps.charToString(name), nameRange, 
115                 modifiers, CharArrayOps.charToString(type), isSecondary);
116         addChild(fNode);
117         fStack.push(fNode);
118 }
119 /**
120
121  */
122 public void enterInitializer(int declarationSourceStart, int modifiers) {
123         int[] sourceRange = {declarationSourceStart, -1};
124         fNode = new DOMInitializer(fDocument, sourceRange, modifiers);
125         addChild(fNode);
126         fStack.push(fNode);
127 }
128 /**
129  */
130 public void enterInterface(int declarationStart, int modifiers, char[] name, int nameStart, int nameEnd, char[][] superinterfaces) {
131         enterType(declarationStart, modifiers, name, nameStart, nameEnd, null,
132                 superinterfaces, false);
133 }
134 /**
135  */
136 public void enterMethod(int declarationStart, int modifiers, char[] returnType, char[] name, int nameStart, int nameEnd, char[][] parameterTypes, char[][] parameterNames, char[][] exceptionTypes) {
137         enterAbstractMethod(declarationStart, modifiers, 
138                 returnType, name, nameStart, nameEnd, parameterTypes,
139                 parameterNames, exceptionTypes,false);
140 }
141 /**
142  */
143 protected void enterType(int declarationStart, int modifiers, char[] name, 
144         int nameStart, int nameEnd, char[] superclass, char[][] superinterfaces, boolean isClass) {
145         if (fBuildingType) {
146                 int[] sourceRange = {declarationStart, -1}; // will be fixed in the exit
147                 int[] nameRange = new int[] {nameStart, nameEnd};
148                 fNode = new DOMType(fDocument, sourceRange, new String(name), nameRange,
149                         modifiers, CharArrayOps.charcharToString(superinterfaces), isClass);
150                 addChild(fNode);
151                 fStack.push(fNode);
152         }
153 }
154 /**
155  * Finishes the configuration of the class DOM object which
156  * was created by a previous enterClass call.
157  *
158  * @see ISourceElementRequestor#exitClass(int)
159  */
160 public void exitClass(int declarationEnd) {
161         exitType(declarationEnd);
162 }
163 /**
164  * Finishes the configuration of the method DOM object which
165  * was created by a previous enterConstructor call.
166  *
167  * @see ISourceElementRequestor#exitConstructor(int)
168  */
169 public void exitConstructor(int declarationEnd) {
170         exitMember(declarationEnd);
171 }
172 /**
173  */
174 public void exitField(int initializationStart, int declarationEnd, int declarationSourceEnd) {
175         exitMember(declarationEnd);
176 }
177 /**
178  */
179 public void exitInitializer(int declarationEnd) {
180         exitMember(declarationEnd);
181 }
182 /**
183  */
184 public void exitInterface(int declarationEnd) {
185         exitType(declarationEnd);
186 }
187 /**
188  * Finishes the configuration of the member.
189  *
190  * @param declarationEnd - a source position corresponding to the end of the method
191  *              declaration.  This can include whitespace and comments following the closing bracket.
192  */
193 protected void exitMember(int declarationEnd) {
194         DOMMember m= (DOMMember) fStack.pop();
195         m.setSourceRangeEnd(declarationEnd);
196         fNode = m;
197 }
198 /**
199  */
200 public void exitMethod(int declarationEnd) {
201         exitMember(declarationEnd);
202 }
203 /**
204  * @see AbstractDOMBuilder#exitType
205  *
206  * @param declarationEnd - a source position corresponding to the end of the class
207  *              declaration.  This can include whitespace and comments following the closing bracket.
208  */
209 protected void exitType(int declarationEnd) {
210         exitType(declarationEnd, declarationEnd);
211 }
212 /**
213  * Creates a new parser.
214  */
215 protected SourceElementParser getParser(Map settings) {
216         return new SourceElementParser(this, new DefaultProblemFactory());//, new CompilerOptions(settings));
217 }
218 }