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