net.sourceforge.pmd.cpd.CSVRenderer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pmd-core Show documentation
Show all versions of pmd-core Show documentation
PMD is an extensible multilanguage static code analyzer. It finds common programming flaws like unused variables,
empty catch blocks, unnecessary object creation, and so forth. It's mainly concerned with Java and
Apex, but supports 16 other languages. It comes with 400+ built-in rules. It can be
extended with custom rules. It uses JavaCC and Antlr to parse source files into abstract syntax trees
(AST) and runs rules against them to find violations. Rules can be written in Java or using a XPath query.
Currently, PMD supports Java, JavaScript, Salesforce.com Apex and Visualforce,
Kotlin, Swift, Modelica, PLSQL, Apache Velocity, JSP, WSDL, Maven POM, HTML, XML and XSL.
Scala is supported, but there are currently no Scala rules available.
Additionally, it includes CPD, the copy-paste-detector. CPD finds duplicated code in
Coco, C/C++, C#, Dart, Fortran, Gherkin, Go, Groovy, HTML, Java, JavaScript, JSP, Julia, Kotlin,
Lua, Matlab, Modelica, Objective-C, Perl, PHP, PLSQL, Python, Ruby, Salesforce.com Apex and
Visualforce, Scala, Swift, T-SQL, Typescript, Apache Velocity, WSDL, XML and XSL.
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import org.apache.commons.lang3.StringEscapeUtils;
import net.sourceforge.pmd.lang.document.FileLocation;
/**
* Renders a report to CSV. The CSV format renders each match (duplication)
* as a single line with the following columns:
*
* - lines (optional): The number of lines the first mark of a match spans.
* Only output if the {@code lineCountPerFile} is disabled (see ctor params).
* - tokens: The number of duplicated tokens in a match (size of the match).
* - occurrences: The number of duplicates in a match (number of times the tokens were found in distinct places).
*
*
* Trailing each line are pairs (or triples, if {@code lineCountPerFile} is enabled)
* of fields describing each file where the duplication was found in the format
* {@code (start line, line count (optional), file path)}. These repeat at least twice.
*
*
Examples
*
* Example without {@code lineCountPerFile}:
*
{@code
* lines,tokens,occurrences
* 10,75,2,48,/var/file1,73,/var/file2
* }
* This describes one match with the following characteristics:
*
* - The first duplicate instance is 10 lines long;
*
- 75 duplicated tokens;
*
- 2 duplicate instances;
*
- The first duplicate instance is in file {@code /var/file1} and starts at line 48;
* - The second duplicate instance is in file {@code /var/file2} and starts at line 73.
*
*
* Example with {@code lineCountPerFile}:
*
{@code
* tokens,occurrences
* 75,2,48,10,/var/file1,73,12,/var/file2
* }
* This describes one match with the following characteristics:
*
* - 75 duplicated tokens
*
- 2 duplicate instances
*
- The first duplicate instance is in file {@code /var/file1}, starts at line 48, and is 10 lines long;
* - The second duplicate instance is in file {@code /var/file2}, starts at line 73, and is 12 lines long.
*
*/
public class CSVRenderer implements CPDReportRenderer {
private final char separator;
private final boolean lineCountPerFile;
public static final char DEFAULT_SEPARATOR = ',';
public static final boolean DEFAULT_LINECOUNTPERFILE = false;
public CSVRenderer() {
this(DEFAULT_SEPARATOR, DEFAULT_LINECOUNTPERFILE);
}
public CSVRenderer(boolean lineCountPerFile) {
this(DEFAULT_SEPARATOR, lineCountPerFile);
}
public CSVRenderer(char separatorChar) {
this(separatorChar, DEFAULT_LINECOUNTPERFILE);
}
public CSVRenderer(char separatorChar, boolean lineCountPerFile) {
this.separator = separatorChar;
this.lineCountPerFile = lineCountPerFile;
}
@Override
public void render(CPDReport report, Writer writer) throws IOException {
if (!lineCountPerFile) {
writer.append("lines").append(separator);
}
writer.append("tokens").append(separator).append("occurrences").append(System.lineSeparator());
for (Match match : report.getMatches()) {
if (!lineCountPerFile) {
writer.append(String.valueOf(match.getLineCount())).append(separator);
}
writer.append(String.valueOf(match.getTokenCount())).append(separator)
.append(String.valueOf(match.getMarkCount())).append(separator);
for (Iterator marks = match.iterator(); marks.hasNext();) {
Mark mark = marks.next();
FileLocation loc = mark.getLocation();
writer.append(String.valueOf(loc.getStartLine())).append(separator);
if (lineCountPerFile) {
writer.append(String.valueOf(loc.getLineCount())).append(separator);
}
writer.append(StringEscapeUtils.escapeCsv(report.getDisplayName(loc.getFileId())));
if (marks.hasNext()) {
writer.append(separator);
}
}
writer.append(System.lineSeparator());
}
writer.flush();
}
}