1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
/*******************************************************************************
* Copyright (c) 2009 Alena Laskavaia
* 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:
* Alena Laskavaia - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.codan.internal.core.model;
import java.text.MessageFormat;
import org.eclipse.cdt.codan.core.model.IProblem;
import org.eclipse.cdt.codan.core.model.IProblemLocation;
import org.eclipse.cdt.codan.core.model.IProblemReporter;
import org.eclipse.cdt.codan.internal.core.CheckersRegisry;
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 CodanMarkerProblemReporter implements IProblemReporter {
/*
* (non-Javadoc)
*
* @see
* org.eclipse.cdt.codan.core.model.IProblemReporter#reportProblem(java.
* lang.String, org.eclipse.cdt.codan.core.model.IProblemLocation,
* java.lang.Object[])
*/
public void reportProblem(String id, IProblemLocation loc, Object... args) {
IFile file = loc.getFile();
int lineNumber = loc.getLineNumber();
if (file == null)
throw new NullPointerException("file");
if (id == null)
throw new NullPointerException("id");
IProblem problem = CheckersRegisry.getInstance().getResourceProfile(
file).findProblem(id);
if (problem == null)
throw new IllegalArgumentException("Id is not registered");
if (problem.isEnabled() == false)
return; // skip
int severity = problem.getSeverity().intValue();
String messagePattern = problem.getMessagePattern();
String message = id;
if (messagePattern == null) {
if (args != null && args.length > 0 && args[0] instanceof String)
message = (String) args[0];
} else {
MessageFormat.format(messagePattern, args);
}
reportProblem(id, severity, file, lineNumber, loc.getStartingChar(),
loc.getEndingChar(), message);
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.cdt.codan.core.model.IProblemReporter#reportProblem(java.
* lang.String, org.eclipse.core.resources.IFile, int, java.lang.String)
*/
public void reportProblem(String id, int severity, IFile file,
int lineNumber, int startChar, int endChar, String message) {
try {
// Do not put in duplicates
IMarker[] cur = file.findMarkers(GENERIC_CODE_ANALYSIS_MARKER_TYPE,
false, IResource.DEPTH_ZERO);
if (cur != null) {
for (IMarker element : cur) {
int line = ((Integer) element
.getAttribute(IMarker.LINE_NUMBER)).intValue();
if (line == lineNumber) {
String mesg = (String) element
.getAttribute(IMarker.MESSAGE);
int sev = ((Integer) element
.getAttribute(IMarker.SEVERITY)).intValue();
if (sev == severity && mesg.equals(message))
return;
}
}
}
IMarker marker = file
.createMarker(GENERIC_CODE_ANALYSIS_MARKER_TYPE);
marker.setAttribute(IMarker.MESSAGE, message);
marker.setAttribute(IMarker.SEVERITY, severity);
marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
marker.setAttribute(IMarker.PROBLEM, id);
marker.setAttribute(IMarker.CHAR_END, endChar);
marker.setAttribute(IMarker.CHAR_START, startChar);
} catch (CoreException e) {
e.printStackTrace();
}
}
public void deleteMarkers(IResource file) {
try {
file.deleteMarkers(GENERIC_CODE_ANALYSIS_MARKER_TYPE, false,
IResource.DEPTH_ZERO);
} catch (CoreException ce) {
ce.printStackTrace();
}
}
public void deleteAllMarkers() {
try {
// TODO delete contributed markers too
ResourcesPlugin.getWorkspace().getRoot().deleteMarkers(
GENERIC_CODE_ANALYSIS_MARKER_TYPE, false,
IResource.DEPTH_INFINITE);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
|