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