View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.cpd;
5   
6   import net.sourceforge.pmd.PMD;
7   
8   import java.util.Iterator;
9   
10  
11  public class CSVRenderer implements Renderer {
12  
13  	private char separator;
14  	private boolean lineCountPerFile;
15  
16  	public static final char DEFAULT_SEPARATOR = ',';
17  	public static final boolean DEFAULT_LINECOUNTPERFILE = false;
18  
19  	public CSVRenderer() {
20  		this(DEFAULT_SEPARATOR, DEFAULT_LINECOUNTPERFILE);
21  	}
22  
23  	public CSVRenderer(boolean lineCountPerFile) {
24  		this(DEFAULT_SEPARATOR, lineCountPerFile);
25  	}
26  
27  	public CSVRenderer(char separatorChar) {
28  		this(separatorChar, DEFAULT_LINECOUNTPERFILE);
29  	}
30  	
31  	public CSVRenderer(char separatorChar, boolean lineCountPerFile) {
32  		this.separator = separatorChar;
33  		this.lineCountPerFile = lineCountPerFile;
34  	}
35  	
36      public String render(Iterator<Match> matches) {
37          StringBuilder csv = new StringBuilder(1000);
38  
39          if (!lineCountPerFile) {
40              csv.append("lines").append(separator);
41          }
42          csv.append("tokens").append(separator)
43             .append("occurrences")
44             .append(PMD.EOL);
45  
46          while (matches.hasNext()) {
47              Match match = matches.next();
48  
49              if (!lineCountPerFile) {
50                  csv.append(match.getLineCount()).append(separator);
51              }
52              csv.append(match.getTokenCount()).append(separator)
53                 .append(match.getMarkCount()).append(separator);
54              for (Iterator<Mark> marks = match.iterator(); marks.hasNext();) {
55                  Mark mark = marks.next();
56  
57                  csv.append(mark.getBeginLine()).append(separator);
58                  if (lineCountPerFile) {
59                      csv.append(mark.getLineCount()).append(separator);
60                  }
61                  csv.append(mark.getFilename());
62                  if (marks.hasNext()) {
63                      csv.append(separator);
64                  }
65              }
66              csv.append(PMD.EOL);
67          }
68          return csv.toString();
69      }
70  }