3 import java.text.MessageFormat;
4 import java.util.Hashtable;
6 import net.sourceforge.phpdt.core.IJavaModelMarker;
7 import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
8 import net.sourceforge.phpdt.internal.ui.util.StringUtil;
9 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
10 import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
11 import net.sourceforge.phpeclipse.builder.PHPBuilder;
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.IMarker;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.ui.texteditor.MarkerUtilities;
20 * The superclass for our PHP parsers.
21 * @author Matthieu Casanova
23 public abstract class PHPParserSuperclass {
24 // strings for external parser call
25 private static final String PARSE_ERROR_STRING = "Parse error"; //$NON-NLS-1$
26 private static final String PARSE_WARNING_STRING = "Warning"; //$NON-NLS-1$
27 public static final int ERROR = 2;
28 public static final int WARNING = 1;
29 public static final int INFO = 0;
30 public static final int TASK = 3;
31 // TODO design error? Analyze why fileToParse must be static ???
32 protected static IFile fileToParse;
35 * Call the php parse command ( php -l -f <filename> )
36 * and create markers according to the external parser output.
37 * @param file the file that will be parsed
39 public static void phpExternalParse(final IFile file) {
40 //IFile file = (IFile) resource;
41 // final IPath path = file.getFullPath();
42 final IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
43 final String filename = file.getLocation().toString();
45 final String[] arguments = {filename};
46 final MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
47 final String command = form.format(arguments);
49 final String parserResult = PHPStartApacheAction.getParserOutput(command, "External parser: ");
52 // parse the buffer to find the errors and warnings
53 createMarkers(parserResult, file);
54 } catch (CoreException e) {
59 * Create markers according to the external parser output.
60 * @param output the external parser output
61 * @param file the file that was parsed.
63 protected static void createMarkers(final String output, final IFile file) throws CoreException {
65 // file.deleteMarkers(IMarker.PROBLEM, false, 0);
66 PHPBuilder.removeProblemsAndTasksFor(file);
71 while ((brIndx = output.indexOf("<br />", indx)) != -1) {
72 // newer php error output (tested with 4.2.3)
73 scanLine(output, file, indx, brIndx);
78 while ((brIndx = output.indexOf("<br>", indx)) != -1) {
79 // older php error output (tested with 4.2.3)
80 scanLine(output, file, indx, brIndx);
86 private static void scanLine(final String output, final IFile file, final int indx, final int brIndx) throws CoreException {
88 // String outLineNumberString; never used
89 final StringBuffer lineNumberBuffer = new StringBuffer(10);
91 current = output.substring(indx, brIndx);
93 if (current.indexOf(PARSE_WARNING_STRING) != -1 || current.indexOf(PARSE_ERROR_STRING) != -1) {
94 final int onLine = current.indexOf("on line <b>");
96 lineNumberBuffer.delete(0, lineNumberBuffer.length());
97 for (int i = onLine; i < current.length(); i++) {
98 ch = current.charAt(i);
99 if ('0' <= ch && '9' >= ch) {
100 lineNumberBuffer.append(ch);
104 final int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
106 final Hashtable attributes = new Hashtable();
108 current = StringUtil.replaceAll(current, "\n", "");
109 current = StringUtil.replaceAll(current, "<b>", "");
110 current = StringUtil.replaceAll(current, "</b>", "");
111 MarkerUtilities.setMessage(attributes, current);
113 if (current.indexOf(PARSE_ERROR_STRING) != -1)
114 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
115 else if (current.indexOf(PARSE_WARNING_STRING) != -1)
116 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
118 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
119 MarkerUtilities.setLineNumber(attributes, lineNumber);
120 // MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
121 MarkerUtilities.createMarker(file, attributes, IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER);
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(
159 final String message,
162 final int errorLevel)
163 throws CoreException {
165 final Hashtable attributes = new Hashtable();
166 MarkerUtilities.setMessage(attributes, message);
167 switch (errorLevel) {
169 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
172 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
175 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
178 attributes.put(IMarker.SEVERITY, new Integer(IMarker.TASK));
181 MarkerUtilities.setCharStart(attributes, charStart);
182 MarkerUtilities.setCharEnd(attributes, charEnd);
183 // MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
184 MarkerUtilities.createMarker(file, attributes, IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER);
189 * This will set a marker.
190 * @param file the file that generated the marker
191 * @param message the message
192 * @param line the line number
193 * @param errorLevel the error level ({@link PHPParserSuperclass#ERROR},
194 * {@link PHPParserSuperclass#INFO},{@link PHPParserSuperclass#WARNING})
195 * @throws CoreException an exception throwed by the MarkerUtilities
197 public static void setMarker(final IFile file,
198 final String message,
200 final int errorLevel,
201 final String location)
202 throws CoreException {
204 // String markerKind = IMarker.PROBLEM;
205 String markerKind = IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER;
206 final Hashtable attributes = new Hashtable();
207 MarkerUtilities.setMessage(attributes, message);
208 switch (errorLevel) {
210 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
213 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
216 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
219 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
220 // markerKind = IMarker.TASK;
221 markerKind = IJavaModelMarker.TASK_MARKER;
224 attributes.put(IMarker.LOCATION, location);
225 MarkerUtilities.setLineNumber(attributes, line);
226 MarkerUtilities.createMarker(file, attributes, markerKind);
231 * This will set a marker.
232 * @param message the message
233 * @param charStart the starting character
234 * @param charEnd the end character
235 * @param errorLevel the error level ({@link PHPParserSuperclass#ERROR},
236 * {@link PHPParserSuperclass#INFO},{@link PHPParserSuperclass#WARNING})
237 * @throws CoreException an exception throwed by the MarkerUtilities
239 public static void setMarker(final String message,
242 final int errorLevel,
243 final String location)
244 throws CoreException {
245 if (fileToParse != null) {
246 setMarker(fileToParse, message, charStart, charEnd, errorLevel, location);
251 * This will set a marker.
252 * @param file the file that generated the marker
253 * @param message the message
254 * @param charStart the starting character
255 * @param charEnd the end character
256 * @param errorLevel the error level ({@link PHPParserSuperclass#ERROR},
257 * {@link PHPParserSuperclass#INFO},{@link PHPParserSuperclass#WARNING})
258 * @param location the location of the error
259 * @throws CoreException an exception throwed by the MarkerUtilities
261 public static void setMarker(final IFile file,
262 final String message,
265 final int errorLevel,
266 final String location)
267 throws CoreException {
269 final Hashtable attributes = new Hashtable();
270 MarkerUtilities.setMessage(attributes, message);
271 switch (errorLevel) {
273 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
276 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
279 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
282 attributes.put(IMarker.SEVERITY, new Integer(IMarker.TASK));
285 attributes.put(IMarker.LOCATION, location);
286 MarkerUtilities.setCharStart(attributes, charStart);
287 MarkerUtilities.setCharEnd(attributes, charEnd);
288 // MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
289 MarkerUtilities.createMarker(file, attributes, IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER);