Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / PHPParserAction.java
1 package net.sourceforge.phpeclipse.phpeditor;
2
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
9
10 Contributors:
11     IBM Corporation - Initial implementation
12     Klaus Hartlage - www.eclipseproject.de
13 **********************************************************************/
14
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;
20
21 import net.sourceforge.phpdt.internal.compiler.util.Util;
22 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
23
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.IMarker;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.jface.preference.IPreferenceStore;
28 import org.eclipse.ui.IEditorInput;
29 import org.eclipse.ui.IFileEditorInput;
30 import org.eclipse.ui.texteditor.ITextEditor;
31 import org.eclipse.ui.texteditor.TextEditorAction;
32
33 import test.PHPParserManager;
34 import test.PHPParserSuperclass;
35
36 /**
37  * ClassDeclaration that defines the action for parsing the current PHP file
38  */
39 public class PHPParserAction extends TextEditorAction {
40
41   private static PHPParserAction instance = new PHPParserAction();
42   private static String[] EXTENSIONS = { ".php", ".php3", ".php4", ".inc", ".phtml" };
43
44   protected IFile fileToParse;
45   protected List fVariables = new ArrayList(100);
46
47   /**
48    * Constructs and updates the action.
49    */
50   private PHPParserAction() {
51     super(PHPEditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
52     update();
53   }
54
55   public static PHPParserAction getInstance() {
56     return instance;
57   }
58
59   /**
60    * Code called when the action is fired.
61    */
62   public void run() {
63     boolean phpFlag = false;
64
65     //  try {
66     fileToParse = getPHPFile();
67     parseFile(fileToParse);
68   }
69
70   public static void parseFile(IFile fileToParse) {
71     boolean phpFlag = false;
72     try {
73
74       if (fileToParse == null) {
75         // TODO should never happen => should throw an exception
76         System.err.println("Error : no file in the editor");
77
78         return;
79       }
80       // TODO use isPHPFile()
81       String name = fileToParse.getName().toLowerCase();
82       for (int i = 0; i < EXTENSIONS.length; i++) {
83         if (name.endsWith(EXTENSIONS[i])) {
84           phpFlag = true; // php file extension
85           break;
86         }
87       }
88       if (phpFlag) {
89         IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
90         if (store.getString(PHPeclipsePlugin.PHP_PARSER_DEFAULT).equals(PHPeclipsePlugin.PHP_INTERNAL_PARSER)) {
91           // first delete all the previous markers
92           fileToParse.deleteMarkers(IMarker.PROBLEM, false, 0);
93
94           //the tasks are removed here
95           fileToParse.deleteMarkers(IMarker.TASK, false, 0);
96
97           //                    try {
98           //                      InputStream iStream = fileToParse.getContents();
99           parse(fileToParse); //, iStream);
100           //                      iStream.close();
101           //                    } catch (IOException e) {
102           //                    }
103         } else {
104           PHPParserSuperclass.phpExternalParse(fileToParse);
105         }
106       }
107     } catch (CoreException e) {
108     }
109
110   }
111   /**
112    * Finds the file that's currently opened in the PHP Text Editor
113    */
114   protected IFile getPHPFile() {
115     ITextEditor editor = getTextEditor();
116
117     IEditorInput editorInput = null;
118     if (editor != null) {
119       editorInput = editor.getEditorInput();
120     }
121
122     if (editorInput instanceof IFileEditorInput)
123       return ((IFileEditorInput) editorInput).getFile();
124
125     // if nothing was found, which should never happen
126     return null;
127   }
128
129   /**
130    * Create marker for the parse error
131    */
132   //  protected void setMarker(String message, int lineNumber) throws CoreException {
133   //
134   //    Hashtable attributes = new Hashtable();
135   //    MarkerUtilities.setMessage(attributes, message);
136   //    if (message.startsWith(ERROR))
137   //      attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
138   //    else if (message.startsWith(WARNING))
139   //      attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
140   //    else
141   //      attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
142   //    MarkerUtilities.setLineNumber(attributes, lineNumber);
143   //    MarkerUtilities.createMarker(fileToParse, attributes, IMarker.PROBLEM);
144   //  }
145
146   //  private String getIdentifier(InputStream iStream, int c) {
147   //    //    int i = 0;
148   //    // char c;
149   //    //  int textLength = text.length();
150   //    StringBuffer identifier = new StringBuffer();
151   //    identifier.append((char) c);
152   //    try {
153   //      while ((c = iStream.read()) != (-1)) {
154   //        if (Scanner.isPHPIdentifierPart((char) c)) {
155   //          identifier.append((char) c);
156   //          //        } else if ((i == 0) && (c == '$')) {
157   //          //          identifier.append((char)c);
158   //        } else {
159   //          return identifier.toString();
160   //        }
161   //        //        i++;
162   //      }
163   //    } catch (IOException e) {
164   //    }
165   //    return identifier.toString();
166   //  }
167
168   protected static void parse(IFile fileToParse) {
169
170     //    StringBuffer buf = new StringBuffer();
171     //    int c0;
172     //    try {
173     //      while ((c0 = iStream.read()) != (-1)) {
174     //        buf.append((char) c0);
175     //      }
176     //    } catch (IOException e) {
177     //      return;
178     //    }
179     //    String input = buf.toString();
180
181     InputStream stream = null;
182     char[] charArray;
183     try {
184       stream = new BufferedInputStream(fileToParse.getContents());
185       charArray = Util.getInputStreamAsCharArray(stream, -1, null);
186       PHPParserSuperclass parser = PHPParserManager.getParser(fileToParse);
187       parser.parse(new String(charArray));
188     } catch (CoreException e) {
189     } catch (IOException e) {
190     } finally {
191       try {
192         if (stream!=null) {
193           stream.close();
194         }
195       } catch (IOException e) {
196       }
197     }
198   }
199 }