Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 itemis AG (http://www.itemis.eu) and others. 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 /
  
  package org.eclipse.xtext.ui.util;
  
 import java.util.Map;
 
 import  org.eclipse.core.resources.IMarker;
 import  org.eclipse.core.runtime.CoreException;
 import  org.eclipse.jface.text.source.Annotation;
 import  org.eclipse.ui.texteditor.MarkerAnnotation;
 import  org.eclipse.ui.texteditor.MarkerUtilities;
 

Author(s):
Heiko Behrens - Initial contribution and API
Holger Schill
 
 public class IssueUtil {
 
 	@Inject(optional=true)
Creates an Issue out of a Marker. setSyntaxError is unset since the current API does not allow fixing systax errors anyway.

Parameters:
marker The marker to create an issue from
Returns:
an issue created out of the given marker or null
 
 	public Issue createIssue(IMarker marker) {
 		Issue.IssueImpl issue = new Issue.IssueImpl();
 		try {
 			Map<StringObjectattributes = marker.getAttributes();
 			String markerType = marker.getType();
 			Object message = attributes.get(IMarker.MESSAGE);
 			issue.setMessage(message  instanceof String ? (Stringmessage : null);
 			Object lineNumber = attributes.get(IMarker.LINE_NUMBER);
 			issue.setLineNumber(lineNumber instanceof Integer ? (IntegerlineNumber -1 : null);
 			Object offset = attributes.get(IMarker.CHAR_START);
 			Object endOffset = attributes.get(IMarker.CHAR_END);
 			if(offset instanceof Integer && endOffset instanceof Integer) {
 				issue.setOffset((Integeroffset);
 				issue.setLength((IntegerendOffset - (Integeroffset); 
 			} else {
 				issue.setOffset(-1);
 				issue.setLength(0);
 			}
 			Object code = attributes.get(.);
 			issue.setCode(code instanceof String ? (Stringcode:null);
 			Object data = attributes.get(.);
 			issue.setData(data instanceof String ? Strings.unpack((Stringdata) : null);
 			Object uri = attributes.get(.);
 			issue.setUriToProblem(uri instanceof String ? URI.createURI((Stringuri) : null);
 			Object severity = attributes.get(IMarker.SEVERITY);
 			Severity translatedSeverity = translateSeverity(severity instanceof Integer ? (Integerseverity : 0);
 			if(translatedSeverity == null)
 				throw new IllegalArgumentException(marker.toString());
 			issue.setSeverity(translatedSeverity);
 			if( != null)
 			else
 				issue.setType(MarkerTypes.toCheckType(markerType));
 		} catch (CoreException e) {
 			return null;
 		}
 		return issue;
 	}

Deprecated:
As we are using IMarker.getAttributes() in createIssue(IMarker), we do not call this method any more
Since:
2.3
 
 	protected CheckType getCheckType(IMarker marker) {
 		String markerType = MarkerUtilities.getMarkerType(marker);
 		if ( != null)
 			return .getCheckType(markerType);
 		return MarkerTypes.toCheckType(markerType);
 	}
 	
 	public Issue getIssueFromAnnotation(Annotation annotation) {
 		if (annotation instanceof XtextAnnotation) {
 			XtextAnnotation xtextAnnotation = (XtextAnnotationannotation;
 			return xtextAnnotation.getIssue();
 		} else if(annotation instanceof MarkerAnnotation) {
			MarkerAnnotation markerAnnotation = (MarkerAnnotation)annotation;
			return createIssue(markerAnnotation.getMarker());
else
			return null;
	}
	public String getCode(Annotation annotation) {
		if (annotation instanceof MarkerAnnotation) {
			MarkerAnnotation ma = (MarkerAnnotation) annotation;
			return getCode(ma.getMarker());
		}
		if (annotation instanceof XtextAnnotation) {
			XtextAnnotation xa = (XtextAnnotationannotation;
			return xa.getIssueCode();	
		}
		return null;
	}
	public String getCode(IMarker marker) {
		return marker.getAttribute(.null);
	}
	public String[] getIssueData(Annotation annotation) {
		if (annotation instanceof MarkerAnnotation) {
			MarkerAnnotation ma = (MarkerAnnotation) annotation;
			return getIssueData(ma.getMarker());
		}
		if (annotation instanceof XtextAnnotation) {
			XtextAnnotation xa = (XtextAnnotationannotation;
			return xa.getIssueData();	
		}
		return null;
	}
	public String[] getIssueData(IMarker marker) {
		return Strings.unpack(marker.getAttribute(.null));
	}
	public URI getUriToProblem(IMarker marker) {
		String uri = marker.getAttribute(.null);
		return uri != null ? URI.createURI(uri) : null;
	}
	public URI getUriToProblem(Annotation annotation) {
		if (annotation instanceof MarkerAnnotation) {
			MarkerAnnotation ma = (MarkerAnnotation) annotation;
			return getUriToProblem(ma.getMarker());
		}
		if (annotation instanceof XtextAnnotation) {
			XtextAnnotation xa = (XtextAnnotationannotation;
			return xa.getUriToProblem();
		}
		return null;
	}

Deprecated:
As we are using IMarker.getAttributes() in createIssue(IMarker), we do not call this method any more
	public Severity getSeverity(IMarker marker) {
		Severity translatedSeverity = translateSeverity(marker.getAttribute(IMarker.SEVERITY, 0));
		if(translatedSeverity == null)
			throw new IllegalArgumentException(marker.toString());
		return translatedSeverity;
	}

Translates IMarker.SEVERITY to Severity

Parameters:
severity Severity to translate
Returns:
Translated severity or null
Since:
2.9
	protected Severity translateSeverity(int severity) {
		switch (severity) {
			case IMarker.SEVERITY_ERROR:
				return .;
			case IMarker.SEVERITY_WARNING:
			case IMarker.SEVERITY_INFO:
				return .;
			default:
				return null;
		}
	}
	public boolean refersToSameIssue(IMarker marker, Annotation annotation) {
		URI markerURI = getUriToProblem(marker);
		String markerIssue = getCode(marker);
		if(markerURI == null || markerIssue == null)
			return false;
		return markerURI.equals(getUriToProblem(annotation)) && markerIssue.equals(getCode(annotation));
	}
New to GrepCode? Check out our FAQ X