3 import java.text.MessageFormat;
4 import java.util.Hashtable;
6 import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
7 import net.sourceforge.phpdt.internal.ui.util.StringUtil;
8 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
9 import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
11 import org.eclipse.core.resources.IFile;
12 import org.eclipse.core.resources.IMarker;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.jface.preference.IPreferenceStore;
15 import org.eclipse.ui.texteditor.MarkerUtilities;
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;
28 public static final int TASK = 3;
29 protected static IFile fileToParse;
32 * Call the php parse command ( php -l -f <filename> )
33 * and create markers according to the external parser output.
34 * @param file the file that will be parsed
36 public static void phpExternalParse(final IFile file) {
37 //IFile file = (IFile) resource;
38 // final IPath path = file.getFullPath();
39 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
40 final String filename = file.getLocation().toString();
42 final String[] arguments = {filename};
43 final MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
44 final String command = form.format(arguments);
46 final String parserResult = 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) throws CoreException {
62 file.deleteMarkers(IMarker.PROBLEM, false, 0);
67 while ((brIndx = output.indexOf("<br />", indx)) != -1) {
68 // newer php error output (tested with 4.2.3)
69 scanLine(output, file, indx, brIndx);
74 while ((brIndx = output.indexOf("<br>", indx)) != -1) {
75 // older php error output (tested with 4.2.3)
76 scanLine(output, file, indx, brIndx);
82 private static void scanLine(final String output, final IFile file, final int indx, final int brIndx) throws CoreException {
84 // String outLineNumberString; never used
85 final StringBuffer lineNumberBuffer = new StringBuffer(10);
87 current = output.substring(indx, brIndx);
89 if (current.indexOf(PARSE_WARNING_STRING) != -1 || 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 = StringUtil.replaceAll(current, "\n", "");
105 current = StringUtil.replaceAll(current, "<b>", "");
106 current = StringUtil.replaceAll(current, "</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)
112 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
114 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
115 MarkerUtilities.setLineNumber(attributes, lineNumber);
116 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
122 * This will parse the file and generate the outline info
123 * @param parent the parent object
124 * @param s the string that should be parsed
125 * @return the outline info
127 public abstract PHPOutlineInfo parseInfo(Object parent, String s);
130 * This will change the file to parse.
131 * @param fileToParse the file that should be parsed
133 public abstract void setFileToParse(IFile fileToParse);
136 * This will parse the given string
137 * @param s the string to parse
138 * @throws CoreException an exception that can be launched
140 public abstract void parse(String s) throws CoreException;
143 * This will set a marker.
144 * @param file the file that generated the marker
145 * @param message the message
146 * @param charStart the starting character
147 * @param charEnd the end character
148 * @param errorLevel the error level ({@link PHPParserSuperclass#ERROR},
149 * {@link PHPParserSuperclass#INFO},{@link PHPParserSuperclass#WARNING}),{@link PHPParserSuperclass#TASK})
150 * @throws CoreException an exception throwed by the MarkerUtilities
152 public static void setMarker(
154 final String message,
157 final int errorLevel)
158 throws CoreException {
160 final Hashtable attributes = new Hashtable();
161 MarkerUtilities.setMessage(attributes, message);
162 switch (errorLevel) {
164 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
167 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
170 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
173 attributes.put(IMarker.SEVERITY, new Integer(IMarker.TASK));
176 MarkerUtilities.setCharStart(attributes, charStart);
177 MarkerUtilities.setCharEnd(attributes, charEnd);
178 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
183 * This will set a marker.
184 * @param file the file that generated the marker
185 * @param message the message
186 * @param line the line number
187 * @param errorLevel the error level ({@link PHPParserSuperclass#ERROR},
188 * {@link PHPParserSuperclass#INFO},{@link PHPParserSuperclass#WARNING})
189 * @throws CoreException an exception throwed by the MarkerUtilities
191 public static void setMarker(final IFile file,
192 final String message,
194 final int errorLevel,
195 final String location)
196 throws CoreException {
198 String markerKind = IMarker.PROBLEM;
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.
224 * @param message the message
225 * @param charStart the starting character
226 * @param charEnd the end character
227 * @param errorLevel the error level ({@link PHPParserSuperclass#ERROR},
228 * {@link PHPParserSuperclass#INFO},{@link PHPParserSuperclass#WARNING})
229 * @throws CoreException an exception throwed by the MarkerUtilities
231 public static void setMarker(final String message,
234 final int errorLevel,
235 final String location)
236 throws CoreException {
237 if (fileToParse != null) {
238 setMarker(fileToParse, message, charStart, charEnd, errorLevel, location);
243 * This will set a marker.
244 * @param file the file that generated the marker
245 * @param message the message
246 * @param charStart the starting character
247 * @param charEnd the end character
248 * @param errorLevel the error level ({@link PHPParserSuperclass#ERROR},
249 * {@link PHPParserSuperclass#INFO},{@link PHPParserSuperclass#WARNING})
250 * @param location the location of the error
251 * @throws CoreException an exception throwed by the MarkerUtilities
253 public static void setMarker(final IFile file,
254 final String message,
257 final int errorLevel,
258 final String location)
259 throws CoreException {
261 final Hashtable attributes = new Hashtable();
262 MarkerUtilities.setMessage(attributes, message);
263 switch (errorLevel) {
265 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
268 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
271 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
274 attributes.put(IMarker.SEVERITY, new Integer(IMarker.TASK));
277 attributes.put(IMarker.LOCATION, location);
278 MarkerUtilities.setCharStart(attributes, charStart);
279 MarkerUtilities.setCharEnd(attributes, charEnd);
280 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);