KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > jdom > SimpleDOMBuilder


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.jdom;
12
13 import java.util.Map JavaDoc;
14
15 import org.eclipse.jdt.core.JavaCore;
16 import org.eclipse.jdt.core.compiler.CategorizedProblem;
17 import org.eclipse.jdt.core.compiler.CharOperation;
18 import org.eclipse.jdt.core.jdom.*;
19 import org.eclipse.jdt.internal.compiler.ISourceElementRequestor;
20 import org.eclipse.jdt.internal.compiler.SourceElementParser;
21 import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
22 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
23 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
24 import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
25 /**
26  * A DOM builder that uses the SourceElementParser
27  * @deprecated The JDOM was made obsolete by the addition in 2.0 of the more
28  * powerful, fine-grained DOM/AST API found in the
29  * org.eclipse.jdt.core.dom package.
30  */

31 public class SimpleDOMBuilder extends AbstractDOMBuilder implements ISourceElementRequestor {
32
33 /**
34  * Does nothing.
35  */

36 public void acceptProblem(CategorizedProblem problem) {
37     // nothing to do
38
}
39
40 public void acceptImport(int declarationStart, int declarationEnd, char[][] tokens, boolean onDemand, int modifiers) {
41     int[] sourceRange = {declarationStart, declarationEnd};
42     String JavaDoc importName = new String JavaDoc(CharOperation.concatWith(tokens, '.'));
43     /** name is set to contain the '*' */
44     if (onDemand) {
45         importName+=".*"; //$NON-NLS-1$
46
}
47     fNode= new DOMImport(fDocument, sourceRange, importName, onDemand, modifiers);
48     addChild(fNode);
49 }
50 public void acceptPackage(int declarationStart, int declarationEnd, char[] name) {
51     int[] sourceRange= new int[] {declarationStart, declarationEnd};
52     fNode= new DOMPackage(fDocument, sourceRange, CharOperation.charToString(name));
53     addChild(fNode);
54 }
55 /**
56  * @see IDOMFactory#createCompilationUnit(String, String)
57  */

58 public IDOMCompilationUnit createCompilationUnit(String JavaDoc sourceCode, String JavaDoc name) {
59     return createCompilationUnit(sourceCode.toCharArray(), name.toCharArray());
60 }
61 /**
62  * @see IDOMFactory#createCompilationUnit(String, String)
63  */

64 public IDOMCompilationUnit createCompilationUnit(ICompilationUnit compilationUnit) {
65     initializeBuild(compilationUnit.getContents(), true, true);
66     getParser(JavaCore.getOptions()).parseCompilationUnit(compilationUnit, false/*diet parse*/);
67     return super.createCompilationUnit(compilationUnit);
68 }
69 /**
70  * Creates a new DOMMethod and inizializes.
71  */

72 protected void enterAbstractMethod(MethodInfo methodInfo) {
73         
74     int[] sourceRange = {methodInfo.declarationStart, -1}; // will be fixed up on exit
75
int[] nameRange = {methodInfo.nameSourceStart, methodInfo.nameSourceEnd};
76     fNode = new DOMMethod(fDocument, sourceRange, CharOperation.charToString(methodInfo.name), nameRange, methodInfo.modifiers,
77         methodInfo.isConstructor, CharOperation.charToString(methodInfo.returnType),
78         CharOperation.charArrayToStringArray(methodInfo.parameterTypes),
79         CharOperation.charArrayToStringArray(methodInfo.parameterNames),
80         CharOperation.charArrayToStringArray(methodInfo.exceptionTypes));
81     addChild(fNode);
82     fStack.push(fNode);
83     
84     // type parameters not supported by JDOM
85
}
86 /**
87  */

88 public void enterConstructor(MethodInfo methodInfo) {
89     /* see 1FVIIQZ */
90     String JavaDoc nameString = new String JavaDoc(fDocument, methodInfo.nameSourceStart, methodInfo.nameSourceEnd - methodInfo.nameSourceStart);
91     int openParenPosition = nameString.indexOf('(');
92     if (openParenPosition > -1)
93         methodInfo.nameSourceEnd = methodInfo.nameSourceStart + openParenPosition - 1;
94
95     enterAbstractMethod(methodInfo);
96 }
97 /**
98  */

99 public void enterField(FieldInfo fieldInfo) {
100
101     int[] sourceRange = {fieldInfo.declarationStart, -1};
102     int[] nameRange = {fieldInfo.nameSourceStart, fieldInfo.nameSourceEnd};
103     boolean isSecondary= false;
104     if (fNode instanceof DOMField) {
105         isSecondary = fieldInfo.declarationStart == fNode.fSourceRange[0];
106     }
107     fNode = new DOMField(fDocument, sourceRange, CharOperation.charToString(fieldInfo.name), nameRange,
108         fieldInfo.modifiers, CharOperation.charToString(fieldInfo.type), isSecondary);
109     addChild(fNode);
110     fStack.push(fNode);
111 }
112 /**
113
114  */

115 public void enterInitializer(int declarationSourceStart, int modifiers) {
116     int[] sourceRange = {declarationSourceStart, -1};
117     fNode = new DOMInitializer(fDocument, sourceRange, modifiers);
118     addChild(fNode);
119     fStack.push(fNode);
120 }
121 /**
122  */

123 public void enterMethod(MethodInfo methodInfo) {
124     enterAbstractMethod(methodInfo);
125 }
126 /**
127  */

128 public void enterType(TypeInfo typeInfo) {
129     if (fBuildingType) {
130         int[] sourceRange = {typeInfo.declarationStart, -1}; // will be fixed in the exit
131
int[] nameRange = new int[] {typeInfo.nameSourceStart, typeInfo.nameSourceEnd};
132         fNode = new DOMType(fDocument, sourceRange, new String JavaDoc(typeInfo.name), nameRange,
133             typeInfo.modifiers, CharOperation.charArrayToStringArray(typeInfo.superinterfaces), TypeDeclaration.kind(typeInfo.modifiers) == TypeDeclaration.CLASS_DECL); // TODO (jerome) should pass in kind
134
addChild(fNode);
135         fStack.push(fNode);
136         
137         // type parameters not supported by JDOM
138
}
139 }
140 /**
141  * Finishes the configuration of the method DOM object which
142  * was created by a previous enterConstructor call.
143  *
144  * @see ISourceElementRequestor#exitConstructor(int)
145  */

146 public void exitConstructor(int declarationEnd) {
147     exitMember(declarationEnd);
148 }
149 /**
150  */

151 public void exitField(int initializationStart, int declarationEnd, int declarationSourceEnd) {
152     exitMember(declarationEnd);
153 }
154 /**
155  */

156 public void exitInitializer(int declarationEnd) {
157     exitMember(declarationEnd);
158 }
159 /**
160  * Finishes the configuration of the member.
161  *
162  * @param declarationEnd - a source position corresponding to the end of the method
163  * declaration. This can include whitespace and comments following the closing bracket.
164  */

165 protected void exitMember(int declarationEnd) {
166     DOMMember m= (DOMMember) fStack.pop();
167     m.setSourceRangeEnd(declarationEnd);
168     fNode = m;
169 }
170 /**
171  */

172 public void exitMethod(int declarationEnd, int defaultValueStart, int defaultValueEnd) {
173     exitMember(declarationEnd);
174 }
175 /**
176  * @see AbstractDOMBuilder#exitType
177  *
178  * @param declarationEnd - a source position corresponding to the end of the class
179  * declaration. This can include whitespace and comments following the closing bracket.
180  */

181 public void exitType(int declarationEnd) {
182     exitType(declarationEnd, declarationEnd);
183 }
184 /**
185  * Creates a new parser.
186  */

187 protected SourceElementParser getParser(Map JavaDoc settings) {
188     return new SourceElementParser(this, new DefaultProblemFactory(), new CompilerOptions(settings), false/*don't report local declarations*/, true/*optimize string literals*/);
189 }
190 }
191
Popular Tags