01 /**
02 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03 */
04 package net.sourceforge.pmd.cpd;
05
06 import net.sourceforge.pmd.PMD;
07 import net.sourceforge.pmd.util.StringUtil;
08
09 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 DEFAULT_SEPARATOR = "=====================================================================";
17
18 public SimpleRenderer() {
19 this(false);
20 }
21
22 public SimpleRenderer(boolean trimLeadingWhitespace) {
23 this(DEFAULT_SEPARATOR);
24 this.trimLeadingWhitespace = trimLeadingWhitespace;
25 }
26
27 public SimpleRenderer(String theSeparator) {
28 separator = theSeparator;
29 }
30
31 private void renderOn(StringBuffer rpt, Match match) {
32
33 rpt.append("Found a ").append(match.getLineCount()).append(" line (").append(match.getTokenCount()).append(" tokens) duplication in the following files: ").append(PMD.EOL);
34
35 for (Iterator<TokenEntry> occurrences = match.iterator(); occurrences.hasNext();) {
36 TokenEntry mark = occurrences.next();
37 rpt.append("Starting at line ").append(mark.getBeginLine()).append(" of ").append(mark.getTokenSrcID()).append(PMD.EOL);
38 }
39
40 rpt.append(PMD.EOL); // add a line to separate the source from the desc above
41
42 String source = match.getSourceCodeSlice();
43
44 if (trimLeadingWhitespace) {
45 String[] lines = source.split("[" + PMD.EOL + "]");
46 int trimDepth = StringUtil.maxCommonLeadingWhitespaceForAll(lines);
47 if (trimDepth > 0) {
48 lines = StringUtil.trimStartOn(lines, trimDepth);
49 }
50 for (int i=0; i<lines.length; i++) {
51 rpt.append(lines[i]).append(PMD.EOL);
52 }
53 return;
54 }
55
56 rpt.append(source).append(PMD.EOL);
57 }
58
59
60 public String render(Iterator<Match> matches) {
61
62 StringBuffer rpt = new StringBuffer(300);
63
64 if (matches.hasNext()) {
65 renderOn(rpt, matches.next());
66 }
67
68 Match match;
69 while (matches.hasNext()) {
70 match = matches.next();
71 rpt.append(separator).append(PMD.EOL);
72 renderOn(rpt, match);
73
74 }
75 return rpt.toString();
76 }
77 }
|