net.sourceforge.pmd.renderers.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.
The newest version!
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.renderers;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.nullness.qual.NonNull;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
import net.sourceforge.pmd.properties.PropertySource;
import net.sourceforge.pmd.renderers.ColumnDescriptor.Accessor;
import net.sourceforge.pmd.reporting.RuleViolation;
/**
* Renderer the results to a comma-delimited text format. All available columns
* are present by default. IDEs can enable/disable columns individually
* (cmd-line control to follow eventually)
*/
public class CSVRenderer extends AbstractIncrementingRenderer {
private final String separator;
private final String cr;
private CSVWriter csvWriter;
private static final String DEFAULT_SEPARATOR = ",";
private static final Map> PROPERTY_DESCRIPTORS_BY_ID = new HashMap<>();
public static final String NAME = "csv";
@SuppressWarnings("unchecked")
private final ColumnDescriptor[] allColumns = new ColumnDescriptor[] {
newColDescriptor("problem", "Problem", (idx, rv, cr) -> Integer.toString(idx)),
newColDescriptor("package", "Package", (idx, rv, cr) -> rv.getAdditionalInfo().getOrDefault(RuleViolation.PACKAGE_NAME, "")),
newColDescriptor("file", "File", (idx, rv, cr) -> determineFileName(rv.getFileId())),
newColDescriptor("priority", "Priority", (idx, rv, cr) -> Integer.toString(rv.getRule().getPriority().getPriority())),
newColDescriptor("line", "Line", (idx, rv, cr) -> Integer.toString(rv.getBeginLine())),
newColDescriptor("desc", "Description", (idx, rv, cr) -> StringUtils.replaceChars(rv.getDescription(), '\"', '\'')),
newColDescriptor("ruleSet", "Rule set", (idx, rv, cr) -> rv.getRule().getRuleSetName()),
newColDescriptor("rule", "Rule", (idx, rv, cr) -> rv.getRule().getName()),
};
private static @NonNull ColumnDescriptor newColDescriptor(String id, String title, Accessor accessor) {
return new ColumnDescriptor<>(id, title, accessor);
}
public CSVRenderer(ColumnDescriptor[] columns, String theSeparator, String theCR) {
super(NAME, "Comma-separated values tabular format.");
separator = theSeparator;
cr = theCR;
for (ColumnDescriptor desc : columns) {
definePropertyDescriptor(booleanPropertyFor(desc.id, desc.title));
}
}
public CSVRenderer() {
super(NAME, "Comma-separated values tabular format.");
separator = DEFAULT_SEPARATOR;
cr = System.lineSeparator();
for (ColumnDescriptor desc : allColumns) {
definePropertyDescriptor(booleanPropertyFor(desc.id, desc.title));
}
}
private static PropertyDescriptor booleanPropertyFor(String id, String label) {
PropertyDescriptor prop = PROPERTY_DESCRIPTORS_BY_ID.get(id);
if (prop != null) {
return prop;
}
prop = PropertyFactory.booleanProperty(id).defaultValue(true).desc("Include " + label + " column").build();
PROPERTY_DESCRIPTORS_BY_ID.put(id, prop);
return prop;
}
private List> activeColumns() {
List> actives = new ArrayList<>();
for (ColumnDescriptor desc : allColumns) {
PropertyDescriptor prop = booleanPropertyFor(desc.id, null);
if (getProperty(prop)) {
actives.add(desc);
}
}
return actives;
}
private CSVWriter csvWriter() {
if (csvWriter != null) {
return csvWriter;
}
csvWriter = new CSVWriter<>(activeColumns(), separator, cr);
return csvWriter;
}
@Override
public void start() throws IOException {
csvWriter().writeTitles(getWriter());
}
@Override
public String defaultFileExtension() {
return "csv";
}
@Override
public void renderFileViolations(Iterator violations) throws IOException {
csvWriter().writeData(getWriter(), violations);
}
/**
* We can't show any violations if we don't have any visible columns.
*
* @see PropertySource#dysfunctionReason()
*/
@Override
public String dysfunctionReason() {
return activeColumns().isEmpty() ? "No columns selected" : null;
}
}