1 /*******************************************************************************
2 * Copyright (c) 2002 International Business Machines Corp. 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 * IBM Corporation - added #createScanner allowing to make comment check stricter
11 ******************************************************************************/
12 package net.sourceforge.phpdt.core;
16 import net.sourceforge.phpdt.core.compiler.IScanner;
17 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
18 import net.sourceforge.phpdt.internal.formatter.CodeFormatter;
19 import net.sourceforge.phpeclipse.PHPCore;
21 import org.eclipse.core.runtime.Plugin;
24 * Factory for creating various compiler tools, such as scanners, parsers and compilers.
26 * This class provides static methods only; it is not intended to be instantiated or subclassed by clients.
31 public class ToolFactory {
34 * Create an instance of a code formatter. A code formatter implementation can be contributed via the
35 * extension point "org.phpeclipse.phpdt.core.codeFormatter". If unable to find a registered extension, the factory
36 * will default to using the default code formatter.
38 * @return an instance of a code formatter
40 * @see ToolFactory#createDefaultCodeFormatter(Map)
42 public static ICodeFormatter createCodeFormatter(){
44 Plugin jdtCorePlugin = PHPCore.getPlugin();
45 if (jdtCorePlugin == null) return null;
47 // IExtensionPoint extension = jdtCorePlugin.getDescriptor().getExtensionPoint(JavaModelManager.FORMATTER_EXTPOINT_ID);
48 // if (extension != null) {
49 // IExtension[] extensions = extension.getExtensions();
50 // for(int i = 0; i < extensions.length; i++){
51 // IConfigurationElement [] configElements = extensions[i].getConfigurationElements();
52 // for(int j = 0; j < configElements.length; j++){
54 // Object execExt = configElements[j].createExecutableExtension("class"); //$NON-NLS-1$
55 // if (execExt instanceof ICodeFormatter){
56 // // use first contribution found
57 // return (ICodeFormatter)execExt;
59 // } catch(CoreException e){
64 // no proper contribution found, use default formatter
65 return createDefaultCodeFormatter(null);
69 * Create an instance of the built-in code formatter. A code formatter implementation can be contributed via the
70 * extension point "org.phpeclipse.phpdt.core.codeFormatter". If unable to find a registered extension, the factory will
71 * default to using the default code formatter.
73 * @param options - the options map to use for formatting with the default code formatter. Recognized options
74 * are documented on <code>JavaCore#getDefaultOptions()</code>. If set to <code>null</code>, then use
75 * the current settings from <code>JavaCore#getOptions</code>.
76 * @return an instance of the built-in code formatter
78 * @see ToolFactory#createCodeFormatter()
79 * @see JavaCore#getOptions()
81 public static ICodeFormatter createDefaultCodeFormatter(Map options){
83 if (options == null) options = PHPCore.getOptions();
84 return new CodeFormatter(options);
88 * Create a scanner, indicating the level of detail requested for tokenizing. The scanner can then be
89 * used to tokenize some source in a Java aware way.
90 * Here is a typical scanning loop:
94 * IScanner scanner = ToolFactory.createScanner(false, false, false, false);
95 * scanner.setSource("int i = 0;".toCharArray());
97 * int token = scanner.getNextToken();
98 * if (token == ITerminalSymbols.TokenNameEOF) break;
99 * System.out.println(token + " : " + new String(scanner.getCurrentTokenSource()));
105 * The returned scanner will tolerate unterminated line comments (missing line separator). It can be made stricter
106 * by using API with extra boolean parameter (<code>strictCommentMode</code>).
108 * @param tokenizeComments if set to <code>false</code>, comments will be silently consumed
109 * @param tokenizeWhiteSpace if set to <code>false</code>, white spaces will be silently consumed,
110 * @param assertKeyword if set to <code>false</code>, occurrences of 'assert' will be reported as identifiers
111 * (<code>ITerminalSymbols#TokenNameIdentifier</code>), whereas if set to <code>true</code>, it
112 * would report assert keywords (<code>ITerminalSymbols#TokenNameassert</code>). Java 1.4 has introduced
113 * a new 'assert' keyword.
114 * @param recordLineSeparator if set to <code>true</code>, the scanner will record positions of encountered line
115 * separator ends. In case of multi-character line separators, the last character position is considered. These positions
116 * can then be extracted using <code>IScanner#getLineEnds</code>. Only non-unicode escape sequences are
117 * considered as valid line separators.
119 * @see ToolFactory#createScanner(boolean,boolean,boolean,boolean, boolean)
120 * @see org.phpeclipse.phpdt.core.compiler.IScanner
122 // public static IScanner createScanner(boolean tokenizeComments, boolean tokenizeWhiteSpace, boolean recordLineSeparator){
123 // return createScanner(tokenizeComments, tokenizeWhiteSpace, recordLineSeparator);
127 * Create a scanner, indicating the level of detail requested for tokenizing. The scanner can then be
128 * used to tokenize some source in a Java aware way.
129 * Here is a typical scanning loop:
133 * IScanner scanner = ToolFactory.createScanner(false, false, false, false);
134 * scanner.setSource("int i = 0;".toCharArray());
136 * int token = scanner.getNextToken();
137 * if (token == ITerminalSymbols.TokenNameEOF) break;
138 * System.out.println(token + " : " + new String(scanner.getCurrentTokenSource()));
143 * @param tokenizeComments if set to <code>false</code>, comments will be silently consumed
144 * @param tokenizeWhiteSpace if set to <code>false</code>, white spaces will be silently consumed,
145 * @param assertMode if set to <code>false</code>, occurrences of 'assert' will be reported as identifiers
146 * (<code>ITerminalSymbols#TokenNameIdentifier</code>), whereas if set to <code>true</code>, it
147 * would report assert keywords (<code>ITerminalSymbols#TokenNameassert</code>). Java 1.4 has introduced
148 * a new 'assert' keyword.
149 * @param recordLineSeparator if set to <code>true</code>, the scanner will record positions of encountered line
150 * separator ends. In case of multi-character line separators, the last character position is considered. These positions
151 * can then be extracted using <code>IScanner#getLineEnds</code>. Only non-unicode escape sequences are
152 * considered as valid line separators.
153 * @param strictCommentMode if set to <code>true</code>, line comments with no trailing line separator will be
154 * treated as invalid tokens.
157 * @see org.phpeclipse.phpdt.core.compiler.IScanner
160 public static IScanner createScanner(boolean tokenizeComments, boolean tokenizeWhiteSpace, boolean recordLineSeparator){
162 Scanner scanner = new Scanner(tokenizeComments, tokenizeWhiteSpace, false/*nls*/);
163 scanner.recordLineSeparator = recordLineSeparator;