Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |
file stats: | LOC: | 168 |  | Methods: | 3
| NCLOC: | 115 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
ReportHTMLPrintVisitor.java | 0% | 0% | 0% | 0% |

1 |  | package net.sourceforge.pmd.dfa.report;
2 |  |
3 |  | import net.sourceforge.pmd.PMD;
4 |  | import net.sourceforge.pmd.RuleViolation;
5 |  | import net.sourceforge.pmd.IRuleViolation;
6 |  |
7 |  | import java.io.BufferedWriter;
8 |  | import java.io.File;
9 |  | import java.io.FileWriter;
10 |  | import java.io.IOException;
11 |  |
12 |  | /**
13 |  | * @author raik
14 |  | * <p/>
15 |  | * * Uses the generated result tree instead of the result list. The visitor
16 |  | * * traverses the tree and creates several html files. The "package view" file
17 |  | * * (index.html) displays an overview of packgages, classes and the number of
18 |  | * * rule violations they contain. All the other html files represent a class
19 |  | * * and show detailed information about the violations.
20 |  | */
21 |  | public class ReportHTMLPrintVisitor extends ReportVisitor {
22 |  |
23 |  | private StringBuffer packageBuf = new StringBuffer();
24 |  | private StringBuffer classBuf = new StringBuffer();
25 |  | private int length;
26 |  |
27 |  | /**
28 |  | * Writes the buffer to file.
29 |  | */
30 | 0 | private void write(String filename, StringBuffer buf) throws IOException {
31 | 0 | String fs = System.getProperty("file.separator");
32 | 0 | String baseDir = ".." + fs; // TODO output destination
33 |  |
34 | 0 | BufferedWriter bw = new BufferedWriter(new FileWriter(new File(baseDir + fs + filename)));
35 | 0 | bw.write(buf.toString(), 0, buf.length());
36 | 0 | bw.close();
37 |  | }
38 |  |
39 |  | /**
40 |  | * Generates a html table with violation information.
41 |  | */
42 | 0 | private String displayRuleViolation(IRuleViolation vio) {
43 | 0 | String ret = "<table border=\"0\">";
44 |  |
45 | 0 | ret += "<tr><td><b>Rule:</b></td><td>" + vio.getRule().getName() + "</td></tr>";
46 |  |
47 | 0 | ret += "<tr><td><b>Description:</b></td><td>" + vio.getDescription() + "</td></tr>";
48 |  |
49 | 0 | if (vio.getVariableName().length() > 0) {
50 | 0 | ret += "<tr><td><b>Variable:</b></td><td>" + vio.getVariableName() + "</td></tr>";
51 |  | }
52 |  |
53 | 0 | if (vio.getEndLine() > 0) {
54 | 0 | ret += "<tr><td><b>Line:</b></td><td>" + vio.getEndLine() + " and " + vio.getBeginLine() + "</td></tr>";
55 |  | } else {
56 | 0 | ret += "<tr><td><b>Line:</b></td><td>" + vio.getBeginLine() + "</td></tr>";
57 |  | }
58 |  |
59 | 0 | ret += "</table>";
60 | 0 | return ret;
61 |  | }
62 |  |
63 |  | /**
64 |  | * The visit method (Visitor Pattern). There are 3 types of ReportNodes:
65 |  | * RuleViolation - contains a RuleViolation, Class - represents a class and
66 |  | * contains the name of the class, Package - represents a package and
67 |  | * contains the name(s) of the package.
68 |  | */
69 | 0 | public void visit(AbstractReportNode node) {
70 |  |
71 |  | /*
72 |  | * The first node of result tree.
73 |  | */
74 | 0 | if (node.getParent() == null) {
75 | 0 | this.packageBuf.insert(0,
76 |  | "<html>" +
77 |  | " <head>" +
78 |  | " <title>PMD</title>" +
79 |  | " </head>" +
80 |  | " <body>" + PMD.EOL + "" +
81 |  | "<h2>Package View</h2>" +
82 |  | "<table border=\"1\" align=\"center\" cellspacing=\"0\" cellpadding=\"3\">" +
83 |  | " <tr>" + PMD.EOL + "" +
84 |  | "<th>Package</th>" +
85 |  | "<th>Class</th>" +
86 |  | "<th>#</th>" +
87 |  | " </tr>" + PMD.EOL);
88 |  |
89 | 0 | this.length = this.packageBuf.length();
90 |  | }
91 |  |
92 |  |
93 | 0 | super.visit(node);
94 |  |
95 |  |
96 | 0 | if (node instanceof ViolationNode) {
97 | 0 | ViolationNode vnode = (ViolationNode) node;
98 | 0 | vnode.getParent().addNumberOfViolation(1);
99 | 0 | IRuleViolation vio = vnode.getRuleViolation();
100 | 0 | classBuf.append("<tr>" +
101 |  | " <td>" + vio.getMethodName() + "</td>" +
102 |  | " <td>" + this.displayRuleViolation(vio) + "</td>" +
103 |  | "</tr>");
104 |  | }
105 | 0 | if (node instanceof ClassNode) {
106 | 0 | ClassNode cnode = (ClassNode) node;
107 | 0 | String str = cnode.getClassName();
108 |  |
109 | 0 | classBuf.insert(0,
110 |  | "<html><head><title>PMD - " + str + "</title></head><body>" + PMD.EOL + "" +
111 |  | "<h2>Class View</h2>" +
112 |  | "<h3 align=\"center\">Class: " + str + "</h3>" +
113 |  | "<table border=\"\" align=\"center\" cellspacing=\"0\" cellpadding=\"3\">" +
114 |  | " <tr>" + PMD.EOL + "" +
115 |  | "<th>Method</th>" +
116 |  | "<th>Violation</th>" +
117 |  | " </tr>" + PMD.EOL);
118 |  |
119 | 0 | classBuf.append("</table>" +
120 |  | " </body>" +
121 |  | "</html>");
122 |  |
123 |  |
124 | 0 | try {
125 | 0 | this.write(str + ".html", classBuf);
126 |  | } catch (Exception e) {
127 | 0 | throw new RuntimeException("Error while writing HTML report: " + e.getMessage());
128 |  | }
129 | 0 | classBuf = new StringBuffer();
130 |  |
131 |  |
132 | 0 | this.packageBuf.insert(this.length,
133 |  | "<tr>" +
134 |  | " <td>-</td>" +
135 |  | " <td><a href=\"" + str + ".html\">" + str + "</a></td>" +
136 |  | " <td>" + node.getNumberOfViolations() + "</td>" +
137 |  | "</tr>" + PMD.EOL);
138 | 0 | node.getParent().addNumberOfViolation(node.getNumberOfViolations());
139 |  | }
140 | 0 | if (node instanceof PackageNode) {
141 | 0 | PackageNode pnode = (PackageNode) node;
142 | 0 | String str;
143 |  |
144 |  | // rootNode
145 | 0 | if (node.getParent() == null) {
146 | 0 | str = "Aggregate";
147 |  | } else { // all the other nodes
148 | 0 | str = pnode.getPackageName();
149 | 0 | node.getParent().addNumberOfViolation(node.getNumberOfViolations());
150 |  | }
151 |  |
152 | 0 | this.packageBuf.insert(this.length,
153 |  | "<tr><td><b>" + str + "</b></td>" +
154 |  | " <td>-</td>" +
155 |  | " <td>" + node.getNumberOfViolations() + "</td>" +
156 |  | "</tr>" + PMD.EOL);
157 |  | }
158 |  | // The first node of result tree.
159 | 0 | if (node.getParent() == null) {
160 | 0 | this.packageBuf.append("</table> </body></html>");
161 | 0 | try {
162 | 0 | this.write("index.html", this.packageBuf);
163 |  | } catch (Exception e) {
164 | 0 | throw new RuntimeException("Error while writing HTML report: " + e.getMessage());
165 |  | }
166 |  | }
167 |  | }
168 |  | }