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;
21 import net.sourceforge.phpeclipse.phpeditor.phpparser.PHPParser;
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.core.resources.IMarker;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.jface.preference.IPreferenceStore;
27 import org.eclipse.ui.IEditorInput;
28 import org.eclipse.ui.IFileEditorInput;
29 import org.eclipse.ui.texteditor.ITextEditor;
30 import org.eclipse.ui.texteditor.TextEditorAction;
33 * Class that defines the action for parsing the current PHP file
35 public class PHPParserAction extends TextEditorAction {
37 private static PHPParserAction instance = new PHPParserAction();
38 private static String[] EXTENSIONS = { ".php", ".php3", ".php4", ".inc", ".phtml" };
40 protected IFile fileToParse;
41 protected List fVariables = new ArrayList(100);
44 * Constructs and updates the action.
46 private PHPParserAction() {
47 super(PHPEditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
51 public static PHPParserAction getInstance() {
56 * Code called when the action is fired.
59 boolean phpFlag = false;
61 fileToParse = getPHPFile();
62 if (fileToParse == null) {
63 // should never happen
64 System.err.println("Error : no file in the editor");
65 // should throw an exception
68 String name = fileToParse.getName();
69 for (int i = 0; i<EXTENSIONS.length; i++) {
70 if (name.endsWith(EXTENSIONS[i])) {
71 phpFlag = true; // php file extension
76 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
77 if (store.getString(PHPeclipsePlugin.PHP_PARSER_DEFAULT).equals(PHPeclipsePlugin.PHP_INTERNAL_PARSER)) {
78 // first delete all the previous markers
79 fileToParse.deleteMarkers(IMarker.PROBLEM, false, 0);
82 InputStream iStream = fileToParse.getContents();
83 // int c = iStream.read();
86 } catch (IOException e) {
89 PHPParser.phpExternalParse(fileToParse);
92 } catch (CoreException e) {
98 * Finds the file that's currently opened in the PHP Text Editor
100 protected IFile getPHPFile() {
101 ITextEditor editor = getTextEditor();
103 IEditorInput editorInput = null;
104 if (editor != null) {
105 editorInput = editor.getEditorInput();
108 if (editorInput instanceof IFileEditorInput)
109 return ((IFileEditorInput) editorInput).getFile();
111 // if nothing was found, which should never happen
116 * Create marker for the parse error
118 // protected void setMarker(String message, int lineNumber) throws CoreException {
120 // Hashtable attributes = new Hashtable();
121 // MarkerUtilities.setMessage(attributes, message);
122 // if (message.startsWith(ERROR))
123 // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
124 // else if (message.startsWith(WARNING))
125 // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
127 // attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
128 // MarkerUtilities.setLineNumber(attributes, lineNumber);
129 // MarkerUtilities.createMarker(fileToParse, attributes, IMarker.PROBLEM);
132 // private String getIdentifier(InputStream iStream, int c) {
135 // // int textLength = text.length();
136 // StringBuffer identifier = new StringBuffer();
137 // identifier.append((char) c);
139 // while ((c = iStream.read()) != (-1)) {
140 // if (Character.isJavaIdentifierPart((char) c)) {
141 // identifier.append((char) c);
142 // // } else if ((i == 0) && (c == '$')) {
143 // // identifier.append((char)c);
145 // return identifier.toString();
149 // } catch (IOException e) {
151 // return identifier.toString();
154 protected void parse(InputStream iStream) {
156 StringBuffer buf = new StringBuffer();
159 while ((c0 = iStream.read()) != (-1)) {
160 buf.append((char) c0);
162 } catch (IOException e) {
165 String input = buf.toString();
167 PHPParser parser = new PHPParser(fileToParse);
170 } catch (CoreException e) {