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;
27 public static final int TASK = 3;
30 * Call the php parse command ( php -l -f <filename> )
31 * and create markers according to the external parser output.
32 * @param file the file that will be parsed
34 public static void phpExternalParse(final IFile file) {
35 //IFile file = (IFile) resource;
36 // final IPath path = file.getFullPath();
37 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
38 final String filename = file.getLocation().toString();
40 final String[] arguments = {filename};
41 final MessageFormat form =
42 new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
43 final String command = form.format(arguments);
45 final String parserResult =
46 PHPStartApacheAction.getParserOutput(command, "External parser: ");
49 // parse the buffer to find the errors and warnings
50 createMarkers(parserResult, file);
51 } catch (CoreException e) {
56 * Create markers according to the external parser output.
57 * @param output the external parser output
58 * @param file the file that was parsed.
60 protected static void createMarkers(final String output, final IFile file)
61 throws CoreException {
63 file.deleteMarkers(IMarker.PROBLEM, false, 0);
68 while ((brIndx = output.indexOf("<br />", indx)) != -1) {
69 // newer php error output (tested with 4.2.3)
70 scanLine(output, file, indx, brIndx);
75 while ((brIndx = output.indexOf("<br>", indx)) != -1) {
76 // older php error output (tested with 4.2.3)
77 scanLine(output, file, indx, brIndx);
83 private static void scanLine(final String output, final IFile file, final int indx, final int brIndx)
84 throws CoreException {
86 // String outLineNumberString; never used
87 final StringBuffer lineNumberBuffer = new StringBuffer(10);
89 current = output.substring(indx, brIndx);
91 if (current.indexOf(PARSE_WARNING_STRING) != -1
92 || current.indexOf(PARSE_ERROR_STRING) != -1) {
93 final int onLine = current.indexOf("on line <b>");
95 lineNumberBuffer.delete(0, lineNumberBuffer.length());
96 for (int i = onLine; i < current.length(); i++) {
97 ch = current.charAt(i);
98 if ('0' <= ch && '9' >= ch) {
99 lineNumberBuffer.append(ch);
103 final int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
105 final Hashtable attributes = new Hashtable();
107 current = current.replaceAll("\n", "");
108 current = current.replaceAll("<b>", "");
109 current = current.replaceAll("</b>", "");
110 MarkerUtilities.setMessage(attributes, current);
112 if (current.indexOf(PARSE_ERROR_STRING) != -1)
113 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
114 else if (current.indexOf(PARSE_WARNING_STRING) != -1)
117 new Integer(IMarker.SEVERITY_WARNING));
119 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
120 MarkerUtilities.setLineNumber(attributes, lineNumber);
121 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
127 * This will parse the file and generate the outline info
128 * @param parent the parent object
129 * @param s the string that should be parsed
130 * @return the outline info
132 public abstract PHPOutlineInfo parseInfo(Object parent, String s);
135 * This will change the file to parse.
136 * @param fileToParse the file that should be parsed
138 public abstract void setFileToParse(IFile fileToParse);
141 * This will parse the given string
142 * @param s the string to parse
143 * @throws CoreException an exception that can be launched
145 public abstract void parse(String s) throws CoreException;
148 * This will set a marker.
149 * @param file the file that generated the marker
150 * @param message the message
151 * @param charStart the starting character
152 * @param charEnd the end character
153 * @param errorLevel the error level ({@link PHPParserSuperclass#ERROR},
154 * {@link PHPParserSuperclass#INFO},{@link PHPParserSuperclass#WARNING}),{@link PHPParserSuperclass#TASK})
155 * @throws CoreException an exception throwed by the MarkerUtilities
157 public static void setMarker(final IFile file,
158 final String message,
161 final int errorLevel)
162 throws CoreException {
164 final Hashtable attributes = new Hashtable();
165 MarkerUtilities.setMessage(attributes, message);
166 switch (errorLevel) {
168 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
171 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
174 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
177 attributes.put(IMarker.SEVERITY, new Integer(IMarker.TASK));
180 MarkerUtilities.setCharStart(attributes, charStart);
181 MarkerUtilities.setCharEnd(attributes, charEnd);
182 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
187 * This will set a marker.
188 * @param file the file that generated the marker
189 * @param message the message
190 * @param line the line number
191 * @param errorLevel the error level ({@link PHPParserSuperclass#ERROR},
192 * {@link PHPParserSuperclass#INFO},{@link PHPParserSuperclass#WARNING})
193 * @throws CoreException an exception throwed by the MarkerUtilities
195 public static void setMarker(final IFile file,
196 final String message,
198 final int errorLevel,
199 final String location)
200 throws CoreException {
202 String markerKind = IMarker.PROBLEM;
203 final Hashtable attributes = new Hashtable();
204 MarkerUtilities.setMessage(attributes, message);
205 switch (errorLevel) {
207 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
210 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
213 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
216 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
217 markerKind = IMarker.TASK;
220 attributes.put(IMarker.LOCATION, location);
221 MarkerUtilities.setLineNumber(attributes, line);
222 MarkerUtilities.createMarker(file, attributes, markerKind);
227 * This will set a marker.
228 * @param file the file that generated the marker
229 * @param message the message
230 * @param charStart the starting character
231 * @param charEnd the end character
232 * @param errorLevel the error level ({@link PHPParserSuperclass#ERROR},
233 * {@link PHPParserSuperclass#INFO},{@link PHPParserSuperclass#WARNING})
234 * @param location the location of the error
235 * @throws CoreException an exception throwed by the MarkerUtilities
237 public static void setMarker(final IFile file,
238 final String message,
241 final int errorLevel,
242 final String location)
243 throws CoreException {
245 final Hashtable attributes = new Hashtable();
246 MarkerUtilities.setMessage(attributes, message);
247 switch (errorLevel) {
249 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
252 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
255 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
258 attributes.put(IMarker.SEVERITY, new Integer(IMarker.TASK));
261 attributes.put(IMarker.LOCATION, location);
262 MarkerUtilities.setCharStart(attributes, charStart);
263 MarkerUtilities.setCharEnd(attributes, charEnd);
264 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);