package net.sourceforge.phpeclipse.phpeditor;

/**********************************************************************
Copyright (c) 2000, 2002 IBM Corp. and others.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Common Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/cpl-v10.html

Contributors:
    IBM Corporation - Initial implementation
    Klaus Hartlage - www.eclipseproject.de
**********************************************************************/

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import net.sourceforge.phpdt.internal.compiler.util.Util;
import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
import net.sourceforge.phpeclipse.PHPeclipsePlugin;
import net.sourceforge.phpeclipse.actions.ExternalPHPParser;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.TextEditorAction;

//import test.PHPParserManager;

/**
 * ClassDeclaration that defines the action for parsing the current PHP file
 */
public class PHPParserAction extends TextEditorAction {

  private static PHPParserAction instance = new PHPParserAction();

  protected IFile fileToParse;
  protected List fVariables = new ArrayList(100);

  /**
   * Constructs and updates the action.
   */
  private PHPParserAction() {
    super(PHPEditorMessages.getResourceBundle(), "ParserAction.", null); //$NON-NLS-1$
    update();
  }

  public static PHPParserAction getInstance() {
    return instance;
  }

  /**
   * Code called when the action is fired.
   */
  public void run() {
    boolean phpFlag = false;

    //  try {
    fileToParse = getPHPFile();
    parseFile(fileToParse);
  }

  public static void parseFile(IFile fileToParse) {
    boolean phpFlag = false;
//    try {

      if (fileToParse == null) {
        // TODO should never happen => should throw an exception
        System.err.println("Error : no file in the editor");

        return;
      }
      // TODO use isPHPFile()
      String name = fileToParse.getName(); //.toLowerCase();
//      for (int i = 0; i < EXTENSIONS.length; i++) {
//        if (name.endsWith(EXTENSIONS[i])) {
//        if (PHPFileUtil.isPHPFileName(name)) {
//          phpFlag = true; // php file extension
//          break;
//        }
//      }
//      if (phpFlag) {
      if (PHPFileUtil.isPHPFileName(name)) {
        IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
//        if (store.getString(PHPeclipsePlugin.PHP_PARSER_DEFAULT).equals(PHPeclipsePlugin.PHP_INTERNAL_PARSER)) {
//          PHPBuilder.removeProblemsAndTasksFor(fileToParse);
//          parse(fileToParse); //, iStream);
//        } else {
          ExternalPHPParser parser = new ExternalPHPParser(fileToParse);
          parser.phpExternalParse();
//        }
      }
  }
  /**
   * Finds the file that's currently opened in the PHP Text Editor
   */
  protected IFile getPHPFile() {
    ITextEditor editor = getTextEditor();

    IEditorInput editorInput = null;
    if (editor != null) {
      editorInput = editor.getEditorInput();
    }

    if (editorInput instanceof IFileEditorInput)
      return ((IFileEditorInput) editorInput).getFile();

    // if nothing was found, which should never happen
    return null;
  }

  

  
  protected static void parse(IFile fileToParse) {
    InputStream stream = null;
    char[] charArray;
    try {
      stream = new BufferedInputStream(fileToParse.getContents());
      charArray = Util.getInputStreamAsCharArray(stream, -1, null);
      ExternalPHPParser parser = new ExternalPHPParser(fileToParse);
      parser.phpExternalParse();
    } catch (CoreException e) {
    } catch (IOException e) {
    } finally {
      try {
      	if (stream!=null) {
          stream.close();
      	}
      } catch (IOException e) {
      }
    }
  }
}