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
9 IBM Corporation - Initial implementation
10 Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.actions;
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;
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;
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$
40 private IWorkbenchPart workbenchPart;
42 * Constructor for Action1.
44 public PHPExternalParserAction() {
49 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
51 public void setActivePart(IAction action, IWorkbenchPart targetPart) {
52 workbenchPart = targetPart;
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());
60 // showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$
64 public void run(IAction action) {
65 ISelectionProvider selectionProvider = null;
66 selectionProvider = workbenchPart.getSite().getSelectionProvider();
68 StructuredSelection selection = null;
69 selection = (StructuredSelection) selectionProvider.getSelection();
71 IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
74 Iterator iterator = null;
75 iterator = selection.iterator();
76 while (iterator.hasNext()) {
77 // obj => selected object in the view
78 Object obj = iterator.next();
81 if (obj instanceof IResource) {
82 IResource resource = (IResource) obj;
84 // check if it's a file resource
85 switch (resource.getType()) {
89 IFile file = (IFile) resource;
90 IPath path = file.getFullPath();
92 String filename = file.getLocation().toString();
94 String[] arguments = { filename };
95 MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF));
96 String command = form.format(arguments);
98 String parserResult = PHPStartApacheAction.execute(command, "External parser: ");
101 // parse the buffer to find the errors and warnings
102 createMarkers(parserResult, file);
103 } catch (CoreException e) {
112 * @see IActionDelegate#selectionChanged(IAction, ISelection)
114 public void selectionChanged(IAction action, ISelection selection) {
118 * Create markers according to the compiler output
120 protected void createMarkers(String output, IFile file) throws CoreException {
121 // delete all markers
122 file.deleteMarkers(IMarker.PROBLEM, false, 0);
127 while ((brIndx = output.indexOf("<br />", indx)) != -1) {
128 // newer php error output (tested with 4.2.3)
129 scanLine(output, file, indx, brIndx);
134 while ((brIndx = output.indexOf("<br>", indx)) != -1) {
135 // older php error output (tested with 4.2.3)
136 scanLine(output, file, indx, brIndx);
141 private void scanLine(String output, IFile file, int indx, int brIndx) throws CoreException {
143 String outLineNumberString;
144 StringBuffer lineNumberBuffer = new StringBuffer(10);
146 current = output.substring(indx, brIndx);
148 if (current.indexOf(WARNING) != -1 || current.indexOf(PARSE_ERROR) != -1) {
149 int onLine = current.indexOf("on line <b>");
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);
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$
163 int lineNumber = Integer.parseInt(lineNumberBuffer.toString());
165 Hashtable attributes = new Hashtable();
167 current = current.replaceAll("\n", "");
168 current = current.replaceAll("<b>", "");
169 current = current.replaceAll("</b>", "");
170 MarkerUtilities.setMessage(attributes, current);
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));
177 attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO));
178 MarkerUtilities.setLineNumber(attributes, lineNumber);
179 MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM);