02ca1635bc535e1c1b9bffac44380451e075a3f6
[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     www.phpeclipse.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.phpdt.internal.ui.util.PHPFileUtil;
23 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24 import net.sourceforge.phpeclipse.actions.ExternalPHPParser;
25
26 import org.eclipse.core.resources.IFile;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.jface.preference.IPreferenceStore;
29 import org.eclipse.ui.IEditorInput;
30 import org.eclipse.ui.IFileEditorInput;
31 import org.eclipse.ui.texteditor.ITextEditor;
32 import org.eclipse.ui.texteditor.TextEditorAction;
33
34 //import test.PHPParserManager;
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
43   protected IFile fileToParse;
44   protected List fVariables = new ArrayList(100);
45
46   /**
47    * Constructs and updates the action.
48    */
49   private PHPParserAction() {
50     super(PHPEditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
51     update();
52   }
53
54   public static PHPParserAction getInstance() {
55     return instance;
56   }
57
58   /**
59    * Code called when the action is fired.
60    */
61   public void run() {
62     boolean phpFlag = false;
63
64     //  try {
65     fileToParse = getPHPFile();
66     parseFile(fileToParse);
67   }
68
69   public static void parseFile(IFile fileToParse) {
70     boolean phpFlag = false;
71 //    try {
72
73       if (fileToParse == null) {
74         // TODO should never happen => should throw an exception
75         System.err.println("Error : no file in the editor");
76
77         return;
78       }
79       // TODO use isPHPFile()
80       String name = fileToParse.getName(); //.toLowerCase();
81 //      for (int i = 0; i < EXTENSIONS.length; i++) {
82 //        if (name.endsWith(EXTENSIONS[i])) {
83 //        if (PHPFileUtil.isPHPFileName(name)) {
84 //          phpFlag = true; // php file extension
85 //          break;
86 //        }
87 //      }
88 //      if (phpFlag) {
89       if (PHPFileUtil.isPHPFileName(name)) {
90         IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
91 //        if (store.getString(PHPeclipsePlugin.PHP_PARSER_DEFAULT).equals(PHPeclipsePlugin.PHP_INTERNAL_PARSER)) {
92 //          PHPBuilder.removeProblemsAndTasksFor(fileToParse);
93 //          parse(fileToParse); //, iStream);
94 //        } else {
95           ExternalPHPParser parser = new ExternalPHPParser(fileToParse);
96           parser.phpExternalParse();
97 //        }
98       }
99   }
100   /**
101    * Finds the file that's currently opened in the PHP Text Editor
102    */
103   protected IFile getPHPFile() {
104     ITextEditor editor = getTextEditor();
105
106     IEditorInput editorInput = null;
107     if (editor != null) {
108       editorInput = editor.getEditorInput();
109     }
110
111     if (editorInput instanceof IFileEditorInput)
112       return ((IFileEditorInput) editorInput).getFile();
113
114     // if nothing was found, which should never happen
115     return null;
116   }
117
118   
119
120   
121   protected static void parse(IFile fileToParse) {
122     InputStream stream = null;
123     char[] charArray;
124     try {
125       stream = new BufferedInputStream(fileToParse.getContents());
126       charArray = Util.getInputStreamAsCharArray(stream, -1, null);
127       ExternalPHPParser parser = new ExternalPHPParser(fileToParse);
128       parser.phpExternalParse();
129     } catch (CoreException e) {
130     } catch (IOException e) {
131     } finally {
132       try {
133         if (stream!=null) {
134           stream.close();
135         }
136       } catch (IOException e) {
137       }
138     }
139   }
140 }