View Javadoc

    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.renderers;
    5
    6   import net.sourceforge.pmd.PMD;
    7   import net.sourceforge.pmd.Report;
    8   import net.sourceforge.pmd.IRuleViolation;
    9
    10  import java.util.Iterator;
    11
    12  public class TextRenderer extends AbstractRenderer implements Renderer {
    13
    14      public String render(Report report) {
    15          StringBuffer buf = new StringBuffer();
    16
    17          if (report.isEmpty()) {
    18              buf.append("No problems found!");
    19              if (showSuppressedViolations) {
    20                  addSuppressed(report, buf);
    21              }
    22              return buf.toString();
    23          }
    24
    25          for (Iterator i = report.iterator(); i.hasNext();) {
    26              IRuleViolation rv = (IRuleViolation) i.next();
    27              buf.append(PMD.EOL + rv.getFilename());
    28              buf.append(":" + Integer.toString(rv.getBeginLine()));
    29              buf.append("\t" + rv.getDescription());
    30          }
    31
    32          for (Iterator i = report.errors(); i.hasNext();) {
    33              Report.ProcessingError error = (Report.ProcessingError) i.next();
    34              buf.append(PMD.EOL + error.getFile());
    35              buf.append("\t-");
    36              buf.append("\t" + error.getMsg());
    37          }
    38
    39          if (showSuppressedViolations) {
    40              addSuppressed(report, buf);
    41          }
    42
    43          return buf.toString();
    44      }
    45
    46      private void addSuppressed(Report report, StringBuffer buf) {
    47          for (Iterator i = report.getSuppressedRuleViolations().iterator(); i.hasNext();) {
    48              Report.SuppressedViolation excluded = (Report.SuppressedViolation) i.next();
    49              buf.append(PMD.EOL + excluded.getRuleViolation().getRule().getName() + " rule violation suppressed by " + (excluded.suppressedByNOPMD() ? "//NOPMD" : "Annotation") + " in " + excluded.getRuleViolation().getFilename());
    50          }
    51      }
    52  }