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.PMD;
7   import net.sourceforge.pmd.Report;
8   import net.sourceforge.pmd.RuleViolation;
9   import net.sourceforge.pmd.util.StringUtil;
10  
11  import java.util.Iterator;
12  
13  public class HTMLRenderer implements Renderer {
14  
15      private String linkPrefix;
16  
17      public HTMLRenderer(String linkPrefix) {
18          this.linkPrefix = linkPrefix;
19      }
20  
21      public HTMLRenderer() {
22          this(null);
23      }
24  
25      public String render(Report report) {
26          StringBuffer buf = new StringBuffer("<html><head><title>PMD</title></head><body>" + PMD.EOL + "<table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL + "<th>#</th><th>File</th><th>Line</th><th>Problem</th></tr>" + PMD.EOL);
27          buf.append(renderBody(report));
28          buf.append("</table></body></html>");
29          return buf.toString();
30      }
31  
32      public String renderBody(Report report) {
33          boolean colorize = true;
34          int violationCount = 1;
35          StringBuffer buf = new StringBuffer();
36          for (Iterator i = report.iterator(); i.hasNext();) {
37              RuleViolation rv = (RuleViolation) i.next();
38              buf.append("<tr");
39              if (colorize) {
40                  buf.append(" bgcolor=\"lightgrey\"");
41                  colorize = false;
42              } else {
43                  colorize = true;
44              }
45              buf.append("> " + PMD.EOL);
46              buf.append("<td align=\"center\">" + violationCount + "</td>" + PMD.EOL);
47              buf.append("<td width=\"*%\">" + maybeWrap(rv.getFilename(), Integer.toString(rv.getLine())) + "</td>" + PMD.EOL);
48              buf.append("<td align=\"center\" width=\"5%\">" + Integer.toString(rv.getLine()) + "</td>" + PMD.EOL);
49  
50              String d = rv.getDescription();
51              d = StringUtil.replaceString(d, '&', "&amp;");
52              d = StringUtil.replaceString(d, '<', "&lt;");
53              d = StringUtil.replaceString(d, '>', "&gt;");
54              if (rv.getRule().getExternalInfoUrl() != null && rv.getRule().getExternalInfoUrl().length() != 0) {
55                  d = "<a href=\"" + rv.getRule().getExternalInfoUrl() + "\">" + d + "</a>";
56              }
57  
58              buf.append("<td width=\"*\">" + d + "</td>" + PMD.EOL);
59  
60              buf.append("</tr>" + PMD.EOL);
61  
62              violationCount++;
63          }
64          return buf.toString();
65      }
66  
67      private String maybeWrap(String filename, String line) {
68          if (linkPrefix == null) {
69              return filename;
70          }
71          String newFileName = filename.substring(0, filename.indexOf(".java"));
72          return "<a href=\"" + linkPrefix + newFileName + ".html#" + line + "\">" + newFileName + "</a>";
73      }
74  }