KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > renderers > CSVRenderer


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.IRuleViolation;
7 import net.sourceforge.pmd.PMD;
8 import net.sourceforge.pmd.Report;
9 import net.sourceforge.pmd.util.StringUtil;
10
11 import java.io.IOException JavaDoc;
12 import java.io.Writer JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 public class CSVRenderer extends AbstractRenderer {
16
17     public void render(Writer JavaDoc writer, Report report) throws IOException JavaDoc {
18         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(300);
19         quoteAndCommify(buf, "Problem");
20         quoteAndCommify(buf, "Package");
21         quoteAndCommify(buf, "File");
22         quoteAndCommify(buf, "Priority");
23         quoteAndCommify(buf, "Line");
24         quoteAndCommify(buf, "Description");
25         quoteAndCommify(buf, "Rule set");
26         quote(buf, "Rule");
27         buf.append(PMD.EOL);
28         writer.write(buf.toString());
29
30         addViolations(writer, report, buf);
31     }
32
33     private void addViolations(Writer JavaDoc writer, Report report, StringBuffer JavaDoc buf) throws IOException JavaDoc {
34         int violationCount = 1;
35         IRuleViolation rv;
36         for (Iterator JavaDoc i = report.iterator(); i.hasNext();) {
37             buf.setLength(0);
38             rv = (IRuleViolation) i.next();
39             quoteAndCommify(buf, Integer.toString(violationCount));
40             quoteAndCommify(buf, rv.getPackageName());
41             quoteAndCommify(buf, rv.getFilename());
42             quoteAndCommify(buf, Integer.toString(rv.getRule().getPriority()));
43             quoteAndCommify(buf, Integer.toString(rv.getBeginLine()));
44             quoteAndCommify(buf, StringUtil.replaceString(rv.getDescription(), '\"', "'"));
45             quoteAndCommify(buf, rv.getRule().getRuleSetName());
46             quote(buf, rv.getRule().getName());
47             buf.append(PMD.EOL);
48             writer.write(buf.toString());
49             violationCount++;
50         }
51     }
52
53     private void quote(StringBuffer JavaDoc sb, String JavaDoc d) {
54         sb.append('"').append(d).append('"');
55     }
56
57     private void quoteAndCommify(StringBuffer JavaDoc sb, String JavaDoc d) {
58         quote(sb, d);
59         sb.append(',');
60     }
61 }
62
Popular Tags