View Javadoc

    1   /**
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.renderers;
    5
    6   import java.io.IOException;
    7   import java.io.Writer;
    8   import java.util.Iterator;
    9
    10  import net.sourceforge.pmd.PMD;
    11  import net.sourceforge.pmd.Report;
    12  import net.sourceforge.pmd.RuleViolation;
    13
    14  /**
    15   * Renderer to another HTML format.
    16   * @author Vladimir
    17   */
    18  public class VBHTMLRenderer extends AbstractIncrementingRenderer {
    19
    20      public static final String NAME = "vbhtml";
    21
    22      public VBHTMLRenderer() {
    23      	super(NAME, "Vladimir Bossicard HTML format.");
    24      }
    25
    26      public String defaultFileExtension() { return "vb.html"; }
    27
    28      /**
    29       * {@inheritDoc}
    30       */
    31      @Override
    32      public void start() throws IOException {
    33      	getWriter().write(header());
    34      }
    35
    36      /**
    37       * {@inheritDoc}
    38       */
    39      @Override
    40      public void renderFileViolations(Iterator<RuleViolation> violations) throws IOException {
    41  		if (!violations.hasNext()) {
    42  		    return;
    43  		}
    44
    45  		Writer writer = getWriter();
    46  		StringBuilder sb = new StringBuilder(500);
    47  		String filename = null;
    48  		String lineSep = PMD.EOL;
    49
    50  		boolean colorize = false;
    51  		while (violations.hasNext()) {
    52  		    sb.setLength(0);
    53  		    RuleViolation rv = violations.next();
    54  			if (!rv.getFilename().equals(filename)) { // New File
    55  				if (filename != null) {
    56  				    sb.append("</table></br>");
    57  				    colorize = false;
    58  				}
    59  				filename = rv.getFilename();
    60  				sb.append("<table border=\"0\" width=\"80%\">");
    61  				sb.append("<tr id=TableHeader><td colspan=\"2\"><font class=title>&nbsp;").append(filename).append(
    62  					"</font></tr>");
    63  				sb.append(lineSep);
    64  			    }
    65
    66  		    if (colorize) {
    67  				sb.append("<tr id=RowColor1>");
    68  			    } else {
    69  				sb.append("<tr id=RowColor2>");
    70  			    }
    71
    72  		    colorize = !colorize;
    73  		    sb.append("<td width=\"50\" align=\"right\"><font class=body>" + rv.getBeginLine()
    74  			    + "&nbsp;&nbsp;&nbsp;</font></td>");
    75  		    sb.append("<td><font class=body>" + rv.getDescription() + "</font></td>");
    76  		    sb.append("</tr>");
    77  		    sb.append(lineSep);
    78  		    writer.write(sb.toString());
    79  			}
    80  		if (filename != null) {
    81  		    writer.write("</table>");
    82  		}
    83      }
    84
    85      /**
    86       * {@inheritDoc}
    87       */
    88      @Override
    89      public void end() throws IOException {
    90  		Writer writer = getWriter();
    91  		StringBuilder sb = new StringBuilder();
    92
    93  		writer.write("<br>");
    94
    95  		// output the problems
    96  		if (!errors.isEmpty()) {
    97  		    sb.setLength(0);
    98  		    sb.append("<table border=\"0\" width=\"80%\">");
    99  		    sb.append("<tr id=TableHeader><td><font class=title>&nbsp;Problems found</font></td></tr>");
    100 		    boolean colorize = false;
    101 		    for (Report.ProcessingError error : errors) {
    102 			if (colorize) {
    103 			    sb.append("<tr id=RowColor1>");
    104 			} else {
    105 			    sb.append("<tr id=RowColor2>");
    106 			}
    107 			colorize = !colorize;
    108 			sb.append("<td><font class=body>").append(error).append("\"</font></td></tr>");
    109 		    }
    110 		    sb.append("</table>");
    111 		    writer.write(sb.toString());
    112 		}
    113
    114 		writer.write(footer());
    115     }
    116
    117     private String header() {
    118     	StringBuilder sb = new StringBuilder(600);
    119 		sb.append("<html><head><title>PMD</title></head>");
    120 		sb.append("<style type=\"text/css\">");
    121 		sb.append("<!--" + PMD.EOL);
    122 		sb.append("body { background-color: white; font-family:verdana, arial, helvetica, geneva; font-size: 16px; font-style: italic; color: black; }"
    123 				+ PMD.EOL);
    124 		sb.append(".title { font-family: verdana, arial, helvetica,geneva; font-size: 12px; font-weight:bold; color: white; }"
    125 				+ PMD.EOL);
    126 		sb.append(".body { font-family: verdana, arial, helvetica, geneva; font-size: 12px; font-weight:plain; color: black; }"
    127 				+ PMD.EOL);
    128 		sb.append("#TableHeader { background-color: #003366; }" + PMD.EOL);
    129 		sb.append("#RowColor1 { background-color: #eeeeee; }" + PMD.EOL);
    130 		sb.append("#RowColor2 { background-color: white; }" + PMD.EOL);
    131 		sb.append("-->");
    132 		sb.append("</style>");
    133 		sb.append("<body><center>");
    134 		return sb.toString();
    135     }
    136
    137     private String footer() {
    138     	return "</center></body></html>" + PMD.EOL;
    139     }
    140
    141 }