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("<h2><center>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.renderBody(writer, report);
    43
    44  	writer.write("</table></body></html>" + PMD.EOL);
    45      }
    46
    47      /**
    48       * Write a Summary HTML table.
    49       *
    50       * @throws IOException
    51       */
    52      public void renderSummary() throws IOException {
    53      	StringBuilder buf = new StringBuilder(500);
    54  		buf.append("<h2><center>Summary</h2></center>");
    55  		buf.append("<table align=\"center\" cellspacing=\"0\" cellpadding=\"3\">");
    56  		buf.append("<th>Rule name</th>");
    57  		buf.append("<th>Number of violations</th>");
    58  		writer.write(buf.toString());
    59  		Map<String, Integer> summary = report.getSummary();
    60  		for (Map.Entry<String, Integer> entry : summary.entrySet()) {
    61  		    String ruleName = entry.getKey();
    62  		    buf.setLength(0);
    63  		    buf.append("<tr>");
    64  		    buf.append("<td>" + ruleName + "</td>");
    65  		    buf.append("<td align=center>" + entry.getValue().intValue() + "</td>");
    66  		    buf.append("</tr>");
    67  		    writer.write(buf.toString());
    68  		}
    69  		writer.write("</table>");
    70      }
    71  }