a7f1dc063926c23cbab03382759619327532d2db
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / PHPExternalParserAction.java
1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.actions;
13
14 import java.text.MessageFormat;
15 import java.util.ArrayList;
16 import java.util.Hashtable;
17 import java.util.Iterator;
18 import java.util.StringTokenizer;
19
20 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IMarker;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.jface.action.IAction;
27 import org.eclipse.jface.preference.IPreferenceStore;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.ISelectionProvider;
30 import org.eclipse.jface.viewers.StructuredSelection;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.IObjectActionDelegate;
33 import org.eclipse.ui.IWorkbenchPart;
34 import org.eclipse.ui.texteditor.MarkerUtilities;
35
36 public class PHPExternalParserAction implements IObjectActionDelegate {
37   private static final String PARSE_ERROR = "Parse error"; //$NON-NLS-1$
38   private static final String WARNING = "Warning"; //$NON-NLS-1$
39
40   private IWorkbenchPart workbenchPart;
41   /**
42    * Constructor for Action1.
43    */
44   public PHPExternalParserAction() {
45     super();
46   }
47
48   /**
49    * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
50    */
51   public void setActivePart(IAction action, IWorkbenchPart targetPart) {
52     workbenchPart = targetPart;
53   }
54
55   //  public static void open(final URL url, final Shell shell, final String dialogTitle) {
56   //    IHelp help= WorkbenchHelp.getHelpSupport();
57   //    if (help != null) {
58   //      WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm());
59   //    } else {
60   //      showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$
61   //    }
62   //  }
63
64   public void run(IAction action) {
65     ISelectionProvider selectionProvider = null;
66     selectionProvider = workbenchPart.getSite().getSelectionProvider();
67
68     StructuredSelection selection = null;
69     selection = (StructuredSelection) selectionProvider.getSelection();
70
71     IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
72
73     Shell shell = null;
74     Iterator iterator = null;
75     iterator = selection.iterator();
76     while (iterator.hasNext()) {
77       //  obj => selected object in the view
78       Object obj = iterator.next();
79
80       // is it a resource
81       if (obj instanceof IResource) {
82         IResource resource = (IResource) obj;
83
84         // check if it's a file resource
85         switch (resource.getType()) {
86
87           case IResource.FILE :
88             // single file:
89             IFile file = (IFile) resource;
90             IPath path = file.getFullPath();
91
92             String filename = file.getLocation().toString();
93
94             String[] arguments = { filename };
95             MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
96             String command = form.format(arguments);
97
98             String parserResult = PHPStartApacheAction.execute(command, "External parser: ");
99
100             try {
101               // parse the buffer to find the errors and warnings
102               createMarkers(parserResult, file);
103             } catch (CoreException e) {
104             }
105
106         }
107       }
108     }
109   }
110
111   /**
112    * @see IActionDelegate#selectionChanged(IAction, ISelection)
113    */
114   public void selectionChanged(IAction action, ISelection selection) {
115   }
116
117   /**
118    * Create markers according to the compiler output
119    */
120   protected void createMarkers(String output, IFile file) throws CoreException {
121     // delete all markers
122     file.deleteMarkers(IMarker.PROBLEM, false, 0);
123
124     int indx = 0;
125     int brIndx = 0;
126     boolean flag = true;
127     while ((brIndx = output.indexOf("<br />", indx)) != -1) {
128       // newer php error output (tested with 4.2.3)
129       scanLine(output, file, indx, brIndx);
130       indx = brIndx + 6;
131       flag = false;
132     }
133     if (flag) {
134       while ((brIndx = output.indexOf("<br>", indx)) != -1) {
135         // older php error output (tested with 4.2.3)
136         scanLine(output, file, indx, brIndx);
137         indx = brIndx + 4;
138       }
139     }
140   }
141   private void scanLine(String output, IFile file, int indx, int brIndx) throws CoreException {
142     String current;
143     String outLineNumberString;
144     StringBuffer lineNumberBuffer = new StringBuffer(10);
145     char ch;
146     current = output.substring(indx, brIndx);
147
148     if (current.indexOf(WARNING) != -1 || current.indexOf(PARSE_ERROR) != -1) {
149       int onLine = current.indexOf("on line <b>");
150       if (onLine != -1) {
151         lineNumberBuffer.delete(0, lineNumberBuffer.length());
152         for (int i = onLine; i < current.length(); i++) {
153           ch = current.charAt(i);
154           if ('0' <= ch && '9' >= ch) {
155             lineNumberBuffer.append(ch);
156           }
157         }
158
159         //          String line = current.substring(current.indexOf(fullPath) + fullPath.length(), current.length());
160         //          String errorsLocation = line.substring(1, line.indexOf(":") - 1); //$NON-NLS-1$
161         //          String message = line.substring(line.indexOf(":") + 2, line.length() - 1); //$NON-NLS-1$
162
163         int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
164
165         Hashtable attributes = new Hashtable();
166
167         current = current.replaceAll("\n", "");
168         current = current.replaceAll("<b>", "");
169         current = current.replaceAll("</b>", "");
170         MarkerUtilities.setMessage(attributes, current);
171
172         if (current.indexOf(PARSE_ERROR) != -1)
173           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR));
174         else if (current.indexOf(WARNING) != -1)
175           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
176         else
177           attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
178         MarkerUtilities.setLineNumber(attributes, lineNumber);
179         MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);
180       }
181     }
182   }
183 }