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