1 |
| |
2 |
| |
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.IRuleViolation; |
9 |
| import net.sourceforge.pmd.util.StringUtil; |
10 |
| |
11 |
| import java.util.Iterator; |
12 |
| |
13 |
| public class HTMLRenderer extends AbstractRenderer implements Renderer { |
14 |
| |
15 |
| private String linkPrefix; |
16 |
| |
17 |
1
| public HTMLRenderer(String linkPrefix) {
|
18 |
1
| this.linkPrefix = linkPrefix;
|
19 |
| } |
20 |
| |
21 |
1
| public HTMLRenderer() {
|
22 |
1
| this(null);
|
23 |
| } |
24 |
| |
25 |
0
| public String render(Report report) {
|
26 |
0
| StringBuffer buf = new StringBuffer("<html><head><title>PMD</title></head><body>" + PMD.EOL);
|
27 |
0
| buf.append(renderBody(report));
|
28 |
0
| buf.append("</body></html>");
|
29 |
0
| return buf.toString();
|
30 |
| } |
31 |
| |
32 |
0
| public String renderBody(Report report) {
|
33 |
0
| StringBuffer buf = glomIRuleViolations(report);
|
34 |
0
| glomProcessingErrors(report, buf);
|
35 |
0
| if (showSuppressedViolations) {
|
36 |
0
| glomSuppressions(report, buf);
|
37 |
| } |
38 |
0
| return buf.toString();
|
39 |
| } |
40 |
| |
41 |
0
| private StringBuffer glomIRuleViolations(Report report) {
|
42 |
0
| boolean colorize = true;
|
43 |
0
| int violationCount = 1;
|
44 |
0
| StringBuffer buf = new StringBuffer();
|
45 |
0
| buf.append("<center><h3>PMD report</h3></center>");
|
46 |
0
| buf.append("<center><h3>Problems found</h3></center>");
|
47 |
0
| buf.append("<table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL + "<th>#</th><th>File</th><th>Line</th><th>Problem</th></tr>" + PMD.EOL);
|
48 |
0
| for (Iterator i = report.iterator(); i.hasNext();) {
|
49 |
0
| IRuleViolation rv = (IRuleViolation) i.next();
|
50 |
0
| buf.append("<tr");
|
51 |
0
| if (colorize) {
|
52 |
0
| buf.append(" bgcolor=\"lightgrey\"");
|
53 |
| } |
54 |
0
| colorize = !colorize;
|
55 |
0
| buf.append("> " + PMD.EOL);
|
56 |
0
| buf.append("<td align=\"center\">" + violationCount + "</td>" + PMD.EOL);
|
57 |
0
| buf.append("<td width=\"*%\">" + maybeWrap(rv.getFilename(), Integer.toString(rv.getBeginLine())) + "</td>" + PMD.EOL);
|
58 |
0
| buf.append("<td align=\"center\" width=\"5%\">" + Integer.toString(rv.getBeginLine()) + "</td>" + PMD.EOL);
|
59 |
| |
60 |
0
| String d = rv.getDescription();
|
61 |
0
| d = StringUtil.replaceString(d, '&', "&");
|
62 |
0
| d = StringUtil.replaceString(d, '<', "<");
|
63 |
0
| d = StringUtil.replaceString(d, '>', ">");
|
64 |
0
| if (rv.getRule().getExternalInfoUrl() != null && rv.getRule().getExternalInfoUrl().length() != 0) {
|
65 |
0
| d = "<a href=\"" + rv.getRule().getExternalInfoUrl() + "\">" + d + "</a>";
|
66 |
| } |
67 |
0
| buf.append("<td width=\"*\">" + d + "</td>" + PMD.EOL);
|
68 |
0
| buf.append("</tr>" + PMD.EOL);
|
69 |
0
| violationCount++;
|
70 |
| } |
71 |
0
| if (violationCount > 0) {
|
72 |
0
| buf.append("</table>");
|
73 |
| } |
74 |
0
| return buf;
|
75 |
| } |
76 |
| |
77 |
0
| private void glomProcessingErrors(Report report, StringBuffer buf) {
|
78 |
0
| boolean colorize = true;
|
79 |
0
| int violationCount;
|
80 |
| |
81 |
0
| if (report.errors().hasNext()) {
|
82 |
0
| buf.append("<hr/>");
|
83 |
0
| buf.append("<center><h3>Processing errors</h3></center>");
|
84 |
0
| buf.append("<table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL + "<th>File</th><th>Problem</th></tr>" + PMD.EOL);
|
85 |
| } |
86 |
0
| violationCount = 0;
|
87 |
0
| for (Iterator i = report.errors(); i.hasNext();) {
|
88 |
0
| Report.ProcessingError pe = (Report.ProcessingError) i.next();
|
89 |
0
| buf.append("<tr");
|
90 |
0
| if (colorize) {
|
91 |
0
| buf.append(" bgcolor=\"lightgrey\"");
|
92 |
| } |
93 |
0
| colorize = !colorize;
|
94 |
0
| buf.append("> " + PMD.EOL);
|
95 |
0
| buf.append("<td>" + pe.getFile() + "</td>" + PMD.EOL);
|
96 |
0
| buf.append("<td>" + pe.getMsg() + "</td>" + PMD.EOL);
|
97 |
0
| buf.append("</tr>" + PMD.EOL);
|
98 |
0
| violationCount++;
|
99 |
| } |
100 |
0
| if (violationCount > 0) {
|
101 |
0
| buf.append("</table>");
|
102 |
| } |
103 |
| } |
104 |
| |
105 |
0
| private void glomSuppressions(Report report, StringBuffer buf) {
|
106 |
0
| boolean colorize = true;
|
107 |
0
| if (!report.getSuppressedRuleViolations().isEmpty()) {
|
108 |
0
| buf.append("<hr/>");
|
109 |
0
| buf.append("<center><h3>Suppressed warnings</h3></center>");
|
110 |
0
| buf.append("<table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL + "<th>File</th><th>Line</th><th>Rule</th><th>NOPMD or Annotation</th></tr>" + PMD.EOL);
|
111 |
| } |
112 |
0
| for (Iterator i = report.getSuppressedRuleViolations().iterator(); i.hasNext();) {
|
113 |
0
| Report.SuppressedViolation sv = (Report.SuppressedViolation) i.next();
|
114 |
0
| buf.append("<tr");
|
115 |
0
| if (colorize) {
|
116 |
0
| buf.append(" bgcolor=\"lightgrey\"");
|
117 |
| } |
118 |
0
| colorize = !colorize;
|
119 |
0
| buf.append("> " + PMD.EOL);
|
120 |
0
| buf.append("<td align=\"left\">" + sv.getRuleViolation().getFilename() + "</td>" + PMD.EOL);
|
121 |
0
| buf.append("<td align=\"center\">" + sv.getRuleViolation().getBeginLine() + "</td>" + PMD.EOL);
|
122 |
0
| buf.append("<td align=\"center\">" + sv.getRuleViolation().getRule().getName() + "</td>" + PMD.EOL);
|
123 |
0
| buf.append("<td align=\"center\">" + (sv.suppressedByNOPMD() ? "NOPMD" : "Annotation") + "</td>" + PMD.EOL);
|
124 |
0
| buf.append("</tr>" + PMD.EOL);
|
125 |
| } |
126 |
0
| if (!report.getSuppressedRuleViolations().isEmpty()) {
|
127 |
0
| buf.append("</table>");
|
128 |
| } |
129 |
| } |
130 |
| |
131 |
0
| private String maybeWrap(String filename, String line) {
|
132 |
0
| if (linkPrefix == null) {
|
133 |
0
| return filename;
|
134 |
| } |
135 |
0
| String newFileName = filename.substring(0, filename.lastIndexOf("."));
|
136 |
0
| return "<a href=\"" + linkPrefix + newFileName + ".html#" + line + "\">" + newFileName + "</a>";
|
137 |
| } |
138 |
| } |