little bugfixes
[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.IOException;
16 import java.io.InputStream;
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
21
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;
32
33 /**
34  * ClassDeclaration that defines the action for parsing the current PHP file
35  */
36 public class PHPParserAction extends TextEditorAction {
37
38   private static PHPParserAction instance = new PHPParserAction();
39   private static String[] EXTENSIONS = { ".php", ".php3", ".php4", ".inc", ".phtml" };
40
41   protected IFile fileToParse;
42   protected List fVariables = new ArrayList(100);
43
44   /**
45    * Constructs and updates the action.
46    */
47   private PHPParserAction() {
48     super(PHPEditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
49     update();
50   }
51
52   public static PHPParserAction getInstance() {
53     return instance;
54   }
55
56   /**
57    * Code called when the action is fired.
58    */
59   public void run() {
60     boolean phpFlag = false;
61                 
62   //  try {
63       fileToParse = getPHPFile();
64                         parseFile(fileToParse);
65   }
66
67         public static void parseFile(IFile fileToParse) {
68                         boolean phpFlag = false;
69                         try {
70                                 
71                                 if (fileToParse == null) {
72                                         // should never happen
73                                         System.err.println("Error : no file in the editor");
74                                         // should throw an exception
75                                         return;
76                                 }
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
81                                                 break;
82                                         }
83                                 }
84                                 if (phpFlag) {
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);
89
90                                                 //the tasks are removed here
91                                                 fileToParse.deleteMarkers(IMarker.TASK, false, 0);
92
93                                                 try {
94                                                         InputStream iStream = fileToParse.getContents();
95                                                         //        int c = iStream.read();
96                                                         parse(fileToParse,iStream);
97                                                         iStream.close();
98                                                 } catch (IOException e) {
99                                                 }
100                                         } else {
101                                                 PHPParserSuperclass.phpExternalParse(fileToParse);
102                                         }
103                                 }
104                         } catch (CoreException e) {
105                         }
106
107                 }
108   /**
109    * Finds the file that's currently opened in the PHP Text Editor
110    */
111   protected IFile getPHPFile() {
112     ITextEditor editor = getTextEditor();
113
114     IEditorInput editorInput = null;
115     if (editor != null) {
116       editorInput = editor.getEditorInput();
117     }
118
119     if (editorInput instanceof IFileEditorInput)
120       return ((IFileEditorInput) editorInput).getFile();
121
122     // if nothing was found, which should never happen
123     return null;
124   }
125
126   /**
127    * Create marker for the parse error
128    */
129   //  protected void setMarker(String message, int lineNumber) throws CoreException {
130   //
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));
137   //    else
138   //      attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
139   //    MarkerUtilities.setLineNumber(attributes, lineNumber);
140   //    MarkerUtilities.createMarker(fileToParse, attributes, IMarker.PROBLEM);
141   //  }
142
143   //  private String getIdentifier(InputStream iStream, int c) {
144   //    //    int i = 0;
145   //    // char c;
146   //    //  int textLength = text.length();
147   //    StringBuffer identifier = new StringBuffer();
148   //    identifier.append((char) c);
149   //    try {
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);
155   //        } else {
156   //          return identifier.toString();
157   //        }
158   //        //        i++;
159   //      }
160   //    } catch (IOException e) {
161   //    }
162   //    return identifier.toString();
163   //  }
164
165   protected static void parse(IFile fileToParse, InputStream iStream) {
166
167     StringBuffer buf = new StringBuffer();
168     int c0;
169     try {
170       while ((c0 = iStream.read()) != (-1)) {
171         buf.append((char) c0);
172       }
173     } catch (IOException e) {
174       return;
175     }
176     String input = buf.toString();
177
178     PHPParserSuperclass parser = PHPParserManager.getParser(fileToParse);
179     try {
180       parser.parse(input);
181     } catch (CoreException e) {
182     }
183   }
184 }