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.IOException;
16 import java.io.InputStream;
17 import java.util.ArrayList;
18 import java.util.List;
20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IMarker;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.ui.IEditorInput;
27 import org.eclipse.ui.IFileEditorInput;
28 import org.eclipse.ui.texteditor.ITextEditor;
29 import org.eclipse.ui.texteditor.TextEditorAction;
30 import test.PHPParserSuperclass;
31 import test.PHPParserManager;
34 * ClassDeclaration that defines the action for parsing the current PHP file
36 public class PHPParserAction extends TextEditorAction {
38 private static PHPParserAction instance = new PHPParserAction();
39 private static String[] EXTENSIONS = { ".php", ".php3", ".php4", ".inc", ".phtml" };
41 protected IFile fileToParse;
42 protected List fVariables = new ArrayList(100);
45 * Constructs and updates the action.
47 private PHPParserAction() {
48 super(PHPEditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
52 public static PHPParserAction getInstance() {
57 * Code called when the action is fired.
60 boolean phpFlag = false;
63 fileToParse = getPHPFile();
64 parseFile(fileToParse);
67 public static void parseFile(IFile fileToParse) {
68 boolean phpFlag = false;
71 if (fileToParse == null) {
72 // should never happen
73 System.err.println("Error : no file in the editor");
74 // should throw an exception
77 String name = fileToParse.getName().toLowerCase();
78 for (int i = 0; i < EXTENSIONS.length; i++) {
79 if (name.endsWith(EXTENSIONS[i])) {
80 phpFlag = true; // php file extension
85 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
86 if (store.getString(PHPeclipsePlugin.PHP_PARSER_DEFAULT).equals(PHPeclipsePlugin.PHP_INTERNAL_PARSER)) {
87 // first delete all the previous markers
88 fileToParse.deleteMarkers(IMarker.PROBLEM, false, 0);
90 //the tasks are removed here
91 fileToParse.deleteMarkers(IMarker.TASK, false, 0);
94 InputStream iStream = fileToParse.getContents();
95 // int c = iStream.read();
96 parse(fileToParse, iStream);
98 } catch (IOException e) {
101 PHPParserSuperclass.phpExternalParse(fileToParse);
104 } catch (CoreException e) {
109 * Finds the file that's currently opened in the PHP Text Editor
111 protected IFile getPHPFile() {
112 ITextEditor editor = getTextEditor();
114 IEditorInput editorInput = null;
115 if (editor != null) {
116 editorInput = editor.getEditorInput();
119 if (editorInput instanceof IFileEditorInput)
120 return ((IFileEditorInput) editorInput).getFile();
122 // if nothing was found, which should never happen
127 * Create marker for the parse error
129 // protected void setMarker(String message, int lineNumber) throws CoreException {
131 // Hashtable attributes = new Hashtable();
132 // MarkerUtilities.setMessage(attributes, message);
133 // if (message.startsWith(ERROR))
134 // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
135 // else if (message.startsWith(WARNING))
136 // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
138 // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
139 // MarkerUtilities.setLineNumber(attributes, lineNumber);
140 // MarkerUtilities.createMarker(fileToParse, attributes, IMarker.PROBLEM);
143 // private String getIdentifier(InputStream iStream, int c) {
146 // // int textLength = text.length();
147 // StringBuffer identifier = new StringBuffer();
148 // identifier.append((char) c);
150 // while ((c = iStream.read()) != (-1)) {
151 // if (Scanner.isPHPIdentifierPart((char) c)) {
152 // identifier.append((char) c);
153 // // } else if ((i == 0) && (c == '$')) {
154 // // identifier.append((char)c);
156 // return identifier.toString();
160 // } catch (IOException e) {
162 // return identifier.toString();
165 protected static void parse(IFile fileToParse, InputStream iStream) {
167 StringBuffer buf = new StringBuffer();
170 while ((c0 = iStream.read()) != (-1)) {
171 buf.append((char) c0);
173 } catch (IOException e) {
176 String input = buf.toString();
178 PHPParserSuperclass parser = PHPParserManager.getParser(fileToParse);
181 } catch (CoreException e) {