All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sourceforge.pmd.renderers.YAHTMLRenderer Maven / Gradle / Ivy

Go to download

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.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.LinkedList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;

import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
import net.sourceforge.pmd.reporting.Report;
import net.sourceforge.pmd.reporting.RuleViolation;
import net.sourceforge.pmd.util.StringUtil;

/**
 * Renderer to another HTML format.
 */
public class YAHTMLRenderer extends AbstractAccumulatingRenderer {

    public static final String NAME = "yahtml";
    public static final PropertyDescriptor OUTPUT_DIR =
        PropertyFactory.stringProperty("outputDir")
                       .desc("Output directory.")
                       .defaultValue(".")
                       .build();

    private SortedMap reportNodesByPackage = new TreeMap<>();

    public YAHTMLRenderer() {
        // YA = Yet Another?
        super(NAME, "Yet Another HTML format.");
        definePropertyDescriptor(OUTPUT_DIR);
    }

    @Override
    public String defaultFileExtension() {
        return "html";
    }

    private void addViolation(RuleViolation violation) {
        String packageName = violation.getAdditionalInfo().getOrDefault(RuleViolation.PACKAGE_NAME, "");
        String className = violation.getAdditionalInfo().getOrDefault(RuleViolation.CLASS_NAME, "");

        // report each part of the package name: e.g. net.sf.pmd.test will create nodes for
        // net, net.sf, net.sf.pmd, and net.sf.pmd.test
        int index = packageName.indexOf('.', 0);
        while (index > -1) {
            String currentPackage = packageName.substring(0, index);
            ReportNode reportNode = reportNodesByPackage.get(currentPackage);
            if (reportNode == null) {
                reportNode = new ReportNode(currentPackage);
                reportNodesByPackage.put(currentPackage, reportNode);
            }
            reportNode.incrementViolations();

            int oldIndex = index;
            index = packageName.indexOf('.', index + 1);
            if (index == -1 && oldIndex != packageName.length()) {
                index = packageName.length();
            }
        }

        // add one node per class collecting the actual violations
        String fqClassName = packageName + "." + className;
        ReportNode classNode = reportNodesByPackage.get(fqClassName);
        if (classNode == null) {
            classNode = new ReportNode(packageName, className);
            reportNodesByPackage.put(fqClassName, classNode);
        }
        classNode.addRuleViolation(violation);

        // count the overall violations in the root node
        ReportNode rootNode = reportNodesByPackage.get(ReportNode.ROOT_NODE_NAME);
        if (rootNode == null) {
            rootNode = new ReportNode("Aggregate");
            reportNodesByPackage.put(ReportNode.ROOT_NODE_NAME, rootNode);
        }
        rootNode.incrementViolations();
    }

    @Override
    public void outputReport(Report report) throws IOException {
        String outputDir = getProperty(OUTPUT_DIR);

        for (RuleViolation ruleViolation : report.getViolations()) {
            addViolation(ruleViolation);
        }

        renderIndex(outputDir);
        renderClasses(outputDir);

        writer.println("

The HTML files are located " + (outputDir == null ? "above the project directory" : "in '" + outputDir + '\'') + ".

"); } private void renderIndex(String outputDir) throws IOException { try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(new File(outputDir, "index.html").toPath(), StandardCharsets.UTF_8))) { out.println(""); out.println(""); out.println(" "); out.println(" "); out.println(" PMD"); out.println(" "); out.println(" "); out.println("

Package View

"); out.println(" "); out.println(" "); for (ReportNode node : reportNodesByPackage.values()) { out.print(" "); out.println(); } out.println("
PackageClass#
"); out.print(node.getPackageName()); out.print(" "); if (node.hasViolations()) { out.print(""); out.print(node.getClassName()); out.print(""); } else { out.print(node.getClassName()); } out.print(" "); out.print(node.getViolationCount()); out.print("
"); out.println(" "); out.println(""); } } private void renderClasses(String outputDir) throws IOException { for (ReportNode node : reportNodesByPackage.values()) { if (node.hasViolations()) { try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(new File(outputDir, node.getClassName() + ".html").toPath(), StandardCharsets.UTF_8))) { out.println(""); out.println(""); out.println(" "); out.println(" "); out.print(" PMD - "); out.print(node.getClassName()); out.println(""); out.println(" "); out.println(" "); out.println("

Class View

"); out.print("

Class: "); out.print(node.getClassName()); out.println("

"); out.println(" "); out.println(" "); for (RuleViolation violation : node.getViolations()) { out.print(" "); out.println(); } out.println("
MethodViolation
"); String methodName = violation.getAdditionalInfo().get(RuleViolation.METHOD_NAME); out.print(StringUtil.nullToEmpty(methodName)); out.print(""); out.print(""); out.print(renderViolationRow("Rule:", violation.getRule().getName())); out.print(renderViolationRow("Description:", violation.getDescription())); String variableName = violation.getAdditionalInfo().get(RuleViolation.VARIABLE_NAME); if (StringUtils.isNotBlank(variableName)) { out.print(renderViolationRow("Variable:", variableName)); } out.print(renderViolationRow("Line:", violation.getEndLine() > 0 ? violation.getBeginLine() + " and " + violation.getEndLine() : String.valueOf(violation.getBeginLine()))); out.print("
"); out.print("
"); out.println(" "); out.println(""); } } } } private String renderViolationRow(String name, String value) { return "" + name + "" + "" + value + ""; } private static class ReportNode { // deliberately starts with a space, so that it is sorted before the packages private static final String ROOT_NODE_NAME = " "; private final String packageName; private final String className; private int violationCount; private final List violations = new LinkedList<>(); ReportNode(String packageName) { this.packageName = packageName; this.className = "-"; } ReportNode(String packageName, String className) { this.packageName = packageName; this.className = className; } public void incrementViolations() { violationCount++; } public void addRuleViolation(RuleViolation violation) { violations.add(violation); } public String getPackageName() { return packageName; } public String getClassName() { return className; } public int getViolationCount() { return violationCount + violations.size(); } public List getViolations() { return violations; } public boolean hasViolations() { return !violations.isEmpty(); } @Override public String toString() { return "ReportNode[packageName=" + packageName + ",className=" + className + ",violationCount=" + violationCount + ",violations=" + violations.size() + "]"; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy