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