TokenMgrError handled (no more exception on unterminated String), I now accept all...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / test / PHPParserSuperclass.java
1 package test;
2
3 import org.eclipse.core.resources.IFile;
4 import org.eclipse.core.resources.IMarker;
5 import org.eclipse.core.runtime.IPath;
6 import org.eclipse.core.runtime.CoreException;
7 import org.eclipse.jface.preference.IPreferenceStore;
8 import org.eclipse.ui.texteditor.MarkerUtilities;
9 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
10 import net.sourceforge.phpeclipse.actions.PHPStartApacheAction;
11 import net.sourceforge.phpdt.internal.compiler.parser.PHPOutlineInfo;
12 import net.sourceforge.phpdt.internal.compiler.parser.Parser;
13
14 import java.text.MessageFormat;
15 import java.util.Hashtable;
16
17 /**
18  * The superclass for our PHP parsers.
19  * @author Matthieu Casanova
20  */
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
29   /**
30    * Call the php parse command ( php -l -f <filename> )
31    * and create markers according to the external parser output
32    */
33   public static void phpExternalParse(IFile file) {
34     //IFile file = (IFile) resource;
35     IPath path = file.getFullPath();
36     IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
37     String filename = file.getLocation().toString();
38
39     String[] arguments = { filename };
40     MessageFormat form =
41       new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
42     String command = form.format(arguments);
43
44     String parserResult =
45       PHPStartApacheAction.getParserOutput(command, "External parser: ");
46
47     try {
48       // parse the buffer to find the errors and warnings
49       createMarkers(parserResult, file);
50     } catch (CoreException e) {
51     }
52   }
53
54   /**
55    * Create markers according to the external parser output
56    */
57   private static void createMarkers(String output, IFile file)
58     throws CoreException {
59     // delete all markers
60     file.deleteMarkers(IMarker.PROBLEM, false, 0);
61
62     int indx = 0;
63     int brIndx = 0;
64     boolean flag = true;
65     while ((brIndx = output.indexOf("<br />", indx)) != -1) {
66       // newer php error output (tested with 4.2.3)
67       scanLine(output, file, indx, brIndx);
68       indx = brIndx + 6;
69       flag = false;
70     }
71     if (flag) {
72       while ((brIndx = output.indexOf("<br>", indx)) != -1) {
73         // older php error output (tested with 4.2.3)
74         scanLine(output, file, indx, brIndx);
75         indx = brIndx + 4;
76       }
77     }
78   }
79
80   private static void scanLine(String output, IFile file, int indx, int brIndx)
81     throws CoreException {
82     String current;
83     String outLineNumberString;
84     StringBuffer lineNumberBuffer = new StringBuffer(10);
85     char ch;
86     current = output.substring(indx, brIndx);
87
88     if (current.indexOf(PARSE_WARNING_STRING) != -1
89       || current.indexOf(PARSE_ERROR_STRING) != -1) {
90       int onLine = current.indexOf("on line <b>");
91       if (onLine != -1) {
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);
97           }
98         }
99
100         int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
101
102         Hashtable attributes = new Hashtable();
103
104         current = current.replaceAll("\n", "");
105         current = current.replaceAll("<b>", "");
106         current = current.replaceAll("</b>", "");
107         MarkerUtilities.setMessage(attributes, current);
108
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(
113             IMarker.SEVERITY,
114             new Integer(IMarker.SEVERITY_WARNING));
115         else
116           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
117         MarkerUtilities.setLineNumber(attributes, lineNumber);
118         MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
119       }
120     }
121   }
122
123   public abstract PHPOutlineInfo parseInfo(Object parent, String s);
124
125   public abstract void setFileToParse(IFile fileToParse);
126
127   public abstract void parse(String s) throws CoreException;
128
129   public static void setMarker(
130     IFile file,
131     String message,
132     int charStart,
133     int charEnd,
134     int errorLevel)
135     throws CoreException {
136     if (file != null) {
137       Hashtable attributes = new Hashtable();
138       MarkerUtilities.setMessage(attributes, message);
139       switch (errorLevel) {
140         case Parser.ERROR :
141           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
142           break;
143         case Parser.WARNING :
144           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
145           break;
146         case Parser.INFO :
147           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
148           break;
149       }
150       MarkerUtilities.setCharStart(attributes, charStart);
151       MarkerUtilities.setCharEnd(attributes, charEnd);
152       // setLineNumber(attributes, lineNumber);
153       MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
154     }
155   }
156
157     public static void setMarker(IFile file,
158                                 String message,
159                                 int charStart,
160                                 int charEnd,
161                                 int errorLevel,
162                                 String location)
163     throws CoreException {
164     if (file != null) {
165       Hashtable attributes = new Hashtable();
166       MarkerUtilities.setMessage(attributes, message);
167       switch (errorLevel) {
168         case Parser.ERROR :
169           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
170           break;
171         case Parser.WARNING :
172           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
173           break;
174         case Parser.INFO :
175           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
176           break;
177       }
178       attributes.put(IMarker.LOCATION,location);
179       MarkerUtilities.setCharStart(attributes, charStart);
180       MarkerUtilities.setCharEnd(attributes, charEnd);
181       // setLineNumber(attributes, lineNumber);
182       MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
183     }
184   }
185 }