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.Map;
8   
9   import net.sourceforge.pmd.PMD;
10  
11  /**
12   * Renderer to a summarized HTML format.
13   */
14  public class SummaryHTMLRenderer extends AbstractAccumulatingRenderer {
15  
16      public static final String NAME = "summaryhtml";
17  
18      public SummaryHTMLRenderer() {
19          super(NAME, "Summary HTML format.");
20  
21          // Note: we define the same properties as HTML Renderer
22          // we have to copy the values later from this renderer to the HTML
23          // Renderer
24          definePropertyDescriptor(HTMLRenderer.LINK_PREFIX);
25          definePropertyDescriptor(HTMLRenderer.LINE_PREFIX);
26      }
27  
28      public String defaultFileExtension() {
29          return "html";
30      }
31  
32      /**
33       * {@inheritDoc}
34       */
35      @Override
36      public void end() throws IOException {
37          writer.write("<html><head><title>PMD</title></head><body>" + PMD.EOL);
38          renderSummary();
39          writer.write("<center><h2>Detail</h2></center>");
40          writer.write("<table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL);
41  
42          HTMLRenderer htmlRenderer = new HTMLRenderer();
43          htmlRenderer.setProperty(HTMLRenderer.LINK_PREFIX, getProperty(HTMLRenderer.LINK_PREFIX));
44          htmlRenderer.setProperty(HTMLRenderer.LINE_PREFIX, getProperty(HTMLRenderer.LINE_PREFIX));
45          htmlRenderer.setShowSuppressedViolations(showSuppressedViolations);
46          htmlRenderer.renderBody(writer, report);
47  
48          writer.write("</tr></table></body></html>" + PMD.EOL);
49      }
50  
51      /**
52       * Write a Summary HTML table.
53       *
54       * @throws IOException
55       */
56      public void renderSummary() throws IOException {
57          writer.write("<center><h2>Summary</h2></center>" + PMD.EOL);
58          writer.write("<table align=\"center\" cellspacing=\"0\" cellpadding=\"3\">" + PMD.EOL);
59          writer.write("<tr><th>Rule name</th><th>Number of violations</th></tr>" + PMD.EOL);
60          Map<String, Integer> summary = report.getSummary();
61          for (Map.Entry<String, Integer> entry : summary.entrySet()) {
62              String ruleName = entry.getKey();
63              writer.write("<tr><td>");
64              writer.write(ruleName);
65              writer.write("</td><td align=center>");
66              writer.write(String.valueOf(entry.getValue().intValue()));
67              writer.write("</td></tr>" + PMD.EOL);
68          }
69          writer.write("</table>" + PMD.EOL);
70      }
71  }