Syntax Highlighting Prefs work now / Automatic parse on save option available
[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 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IMarker;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.ui.IEditorInput;
26 import org.eclipse.ui.IFileEditorInput;
27 import org.eclipse.ui.texteditor.ITextEditor;
28 import org.eclipse.ui.texteditor.TextEditorAction;
29
30 /**
31  * Class that defines the action for parsing the current PHP file
32  */
33 public class PHPParserAction extends TextEditorAction {
34
35   private static PHPParserAction instance = new PHPParserAction();
36
37   protected IFile fileToParse;
38   protected List fVariables = new ArrayList(100);
39
40   /**
41    * Constructs and updates the action.
42    */
43   private PHPParserAction() {
44     super(PHPEditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
45     update();
46   }
47
48   public static PHPParserAction getInstance() {
49     return instance;
50   }
51
52   /**
53    * Code called when the action is fired.
54    */
55   public void run() {
56     try {
57       fileToParse = getPHPFile();
58       if (fileToParse == null) {
59         // should never happen
60         System.err.println("Error : no file in the editor");
61         // should throw an exception
62         return;
63       }
64       IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
65       if (store.getBoolean(PHPeclipsePlugin.PHP_PARSER_DEFAULT)) {
66         // first delete all the previous markers
67         fileToParse.deleteMarkers(IMarker.PROBLEM, false, 0);
68
69         try {
70           InputStream iStream = fileToParse.getContents();
71           //        int c = iStream.read();
72           parse(iStream);
73           iStream.close();
74         } catch (IOException e) {
75         }
76       } else {
77         PHPParser.phpExternalParse(fileToParse);
78       }
79
80     } catch (CoreException e) {
81     }
82
83   }
84
85   /**
86    * Finds the file that's currently opened in the PHP Text Editor
87    */
88   protected IFile getPHPFile() {
89     ITextEditor editor = getTextEditor();
90
91     IEditorInput editorInput = null;
92     if (editor != null) {
93       editorInput = editor.getEditorInput();
94     }
95
96     if (editorInput instanceof IFileEditorInput)
97       return ((IFileEditorInput) editorInput).getFile();
98
99     // if nothing was found, which should never happen
100     return null;
101   }
102
103   /**
104    * Create marker for the parse error
105    */
106   //  protected void setMarker(String message, int lineNumber) throws CoreException {
107   //
108   //    Hashtable attributes = new Hashtable();
109   //    MarkerUtilities.setMessage(attributes, message);
110   //    if (message.startsWith(ERROR))
111   //      attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
112   //    else if (message.startsWith(WARNING))
113   //      attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
114   //    else
115   //      attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
116   //    MarkerUtilities.setLineNumber(attributes, lineNumber);
117   //    MarkerUtilities.createMarker(fileToParse, attributes, IMarker.PROBLEM);
118   //  }
119
120   //  private String getIdentifier(InputStream iStream, int c) {
121   //    //    int i = 0;
122   //    // char c;
123   //    //  int textLength = text.length();
124   //    StringBuffer identifier = new StringBuffer();
125   //    identifier.append((char) c);
126   //    try {
127   //      while ((c = iStream.read()) != (-1)) {
128   //        if (Character.isJavaIdentifierPart((char) c)) {
129   //          identifier.append((char) c);
130   //          //        } else if ((i == 0) && (c == '$')) {
131   //          //          identifier.append((char)c);
132   //        } else {
133   //          return identifier.toString();
134   //        }
135   //        //        i++;
136   //      }
137   //    } catch (IOException e) {
138   //    }
139   //    return identifier.toString();
140   //  }
141
142   protected void parse(InputStream iStream) {
143
144     StringBuffer buf = new StringBuffer();
145     int c0;
146     try {
147       while ((c0 = iStream.read()) != (-1)) {
148         buf.append((char) c0);
149       }
150     } catch (IOException e) {
151       return;
152     }
153     String input = buf.toString();
154
155     PHPParser parser = new PHPParser(fileToParse);
156     parser.htmlParse(input);
157   }
158 }