01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd.renderers;
04:
05: import net.sourceforge.pmd.IRuleViolation;
06: import net.sourceforge.pmd.PMD;
07: import net.sourceforge.pmd.util.StringUtil;
08:
09: import java.io.IOException;
10: import java.io.Writer;
11: import java.util.Iterator;
12:
13: public class CSVRenderer extends OnTheFlyRenderer {
14:
15: private int violationCount = 1;
16:
17: public void start() throws IOException {
18: StringBuffer buf = new StringBuffer(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: getWriter().write(buf.toString());
29: }
30:
31: public void renderFileViolations(Iterator<IRuleViolation> violations)
32: throws IOException {
33: StringBuffer buf = new StringBuffer(300);
34: Writer writer = getWriter();
35:
36: IRuleViolation rv;
37: while (violations.hasNext()) {
38: buf.setLength(0);
39: rv = violations.next();
40: quoteAndCommify(buf, Integer.toString(violationCount));
41: quoteAndCommify(buf, rv.getPackageName());
42: quoteAndCommify(buf, rv.getFilename());
43: quoteAndCommify(buf, Integer.toString(rv.getRule()
44: .getPriority()));
45: quoteAndCommify(buf, Integer.toString(rv.getBeginLine()));
46: quoteAndCommify(buf, StringUtil.replaceString(rv
47: .getDescription(), '\"', "'"));
48: quoteAndCommify(buf, rv.getRule().getRuleSetName());
49: quote(buf, rv.getRule().getName());
50: buf.append(PMD.EOL);
51: writer.write(buf.toString());
52: violationCount++;
53: }
54: }
55:
56: public void end() throws IOException {
57: }
58:
59: private void quote(StringBuffer sb, String d) {
60: sb.append('"').append(d).append('"');
61: }
62:
63: private void quoteAndCommify(StringBuffer sb, String d) {
64: quote(sb, d);
65: sb.append(',');
66: }
67: }
|