View Javadoc

    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</str<a href="../../../../../ong> StringBuffer packageBuf = <strong>new</strong> StringBuffer()/package-summary.html">ong> StringBuffer packageBuf = new StringBuffer();
    24      private StringBuffer classBuf = new StringBuffer();
    25      private int length;
    26
    27      /***
    28       * Writes the buffer to file.
    29       */
    30      private void write(String filename, StringBuffer buf) throws IOException {
    31          String fs = System.getProperty("file.separator");
    32          String baseDir = ".." + fs; // TODO output destination
    33
    34          BufferedWriter bw = new BufferedWriter(new FileWriter(new File(baseDir + fs + filename)));
    35          bw.write(buf.toString(), 0, buf.length());
    36          bw.close();
    37      }
    38
    39      /***
    40       * Generates a html table with violation information.
    41       */
    42      private String displayRuleViolation(IRuleViolation vio) {
    43          String ret = "<table border=\"0\">";
    44
    45          ret += "<tr><td><b>Rule:</b></td><td>" + vio.getRule().getName() + "</td></tr>";
    46
    47          ret += "<tr><td><b>Description:</b></td><td>" + vio.getDescription() + "</td></tr>";
    48
    49          if (vio.getVariableName().length() > 0) {
    50              ret += "<tr><td><b>Variable:</b></td><td>" + vio.getVariableName() + "</td></tr>";
    51          }
    52
    53          if (vio.getEndLine() > 0) {
    54              ret += "<tr><td><b>Line:</b></td><td>" + vio.getEndLine() + " and " + vio.getBeginLine() + "</td></tr>";
    55          } else {
    56              ret += "<tr><td><b>Line:</b></td><td>" + vio.getBeginLine() + "</td></tr>";
    57          }
    58
    59          ret += "</table>";
    60          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      public void visit(AbstractReportNode node) {
    70
    71          /*
    72           * The first node of result tree.
    73           */
    74          if (node.getParent() == null) {
    75              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              this</st<a href="../../../../../rong>/length = <strong>this</strong>/packageBuf/length()/package-summary.html">rong>.length = this.packageBuf.length();
    90          }
    91
    92
    93          super.visit(node);
    94
    95
    96          if (node instanceof ViolationNode) {
    97              ViolationNode vnode = (ViolationNode) node;
    98              vnode.getParent().addNumberOfViolation(1);
    99              IRuleViolation vio = vnode.getRuleViolation();
    100             classBuf.append("<tr>" +
    101                     " <td>" + vio.getMethodName() + "</td>" +
    102                     " <td>" + this.displayRuleViolation(vio) + "</td>" +
    103                     "</tr>");
    104         }
    105         if (node instanceof ClassNode) {
    106             ClassNode cnode = (ClassNode) node;
    107             String str = cnode.getClassName();
    108
    109             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             classBuf.append("</table>" +
    120                     " </body>" +
    121                     "</html>");
    122
    123
    124             try {
    125                 this.write(str + ".html", classBuf);
    126             } catch (Exception e) {
    127                 throw new RuntimeException("Error while writing HTML report: " + e.getMessage());
    128             }
    129             classBuf = new StringBuffer();
    130
    131
    132             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             node.getParent().addNumberOfViolation(node.getNumberOfViolations());
    139         }
    140         if (node instanceof PackageNode) {
    141             PackageNode pnode = (PackageNode) node;
    142             String str;
    143
    144             // rootNode
    145             if (node.getParent() == null) {
    146                 str = "Aggregate";
    147             } else {           // all the other nodes
    148                 str = pnode.getPackageName();
    149                 node.getParent().addNumberOfViolation(node.getNumberOfViolations());
    150             }
    151
    152             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         if (node.getParent() == null) {
    160             this.packageBuf.append("</table> </body></html>");
    161             try {
    162                 this.write("index.html", this.packageBuf);
    163             } catch (Exception e) {
    164                 throw new RuntimeException("Error while writing HTML report: " + e.getMessage());
    165             }
    166         }
    167     }
    168 }