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 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 CSVRenderer implements Renderer {
14      public String render(Report report) {
15          StringBuffer buf = new StringBuffer(quoteAndCommify("Problem"));
16          buf.append(quoteAndCommify("Package"));
17          buf.append(quoteAndCommify("File"));
18          buf.append(quoteAndCommify("Line"));
19          buf.append(quoteAndCommify("Priority"));
20          buf.append(quoteAndCommify("Description"));
21          buf.append(quoteAndCommify("Rule set"));
22          buf.append(quote("Rule"));
23          buf.append(PMD.EOL);
24  
25          int violationCount = 1;
26          for (Iterator i = report.iterator(); i.hasNext();) {
27              RuleViolation rv = (RuleViolation) i.next();
28              buf.append(quoteAndCommify(Integer.toString(violationCount)));
29              buf.append(quoteAndCommify(rv.getPackageName()));
30              buf.append(quoteAndCommify(rv.getFilename()));
31              buf.append(quoteAndCommify(Integer.toString(rv.getRule().getPriority())));
32              buf.append(quoteAndCommify(Integer.toString(rv.getLine())));
33              buf.append(quoteAndCommify(StringUtil.replaceString(rv.getDescription(), '\"', "'")));
34              buf.append(quoteAndCommify(rv.getRule().getRuleSetName()));
35              buf.append(quote(rv.getRule().getName()));
36              buf.append(PMD.EOL);
37              violationCount++;
38          }
39          return buf.toString();
40      }
41  
42      private String quote(String d) {
43          return "\"" + d + "\"";
44      }
45  
46      private String quoteAndCommify(String d) {
47          return quote(d) + ",";
48      }
49  
50  }