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;
18 import net.sourceforge.phpeclipse.PHPCore;
20 import org.eclipse.core.runtime.Plugin;
23 * Factory for creating various compiler tools, such as scanners, parsers and compilers.
25 * This class provides static methods only; it is not intended to be instantiated or subclassed by clients.
30 public class ToolFactory {
33 * Create an instance of a code formatter. A code formatter implementation can be contributed via the
34 * extension point "org.phpeclipse.phpdt.core.codeFormatter". If unable to find a registered extension, the factory
35 * will default to using the default code formatter.
37 * @return an instance of a code formatter
39 * @see ToolFactory#createDefaultCodeFormatter(Map)
41 public static ICodeFormatter createCodeFormatter(){
43 Plugin jdtCorePlugin = PHPCore.getPlugin();
44 if (jdtCorePlugin == null) return null;
46 // IExtensionPoint extension = jdtCorePlugin.getDescriptor().getExtensionPoint(JavaModelManager.FORMATTER_EXTPOINT_ID);
47 // if (extension != null) {
48 // IExtension[] extensions = extension.getExtensions();
49 // for(int i = 0; i < extensions.length; i++){
50 // IConfigurationElement [] configElements = extensions[i].getConfigurationElements();
51 // for(int j = 0; j < configElements.length; j++){
53 // Object execExt = configElements[j].createExecutableExtension("class"); //$NON-NLS-1$
54 // if (execExt instanceof ICodeFormatter){
55 // // use first contribution found
56 // return (ICodeFormatter)execExt;
58 // } catch(CoreException e){
63 // no proper contribution found, use default formatter
64 return createDefaultCodeFormatter(null);
68 * Create an instance of the built-in code formatter. A code formatter implementation can be contributed via the
69 * extension point "org.phpeclipse.phpdt.core.codeFormatter". If unable to find a registered extension, the factory will
70 * default to using the default code formatter.
72 * @param options - the options map to use for formatting with the default code formatter. Recognized options
73 * are documented on <code>JavaCore#getDefaultOptions()</code>. If set to <code>null</code>, then use
74 * the current settings from <code>JavaCore#getOptions</code>.
75 * @return an instance of the built-in code formatter
77 * @see ToolFactory#createCodeFormatter()
78 * @see JavaCore#getOptions()
80 public static ICodeFormatter createDefaultCodeFormatter(Map options){
82 if (options == null) options = PHPCore.getOptions();
83 return new CodeFormatter(options);
87 * Create a scanner, indicating the level of detail requested for tokenizing. The scanner can then be
88 * used to tokenize some source in a Java aware way.
89 * Here is a typical scanning loop:
93 * IScanner scanner = ToolFactory.createScanner(false, false, false, false);
94 * scanner.setSource("int i = 0;".toCharArray());
96 * int token = scanner.getNextToken();
97 * if (token == ITerminalSymbols.TokenNameEOF) break;
98 * System.out.println(token + " : " + new String(scanner.getCurrentTokenSource()));
104 * The returned scanner will tolerate unterminated line comments (missing line separator). It can be made stricter
105 * by using API with extra boolean parameter (<code>strictCommentMode</code>).
107 * @param tokenizeComments if set to <code>false</code>, comments will be silently consumed
108 * @param tokenizeWhiteSpace if set to <code>false</code>, white spaces will be silently consumed,
109 * @param assertKeyword if set to <code>false</code>, occurrences of 'assert' will be reported as identifiers
110 * (<code>ITerminalSymbols#TokenNameIdentifier</code>), whereas if set to <code>true</code>, it
111 * would report assert keywords (<code>ITerminalSymbols#TokenNameassert</code>). Java 1.4 has introduced
112 * a new 'assert' keyword.
113 * @param recordLineSeparator if set to <code>true</code>, the scanner will record positions of encountered line
114 * separator ends. In case of multi-character line separators, the last character position is considered. These positions
115 * can then be extracted using <code>IScanner#getLineEnds</code>. Only non-unicode escape sequences are
116 * considered as valid line separators.
118 * @see ToolFactory#createScanner(boolean,boolean,boolean,boolean, boolean)
119 * @see org.phpeclipse.phpdt.core.compiler.IScanner
121 // public static IScanner createScanner(boolean tokenizeComments, boolean tokenizeWhiteSpace, boolean recordLineSeparator){
122 // return createScanner(tokenizeComments, tokenizeWhiteSpace, recordLineSeparator);
126 * Create a scanner, indicating the level of detail requested for tokenizing. The scanner can then be
127 * used to tokenize some source in a Java aware way.
128 * Here is a typical scanning loop:
132 * IScanner scanner = ToolFactory.createScanner(false, false, false, false);
133 * scanner.setSource("int i = 0;".toCharArray());
135 * int token = scanner.getNextToken();
136 * if (token == ITerminalSymbols.TokenNameEOF) break;
137 * System.out.println(token + " : " + new String(scanner.getCurrentTokenSource()));
142 * @param tokenizeComments if set to <code>false</code>, comments will be silently consumed
143 * @param tokenizeWhiteSpace if set to <code>false</code>, white spaces will be silently consumed,
144 * @param assertMode if set to <code>false</code>, occurrences of 'assert' will be reported as identifiers
145 * (<code>ITerminalSymbols#TokenNameIdentifier</code>), whereas if set to <code>true</code>, it
146 * would report assert keywords (<code>ITerminalSymbols#TokenNameassert</code>). Java 1.4 has introduced
147 * a new 'assert' keyword.
148 * @param recordLineSeparator if set to <code>true</code>, the scanner will record positions of encountered line
149 * separator ends. In case of multi-character line separators, the last character position is considered. These positions
150 * can then be extracted using <code>IScanner#getLineEnds</code>. Only non-unicode escape sequences are
151 * considered as valid line separators.
152 * @param strictCommentMode if set to <code>true</code>, line comments with no trailing line separator will be
153 * treated as invalid tokens.
156 * @see org.phpeclipse.phpdt.core.compiler.IScanner
159 public static Scanner createScanner(boolean tokenizeComments, boolean tokenizeWhiteSpace, boolean recordLineSeparator){
161 Scanner scanner = new Scanner(tokenizeComments, tokenizeWhiteSpace, false/*nls*/);
162 scanner.recordLineSeparator = recordLineSeparator;