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;
62 fileToParse = getPHPFile();
63 if (fileToParse == null) {
64 // should never happen
65 System.err.println("Error : no file in the editor");
66 // should throw an exception
69 String name = fileToParse.getName();
70 for (int i = 0; i<EXTENSIONS.length; i++) {
71 if (name.endsWith(EXTENSIONS[i])) {
72 phpFlag = true; // php file extension
77 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
78 if (store.getString(PHPeclipsePlugin.PHP_PARSER_DEFAULT).equals(PHPeclipsePlugin.PHP_INTERNAL_PARSER)) {
79 // first delete all the previous markers
80 fileToParse.deleteMarkers(IMarker.PROBLEM, false, 0);
83 InputStream iStream = fileToParse.getContents();
84 // int c = iStream.read();
87 } catch (IOException e) {
90 PHPParserSuperclass.phpExternalParse(fileToParse);
93 } catch (CoreException e) {
99 * Finds the file that's currently opened in the PHP Text Editor
101 protected IFile getPHPFile() {
102 ITextEditor editor = getTextEditor();
104 IEditorInput editorInput = null;
105 if (editor != null) {
106 editorInput = editor.getEditorInput();
109 if (editorInput instanceof IFileEditorInput)
110 return ((IFileEditorInput) editorInput).getFile();
112 // if nothing was found, which should never happen
117 * Create marker for the parse error
119 // protected void setMarker(String message, int lineNumber) throws CoreException {
121 // Hashtable attributes = new Hashtable();
122 // MarkerUtilities.setMessage(attributes, message);
123 // if (message.startsWith(ERROR))
124 // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
125 // else if (message.startsWith(WARNING))
126 // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
128 // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
129 // MarkerUtilities.setLineNumber(attributes, lineNumber);
130 // MarkerUtilities.createMarker(fileToParse, attributes, IMarker.PROBLEM);
133 // private String getIdentifier(InputStream iStream, int c) {
136 // // int textLength = text.length();
137 // StringBuffer identifier = new StringBuffer();
138 // identifier.append((char) c);
140 // while ((c = iStream.read()) != (-1)) {
141 // if (Scanner.isPHPIdentifierPart((char) c)) {
142 // identifier.append((char) c);
143 // // } else if ((i == 0) && (c == '$')) {
144 // // identifier.append((char)c);
146 // return identifier.toString();
150 // } catch (IOException e) {
152 // return identifier.toString();
155 protected void parse(InputStream iStream) {
157 StringBuffer buf = new StringBuffer();
160 while ((c0 = iStream.read()) != (-1)) {
161 buf.append((char) c0);
163 } catch (IOException e) {
166 String input = buf.toString();
168 PHPParserSuperclass parser = PHPParserManager.getParser(fileToParse);
171 } catch (CoreException e) {