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