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 compilers.
24 * This class provides static methods only; it is not intended to be instantiated or subclassed by clients.
29 public class ToolFactory {
32 * Create an instance of a code formatter. A code formatter implementation can be contributed via the
33 * extension point "org.phpeclipse.phpdt.core.codeFormatter". If unable to find a registered extension, the factory
34 * will default to using the default code formatter.
36 * @return an instance of a code formatter
38 * @see ToolFactory#createDefaultCodeFormatter(Map)
40 public static ICodeFormatter createCodeFormatter(){
42 Plugin jdtCorePlugin = JavaCore.getPlugin();
43 if (jdtCorePlugin == null) return null;
45 // IExtensionPoint extension = jdtCorePlugin.getDescriptor().getExtensionPoint(JavaModelManager.FORMATTER_EXTPOINT_ID);
46 // if (extension != null) {
47 // IExtension[] extensions = extension.getExtensions();
48 // for(int i = 0; i < extensions.length; i++){
49 // IConfigurationElement [] configElements = extensions[i].getConfigurationElements();
50 // for(int j = 0; j < configElements.length; j++){
52 // Object execExt = configElements[j].createExecutableExtension("class"); //$NON-NLS-1$
53 // if (execExt instanceof ICodeFormatter){
54 // // use first contribution found
55 // return (ICodeFormatter)execExt;
57 // } catch(CoreException e){
62 // no proper contribution found, use default formatter
63 return createDefaultCodeFormatter(null);
67 * Create an instance of the built-in code formatter. A code formatter implementation can be contributed via the
68 * extension point "org.phpeclipse.phpdt.core.codeFormatter". If unable to find a registered extension, the factory will
69 * default to using the default code formatter.
71 * @param options - the options map to use for formatting with the default code formatter. Recognized options
72 * are documented on <code>JavaCore#getDefaultOptions()</code>. If set to <code>null</code>, then use
73 * the current settings from <code>JavaCore#getOptions</code>.
74 * @return an instance of the built-in code formatter
76 * @see ToolFactory#createCodeFormatter()
77 * @see JavaCore#getOptions()
79 public static ICodeFormatter createDefaultCodeFormatter(Map options){
81 if (options == null) options = JavaCore.getOptions();
82 return new CodeFormatter(options);
86 * Create a scanner, indicating the level of detail requested for tokenizing. The scanner can then be
87 * used to tokenize some source in a Java aware way.
88 * Here is a typical scanning loop:
92 * IScanner scanner = ToolFactory.createScanner(false, false, false, false);
93 * scanner.setSource("int i = 0;".toCharArray());
95 * int token = scanner.getNextToken();
96 * if (token == ITerminalSymbols.TokenNameEOF) break;
97 * System.out.println(token + " : " + new String(scanner.getCurrentTokenSource()));
103 * The returned scanner will tolerate unterminated line comments (missing line separator). It can be made stricter
104 * by using API with extra boolean parameter (<code>strictCommentMode</code>).
106 * @param tokenizeComments if set to <code>false</code>, comments will be silently consumed
107 * @param tokenizeWhiteSpace if set to <code>false</code>, white spaces will be silently consumed,
108 * @param assertKeyword if set to <code>false</code>, occurrences of 'assert' will be reported as identifiers
109 * (<code>ITerminalSymbols#TokenNameIdentifier</code>), whereas if set to <code>true</code>, it
110 * would report assert keywords (<code>ITerminalSymbols#TokenNameassert</code>). Java 1.4 has introduced
111 * a new 'assert' keyword.
112 * @param recordLineSeparator if set to <code>true</code>, the scanner will record positions of encountered line
113 * separator ends. In case of multi-character line separators, the last character position is considered. These positions
114 * can then be extracted using <code>IScanner#getLineEnds</code>. Only non-unicode escape sequences are
115 * considered as valid line separators.
117 * @see ToolFactory#createScanner(boolean,boolean,boolean,boolean, boolean)
118 * @see org.phpeclipse.phpdt.core.compiler.IScanner
120 // public static IScanner createScanner(boolean tokenizeComments, boolean tokenizeWhiteSpace, boolean recordLineSeparator){
121 // return createScanner(tokenizeComments, tokenizeWhiteSpace, recordLineSeparator);
125 * Create a scanner, indicating the level of detail requested for tokenizing. The scanner can then be
126 * used to tokenize some source in a Java aware way.
127 * Here is a typical scanning loop:
131 * IScanner scanner = ToolFactory.createScanner(false, false, false, false);
132 * scanner.setSource("int i = 0;".toCharArray());
134 * int token = scanner.getNextToken();
135 * if (token == ITerminalSymbols.TokenNameEOF) break;
136 * System.out.println(token + " : " + new String(scanner.getCurrentTokenSource()));
141 * @param tokenizeComments if set to <code>false</code>, comments will be silently consumed
142 * @param tokenizeWhiteSpace if set to <code>false</code>, white spaces will be silently consumed,
143 * @param assertMode if set to <code>false</code>, occurrences of 'assert' will be reported as identifiers
144 * (<code>ITerminalSymbols#TokenNameIdentifier</code>), whereas if set to <code>true</code>, it
145 * would report assert keywords (<code>ITerminalSymbols#TokenNameassert</code>). Java 1.4 has introduced
146 * a new 'assert' keyword.
147 * @param recordLineSeparator if set to <code>true</code>, the scanner will record positions of encountered line
148 * separator ends. In case of multi-character line separators, the last character position is considered. These positions
149 * can then be extracted using <code>IScanner#getLineEnds</code>. Only non-unicode escape sequences are
150 * considered as valid line separators.
151 * @param strictCommentMode if set to <code>true</code>, line comments with no trailing line separator will be
152 * treated as invalid tokens.
155 * @see org.phpeclipse.phpdt.core.compiler.IScanner
158 public static Scanner createScanner(boolean tokenizeComments, boolean tokenizeWhiteSpace, boolean recordLineSeparator){
160 Scanner scanner = new Scanner(tokenizeComments, tokenizeWhiteSpace, false/*nls*/);
161 scanner.recordLineSeparator = recordLineSeparator;