3 import org.eclipse.core.resources.IFile;
 
   4 import org.eclipse.core.resources.IMarker;
 
   5 import org.eclipse.core.runtime.IPath;
 
   6 import org.eclipse.core.runtime.CoreException;
 
   7 import org.eclipse.jface.preference.IPreferenceStore;
 
   8 import org.eclipse.ui.texteditor.MarkerUtilities;
 
   9 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
  10 import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
 
  11 import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
 
  13 import java.text.MessageFormat;
 
  14 import java.util.Hashtable;
 
  17  * The superclass for our PHP parsers.
 
  18  * @author Matthieu Casanova
 
  20 public abstract class PHPParserSuperclass {
 
  21   // strings for external parser call
 
  22   private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$
 
  23   private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$
 
  26    * Call the php parse command ( php -l -f <filename> )
 
  27    * and create markers according to the external parser output
 
  29   public static void phpExternalParse(IFile file) {
 
  30     //IFile file = (IFile) resource;
 
  31     IPath path = file.getFullPath();
 
  32     IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
 
  33     String filename = file.getLocation().toString();
 
  35     String[] arguments = { filename };
 
  37       new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
 
  38     String command = form.format(arguments);
 
  41       PHPStartApacheAction.getParserOutput(command, "External parser: ");
 
  44       // parse the buffer to find the errors and warnings
 
  45       PHPParserSuperclass.createMarkers(parserResult, file);
 
  46     } catch (CoreException e) {
 
  51    * Create markers according to the external parser output
 
  53   private static void createMarkers(String output, IFile file)
 
  54     throws CoreException {
 
  56     file.deleteMarkers(IMarker.PROBLEM, false, 0);
 
  61     while ((brIndx = output.indexOf("<br />", indx)) != -1) {
 
  62       // newer php error output (tested with 4.2.3)
 
  63       scanLine(output, file, indx, brIndx);
 
  68       while ((brIndx = output.indexOf("<br>", indx)) != -1) {
 
  69         // older php error output (tested with 4.2.3)
 
  70         scanLine(output, file, indx, brIndx);
 
  76   private static void scanLine(String output, IFile file, int indx, int brIndx)
 
  77     throws CoreException {
 
  79     String outLineNumberString;
 
  80     StringBuffer lineNumberBuffer = new StringBuffer(10);
 
  82     current = output.substring(indx, brIndx);
 
  84     if (current.indexOf(PARSE_WARNING_STRING) != -1
 
  85       || current.indexOf(PARSE_ERROR_STRING) != -1) {
 
  86       int onLine = current.indexOf("on line <b>");
 
  88         lineNumberBuffer.delete(0, lineNumberBuffer.length());
 
  89         for (int i = onLine; i < current.length(); i++) {
 
  90           ch = current.charAt(i);
 
  91           if ('0' <= ch && '9' >= ch) {
 
  92             lineNumberBuffer.append(ch);
 
  96         int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
 
  98         Hashtable attributes = new Hashtable();
 
 100         current = current.replaceAll("\n", "");
 
 101         current = current.replaceAll("<b>", "");
 
 102         current = current.replaceAll("</b>", "");
 
 103         MarkerUtilities.setMessage(attributes, current);
 
 105         if (current.indexOf(PARSE_ERROR_STRING) != -1)
 
 106           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
 
 107         else if (current.indexOf(PARSE_WARNING_STRING) != -1)
 
 110             new Integer(IMarker.SEVERITY_WARNING));
 
 112           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
 
 113         MarkerUtilities.setLineNumber(attributes, lineNumber);
 
 114         MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
 
 119   public abstract PHPOutlineInfo parseInfo(Object parent, String s);
 
 121   public abstract void setFileToParse(IFile fileToParse);
 
 123   public abstract void parse(String s) throws CoreException;