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.IRuleViolation;
    7   import net.sourceforge.pmd.PMD;
    8   import net.sourceforge.pmd.util.StringUtil;
    9
    10  import java.io.IOException;
    11  import java.io.Writer;
    12  import java.util.Iterator;
    13
    14  public class CSVRenderer extends OnTheFlyRenderer {
    15
    16      private int violationCount = 1;
    17
    18      public void start() throws IOException {
    19          StringBuffer buf = new StringBuffer(300);
    20          quoteAndCommify(buf, "Problem");
    21          quoteAndCommify(buf, "Package");
    22          quoteAndCommify(buf, "File");
    23          quoteAndCommify(buf, "Priority");
    24          quoteAndCommify(buf, "Line");
    25          quoteAndCommify(buf, "Description");
    26          quoteAndCommify(buf, "Rule set");
    27          quote(buf, "Rule");
    28          buf.append(PMD.EOL);
    29          getWriter().write(buf.toString());
    30      }
    31
    32      public void renderFileViolations(Iterator<IRuleViolation> violations) 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().getPriority()));
    44              quoteAndCommify(buf, Integer.toString(rv.getBeginLine()));
    45              quoteAndCommify(buf, StringUtil.replaceString(rv.getDescription(), '\"', "'"));
    46              quoteAndCommify(buf, rv.getRule().getRuleSetName());
    47              quote(buf, rv.getRule().getName());
    48              buf.append(PMD.EOL);
    49              writer.write(buf.toString());
    50              violationCount++;
    51          }
    52  	}
    53
    54      public void end() throws IOException {
    55      }
    56
    57      private void quote(StringBuffer sb, String d) {
    58          sb.append('"').append(d).append('"');
    59      }
    60
    61      private void quoteAndCommify(StringBuffer sb, String d) {
    62      	quote(sb, d);
    63          sb.append(',');
    64      }
    65  }