e008a2194d49d034ca67d14bd5bf14b82bef697f
[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 compilers.
23  * <p>
24  *  This class provides static methods only; it is not intended to be instantiated or subclassed by clients.
25  * </p>
26  * 
27  * @since 2.0
28  */
29 public class ToolFactory {
30
31         /**
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.
35          * 
36          * @return an instance of a code formatter
37          * @see ICodeFormatter
38          * @see ToolFactory#createDefaultCodeFormatter(Map)
39          */
40         public static ICodeFormatter createCodeFormatter(){
41                 
42                         Plugin jdtCorePlugin = JavaCore.getPlugin();
43                         if (jdtCorePlugin == null) return null;
44                 
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++){
51 //                                              try {
52 //                                                      Object execExt = configElements[j].createExecutableExtension("class"); //$NON-NLS-1$
53 //                                                      if (execExt instanceof ICodeFormatter){
54 //                                                              // use first contribution found
55 //                                                              return (ICodeFormatter)execExt;
56 //                                                      }
57 //                                              } catch(CoreException e){
58 //                                              }
59 //                                      }
60 //                              }       
61 //                      }
62                 // no proper contribution found, use default formatter                  
63                 return createDefaultCodeFormatter(null);
64         }
65
66         /**
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.
70          * 
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
75          * @see ICodeFormatter
76          * @see ToolFactory#createCodeFormatter()
77          * @see JavaCore#getOptions()
78          */
79         public static ICodeFormatter createDefaultCodeFormatter(Map options){
80
81                 if (options == null) options = JavaCore.getOptions();
82                 return new CodeFormatter(options);
83         }
84         
85         /**
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:
89          * 
90          * <code>
91          * <pre>
92          *   IScanner scanner = ToolFactory.createScanner(false, false, false, false);
93          *   scanner.setSource("int i = 0;".toCharArray());
94          *   while (true) {
95          *     int token = scanner.getNextToken();
96          *     if (token == ITerminalSymbols.TokenNameEOF) break;
97          *     System.out.println(token + " : " + new String(scanner.getCurrentTokenSource()));
98          *   }
99          * </pre>
100          * </code>
101          * 
102          * <p>
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>).
105          * <p>
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.
116          * @return a scanner
117          * @see ToolFactory#createScanner(boolean,boolean,boolean,boolean, boolean)
118          * @see org.phpeclipse.phpdt.core.compiler.IScanner
119          */
120 //      public static IScanner createScanner(boolean tokenizeComments, boolean tokenizeWhiteSpace, boolean recordLineSeparator){
121 //              return createScanner(tokenizeComments, tokenizeWhiteSpace, recordLineSeparator);
122 //      }
123         
124         /**
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:
128          * 
129          * <code>
130          * <pre>
131          *   IScanner scanner = ToolFactory.createScanner(false, false, false, false);
132          *   scanner.setSource("int i = 0;".toCharArray());
133          *   while (true) {
134          *     int token = scanner.getNextToken();
135          *     if (token == ITerminalSymbols.TokenNameEOF) break;
136          *     System.out.println(token + " : " + new String(scanner.getCurrentTokenSource()));
137          *   }
138          * </pre>
139          * </code>
140          * 
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.
153          * @return a scanner
154          * 
155          * @see org.phpeclipse.phpdt.core.compiler.IScanner
156          * @since 2.1
157          */
158         public static Scanner createScanner(boolean tokenizeComments, boolean tokenizeWhiteSpace, boolean recordLineSeparator){
159
160                 Scanner scanner = new Scanner(tokenizeComments, tokenizeWhiteSpace, false/*nls*/);
161                 scanner.recordLineSeparator = recordLineSeparator;
162                 return scanner;
163         }
164 }