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.util.Iterator;
    8   import java.util.LinkedList;
    9   import java.util.List;
    10
    11  import net.sourceforge.pmd.Report;
    12  import net.sourceforge.pmd.RuleViolation;
    13  import net.sourceforge.pmd.util.datasource.DataSource;
    14
    15  /**
    16   * Abstract base class for {@link Renderer} implementations which can produce
    17   * output incrementally for {@link RuleViolation}s as source files are
    18   * processed.  Such {@link Renderer}s are able to produce large reports with
    19   * significantly less working memory at any given time.  Variations in the
    20   * delivery of source file reports are reflected in the output of the
    21   * {@link Renderer}, so report output can be different between runs.
    22   *
    23   * Only processing errors and suppressed violations are accumulated across all
    24   * files.  These are intended to be processed in the {@link #end()} method.
    25   */
    26  public abstract class AbstractIncrementingRenderer extends AbstractRenderer {
    27
    28      /**
    29       * Accumulated processing errors.
    30       */
    31      protected List<Report.ProcessingError> errors = new LinkedList<Report.ProcessingError>();
    32
    33      /**
    34       * Accumulated suppressed violations.
    35       */
    36      protected List<Report.SuppressedViolation> suppressed = new LinkedList<Report.SuppressedViolation>();
    37
    38      public AbstractIncrementingRenderer(String name, String description) {
    39  	super(name, description);
    40      }
    41
    42      /**
    43       * {@inheritDoc}
    44       */
    45      public void start() throws IOException {
    46      }
    47
    48      /**
    49       * {@inheritDoc}
    50       */
    51      public void startFileAnalysis(DataSource dataSource) {
    52      }
    53
    54      /**
    55       * {@inheritDoc}
    56       */
    57      public void renderFileReport(Report report) throws IOException {
    58  	Iterator<RuleViolation> violations = report.iterator();
    59  	if (violations.hasNext()) {
    60  	    renderFileViolations(violations);
    61  	    getWriter().flush();
    62  	}
    63
    64  	for (Iterator<Report.ProcessingError> i = report.errors(); i.hasNext();) {
    65  	    errors.add(i.next());
    66  	}
    67
    68  	if (showSuppressedViolations) {
    69  	    suppressed.addAll(report.getSuppressedRuleViolations());
    70  	}
    71      }
    72
    73      /**
    74       * Render a series of {@link RuleViolation}s.
    75       * @param violations The iterator of violations to render.
    76       * @throws IOException
    77       */
    78      public abstract void renderFileViolations(Iterator<RuleViolation> violations) throws IOException;
    79
    80      /**
    81       * {@inheritDoc}
    82       */
    83      public void end() throws IOException {
    84      }
    85  }