*** empty log message ***
[phpeclipse.git] / net.sourceforge.phpeclipse / src / test / PHPParserSuperclass.java
1 package test;
2
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;
12
13 import java.text.MessageFormat;
14 import java.util.Hashtable;
15
16 /**
17  * The superclass for our PHP parsers.
18  * @author Matthieu Casanova
19  */
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$
24
25   /**
26    * Call the php parse command ( php -l -f <filename> )
27    * and create markers according to the external parser output
28    */
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();
34
35     String[] arguments = { filename };
36     MessageFormat form =
37       new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
38     String command = form.format(arguments);
39
40     String parserResult =
41       PHPStartApacheAction.getParserOutput(command, "External parser: ");
42
43     try {
44       // parse the buffer to find the errors and warnings
45       createMarkers(parserResult, file);
46     } catch (CoreException e) {
47     }
48   }
49
50   /**
51    * Create markers according to the external parser output
52    */
53   private static void createMarkers(String output, IFile file)
54     throws CoreException {
55     // delete all markers
56     file.deleteMarkers(IMarker.PROBLEM, false, 0);
57
58     int indx = 0;
59     int brIndx = 0;
60     boolean flag = true;
61     while ((brIndx = output.indexOf("<br />", indx)) != -1) {
62       // newer php error output (tested with 4.2.3)
63       scanLine(output, file, indx, brIndx);
64       indx = brIndx + 6;
65       flag = false;
66     }
67     if (flag) {
68       while ((brIndx = output.indexOf("<br>", indx)) != -1) {
69         // older php error output (tested with 4.2.3)
70         scanLine(output, file, indx, brIndx);
71         indx = brIndx + 4;
72       }
73     }
74   }
75
76   private static void scanLine(String output, IFile file, int indx, int brIndx)
77     throws CoreException {
78     String current;
79     String outLineNumberString;
80     StringBuffer lineNumberBuffer = new StringBuffer(10);
81     char ch;
82     current = output.substring(indx, brIndx);
83
84     if (current.indexOf(PARSE_WARNING_STRING) != -1
85       || current.indexOf(PARSE_ERROR_STRING) != -1) {
86       int onLine = current.indexOf("on line <b>");
87       if (onLine != -1) {
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);
93           }
94         }
95
96         int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
97
98         Hashtable attributes = new Hashtable();
99
100         current = current.replaceAll("\n", "");
101         current = current.replaceAll("<b>", "");
102         current = current.replaceAll("</b>", "");
103         MarkerUtilities.setMessage(attributes, current);
104
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)
108           attributes.put(
109             IMarker.SEVERITY,
110             new Integer(IMarker.SEVERITY_WARNING));
111         else
112           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
113         MarkerUtilities.setLineNumber(attributes, lineNumber);
114         MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
115       }
116     }
117   }
118
119   public abstract PHPOutlineInfo parseInfo(Object parent, String s);
120
121   public abstract void setFileToParse(IFile fileToParse);
122
123   public abstract void parse(String s) throws CoreException;
124 }