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);
82 //the tasks are removed here
83 fileToParse.deleteMarkers(IMarker.TASK, false, 0);
86 InputStream iStream = fileToParse.getContents();
87 // int c = iStream.read();
90 } catch (IOException e) {
93 PHPParserSuperclass.phpExternalParse(fileToParse);
96 } catch (CoreException e) {
102 * Finds the file that's currently opened in the PHP Text Editor
104 protected IFile getPHPFile() {
105 ITextEditor editor = getTextEditor();
107 IEditorInput editorInput = null;
108 if (editor != null) {
109 editorInput = editor.getEditorInput();
112 if (editorInput instanceof IFileEditorInput)
113 return ((IFileEditorInput) editorInput).getFile();
115 // if nothing was found, which should never happen
120 * Create marker for the parse error
122 // protected void setMarker(String message, int lineNumber) throws CoreException {
124 // Hashtable attributes = new Hashtable();
125 // MarkerUtilities.setMessage(attributes, message);
126 // if (message.startsWith(ERROR))
127 // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
128 // else if (message.startsWith(WARNING))
129 // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
131 // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
132 // MarkerUtilities.setLineNumber(attributes, lineNumber);
133 // MarkerUtilities.createMarker(fileToParse, attributes, IMarker.PROBLEM);
136 // private String getIdentifier(InputStream iStream, int c) {
139 // // int textLength = text.length();
140 // StringBuffer identifier = new StringBuffer();
141 // identifier.append((char) c);
143 // while ((c = iStream.read()) != (-1)) {
144 // if (Scanner.isPHPIdentifierPart((char) c)) {
145 // identifier.append((char) c);
146 // // } else if ((i == 0) && (c == '$')) {
147 // // identifier.append((char)c);
149 // return identifier.toString();
153 // } catch (IOException e) {
155 // return identifier.toString();
158 protected void parse(InputStream iStream) {
160 StringBuffer buf = new StringBuffer();
163 while ((c0 = iStream.read()) != (-1)) {
164 buf.append((char) c0);
166 } catch (IOException e) {
169 String input = buf.toString();
171 PHPParserSuperclass parser = PHPParserManager.getParser(fileToParse);
174 } catch (CoreException e) {