1 package net.sourceforge.phpeclipse.phpeditor;
3 /**********************************************************************
4 Copyright (c) 2000, 2002 IBM Corp. and others.
5 All rights reserved. This program and the accompanying materials
6 are made available under the terms of the Common Public License v1.0
7 which accompanies this distribution, and is available at
8 http://www.eclipse.org/legal/cpl-v10.html
11 IBM Corporation - Initial implementation
12 Klaus Hartlage - www.eclipseproject.de
13 **********************************************************************/
15 import java.io.BufferedInputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.util.ArrayList;
19 import java.util.List;
21 import net.sourceforge.phpdt.internal.compiler.util.Util;
22 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
23 import net.sourceforge.phpeclipse.builder.PHPBuilder;
25 import org.eclipse.core.resources.IFile;
26 import org.eclipse.core.resources.IMarker;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.jface.preference.IPreferenceStore;
29 import org.eclipse.ui.IEditorInput;
30 import org.eclipse.ui.IFileEditorInput;
31 import org.eclipse.ui.texteditor.ITextEditor;
32 import org.eclipse.ui.texteditor.TextEditorAction;
34 import test.PHPParserManager;
35 import test.PHPParserSuperclass;
38 * ClassDeclaration that defines the action for parsing the current PHP file
40 public class PHPParserAction extends TextEditorAction {
42 private static PHPParserAction instance = new PHPParserAction();
43 private static String[] EXTENSIONS = { ".php", ".php3", ".php4", ".inc", ".phtml" };
45 protected IFile fileToParse;
46 protected List fVariables = new ArrayList(100);
49 * Constructs and updates the action.
51 private PHPParserAction() {
52 super(PHPEditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
56 public static PHPParserAction getInstance() {
61 * Code called when the action is fired.
64 boolean phpFlag = false;
67 fileToParse = getPHPFile();
68 parseFile(fileToParse);
71 public static void parseFile(IFile fileToParse) {
72 boolean phpFlag = false;
75 if (fileToParse == null) {
76 // TODO should never happen => should throw an exception
77 System.err.println("Error : no file in the editor");
81 // TODO use isPHPFile()
82 String name = fileToParse.getName().toLowerCase();
83 for (int i = 0; i < EXTENSIONS.length; i++) {
84 if (name.endsWith(EXTENSIONS[i])) {
85 phpFlag = true; // php file extension
90 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
91 if (store.getString(PHPeclipsePlugin.PHP_PARSER_DEFAULT).equals(PHPeclipsePlugin.PHP_INTERNAL_PARSER)) {
92 // // first delete all the previous markers
93 // fileToParse.deleteMarkers(IMarker.PROBLEM, false, 0);
95 // //the tasks are removed here
96 // fileToParse.deleteMarkers(IMarker.TASK, false, 0);
97 PHPBuilder.removeProblemsAndTasksFor(fileToParse);
99 // InputStream iStream = fileToParse.getContents();
100 parse(fileToParse); //, iStream);
102 // } catch (IOException e) {
105 PHPParserSuperclass.phpExternalParse(fileToParse);
108 // } catch (CoreException e) {
113 * Finds the file that's currently opened in the PHP Text Editor
115 protected IFile getPHPFile() {
116 ITextEditor editor = getTextEditor();
118 IEditorInput editorInput = null;
119 if (editor != null) {
120 editorInput = editor.getEditorInput();
123 if (editorInput instanceof IFileEditorInput)
124 return ((IFileEditorInput) editorInput).getFile();
126 // if nothing was found, which should never happen
131 * Create marker for the parse error
133 // protected void setMarker(String message, int lineNumber) throws CoreException {
135 // Hashtable attributes = new Hashtable();
136 // MarkerUtilities.setMessage(attributes, message);
137 // if (message.startsWith(ERROR))
138 // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
139 // else if (message.startsWith(WARNING))
140 // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
142 // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
143 // MarkerUtilities.setLineNumber(attributes, lineNumber);
144 // MarkerUtilities.createMarker(fileToParse, attributes, IMarker.PROBLEM);
147 // private String getIdentifier(InputStream iStream, int c) {
150 // // int textLength = text.length();
151 // StringBuffer identifier = new StringBuffer();
152 // identifier.append((char) c);
154 // while ((c = iStream.read()) != (-1)) {
155 // if (Scanner.isPHPIdentifierPart((char) c)) {
156 // identifier.append((char) c);
157 // // } else if ((i == 0) && (c == '$')) {
158 // // identifier.append((char)c);
160 // return identifier.toString();
164 // } catch (IOException e) {
166 // return identifier.toString();
169 protected static void parse(IFile fileToParse) {
171 // StringBuffer buf = new StringBuffer();
174 // while ((c0 = iStream.read()) != (-1)) {
175 // buf.append((char) c0);
177 // } catch (IOException e) {
180 // String input = buf.toString();
182 InputStream stream = null;
185 stream = new BufferedInputStream(fileToParse.getContents());
186 charArray = Util.getInputStreamAsCharArray(stream, -1, null);
187 PHPParserSuperclass parser = PHPParserManager.getParser(fileToParse);
188 parser.parse(new String(charArray));
189 } catch (CoreException e) {
190 } catch (IOException e) {
196 } catch (IOException e) {