1 package net.sourceforge.phpeclipse.actions;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.text.MessageFormat;
6 import java.util.Hashtable;
8 import net.sourceforge.phpdt.internal.ui.util.StringUtil;
9 import net.sourceforge.phpeclipse.PHPConsole;
10 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
12 import org.eclipse.core.resources.IFile;
13 import org.eclipse.core.resources.IMarker;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.ui.texteditor.MarkerUtilities;
20 * Calls the external parser and generates problem markers if necessary
22 public class ExternalPHPParser {
23 private final static String PROBLEM_ID = "net.sourceforge.phpeclipse.problem";
25 // strings for external parser call
26 private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$
28 private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$
30 public static final int ERROR = 2;
32 public static final int WARNING = 1;
34 public static final int INFO = 0;
36 public static final int TASK = 3;
38 // TODO design error? Analyze why fileToParse must be static ???
39 final protected IFile fFileToParse;
41 public ExternalPHPParser(IFile file) {
46 * Call the php parse command ( php -l -f <filename> ) and create markers according to the external parser output.
49 * the file that will be parsed
51 public void phpExternalParse() {
52 //IFile file = (IFile) resource;
53 // final IPath path = file.getFullPath();
54 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
55 final String filename = fFileToParse.getLocation().toString();
57 final String[] arguments = { filename };
58 final MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
59 final String command = form.format(arguments);
61 final String parserResult = getParserOutput(command, "External parser: ");
64 // parse the buffer to find the errors and warnings
65 createMarkers(parserResult, fFileToParse);
66 } catch (CoreException e) {
71 * Create markers according to the external parser output.
74 * the external parser output
76 * the file that was parsed.
78 protected void createMarkers(final String output, final IFile file) throws CoreException {
80 file.deleteMarkers(PROBLEM_ID, false, 0);
85 while ((brIndx = output.indexOf("<br />", indx)) != -1) {
86 // newer php error output (tested with 4.2.3)
87 scanLine(output, file, indx, brIndx);
92 while ((brIndx = output.indexOf("<br>", indx)) != -1) {
93 // older php error output (tested with 4.2.3)
94 scanLine(output, file, indx, brIndx);
100 private void scanLine(final String output, final IFile file, final int indx, final int brIndx) throws CoreException {
102 // String outLineNumberString; never used
103 final StringBuffer lineNumberBuffer = new StringBuffer(10);
105 current = output.substring(indx, brIndx);
107 if (current.indexOf(PARSE_WARNING_STRING) != -1 || current.indexOf(PARSE_ERROR_STRING) != -1) {
108 final int onLine = current.indexOf("on line <b>");
110 lineNumberBuffer.delete(0, lineNumberBuffer.length());
111 for (int i = onLine; i < current.length(); i++) {
112 ch = current.charAt(i);
113 if ('0' <= ch && '9' >= ch) {
114 lineNumberBuffer.append(ch);
118 final int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
120 final Hashtable attributes = new Hashtable();
122 current = StringUtil.replaceAll(current, "\n", "");
123 current = StringUtil.replaceAll(current, "<b>", "");
124 current = StringUtil.replaceAll(current, "</b>", "");
125 MarkerUtilities.setMessage(attributes, current);
127 if (current.indexOf(PARSE_ERROR_STRING) != -1)
128 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
129 else if (current.indexOf(PARSE_WARNING_STRING) != -1)
130 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
132 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
133 MarkerUtilities.setLineNumber(attributes, lineNumber);
134 MarkerUtilities.createMarker(file, attributes, PROBLEM_ID);
140 * This will set a marker.
143 * the file that generated the marker
147 * the starting character
151 * the error level ({@link ExternalPHPParser#ERROR},{@link ExternalPHPParser#INFO},{@link ExternalPHPParser#WARNING}),
152 * {@link ExternalPHPParser#TASK})
153 * @throws CoreException
154 * an exception throwed by the MarkerUtilities
156 private void setMarker(final IFile file, final String message, final int charStart, final int charEnd, 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, PROBLEM_ID);
182 * This will set a marker.
185 * the file that generated the marker
191 * the error level ({@link ExternalPHPParser#ERROR},{@link ExternalPHPParser#INFO},{@link ExternalPHPParser#WARNING})
192 * @throws CoreException
193 * an exception throwed by the MarkerUtilities
195 private void setMarker(final IFile file, final String message, final int line, final int errorLevel, final String location)
196 throws CoreException {
198 String markerKind = PROBLEM_ID;
199 final Hashtable attributes = new Hashtable();
200 MarkerUtilities.setMessage(attributes, message);
201 switch (errorLevel) {
203 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
206 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
209 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
212 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
213 markerKind = IMarker.TASK;
216 attributes.put(IMarker.LOCATION, location);
217 MarkerUtilities.setLineNumber(attributes, line);
218 MarkerUtilities.createMarker(file, attributes, markerKind);
223 * This will set a marker.
228 * the starting character
232 * the error level ({@link ExternalPHPParser#ERROR},{@link ExternalPHPParser#INFO},{@link ExternalPHPParser#WARNING})
233 * @throws CoreException
234 * an exception throwed by the MarkerUtilities
236 private void setMarker(final String message, final int charStart, final int charEnd, final int errorLevel, final String location)
237 throws CoreException {
238 if (fFileToParse != null) {
239 setMarker(fFileToParse, message, charStart, charEnd, errorLevel, location);
244 * This will set a marker.
247 * the file that generated the marker
251 * the starting character
255 * the error level ({@link ExternalPHPParser#ERROR},{@link ExternalPHPParser#INFO},{@link ExternalPHPParser#WARNING})
257 * the location of the error
258 * @throws CoreException
259 * an exception throwed by the MarkerUtilities
261 private void setMarker(final IFile file, final String message, final int charStart, final int charEnd, final int errorLevel,
262 final String location) throws CoreException {
264 final Hashtable attributes = new Hashtable();
265 MarkerUtilities.setMessage(attributes, message);
266 switch (errorLevel) {
268 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
271 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
274 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
277 attributes.put(IMarker.SEVERITY, new Integer(IMarker.TASK));
280 attributes.put(IMarker.LOCATION, location);
281 MarkerUtilities.setCharStart(attributes, charStart);
282 MarkerUtilities.setCharEnd(attributes, charEnd);
283 MarkerUtilities.createMarker(file, attributes, PROBLEM_ID); //IMarker.PROBLEM);
287 private String getParserOutput(String command, String consoleMessage) {
289 PHPConsole console = new PHPConsole();
291 console.println(consoleMessage + command);
292 } catch (Throwable th) {
296 Runtime runtime = Runtime.getRuntime();
299 Process p = runtime.exec(command);
301 // gets the input stream to have the post-compile-time information
302 InputStream stream = p.getInputStream();
304 // get the string from Stream
305 String consoleOutput = PHPConsole.getStringFromStream(stream);
307 // prints out the information
308 if (console != null) {
309 console.print(consoleOutput);
311 return consoleOutput;
313 } catch (IOException e) {
314 MessageDialog.openInformation(null, "IOException: ", e.getMessage());