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 import net.sourceforge.phpdt.internal.compiler.parser.Parser;
14 import java.text.MessageFormat;
15 import java.util.Hashtable;
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;
30 * Call the php parse command ( php -l -f <filename> )
31 * and create markers according to the external parser output
33 public static void phpExternalParse(IFile file) {
34 //IFile file = (IFile) resource;
35 IPath path = file.getFullPath();
36 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
37 String filename = file.getLocation().toString();
39 String[] arguments = { filename };
41 new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
42 String command = form.format(arguments);
45 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
57 private static void createMarkers(String output, IFile file)
58 throws CoreException {
60 file.deleteMarkers(IMarker.PROBLEM, false, 0);
65 while ((brIndx = output.indexOf("<br />", indx)) != -1) {
66 // newer php error output (tested with 4.2.3)
67 scanLine(output, file, indx, brIndx);
72 while ((brIndx = output.indexOf("<br>", indx)) != -1) {
73 // older php error output (tested with 4.2.3)
74 scanLine(output, file, indx, brIndx);
80 private static void scanLine(String output, IFile file, int indx, int brIndx)
81 throws CoreException {
83 String outLineNumberString;
84 StringBuffer lineNumberBuffer = new StringBuffer(10);
86 current = output.substring(indx, brIndx);
88 if (current.indexOf(PARSE_WARNING_STRING) != -1
89 || current.indexOf(PARSE_ERROR_STRING) != -1) {
90 int onLine = current.indexOf("on line <b>");
92 lineNumberBuffer.delete(0, lineNumberBuffer.length());
93 for (int i = onLine; i < current.length(); i++) {
94 ch = current.charAt(i);
95 if ('0' <= ch && '9' >= ch) {
96 lineNumberBuffer.append(ch);
100 int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
102 Hashtable attributes = new Hashtable();
104 current = current.replaceAll("\n", "");
105 current = current.replaceAll("<b>", "");
106 current = current.replaceAll("</b>", "");
107 MarkerUtilities.setMessage(attributes, current);
109 if (current.indexOf(PARSE_ERROR_STRING) != -1)
110 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
111 else if (current.indexOf(PARSE_WARNING_STRING) != -1)
114 new Integer(IMarker.SEVERITY_WARNING));
116 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
117 MarkerUtilities.setLineNumber(attributes, lineNumber);
118 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
123 public abstract PHPOutlineInfo parseInfo(Object parent, String s);
125 public abstract void setFileToParse(IFile fileToParse);
127 public abstract void parse(String s) throws CoreException;
129 public static void setMarker(
135 throws CoreException {
137 Hashtable attributes = new Hashtable();
138 MarkerUtilities.setMessage(attributes, message);
139 switch (errorLevel) {
141 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
143 case Parser.WARNING :
144 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
147 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
150 MarkerUtilities.setCharStart(attributes, charStart);
151 MarkerUtilities.setCharEnd(attributes, charEnd);
152 // setLineNumber(attributes, lineNumber);
153 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
157 public static void setMarker(IFile file,
163 throws CoreException {
165 Hashtable attributes = new Hashtable();
166 MarkerUtilities.setMessage(attributes, message);
167 switch (errorLevel) {
169 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
171 case Parser.WARNING :
172 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
175 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
178 attributes.put(IMarker.LOCATION,location);
179 MarkerUtilities.setCharStart(attributes, charStart);
180 MarkerUtilities.setCharEnd(attributes, charEnd);
181 // setLineNumber(attributes, lineNumber);
182 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);