3 import java.text.MessageFormat;
 
   4 import java.util.Hashtable;
 
   6 import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
 
   7 import net.sourceforge.phpdt.internal.ui.util.StringUtil;
 
   8 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
   9 import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
 
  11 import org.eclipse.core.resources.IFile;
 
  12 import org.eclipse.core.resources.IMarker;
 
  13 import org.eclipse.core.runtime.CoreException;
 
  14 import org.eclipse.jface.preference.IPreferenceStore;
 
  15 import org.eclipse.ui.texteditor.MarkerUtilities;
 
  18  * The superclass for our PHP parsers.
 
  19  * @author Matthieu Casanova
 
  21 public abstract class PHPParserSuperclass {
 
  22   // strings for external parser call
 
  23   private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$
 
  24   private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$
 
  25   public static final int ERROR = 2;
 
  26   public static final int WARNING = 1;
 
  27   public static final int INFO = 0;
 
  28   public static final int TASK = 3;
 
  31    * Call the php parse command ( php -l -f <filename> )
 
  32    * and create markers according to the external parser output.
 
  33    * @param file the file that will be parsed
 
  35   public static void phpExternalParse(final IFile file) {
 
  36     //IFile file = (IFile) resource;
 
  37     //  final IPath path = file.getFullPath();
 
  38     final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
 
  39     final String filename = file.getLocation().toString();
 
  41     final String[] arguments = { filename };
 
  42     final MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
 
  43     final String command = form.format(arguments);
 
  45     final String parserResult = PHPStartApacheAction.getParserOutput(command, "External parser: ");
 
  48       // parse the buffer to find the errors and warnings
 
  49       createMarkers(parserResult, file);
 
  50     } catch (CoreException e) {
 
  55    * Create markers according to the external parser output.
 
  56    * @param output the external parser output
 
  57    * @param file the file that was parsed.
 
  59   protected static void createMarkers(final String output, final IFile file) throws CoreException {
 
  61     file.deleteMarkers(IMarker.PROBLEM, false, 0);
 
  66     while ((brIndx = output.indexOf("<br />", indx)) != -1) {
 
  67       // newer php error output (tested with 4.2.3)
 
  68       scanLine(output, file, indx, brIndx);
 
  73       while ((brIndx = output.indexOf("<br>", indx)) != -1) {
 
  74         // older php error output (tested with 4.2.3)
 
  75         scanLine(output, file, indx, brIndx);
 
  81   private static void scanLine(final String output, final IFile file, final int indx, final int brIndx) throws CoreException {
 
  83     //  String outLineNumberString; never used
 
  84     final StringBuffer lineNumberBuffer = new StringBuffer(10);
 
  86     current = output.substring(indx, brIndx);
 
  88     if (current.indexOf(PARSE_WARNING_STRING) != -1 || current.indexOf(PARSE_ERROR_STRING) != -1) {
 
  89       final int onLine = current.indexOf("on line <b>");
 
  91         lineNumberBuffer.delete(0, lineNumberBuffer.length());
 
  92         for (int i = onLine; i < current.length(); i++) {
 
  93           ch = current.charAt(i);
 
  94           if ('0' <= ch && '9' >= ch) {
 
  95             lineNumberBuffer.append(ch);
 
  99         final int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
 
 101         final Hashtable attributes = new Hashtable();
 
 103         current = StringUtil.replaceAll(current, "\n", "");
 
 104         current = StringUtil.replaceAll(current, "<b>", "");
 
 105         current = StringUtil.replaceAll(current, "</b>", "");
 
 106         MarkerUtilities.setMessage(attributes, current);
 
 108         if (current.indexOf(PARSE_ERROR_STRING) != -1)
 
 109           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
 
 110         else if (current.indexOf(PARSE_WARNING_STRING) != -1)
 
 111           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
 
 113           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
 
 114         MarkerUtilities.setLineNumber(attributes, lineNumber);
 
 115         MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
 
 121    * This will parse the file and generate the outline info
 
 122    * @param parent the parent object
 
 123    * @param s the string that should be parsed
 
 124    * @return the outline info
 
 126   public abstract PHPOutlineInfo parseInfo(Object parent, String s);
 
 129    * This will change the file to parse.
 
 130    * @param fileToParse the file that should be parsed
 
 132   public abstract void setFileToParse(IFile fileToParse);
 
 135    * This will parse the given string
 
 136    * @param s the string to parse
 
 137    * @throws CoreException an exception that can be launched
 
 139   public abstract void parse(String s) throws CoreException;
 
 142    * This will set a marker.
 
 143    * @param file the file that generated the marker
 
 144    * @param message the message
 
 145    * @param charStart the starting character
 
 146    * @param charEnd the end character
 
 147    * @param errorLevel the error level ({@link PHPParserSuperclass#ERROR},
 
 148    *        {@link PHPParserSuperclass#INFO},{@link PHPParserSuperclass#WARNING}),{@link PHPParserSuperclass#TASK})
 
 149    * @throws CoreException an exception throwed by the MarkerUtilities
 
 151   public static void setMarker(
 
 153     final String message,
 
 156     final int errorLevel)
 
 157     throws CoreException {
 
 159       final Hashtable attributes = new Hashtable();
 
 160       MarkerUtilities.setMessage(attributes, message);
 
 161       switch (errorLevel) {
 
 163           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
 
 166           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
 
 169           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
 
 172           attributes.put(IMarker.SEVERITY, new Integer(IMarker.TASK));
 
 175       MarkerUtilities.setCharStart(attributes, charStart);
 
 176       MarkerUtilities.setCharEnd(attributes, charEnd);
 
 177       MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
 
 182    * This will set a marker.
 
 183    * @param file the file that generated the marker
 
 184    * @param message the message
 
 185    * @param line the line number
 
 186    * @param errorLevel the error level ({@link PHPParserSuperclass#ERROR},
 
 187    *        {@link PHPParserSuperclass#INFO},{@link PHPParserSuperclass#WARNING})
 
 188    * @throws CoreException an exception throwed by the MarkerUtilities
 
 190   public static void setMarker(final IFile file, final String message, final int line, final int errorLevel, final String location)
 
 191     throws CoreException {
 
 193       String markerKind = IMarker.PROBLEM;
 
 194       final Hashtable attributes = new Hashtable();
 
 195       MarkerUtilities.setMessage(attributes, message);
 
 196       switch (errorLevel) {
 
 198           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
 
 201           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
 
 204           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
 
 207           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
 
 208           markerKind = IMarker.TASK;
 
 211       attributes.put(IMarker.LOCATION, location);
 
 212       MarkerUtilities.setLineNumber(attributes, line);
 
 213       MarkerUtilities.createMarker(file, attributes, markerKind);
 
 218    * This will set a marker.
 
 219    * @param file the file that generated the marker
 
 220    * @param message the message
 
 221    * @param charStart the starting character
 
 222    * @param charEnd the end character
 
 223    * @param errorLevel the error level ({@link PHPParserSuperclass#ERROR},
 
 224    *        {@link PHPParserSuperclass#INFO},{@link PHPParserSuperclass#WARNING})
 
 225    * @param location the location of the error
 
 226    * @throws CoreException an exception throwed by the MarkerUtilities
 
 228   public static void setMarker(
 
 230     final String message,
 
 233     final int errorLevel,
 
 234     final String location)
 
 235     throws CoreException {
 
 237       final Hashtable attributes = new Hashtable();
 
 238       MarkerUtilities.setMessage(attributes, message);
 
 239       switch (errorLevel) {
 
 241           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
 
 244           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
 
 247           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
 
 250           attributes.put(IMarker.SEVERITY, new Integer(IMarker.TASK));
 
 253       attributes.put(IMarker.LOCATION, location);
 
 254       MarkerUtilities.setCharStart(attributes, charStart);
 
 255       MarkerUtilities.setCharEnd(attributes, charEnd);
 
 256       MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);