3 import org.eclipse.core.resources.IFile;
4 import org.eclipse.core.resources.IMarker;
5 import org.eclipse.core.runtime.CoreException;
6 import org.eclipse.jface.preference.IPreferenceStore;
7 import org.eclipse.ui.texteditor.MarkerUtilities;
8 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
9 import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
10 import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
11 import net.sourceforge.phpdt.internal.compiler.parser.Parser;
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$
24 public static final int ERROR = 2;
25 public static final int WARNING = 1;
26 public static final int INFO = 0;
29 * Call the php parse command ( php -l -f <filename> )
30 * and create markers according to the external parser output.
31 * @param file the file that will be parsed
33 public static void phpExternalParse(final IFile file) {
34 //IFile file = (IFile) resource;
35 // final IPath path = file.getFullPath();
36 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
37 final String filename = file.getLocation().toString();
39 final String[] arguments = { filename };
40 final MessageFormat form =
41 new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
42 final String command = form.format(arguments);
44 final String parserResult =
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(final String output, final 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(final String output, final IFile file, final int indx, final int brIndx)
81 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
89 || current.indexOf(PARSE_ERROR_STRING) != -1) {
90 final 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 final int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
102 final 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);
124 * This will parse the file and generate the outline info
125 * @param parent the parent object
126 * @param s the string that should be parsed
127 * @return the outline info
129 public abstract PHPOutlineInfo parseInfo(Object parent, String s);
132 * This will change the file to parse.
133 * @param fileToParse the file that should be parsed
135 public abstract void setFileToParse(IFile fileToParse);
138 * This will parse the given string
139 * @param s the string to parse
140 * @throws CoreException an exception that can be launched
142 public abstract void parse(String s) throws CoreException;
145 * This will set a marker.
146 * @param file the file that generated the marker
147 * @param message the message
148 * @param charStart the starting character
149 * @param charEnd the end character
150 * @param errorLevel the error level ({@link PHPParserSuperclass#ERROR},
151 * {@link PHPParserSuperclass#INFO},{@link PHPParserSuperclass#WARNING})
152 * @throws CoreException an exception throwed by the MarkerUtilities
154 public static void setMarker(final IFile file,
155 final String message,
158 final int errorLevel)
159 throws CoreException {
161 final Hashtable attributes = new Hashtable();
162 MarkerUtilities.setMessage(attributes, message);
163 switch (errorLevel) {
165 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
167 case Parser.WARNING :
168 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
171 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
174 MarkerUtilities.setCharStart(attributes, charStart);
175 MarkerUtilities.setCharEnd(attributes, charEnd);
176 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
181 * This will set a marker.
182 * @param file the file that generated the marker
183 * @param message the message
184 * @param charStart the starting character
185 * @param charEnd the end character
186 * @param errorLevel the error level ({@link PHPParserSuperclass#ERROR},
187 * {@link PHPParserSuperclass#INFO},{@link PHPParserSuperclass#WARNING})
188 * @param location the location of the error
189 * @throws CoreException an exception throwed by the MarkerUtilities
191 public static void setMarker(final IFile file,
192 final String message,
195 final int errorLevel,
196 final String location)
197 throws CoreException {
199 final Hashtable attributes = new Hashtable();
200 MarkerUtilities.setMessage(attributes, message);
201 switch (errorLevel) {
203 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
205 case Parser.WARNING :
206 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
209 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
212 attributes.put(IMarker.LOCATION,location);
213 MarkerUtilities.setCharStart(attributes, charStart);
214 MarkerUtilities.setCharEnd(attributes, charEnd);
215 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);