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.IRuleViolation;
7   import net.sourceforge.pmd.PMD;
8   import net.sourceforge.pmd.Report;
9   
10  import java.io.IOException;
11  import java.io.Writer;
12  import java.util.Iterator;
13  
14  public class TextRenderer extends OnTheFlyRenderer {
15  
16      private boolean empty;
17  
18      public void start() throws IOException {
19          empty = true;
20      }
21  
22      public void renderFileViolations(Iterator<IRuleViolation> violations) throws IOException {
23          Writer writer = getWriter();
24          StringBuffer buf = new StringBuffer();
25  
26          empty = !violations.hasNext();
27          while (violations.hasNext()) {
28              buf.setLength(0);
29              IRuleViolation rv = violations.next();
30              buf.append(PMD.EOL).append(rv.getFilename());
31              buf.append(':').append(Integer.toString(rv.getBeginLine()));
32              buf.append('\t').append(rv.getDescription());
33              writer.write(buf.toString());
34          }
35      }
36  
37      public void end() throws IOException {
38          Writer writer = getWriter();
39          StringBuffer buf = new StringBuffer();
40          if (!errors.isEmpty()) {
41              empty = false;
42  
43              for(Report.ProcessingError error: errors) {
44                  buf.setLength(0);
45                  buf.append(PMD.EOL).append(error.getFile());
46                  buf.append("\t-\t").append(error.getMsg());
47                  writer.write(buf.toString());
48              }
49          }
50  
51          for(Report.SuppressedViolation excluded: suppressed) {
52              buf.setLength(0);
53              buf.append(PMD.EOL);
54              buf.append(excluded.getRuleViolation().getRule().getName());
55              buf.append(" rule violation suppressed by ");
56              buf.append(excluded.suppressedByNOPMD() ? "//NOPMD" : "Annotation");
57              buf.append(" in ").append(excluded.getRuleViolation().getFilename());
58              writer.write(buf.toString());
59          }
60  
61          if (empty) {
62              getWriter().write("No problems found!");
63          }
64      }
65  
66  }