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