*** empty log message ***
[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     try {
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
67         return;
68       }
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
73           break;
74         }
75       }
76       if (phpFlag) {
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);
81
82           try {
83             InputStream iStream = fileToParse.getContents();
84             //        int c = iStream.read();
85             parse(iStream);
86             iStream.close();
87           } catch (IOException e) {
88           }
89         } else {
90           PHPParserSuperclass.phpExternalParse(fileToParse);
91         }
92       }
93     } catch (CoreException e) {
94     }
95
96   }
97
98   /**
99    * Finds the file that's currently opened in the PHP Text Editor
100    */
101   protected IFile getPHPFile() {
102     ITextEditor editor = getTextEditor();
103
104     IEditorInput editorInput = null;
105     if (editor != null) {
106       editorInput = editor.getEditorInput();
107     }
108
109     if (editorInput instanceof IFileEditorInput)
110       return ((IFileEditorInput) editorInput).getFile();
111
112     // if nothing was found, which should never happen
113     return null;
114   }
115
116   /**
117    * Create marker for the parse error
118    */
119   //  protected void setMarker(String message, int lineNumber) throws CoreException {
120   //
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));
127   //    else
128   //      attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
129   //    MarkerUtilities.setLineNumber(attributes, lineNumber);
130   //    MarkerUtilities.createMarker(fileToParse, attributes, IMarker.PROBLEM);
131   //  }
132
133   //  private String getIdentifier(InputStream iStream, int c) {
134   //    //    int i = 0;
135   //    // char c;
136   //    //  int textLength = text.length();
137   //    StringBuffer identifier = new StringBuffer();
138   //    identifier.append((char) c);
139   //    try {
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);
145   //        } else {
146   //          return identifier.toString();
147   //        }
148   //        //        i++;
149   //      }
150   //    } catch (IOException e) {
151   //    }
152   //    return identifier.toString();
153   //  }
154
155   protected void parse(InputStream iStream) {
156
157     StringBuffer buf = new StringBuffer();
158     int c0;
159     try {
160       while ((c0 = iStream.read()) != (-1)) {
161         buf.append((char) c0);
162       }
163     } catch (IOException e) {
164       return;
165     }
166     String input = buf.toString();
167
168     PHPParserSuperclass parser = PHPParserManager.getParser(fileToParse);
169     try {
170       parser.parse(input);
171     } catch (CoreException e) {
172     }
173   }
174 }