| /*=============================================================================# |
| # Copyright (c) 2021 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.ltk.issues.core.impl; |
| |
| import static org.eclipse.statet.jcommons.lang.ObjectUtils.nonNullAssert; |
| |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.resources.IMarker; |
| import org.eclipse.core.runtime.CoreException; |
| |
| import org.eclipse.statet.jcommons.collections.ImList; |
| import org.eclipse.statet.jcommons.lang.NonNullByDefault; |
| import org.eclipse.statet.jcommons.lang.Nullable; |
| |
| import org.eclipse.statet.ltk.core.Ltk; |
| import org.eclipse.statet.ltk.issues.core.IssueMarkers; |
| import org.eclipse.statet.ltk.issues.core.IssueTypeSet; |
| import org.eclipse.statet.ltk.issues.core.IssueTypeSet.ProblemTypes; |
| import org.eclipse.statet.ltk.issues.core.Problem; |
| import org.eclipse.statet.ltk.issues.core.Task; |
| |
| |
| @NonNullByDefault |
| public class ResourceMarkerIssueRequestor extends BasicIssueRequestor { |
| |
| |
| public static @Nullable ResourceMarkerIssueRequestor create(final @Nullable Object resource, |
| final IssueTypeSet issueTypeSet) { |
| if (resource instanceof IFile) { |
| return new ResourceMarkerIssueRequestor((IFile)resource, issueTypeSet); |
| } |
| return null; |
| } |
| |
| |
| private final IFile resource; |
| |
| |
| public ResourceMarkerIssueRequestor(final IFile file, final IssueTypeSet issueTypeSet) { |
| super(issueTypeSet, Ltk.PERSISTENCE_CONTEXT); |
| |
| this.resource= file; |
| } |
| |
| |
| @Override |
| protected void reportProblems(final ImList<ProblemCategory> problemCategories) throws CoreException { |
| for (final var problemCategory : problemCategories) { |
| final ProblemTypes types= nonNullAssert( |
| problemCategory.getDef().getTypes(Ltk.PERSISTENCE_CONTEXT) ); |
| for (final var problem : problemCategory.getAcceptedIssues()) { |
| final var attributes= createProblemAttributes(problem); |
| this.resource.createMarker(types.getType(problem.getSeverity()), attributes); |
| } |
| } |
| } |
| |
| protected Map<String, Object> createProblemAttributes(final Problem problem) { |
| final var attributes= new HashMap<String, Object>(10); |
| attributes.put(IMarker.SOURCE_ID, getIssueTypeSet().getSourceId()); |
| attributes.put(IssueMarkers.CATEGORY_ID_ATTR_NAME, problem.getCategoryId()); |
| attributes.put(IMarker.SEVERITY, IssueMarkers.toMarkerSeverity(problem.getSeverity())); |
| attributes.put(IssueMarkers.CODE_ATTR_NAME, problem.getCode()); |
| attributes.put(IMarker.MESSAGE, problem.getMessage()); |
| |
| attributes.put(IMarker.LINE_NUMBER, (problem.getSourceLine() > 0) ? problem.getSourceLine() : 1); |
| if (problem.getSourceStartOffset() >= 0) { |
| attributes.put(IMarker.CHAR_START, problem.getSourceStartOffset()); |
| attributes.put(IMarker.CHAR_END, problem.getSourceEndOffset()); |
| } |
| attributes.put(IMarker.USER_EDITABLE, false); |
| |
| return attributes; |
| } |
| |
| |
| @Override |
| protected void reportTasks(final @Nullable TaskCategory taskCategory) throws CoreException { |
| if (taskCategory != null) { |
| final String type= nonNullAssert( |
| taskCategory.getDef().getType(Ltk.PERSISTENCE_CONTEXT) ); |
| for (final var task : taskCategory.getAcceptedIssues()) { |
| final var attributes= createTaskAttributes(task); |
| this.resource.createMarker(type, attributes); |
| } |
| } |
| } |
| |
| protected Map<String, Object> createTaskAttributes(final Task task) { |
| final var attributes= new HashMap<String, Object>(10); |
| attributes.put(IMarker.SOURCE_ID, getIssueTypeSet().getSourceId()); |
| attributes.put(IMarker.PRIORITY, IssueMarkers.toMarkerPriority(task.getPriority())); |
| attributes.put(IMarker.MESSAGE, task.getMessage()); |
| |
| attributes.put(IMarker.LINE_NUMBER, (task.getSourceLine() > 0) ? task.getSourceLine() : 1); |
| if (task.getSourceStartOffset() >= 0) { |
| attributes.put(IMarker.CHAR_START, task.getSourceStartOffset()); |
| attributes.put(IMarker.CHAR_END, task.getSourceEndOffset()); |
| } |
| attributes.put(IMarker.USER_EDITABLE, false); |
| |
| return attributes; |
| } |
| |
| |
| } |