From: khartlage Date: Wed, 27 Nov 2002 22:46:37 +0000 (+0000) Subject: external syntax parse check: /apache/php/php -l -f X-Git-Url: http://git.phpeclipse.com?hp=dfa01d32941866a1d27a959a73c096b9d8b86378 external syntax parse check: /apache/php/php -l -f --- diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPExternalParserAction.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPExternalParserAction.java new file mode 100644 index 0000000..a7f1dc0 --- /dev/null +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/actions/PHPExternalParserAction.java @@ -0,0 +1,183 @@ +/********************************************************************** +Copyright (c) 2000, 2002 IBM Corp. and others. +All rights reserved. This program and the accompanying materials +are made available under the terms of the Common Public License v1.0 +which accompanies this distribution, and is available at +http://www.eclipse.org/legal/cpl-v10.html + +Contributors: + IBM Corporation - Initial implementation + Klaus Hartlage - www.eclipseproject.de +**********************************************************************/ +package net.sourceforge.phpeclipse.actions; + +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.StringTokenizer; + +import net.sourceforge.phpeclipse.PHPeclipsePlugin; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.ISelectionProvider; +import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IObjectActionDelegate; +import org.eclipse.ui.IWorkbenchPart; +import org.eclipse.ui.texteditor.MarkerUtilities; + +public class PHPExternalParserAction implements IObjectActionDelegate { + private static final String PARSE_ERROR = "Parse error"; //$NON-NLS-1$ + private static final String WARNING = "Warning"; //$NON-NLS-1$ + + private IWorkbenchPart workbenchPart; + /** + * Constructor for Action1. + */ + public PHPExternalParserAction() { + super(); + } + + /** + * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart) + */ + public void setActivePart(IAction action, IWorkbenchPart targetPart) { + workbenchPart = targetPart; + } + + // public static void open(final URL url, final Shell shell, final String dialogTitle) { + // IHelp help= WorkbenchHelp.getHelpSupport(); + // if (help != null) { + // WorkbenchHelp.getHelpSupport().displayHelpResource(url.toExternalForm()); + // } else { + // showMessage(shell, dialogTitle, ActionMessages.getString("OpenBrowserUtil.help_not_available"), false); //$NON-NLS-1$ + // } + // } + + public void run(IAction action) { + ISelectionProvider selectionProvider = null; + selectionProvider = workbenchPart.getSite().getSelectionProvider(); + + StructuredSelection selection = null; + selection = (StructuredSelection) selectionProvider.getSelection(); + + IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore(); + + Shell shell = null; + Iterator iterator = null; + iterator = selection.iterator(); + while (iterator.hasNext()) { + // obj => selected object in the view + Object obj = iterator.next(); + + // is it a resource + if (obj instanceof IResource) { + IResource resource = (IResource) obj; + + // check if it's a file resource + switch (resource.getType()) { + + case IResource.FILE : + // single file: + IFile file = (IFile) resource; + IPath path = file.getFullPath(); + + String filename = file.getLocation().toString(); + + String[] arguments = { filename }; + MessageFormat form = new MessageFormat(store.getString(PHPeclipsePlugin.EXTERNAL_PARSER_PREF)); + String command = form.format(arguments); + + String parserResult = PHPStartApacheAction.execute(command, "External parser: "); + + try { + // parse the buffer to find the errors and warnings + createMarkers(parserResult, file); + } catch (CoreException e) { + } + + } + } + } + } + + /** + * @see IActionDelegate#selectionChanged(IAction, ISelection) + */ + public void selectionChanged(IAction action, ISelection selection) { + } + + /** + * Create markers according to the compiler output + */ + protected void createMarkers(String output, IFile file) throws CoreException { + // delete all markers + file.deleteMarkers(IMarker.PROBLEM, false, 0); + + int indx = 0; + int brIndx = 0; + boolean flag = true; + while ((brIndx = output.indexOf("
", indx)) != -1) { + // newer php error output (tested with 4.2.3) + scanLine(output, file, indx, brIndx); + indx = brIndx + 6; + flag = false; + } + if (flag) { + while ((brIndx = output.indexOf("
", indx)) != -1) { + // older php error output (tested with 4.2.3) + scanLine(output, file, indx, brIndx); + indx = brIndx + 4; + } + } + } + private void scanLine(String output, IFile file, int indx, int brIndx) throws CoreException { + String current; + String outLineNumberString; + StringBuffer lineNumberBuffer = new StringBuffer(10); + char ch; + current = output.substring(indx, brIndx); + + if (current.indexOf(WARNING) != -1 || current.indexOf(PARSE_ERROR) != -1) { + int onLine = current.indexOf("on line "); + if (onLine != -1) { + lineNumberBuffer.delete(0, lineNumberBuffer.length()); + for (int i = onLine; i < current.length(); i++) { + ch = current.charAt(i); + if ('0' <= ch && '9' >= ch) { + lineNumberBuffer.append(ch); + } + } + + // String line = current.substring(current.indexOf(fullPath) + fullPath.length(), current.length()); + // String errorsLocation = line.substring(1, line.indexOf(":") - 1); //$NON-NLS-1$ + // String message = line.substring(line.indexOf(":") + 2, line.length() - 1); //$NON-NLS-1$ + + int lineNumber = Integer.parseInt(lineNumberBuffer.toString()); + + Hashtable attributes = new Hashtable(); + + current = current.replaceAll("\n", ""); + current = current.replaceAll("", ""); + current = current.replaceAll("", ""); + MarkerUtilities.setMessage(attributes, current); + + if (current.indexOf(PARSE_ERROR) != -1) + attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_ERROR)); + else if (current.indexOf(WARNING) != -1) + attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING)); + else + attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_INFO)); + MarkerUtilities.setLineNumber(attributes, lineNumber); + MarkerUtilities.createMarker(file, attributes, IMarker.PROBLEM); + } + } + } +}