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

net.sourceforge.pmd.renderers.HTMLRenderer 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.

There is a newer version: 7.5.0-metrics
Show 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.io.PrintWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;

import net.sourceforge.pmd.lang.document.FileId;
import net.sourceforge.pmd.lang.rule.Rule;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
import net.sourceforge.pmd.reporting.Report;
import net.sourceforge.pmd.reporting.Report.ConfigurationError;
import net.sourceforge.pmd.reporting.RuleViolation;

/**
 * Renderer to basic HTML format.
 *
 * FIXME: this class should just work with the XMLRenderer and then apply an
 * XSLT transformation + stylesheet. No need to hard-code HTML markup here.
 */
public class HTMLRenderer extends AbstractIncrementingRenderer {

    public static final String NAME = "html";

    public static final PropertyDescriptor> LINE_PREFIX =
        PropertyFactory.stringProperty("linePrefix")
                       .desc("Prefix for line number anchor in the source file.")
                       .toOptional("")
                       .defaultValue(Optional.empty())
                       .build();

    public static final PropertyDescriptor LINK_PREFIX =
        PropertyFactory.stringProperty("linkPrefix").desc("Path to HTML source.").defaultValue("").build();

    public static final PropertyDescriptor HTML_EXTENSION =
        PropertyFactory.booleanProperty("htmlExtension")
                       .desc("Replace file extension with .html for the links.")
                       // default value is false - to have the old (pre 6.23.0) behavior, this needs to be set to true.
                       .defaultValue(false)
                       .build();

    private String linkPrefix;
    private String linePrefix;
    private boolean replaceHtmlExtension;

    boolean colorize = true;

    public HTMLRenderer() {
        super(NAME, "HTML format");

        definePropertyDescriptor(LINK_PREFIX);
        definePropertyDescriptor(LINE_PREFIX);
        definePropertyDescriptor(HTML_EXTENSION);
    }

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

    /**
     * Write the body of the main body of the HTML content.
     */
    public void renderBody(PrintWriter writer, Report report) throws IOException {
        linkPrefix = getProperty(LINK_PREFIX);
        linePrefix = getProperty(LINE_PREFIX).orElse(null);
        replaceHtmlExtension = getProperty(HTML_EXTENSION);

        writer.write("

PMD report

"); writer.write("

Problems found

"); writer.println(""); writer.println(""); setWriter(writer); renderFileReport(report); writer.write("
#FileLineProblem
"); glomProcessingErrors(writer, errors); if (showSuppressedViolations) { glomSuppressions(writer, suppressed); } glomConfigurationErrors(writer, configErrors); } @Override public void start() throws IOException { linkPrefix = getProperty(LINK_PREFIX); linePrefix = getProperty(LINE_PREFIX).orElse(null); replaceHtmlExtension = getProperty(HTML_EXTENSION); writer.println("PMD"); writer.write("

PMD report

"); writer.write("

Problems found

"); writer.println(""); writer.println(""); } @Override public void renderFileViolations(Iterator violations) throws IOException { glomRuleViolations(writer, violations); } @Override public void end() throws IOException { writer.write("
#FileLineProblem
"); glomProcessingErrors(writer, errors); if (showSuppressedViolations) { glomSuppressions(writer, suppressed); } glomConfigurationErrors(writer, configErrors); writer.println(""); } private void glomRuleViolations(Writer writer, Iterator violations) throws IOException { int violationCount = 1; StringBuilder buf = new StringBuilder(500); while (violations.hasNext()) { RuleViolation rv = violations.next(); buf.setLength(0); buf.append(" ").append(System.lineSeparator()); buf.append("").append(violationCount).append("").append(System.lineSeparator()); buf.append("") .append(renderFileName(rv.getFileId(), rv.getBeginLine())) .append("") .append(System.lineSeparator()); buf.append("").append(rv.getBeginLine()).append("").append(System.lineSeparator()); String d = StringEscapeUtils.escapeHtml4(rv.getDescription()); String infoUrl = rv.getRule().getExternalInfoUrl(); if (StringUtils.isNotBlank(infoUrl)) { d = "" + d + ""; } buf.append("") .append(d) .append("") .append(System.lineSeparator()) .append("") .append(System.lineSeparator()); writer.write(buf.toString()); violationCount++; } } private String renderFileName(FileId fileId, int beginLine) { return maybeWrap(StringEscapeUtils.escapeHtml4(determineFileName(fileId)), linePrefix == null || beginLine < 0 ? "" : linePrefix + beginLine); } private String renderRuleName(Rule rule) { String name = rule.getName(); String infoUrl = rule.getExternalInfoUrl(); if (StringUtils.isNotBlank(infoUrl)) { return "" + name + ""; } return name; } private void glomProcessingErrors(PrintWriter writer, List errors) throws IOException { if (errors.isEmpty()) { return; } writer.write("
"); writer.write("

Processing errors

"); writer.println(""); writer.println(""); StringBuilder buf = new StringBuilder(500); boolean colorize = true; for (Report.ProcessingError pe : errors) { buf.setLength(0); buf.append(" ").append(System.lineSeparator()); buf.append("").append(System.lineSeparator()); buf.append("").append(System.lineSeparator()); buf.append("").append(System.lineSeparator()); writer.write(buf.toString()); } writer.write("
FileProblem
").append(renderFileName(pe.getFileId(), -1)).append("
").append(pe.getDetail()).append("
"); } private void glomSuppressions(PrintWriter writer, List suppressed) throws IOException { if (suppressed.isEmpty()) { return; } writer.write("
"); writer.write("

Suppressed warnings

"); writer.println(""); writer.println(""); StringBuilder buf = new StringBuilder(500); boolean colorize = true; for (Report.SuppressedViolation sv : suppressed) { buf.setLength(0); buf.append(" ").append(System.lineSeparator()); RuleViolation rv = sv.getRuleViolation(); buf.append("").append(System.lineSeparator()); buf.append("").append(System.lineSeparator()); buf.append("").append(System.lineSeparator()); buf.append("").append(System.lineSeparator()); buf.append("").append(System.lineSeparator()); buf.append("").append(System.lineSeparator()); writer.write(buf.toString()); } writer.write("
FileLineRuleNOPMD or AnnotationReason
").append(renderFileName(rv.getFileId(), rv.getBeginLine())).append("").append(rv.getBeginLine()).append("").append(renderRuleName(rv.getRule())).append("").append(sv.getSuppressor().getId()).append("").append(sv.getUserMessage() == null ? "" : sv.getUserMessage()).append("
"); } private void glomConfigurationErrors(final PrintWriter writer, final List configErrors) throws IOException { if (configErrors.isEmpty()) { return; } writer.write("
"); writer.write("

Configuration errors

"); writer.println(""); writer.println(""); StringBuilder buf = new StringBuilder(500); boolean colorize = true; for (Report.ConfigurationError ce : configErrors) { buf.setLength(0); buf.append(" ").append(System.lineSeparator()); buf.append("").append(System.lineSeparator()); buf.append("").append(System.lineSeparator()); buf.append("").append(System.lineSeparator()); writer.write(buf.toString()); } writer.write("
RuleProblem
").append(renderRuleName(ce.rule())).append("").append(ce.issue()).append("
"); } private String maybeWrap(String filename, String line) { if (StringUtils.isBlank(linkPrefix)) { return filename; } String newFileName = filename.replace('\\', '/'); if (replaceHtmlExtension) { int index = filename.lastIndexOf('.'); if (index >= 0) { newFileName = filename.substring(0, index); } } return "" + newFileName + ""; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy