A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / core / ToolFactory.java
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
7  * 
8  * Contributors:
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;
13
14 import java.util.Map;
15
16 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
17 import net.sourceforge.phpdt.internal.formatter.CodeFormatter;
18
19 import org.eclipse.core.runtime.Plugin;
20
21 /**
22  * Factory for creating various compiler tools, such as scanners, parsers and
23  * compilers.
24  * <p>
25  * This class provides static methods only; it is not intended to be
26  * instantiated or subclassed by clients.
27  * </p>
28  * 
29  * @since 2.0
30  */
31 public class ToolFactory {
32
33         /**
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.
38          * 
39          * @return an instance of a code formatter
40          * @see ICodeFormatter
41          * @see ToolFactory#createDefaultCodeFormatter(Map)
42          */
43         public static ICodeFormatter createCodeFormatter() {
44
45                 Plugin jdtCorePlugin = JavaCore.getPlugin();
46                 if (jdtCorePlugin == null)
47                         return null;
48
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++){
57                 // try {
58                 // Object execExt =
59                 // configElements[j].createExecutableExtension("class"); //$NON-NLS-1$
60                 // if (execExt instanceof ICodeFormatter){
61                 // // use first contribution found
62                 // return (ICodeFormatter)execExt;
63                 // }
64                 // } catch(CoreException e){
65                 // }
66                 // }
67                 // }
68                 // }
69                 // no proper contribution found, use default formatter
70                 return createDefaultCodeFormatter(null);
71         }
72
73         /**
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.
78          * 
79          * @param options -
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
86          * @see ICodeFormatter
87          * @see ToolFactory#createCodeFormatter()
88          * @see JavaCore#getOptions()
89          */
90         public static ICodeFormatter createDefaultCodeFormatter(Map options) {
91
92                 if (options == null)
93                         options = JavaCore.getOptions();
94                 return new CodeFormatter(options);
95         }
96
97         /**
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:
101          * 
102          * <code>
103          * <pre>
104          * IScanner scanner = ToolFactory.createScanner(false, false, false, false);
105          * scanner.setSource(&quot;int i = 0;&quot;.toCharArray());
106          * while (true) {
107          *      int token = scanner.getNextToken();
108          *      if (token == ITerminalSymbols.TokenNameEOF)
109          *              break;
110          *      System.out.println(token + &quot; : &quot;
111          *                      + new String(scanner.getCurrentTokenSource()));
112          * }
113          * </pre>
114          * </code>
115          * 
116          * <p>
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>).
120          * <p>
121          * 
122          * @param tokenizeComments
123          *            if set to <code>false</code>, comments will be silently
124          *            consumed
125          * @param tokenizeWhiteSpace
126          *            if set to <code>false</code>, white spaces will be silently
127          *            consumed,
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.
141          * @return a scanner
142          * @see ToolFactory#createScanner(boolean,boolean,boolean,boolean, boolean)
143          * @see org.phpeclipse.phpdt.core.compiler.IScanner
144          */
145         // public static IScanner createScanner(boolean tokenizeComments, boolean
146         // tokenizeWhiteSpace, boolean recordLineSeparator){
147         // return createScanner(tokenizeComments, tokenizeWhiteSpace,
148         // recordLineSeparator);
149         // }
150         /**
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:
154          * 
155          * <code>
156          * <pre>
157          * IScanner scanner = ToolFactory.createScanner(false, false, false, false);
158          * scanner.setSource(&quot;int i = 0;&quot;.toCharArray());
159          * while (true) {
160          *      int token = scanner.getNextToken();
161          *      if (token == ITerminalSymbols.TokenNameEOF)
162          *              break;
163          *      System.out.println(token + &quot; : &quot;
164          *                      + new String(scanner.getCurrentTokenSource()));
165          * }
166          * </pre>
167          * </code>
168          * 
169          * @param tokenizeComments
170          *            if set to <code>false</code>, comments will be silently
171          *            consumed
172          * @param tokenizeWhiteSpace
173          *            if set to <code>false</code>, white spaces will be silently
174          *            consumed,
175          * @param assertMode
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.
191          * @return a scanner
192          * 
193          * @see org.phpeclipse.phpdt.core.compiler.IScanner
194          * @since 2.1
195          */
196         public static Scanner createScanner(boolean tokenizeComments,
197                         boolean tokenizeWhiteSpace, boolean recordLineSeparator) {
198
199                 Scanner scanner = new Scanner(tokenizeComments, tokenizeWhiteSpace,
200                                 false/* nls */);
201                 scanner.recordLineSeparator = recordLineSeparator;
202                 return scanner;
203         }
204
205         public static Scanner createScanner(boolean tokenizeComments,
206                         boolean tokenizeWhiteSpace, boolean recordLineSeparator,
207                         boolean phpMode) {
208
209                 Scanner scanner = new Scanner(tokenizeComments, tokenizeWhiteSpace,
210                                 false/* nls */);
211                 scanner.recordLineSeparator = recordLineSeparator;
212                 scanner.setPHPMode(phpMode);
213                 return scanner;
214         }
215 }