Java tutorial
/******************************************************************************* * Copyright (c) 2013, 2014 ETAS GmbH. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Dennis Eder (ETAS GmbH) - initial API and implementation and/or initial documentation *******************************************************************************/ package mbtarranger; import java.io.File; import java.io.PrintWriter; import java.io.StringWriter; import java.util.HashMap; import mbtarranger.languages.arrange.ast1.AST; import org.antlr.v4.runtime.ParserRuleContext; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; public class BadBank { // private static org.eclipse.swt.widgets.Display c_display ; // public static void initialize(final org.eclipse.swt.widgets.Display // display ){ // c_display = display; // } private static HashMap<File, IResource> osfile2eclipseResource = new HashMap<>(); public static void registerResource(File osPath, IResource eclipsePath) { osfile2eclipseResource.put(osPath.getAbsoluteFile(), eclipsePath); } public static void reset4resource(IResource rsrc) throws CoreException { rsrc.deleteMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE); } public static void reset4file(File osPath) throws CoreException, MbtArrangerException { IFile rsrc = findResource(osPath); rsrc.deleteMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE); } public static void report(Throwable e, IResource rsrc, int startline) { String title; if (e instanceof MbtArrangerUserinputError) { title = "Usage Error"; } else if (e instanceof MbtArrangerException) { title = "Exception"; e.printStackTrace(); } else { title = "unhandled exception"; e.printStackTrace(); } StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String msg = sw.toString(); reportProblem(startline, rsrc, title + ":" + msg); } private static boolean isRedundant(IResource rsrc, String markerType, String msg, int startline) { try { for (IMarker m : rsrc.findMarkers(markerType, true, 1)) { if ((m.getAttribute(IMarker.MESSAGE) != null && ((String) m.getAttribute(IMarker.MESSAGE)).equals(msg)) && (m.getAttribute(IMarker.LINE_NUMBER) != null && ((int) m.getAttribute(IMarker.LINE_NUMBER)) == startline)) return true; } return false; } catch (CoreException e) { e.printStackTrace(); return false; } } public static void reportProblem(final int startline, IResource rsrc, final String msg) { try { if (!(isRedundant(rsrc, IMarker.PROBLEM, msg, startline))) { IMarker marker = rsrc.createMarker(IMarker.PROBLEM); marker.setAttribute(IMarker.MESSAGE, msg); marker.setAttribute(IMarker.LINE_NUMBER, startline); marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH); marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR); } } catch (CoreException e) { e.printStackTrace(); } } public static void reportWarning(int startline, File osPath, String msg) { try { IFile[] rsrcs = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(osPath.toURI()); if (rsrcs.length != 1) throw new MbtArrangerException( String.format("could not find eclipse resource for file '%s'", osPath.getAbsolutePath())); if (!(isRedundant(rsrcs[0], IMarker.PROBLEM, msg, startline))) { IMarker marker = rsrcs[0].createMarker(IMarker.PROBLEM); marker.setAttribute(IMarker.MESSAGE, msg); marker.setAttribute(IMarker.LINE_NUMBER, startline); marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH); marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING); } } catch (CoreException | MbtArrangerException e) { e.printStackTrace(); } } public static void reportProblem(final int startline, File osPath, final String msg) { try { IFile rsrc = findResource(osPath); if (!(isRedundant(rsrc, IMarker.PROBLEM, msg, startline))) { IMarker marker = rsrc.createMarker(IMarker.PROBLEM); marker.setAttribute(IMarker.MESSAGE, msg); marker.setAttribute(IMarker.LINE_NUMBER, startline); marker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH); marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR); } } catch (CoreException | MbtArrangerException e) { e.printStackTrace(); } } private static IFile findResource(File osPath) throws MbtArrangerException { IFile[] rsrcs = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(osPath.toURI()); if (rsrcs.length != 1) throw new MbtArrangerException( String.format("could not find eclipse resource for file '%s'", osPath.getAbsolutePath())); return rsrcs[0]; } public static void report(Throwable e, int startline, File filePath) { String title; if (e instanceof MbtArrangerUserinputError) { title = "Usage Error"; } else if (e instanceof MbtArrangerException) { title = "Exception"; e.printStackTrace(); } else { title = "unhandled exception"; e.printStackTrace(); } StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); String msg = sw.toString(); reportProblem(startline, filePath, title + ":" + msg); } public static void report(Throwable e, AST<org.antlr.v4.runtime.ParserRuleContext> ast, File filePath) { report(e, ast.userInputScope.getStart().getLine(), filePath); } protected static void _report(Throwable e, ParserRuleContext ctx, File filePath) { report(e, ctx.getStart().getLine(), filePath); } public static void report(Throwable e, IFile file, ParserRuleContext ctx) { report(e, file, ctx.getStart().getLine()); } public static void report(Throwable e, Object userInputScope, IFile file) { report(e, userInputScope, file.getRawLocation().toFile()); } public static void report(Throwable e, Object userInputScope, File file) { if (userInputScope instanceof ParserRuleContext) { _report(e, (ParserRuleContext) userInputScope, file); } else { report(e, -1, file); } } public static void reportProblem(ParserRuleContext userInputScope, File arrFile, String messageToUser) { reportProblem(userInputScope.getStart().getLine(), arrFile, messageToUser); } public static void reportProblem(String messageToUser, Object userInputScope, File file) { if (userInputScope instanceof ParserRuleContext) { reportProblem((ParserRuleContext) userInputScope, file, messageToUser); } else { reportProblem(-1, file, messageToUser); } } }