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.internal.compiler.parser.Scanner;
17 import net.sourceforge.phpdt.internal.formatter.CodeFormatter;
19 import org.eclipse.core.runtime.Plugin;
22 * Factory for creating various compiler tools, such as scanners, parsers and
25 * This class provides static methods only; it is not intended to be
26 * instantiated or subclassed by clients.
31 public class ToolFactory {
34 * Create an instance of a code formatter. A code formatter implementation
35 * can be contributed via the extension point
36 * "org.phpeclipse.phpdt.core.codeFormatter". If unable to find a registered
37 * extension, the factory will default to using the default code formatter.
39 * @return an instance of a code formatter
41 * @see ToolFactory#createDefaultCodeFormatter(Map)
43 public static ICodeFormatter createCodeFormatter() {
45 Plugin jdtCorePlugin = JavaCore.getPlugin();
46 if (jdtCorePlugin == null)
49 // IExtensionPoint extension =
50 // jdtCorePlugin.getDescriptor().getExtensionPoint(JavaModelManager.FORMATTER_EXTPOINT_ID);
51 // if (extension != null) {
52 // IExtension[] extensions = extension.getExtensions();
53 // for(int i = 0; i < extensions.length; i++){
54 // IConfigurationElement [] configElements =
55 // extensions[i].getConfigurationElements();
56 // for(int j = 0; j < configElements.length; j++){
59 // configElements[j].createExecutableExtension("class"); //$NON-NLS-1$
60 // if (execExt instanceof ICodeFormatter){
61 // // use first contribution found
62 // return (ICodeFormatter)execExt;
64 // } catch(CoreException e){
69 // no proper contribution found, use default formatter
70 return createDefaultCodeFormatter(null);
74 * Create an instance of the built-in code formatter. A code formatter
75 * implementation can be contributed via the extension point
76 * "org.phpeclipse.phpdt.core.codeFormatter". If unable to find a registered
77 * extension, the factory will default to using the default code formatter.
80 * the options map to use for formatting with the default code
81 * formatter. Recognized options are documented on
82 * <code>JavaCore#getDefaultOptions()</code>. If set to
83 * <code>null</code>, then use the current settings from
84 * <code>JavaCore#getOptions</code>.
85 * @return an instance of the built-in code formatter
87 * @see ToolFactory#createCodeFormatter()
88 * @see JavaCore#getOptions()
90 public static ICodeFormatter createDefaultCodeFormatter(Map options) {
93 options = JavaCore.getOptions();
94 return new CodeFormatter(options);
98 * Create a scanner, indicating the level of detail requested for
99 * tokenizing. The scanner can then be used to tokenize some source in a
100 * Java aware way. Here is a typical scanning loop:
104 * IScanner scanner = ToolFactory.createScanner(false, false, false, false);
105 * scanner.setSource("int i = 0;".toCharArray());
107 * int token = scanner.getNextToken();
108 * if (token == ITerminalSymbols.TokenNameEOF)
110 * System.out.println(token + " : "
111 * + new String(scanner.getCurrentTokenSource()));
117 * The returned scanner will tolerate unterminated line comments (missing
118 * line separator). It can be made stricter by using API with extra boolean
119 * parameter (<code>strictCommentMode</code>).
122 * @param tokenizeComments
123 * if set to <code>false</code>, comments will be silently
125 * @param tokenizeWhiteSpace
126 * if set to <code>false</code>, white spaces will be silently
128 * @param assertKeyword
129 * if set to <code>false</code>, occurrences of 'assert' will
130 * be reported as identifiers (<code>ITerminalSymbols#TokenNameIdentifier</code>),
131 * whereas if set to <code>true</code>, it would report assert
132 * keywords (<code>ITerminalSymbols#TokenNameassert</code>).
133 * Java 1.4 has introduced a new 'assert' keyword.
134 * @param recordLineSeparator
135 * if set to <code>true</code>, the scanner will record
136 * positions of encountered line separator ends. In case of
137 * multi-character line separators, the last character position
138 * is considered. These positions can then be extracted using
139 * <code>IScanner#getLineEnds</code>. Only non-unicode escape
140 * sequences are considered as valid line separators.
142 * @see ToolFactory#createScanner(boolean,boolean,boolean,boolean, boolean)
143 * @see org.phpeclipse.phpdt.core.compiler.IScanner
145 // public static IScanner createScanner(boolean tokenizeComments, boolean
146 // tokenizeWhiteSpace, boolean recordLineSeparator){
147 // return createScanner(tokenizeComments, tokenizeWhiteSpace,
148 // recordLineSeparator);
151 * Create a scanner, indicating the level of detail requested for
152 * tokenizing. The scanner can then be used to tokenize some source in a
153 * Java aware way. Here is a typical scanning loop:
157 * IScanner scanner = ToolFactory.createScanner(false, false, false, false);
158 * scanner.setSource("int i = 0;".toCharArray());
160 * int token = scanner.getNextToken();
161 * if (token == ITerminalSymbols.TokenNameEOF)
163 * System.out.println(token + " : "
164 * + new String(scanner.getCurrentTokenSource()));
169 * @param tokenizeComments
170 * if set to <code>false</code>, comments will be silently
172 * @param tokenizeWhiteSpace
173 * if set to <code>false</code>, white spaces will be silently
176 * if set to <code>false</code>, occurrences of 'assert' will
177 * be reported as identifiers (<code>ITerminalSymbols#TokenNameIdentifier</code>),
178 * whereas if set to <code>true</code>, it would report assert
179 * keywords (<code>ITerminalSymbols#TokenNameassert</code>).
180 * Java 1.4 has introduced a new 'assert' keyword.
181 * @param recordLineSeparator
182 * if set to <code>true</code>, the scanner will record
183 * positions of encountered line separator ends. In case of
184 * multi-character line separators, the last character position
185 * is considered. These positions can then be extracted using
186 * <code>IScanner#getLineEnds</code>. Only non-unicode escape
187 * sequences are considered as valid line separators.
188 * @param strictCommentMode
189 * if set to <code>true</code>, line comments with no trailing
190 * line separator will be treated as invalid tokens.
193 * @see org.phpeclipse.phpdt.core.compiler.IScanner
196 public static Scanner createScanner(boolean tokenizeComments,
197 boolean tokenizeWhiteSpace, boolean recordLineSeparator) {
199 Scanner scanner = new Scanner(tokenizeComments, tokenizeWhiteSpace,
201 scanner.recordLineSeparator = recordLineSeparator;
205 public static Scanner createScanner(boolean tokenizeComments,
206 boolean tokenizeWhiteSpace, boolean recordLineSeparator,
209 Scanner scanner = new Scanner(tokenizeComments, tokenizeWhiteSpace,
211 scanner.recordLineSeparator = recordLineSeparator;
212 scanner.setPHPMode(phpMode);