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.internal.compiler.ISourceElementRequestor;
 
  18 import net.sourceforge.phpdt.internal.compiler.SourceElementParser;
 
  19 import net.sourceforge.phpdt.internal.compiler.env.ICompilationUnit;
 
  20 import net.sourceforge.phpdt.internal.compiler.problem.DefaultProblemFactory;
 
  21 import net.sourceforge.phpdt.internal.core.util.CharArrayOps;
 
  24  * A DOM builder that uses the SourceElementParser
 
  26 public class SimpleDOMBuilder extends AbstractDOMBuilder implements ISourceElementRequestor {
 
  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 '*' */
 
  33                 importName+=".*"; //$NON-NLS-1$
 
  35         fNode= new DOMImport(fDocument, sourceRange, importName, onDemand);
 
  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));
 
  44  * @see IDOMFactory#createCompilationUnit(String, String)
 
  46 public IDOMCompilationUnit createCompilationUnit(String sourceCode, String name) {
 
  47         return createCompilationUnit(sourceCode.toCharArray(), name.toCharArray());
 
  50  * @see IDOMFactory#createCompilationUnit(String, String)
 
  52 public IDOMCompilationUnit createCompilationUnit(ICompilationUnit compilationUnit) {
 
  53         initializeBuild(compilationUnit.getContents(), true, true);
 
  54         getParser(JavaCore.getOptions()).parseCompilationUnit(compilationUnit, false);
 
  55         return super.createCompilationUnit(compilationUnit);
 
  58  * Creates a new DOMMethod and inizializes.
 
  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
 
  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) {
 
  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));
 
  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);
 
  93 public void enterConstructor(int declarationStart, int modifiers, char[] name, int nameStart, int nameEnd, char[][] parameterTypes, char[][] parameterNames, char[][] exceptionTypes) {
 
  95         String nameString = new String(fDocument, nameStart, nameEnd - nameStart);
 
  96         int openParenPosition = nameString.indexOf('(');
 
  97         if (openParenPosition > -1)
 
  98                 nameEnd = nameStart + openParenPosition - 1;
 
 100         enterAbstractMethod(declarationStart, modifiers, 
 
 101                 null, name, nameStart, nameEnd, parameterTypes,
 
 102                 parameterNames, exceptionTypes,true);
 
 106 public void enterField(int declarationStart, int modifiers, char[] type, char[] name, int nameStart, int nameEnd) {
 
 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];
 
 114         fNode = new DOMField(fDocument, sourceRange, CharArrayOps.charToString(name), nameRange, 
 
 115                 modifiers, CharArrayOps.charToString(type), isSecondary);
 
 122 public void enterInitializer(int declarationSourceStart, int modifiers) {
 
 123         int[] sourceRange = {declarationSourceStart, -1};
 
 124         fNode = new DOMInitializer(fDocument, sourceRange, modifiers);
 
 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);
 
 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);
 
 143 protected void enterType(int declarationStart, int modifiers, char[] name, 
 
 144         int nameStart, int nameEnd, char[] superclass, char[][] superinterfaces, boolean isClass) {
 
 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);
 
 155  * Finishes the configuration of the class DOM object which
 
 156  * was created by a previous enterClass call.
 
 158  * @see ISourceElementRequestor#exitClass(int)
 
 160 public void exitClass(int declarationEnd) {
 
 161         exitType(declarationEnd);
 
 164  * Finishes the configuration of the method DOM object which
 
 165  * was created by a previous enterConstructor call.
 
 167  * @see ISourceElementRequestor#exitConstructor(int)
 
 169 public void exitConstructor(int declarationEnd) {
 
 170         exitMember(declarationEnd);
 
 174 public void exitField(int initializationStart, int declarationEnd, int declarationSourceEnd) {
 
 175         exitMember(declarationEnd);
 
 179 public void exitInitializer(int declarationEnd) {
 
 180         exitMember(declarationEnd);
 
 184 public void exitInterface(int declarationEnd) {
 
 185         exitType(declarationEnd);
 
 188  * Finishes the configuration of the member.
 
 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.
 
 193 protected void exitMember(int declarationEnd) {
 
 194         DOMMember m= (DOMMember) fStack.pop();
 
 195         m.setSourceRangeEnd(declarationEnd);
 
 200 public void exitMethod(int declarationEnd) {
 
 201         exitMember(declarationEnd);
 
 204  * @see AbstractDOMBuilder#exitType
 
 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.
 
 209 protected void exitType(int declarationEnd) {
 
 210         exitType(declarationEnd, declarationEnd);
 
 213  * Creates a new parser.
 
 215 protected SourceElementParser getParser(Map settings) {
 
 216         return new SourceElementParser(this, new DefaultProblemFactory());//, new CompilerOptions(settings));