01: package org.acm.seguin.pmd;
02:
03: import java.util.Comparator;
04:
05: public class RuleViolation {
06:
07: public static class RuleViolationComparator implements Comparator {
08: //
09: // Changed logic of Comparator so that rules in the same file
10: // get grouped together in the output report.
11: // DDP 7/11/2002
12: //
13: public int compare(Object o1, Object o2) {
14: RuleViolation r1 = (RuleViolation) o1;
15: RuleViolation r2 = (RuleViolation) o2;
16: if (!r1.getFilename().equals(r2.getFilename())) {
17: return r1.getFilename().compareTo(r2.getFilename());
18: }
19:
20: if (r1.getLine() != r2.getLine())
21: return r1.getLine() - r2.getLine();
22:
23: if (r1.getDescription() != null
24: && r2.getDescription() != null
25: && !r1.getDescription().equals(r2.getDescription())) {
26: return r1.getDescription().compareTo(
27: r2.getDescription());
28: }
29: // line number diff maps nicely to compare()
30: return r1.getLine() - r2.getLine();
31: }
32: }
33:
34: private int line;
35: private Rule rule;
36: private String description;
37: private String filename;
38:
39: public RuleViolation(Rule rule, int line, RuleContext ctx) {
40: this (rule, line, rule.getMessage(), ctx);
41: }
42:
43: public RuleViolation(Rule rule, int line,
44: String specificDescription, RuleContext ctx) {
45: this .line = line;
46: this .rule = rule;
47: this .description = specificDescription;
48: this .filename = ctx.getSourceCodeFilename();
49: }
50:
51: public Rule getRule() {
52: return rule;
53: }
54:
55: public int getLine() {
56: return line;
57: }
58:
59: public String getDescription() {
60: return description;
61: }
62:
63: public String getFilename() {
64: return filename;
65: }
66: }
|