1 |
| |
2 |
| |
3 |
| |
4 |
| package net.sourceforge.pmd.cpd; |
5 |
| |
6 |
| import net.sourceforge.pmd.PMD; |
7 |
| import net.sourceforge.pmd.util.StringUtil; |
8 |
| |
9 |
| import java.util.Iterator; |
10 |
| |
11 |
| public class SimpleRenderer implements Renderer { |
12 |
| |
13 |
| private String separator; |
14 |
| private boolean trimLeadingWhitespace; |
15 |
| |
16 |
| public static final String defaultSeparator = "====================================================================="; |
17 |
| |
18 |
0
| public SimpleRenderer() {
|
19 |
0
| this(false);
|
20 |
| } |
21 |
| |
22 |
0
| public SimpleRenderer(boolean trimLeadingWhitespace) {
|
23 |
0
| this(defaultSeparator);
|
24 |
0
| this.trimLeadingWhitespace = trimLeadingWhitespace;
|
25 |
| } |
26 |
| |
27 |
0
| public SimpleRenderer(String theSeparator) {
|
28 |
0
| separator = theSeparator;
|
29 |
| } |
30 |
| |
31 |
0
| private void renderOn(StringBuffer rpt, Match match) {
|
32 |
| |
33 |
0
| rpt.append("Found a ").append(match.getLineCount()).append(" line (").append(match.getTokenCount()).append(" tokens) duplication in the following files: ").append(PMD.EOL);
|
34 |
| |
35 |
0
| TokenEntry mark;
|
36 |
0
| for (Iterator occurrences = match.iterator(); occurrences.hasNext();) {
|
37 |
0
| mark = (TokenEntry) occurrences.next();
|
38 |
0
| rpt.append("Starting at line ").append(mark.getBeginLine()).append(" of ").append(mark.getTokenSrcID()).append(PMD.EOL);
|
39 |
| } |
40 |
| |
41 |
0
| rpt.append(PMD.EOL);
|
42 |
| |
43 |
0
| String source = match.getSourceCodeSlice();
|
44 |
| |
45 |
0
| if (trimLeadingWhitespace) {
|
46 |
0
| String[] lines = source.split("[" + PMD.EOL + "]");
|
47 |
0
| int trimDepth = StringUtil.maxCommonLeadingWhitespaceForAll(lines);
|
48 |
0
| if (trimDepth > 0) {
|
49 |
0
| lines = StringUtil.trimStartOn(lines, trimDepth);
|
50 |
| } |
51 |
0
| for (int i=0; i<lines.length; i++) {
|
52 |
0
| rpt.append(lines[i]).append(PMD.EOL);
|
53 |
| } |
54 |
0
| return;
|
55 |
| } |
56 |
| |
57 |
0
| rpt.append(source).append(PMD.EOL);
|
58 |
| } |
59 |
| |
60 |
| |
61 |
0
| public String render(Iterator matches) {
|
62 |
| |
63 |
0
| StringBuffer rpt = new StringBuffer(300);
|
64 |
| |
65 |
0
| if (matches.hasNext()) {
|
66 |
0
| renderOn(rpt, (Match)matches.next());
|
67 |
| } |
68 |
| |
69 |
0
| Match match;
|
70 |
0
| while (matches.hasNext()) {
|
71 |
0
| match = (Match) matches.next();
|
72 |
0
| rpt.append(separator).append(PMD.EOL);
|
73 |
0
| renderOn(rpt, match);
|
74 |
| |
75 |
| } |
76 |
0
| return rpt.toString();
|
77 |
| } |
78 |
| } |