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 Renderer
23  	definePropertyDescriptor(HTMLRenderer.LINK_PREFIX);
24  	definePropertyDescriptor(HTMLRenderer.LINE_PREFIX);
25      }
26  
27      public String defaultFileExtension() { return "html"; }
28      
29      /**
30       * {@inheritDoc}
31       */
32      @Override
33      public void end() throws IOException {
34  	writer.write("<html><head><title>PMD</title></head><body>" + PMD.EOL);
35  	renderSummary();
36  	writer.write("<center><h2>Detail</h2></center>");
37  	writer.write("<table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL);
38  
39  	HTMLRenderer htmlRenderer = new HTMLRenderer();
40  	htmlRenderer.setProperty(HTMLRenderer.LINK_PREFIX, getProperty(HTMLRenderer.LINK_PREFIX));
41  	htmlRenderer.setProperty(HTMLRenderer.LINE_PREFIX, getProperty(HTMLRenderer.LINE_PREFIX));
42  	htmlRenderer.setShowSuppressedViolations(showSuppressedViolations);
43  	htmlRenderer.renderBody(writer, report);
44  
45  	writer.write("</tr></table></body></html>" + PMD.EOL);
46      }
47  
48      /**
49       * Write a Summary HTML table.
50       *
51       * @throws IOException
52       */
53      public void renderSummary() throws IOException {
54      	StringBuilder buf = new StringBuilder(500);
55  		buf.append("<center><h2>Summary</h2></center>");
56  		buf.append("<table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>");
57  		buf.append("<th>Rule name</th>");
58  		buf.append("<th>Number of violations</th></tr>");
59  		writer.write(buf.toString());
60  		Map<String, Integer> summary = report.getSummary();
61  		for (Map.Entry<String, Integer> entry : summary.entrySet()) {
62  		    String ruleName = entry.getKey();
63  		    buf.setLength(0);
64  		    buf.append("<tr>");
65  		    buf.append("<td>" + ruleName + "</td>");
66  		    buf.append("<td align=center>" + entry.getValue().intValue() + "</td>");
67  		    buf.append("</tr>");
68  		    writer.write(buf.toString());
69  		}
70  		writer.write("</table>");
71      }
72  }