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 java.io.IOException;
    7   import java.io.Writer;
    8   import java.util.Iterator;
    9
    10  import net.sourceforge.pmd.PMD;
    11  import net.sourceforge.pmd.Report;
    12  import net.sourceforge.pmd.RuleViolation;
    13
    14  /**
    15   * Renderer to simple text format.
    16   */
    17  public class TextRenderer extends AbstractIncrementingRenderer {
    18
    19      public static final String NAME = "text";
    20
    21      public TextRenderer() {
    22      	super(NAME, "Text format.");
    23      }
    24
    25
    26      public String defaultFileExtension() { return "txt"; }
    27
    28      /**
    29       * {@inheritDoc}
    30       */
    31      @Override
    32      public void start() throws IOException {
    33      }
    34
    35      /**
    36       * {@inheritDoc}
    37       */
    38      @Override
    39      public void renderFileViolations(Iterator<RuleViolation> violations) throws IOException {
    40  		Writer writer = getWriter();
    41  		StringBuilder buf = new StringBuilder();
    42
    43  		while (violations.hasNext()) {
    44  		    buf.setLength(0);
    45  		    RuleViolation rv = violations.next();
    46  		    buf.append(rv.getFilename());
    47  		    buf.append(':').append(Integer.toString(rv.getBeginLine()));
    48  		    buf.append(":\t").append(rv.getDescription()).append(PMD.EOL);
    49  		    writer.write(buf.toString());
    50  		}
    51      }
    52
    53      /**
    54       * {@inheritDoc}
    55       */
    56      @Override
    57      public void end() throws IOException {
    58  		Writer writer = getWriter();
    59  		StringBuilder buf = new StringBuilder(500);
    60  		if (!errors.isEmpty()) {
    61
    62  		    for (Report.ProcessingError error : errors) {
    63  			buf.setLength(0);
    64  			buf.append(error.getFile());
    65  			buf.append("\t-\t").append(error.getMsg()).append(PMD.EOL);
    66  			writer.write(buf.toString());
    67  		    }
    68  		}
    69
    70  		for (Report.SuppressedViolation excluded : suppressed) {
    71  		    buf.setLength(0);
    72  		    buf.append(excluded.getRuleViolation().getRule().getName());
    73  		    buf.append(" rule violation suppressed by ");
    74  		    buf.append(excluded.suppressedByNOPMD() ? "//NOPMD" : "Annotation");
    75  		    buf.append(" in ").append(excluded.getRuleViolation().getFilename()).append(PMD.EOL);
    76  		    writer.write(buf.toString());
    77  		}
    78      }
    79
    80  }