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 net.sourceforge.pmd.PMD; 7 import net.sourceforge.pmd.Report; 8 import net.sourceforge.pmd.RuleViolation; 9 import net.sourceforge.pmd.util.StringUtil; 10 11 import java.util.Iterator; 12 13 public class HTMLRenderer implements Renderer { 14 15 public String render(Report report) { 16 StringBuffer buf = new StringBuffer("<html><head><title>PMD</title></head><body>" + PMD.EOL + "<table align=\"center\" cellspacing=\"0\" cellpadding=\"3\"><tr>" + PMD.EOL + "<th>#</th><th>File</th><th>Line</th><th>Problem</th></tr>" + PMD.EOL); 17 buf.append(renderBody(report)); 18 buf.append("</table></body></html>"); 19 return buf.toString(); 20 } 21 22 public String renderBody(Report report) { 23 boolean colorize = true; 24 int violationCount = 1; 25 StringBuffer buf = new StringBuffer(); 26 for (Iterator i = report.iterator(); i.hasNext();) { 27 RuleViolation rv = (RuleViolation) i.next(); 28 buf.append("<tr"); 29 if (colorize) { 30 buf.append(" bgcolor=\"lightgrey\""); 31 colorize = false; 32 } else { 33 colorize = true; 34 } 35 buf.append("> " + PMD.EOL); 36 buf.append("<td align=\"center\">" + violationCount + "</td>" + PMD.EOL); 37 buf.append("<td width=\"*%\">" + rv.getFilename() + "</td>" + PMD.EOL); 38 buf.append("<td align=\"center\" width=\"5%\">" + Integer.toString(rv.getLine()) + "</td>" + PMD.EOL); 39 40 String d = rv.getDescription(); 41 d = StringUtil.replaceString(d, '&', "&"); 42 d = StringUtil.replaceString(d, '<', "<"); 43 d = StringUtil.replaceString(d, '>', ">"); 44 buf.append("<td width=\"*\">" + d + "</td>" + PMD.EOL); 45 46 buf.append("</tr>" + PMD.EOL); 47 48 violationCount++; 49 } 50 return buf.toString(); 51 } 52 }