1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd; |
5 |
| |
6 |
| import net.sourceforge.pmd.dfa.report.ReportTree; |
7 |
| import net.sourceforge.pmd.stat.Metric; |
8 |
| import net.sourceforge.pmd.util.NumericConstants; |
9 |
| |
10 |
| import java.util.ArrayList; |
11 |
| import java.util.HashMap; |
12 |
| import java.util.HashSet; |
13 |
| import java.util.Iterator; |
14 |
| import java.util.List; |
15 |
| import java.util.Map; |
16 |
| import java.util.Set; |
17 |
| import java.util.TreeSet; |
18 |
| |
19 |
| public class Report { |
20 |
| |
21 |
| public static class ReadableDuration { |
22 |
| private long duration; |
23 |
| |
24 |
12
| public ReadableDuration(long duration) {
|
25 |
12
| this.duration = duration;
|
26 |
| } |
27 |
| |
28 |
12
| public String getTime() {
|
29 |
12
| long seconds = 0;
|
30 |
12
| long minutes = 0;
|
31 |
12
| long hours = 0;
|
32 |
| |
33 |
12
| if (duration > 1000) {
|
34 |
4
| seconds = duration / 1000;
|
35 |
| } |
36 |
| |
37 |
12
| if (seconds > 60) {
|
38 |
3
| minutes = seconds / 60;
|
39 |
3
| seconds = seconds % 60;
|
40 |
| } |
41 |
| |
42 |
12
| if (minutes > 60) {
|
43 |
1
| hours = minutes / 60;
|
44 |
1
| minutes = minutes % 60;
|
45 |
| } |
46 |
| |
47 |
12
| StringBuffer res = new StringBuffer();
|
48 |
12
| if (hours > 0) {
|
49 |
1
| res.append(hours).append("h ");
|
50 |
| } |
51 |
12
| if (hours > 0 || minutes > 0) {
|
52 |
3
| res.append(minutes).append("m ");
|
53 |
| } |
54 |
12
| res.append(seconds).append('s');
|
55 |
12
| return res.toString();
|
56 |
| } |
57 |
| } |
58 |
| |
59 |
| public static class ProcessingError { |
60 |
| private String msg; |
61 |
| private String file; |
62 |
| |
63 |
10
| public ProcessingError(String msg, String file) {
|
64 |
10
| this.msg = msg;
|
65 |
10
| this.file = file;
|
66 |
| } |
67 |
| |
68 |
4
| public String getMsg() {
|
69 |
4
| return msg;
|
70 |
| } |
71 |
| |
72 |
4
| public String getFile() {
|
73 |
4
| return file;
|
74 |
| } |
75 |
| } |
76 |
| |
77 |
| public static class SuppressedViolation { |
78 |
| private IRuleViolation rv; |
79 |
| private boolean isNOPMD; |
80 |
| private String userMessage; |
81 |
| |
82 |
15
| public SuppressedViolation(IRuleViolation rv, boolean isNOPMD, String userMessage) {
|
83 |
15
| this.isNOPMD = isNOPMD;
|
84 |
15
| this.rv = rv;
|
85 |
15
| this.userMessage = userMessage;
|
86 |
| } |
87 |
| |
88 |
0
| public boolean suppressedByNOPMD() {
|
89 |
0
| return this.isNOPMD;
|
90 |
| } |
91 |
| |
92 |
0
| public boolean suppressedByAnnotation() {
|
93 |
0
| return !this.isNOPMD;
|
94 |
| } |
95 |
| |
96 |
0
| public IRuleViolation getRuleViolation() {
|
97 |
0
| return this.rv;
|
98 |
| } |
99 |
| |
100 |
0
| public String getUserMessage() {
|
101 |
0
| return userMessage;
|
102 |
| } |
103 |
| } |
104 |
| |
105 |
| private static final RuleViolation.RuleViolationComparator COMPARATOR = new RuleViolation.RuleViolationComparator(); |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| private ReportTree violationTree = new ReportTree(); |
113 |
| |
114 |
| |
115 |
| private Set violations = new TreeSet(COMPARATOR); |
116 |
| private Set metrics = new HashSet(); |
117 |
| private List listeners = new ArrayList(); |
118 |
| private List errors = new ArrayList(); |
119 |
| private Map linesToExclude = new HashMap(); |
120 |
| private long start; |
121 |
| private long end; |
122 |
| |
123 |
| private List suppressedRuleViolations = new ArrayList(); |
124 |
| |
125 |
1261
| public void exclude(Map lines) {
|
126 |
1261
| linesToExclude = lines;
|
127 |
| } |
128 |
| |
129 |
0
| public Map getCountSummary() {
|
130 |
0
| Map summary = new HashMap();
|
131 |
0
| for (Iterator iter = violationTree.iterator(); iter.hasNext();) {
|
132 |
0
| IRuleViolation rv = (IRuleViolation) iter.next();
|
133 |
0
| String key = "";
|
134 |
0
| if (rv.getPackageName() != null && rv.getPackageName().length() != 0) {
|
135 |
0
| key = rv.getPackageName() + '.' + rv.getClassName();
|
136 |
| } |
137 |
0
| Object o = summary.get(key);
|
138 |
0
| if (o == null) {
|
139 |
0
| summary.put(key, NumericConstants.ONE);
|
140 |
| } else { |
141 |
0
| Integer value = (Integer) o;
|
142 |
0
| summary.put(key, new Integer(value.intValue() + 1));
|
143 |
| } |
144 |
| } |
145 |
0
| return summary;
|
146 |
| } |
147 |
| |
148 |
4
| public ReportTree getViolationTree() {
|
149 |
4
| return this.violationTree;
|
150 |
| } |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
5
| public Map getSummary() {
|
157 |
5
| Map summary = new HashMap();
|
158 |
5
| for (Iterator i = violations.iterator(); i.hasNext();) {
|
159 |
6
| IRuleViolation rv = (IRuleViolation) i.next();
|
160 |
6
| String name = rv.getRule().getName();
|
161 |
6
| if (!summary.containsKey(name)) {
|
162 |
4
| summary.put(name, NumericConstants.ZERO);
|
163 |
| } |
164 |
6
| Integer count = (Integer) summary.get(name);
|
165 |
6
| summary.put(name, new Integer(count.intValue() + 1));
|
166 |
| } |
167 |
5
| return summary;
|
168 |
| } |
169 |
| |
170 |
2
| public void addListener(ReportListener listener) {
|
171 |
2
| listeners.add(listener);
|
172 |
| } |
173 |
| |
174 |
30
| public List getSuppressedRuleViolations() {
|
175 |
30
| return suppressedRuleViolations;
|
176 |
| } |
177 |
| |
178 |
2893
| public void addRuleViolation(IRuleViolation violation) {
|
179 |
| |
180 |
| |
181 |
2893
| Integer line = new Integer(violation.getBeginLine());
|
182 |
2893
| if (linesToExclude.keySet().contains(line)) {
|
183 |
4
| suppressedRuleViolations.add(new SuppressedViolation(violation, true, (String)linesToExclude.get(line)));
|
184 |
4
| return;
|
185 |
| } |
186 |
| |
187 |
2889
| if (violation.isSuppressed()) {
|
188 |
11
| suppressedRuleViolations.add(new SuppressedViolation(violation, false, null));
|
189 |
11
| return;
|
190 |
| } |
191 |
| |
192 |
| |
193 |
2878
| violations.add(violation);
|
194 |
2878
| violationTree.addRuleViolation(violation);
|
195 |
2878
| for (Iterator i = listeners.iterator(); i.hasNext();) {
|
196 |
2
| ReportListener listener = (ReportListener) i.next();
|
197 |
2
| listener.ruleViolationAdded(violation);
|
198 |
| } |
199 |
| } |
200 |
| |
201 |
196
| public void addMetric(Metric metric) {
|
202 |
196
| metrics.add(metric);
|
203 |
196
| for (Iterator i = listeners.iterator(); i.hasNext();) {
|
204 |
1
| ReportListener listener = (ReportListener) i.next();
|
205 |
1
| listener.metricAdded(metric);
|
206 |
| } |
207 |
| } |
208 |
| |
209 |
10
| public void addError(ProcessingError error) {
|
210 |
10
| errors.add(error);
|
211 |
| } |
212 |
| |
213 |
0
| public void merge(Report r) {
|
214 |
0
| Iterator i = r.errors();
|
215 |
0
| while (i.hasNext()) {
|
216 |
0
| addError((ProcessingError)i.next());
|
217 |
| } |
218 |
0
| i = r.metrics();
|
219 |
0
| while (i.hasNext()) {
|
220 |
0
| addMetric((Metric)i.next());
|
221 |
| } |
222 |
0
| i = r.iterator();
|
223 |
0
| while (i.hasNext()) {
|
224 |
0
| addRuleViolation((IRuleViolation)i.next());
|
225 |
| } |
226 |
| } |
227 |
| |
228 |
3
| public boolean hasMetrics() {
|
229 |
3
| return !metrics.isEmpty();
|
230 |
| } |
231 |
| |
232 |
2
| public Iterator metrics() {
|
233 |
2
| return metrics.iterator();
|
234 |
| } |
235 |
| |
236 |
13
| public boolean isEmpty() {
|
237 |
13
| return !violations.iterator().hasNext() && errors.isEmpty();
|
238 |
| } |
239 |
| |
240 |
0
| public boolean treeIsEmpty() {
|
241 |
0
| return !violationTree.iterator().hasNext();
|
242 |
| } |
243 |
| |
244 |
0
| public Iterator treeIterator() {
|
245 |
0
| return violationTree.iterator();
|
246 |
| } |
247 |
| |
248 |
44
| public Iterator iterator() {
|
249 |
44
| return violations.iterator();
|
250 |
| } |
251 |
| |
252 |
29
| public Iterator errors() {
|
253 |
29
| return errors.iterator();
|
254 |
| } |
255 |
| |
256 |
0
| public int treeSize() {
|
257 |
0
| return violationTree.size();
|
258 |
| } |
259 |
| |
260 |
1434
| public int size() {
|
261 |
1434
| return violations.size();
|
262 |
| } |
263 |
| |
264 |
0
| public void start() {
|
265 |
0
| start = System.currentTimeMillis();
|
266 |
| } |
267 |
| |
268 |
0
| public void end() {
|
269 |
0
| end = System.currentTimeMillis();
|
270 |
| } |
271 |
| |
272 |
7
| public long getElapsedTimeInMillis() {
|
273 |
7
| return end - start;
|
274 |
| } |
275 |
| } |