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 line the line number
185 * @param errorLevel the error level ({@link PHPParserSuperclass#ERROR},
186 * {@link PHPParserSuperclass#INFO},{@link PHPParserSuperclass#WARNING})
187 * @throws CoreException an exception throwed by the MarkerUtilities
189 public static void setMarker(final IFile file,
190 final String message,
192 final int errorLevel,
193 final String location)
194 throws CoreException {
196 final Hashtable attributes = new Hashtable();
197 MarkerUtilities.setMessage(attributes, message);
198 switch (errorLevel) {
200 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
202 case Parser.WARNING :
203 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
206 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
209 attributes.put(IMarker.LOCATION,location);
210 MarkerUtilities.setLineNumber(attributes, line);
211 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
215 * This will set a marker.
216 * @param file the file that generated the marker
217 * @param message the message
218 * @param charStart the starting character
219 * @param charEnd the end character
220 * @param errorLevel the error level ({@link PHPParserSuperclass#ERROR},
221 * {@link PHPParserSuperclass#INFO},{@link PHPParserSuperclass#WARNING})
222 * @param location the location of the error
223 * @throws CoreException an exception throwed by the MarkerUtilities
225 public static void setMarker(final IFile file,
226 final String message,
229 final int errorLevel,
230 final String location)
231 throws CoreException {
233 final Hashtable attributes = new Hashtable();
234 MarkerUtilities.setMessage(attributes, message);
235 switch (errorLevel) {
237 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
239 case Parser.WARNING :
240 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
243 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
246 attributes.put(IMarker.LOCATION,location);
247 MarkerUtilities.setCharStart(attributes, charStart);
248 MarkerUtilities.setCharEnd(attributes, charEnd);
249 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);