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.Report;
    9   import net.sourceforge.pmd.util.StringUtil;
    10
    11  import java.io.IOException;
    12  import java.io.Writer;
    13  import java.util.Iterator;
    14
    15  public class CSVRenderer extends AbstractRenderer {
    16
    17      public void render(Writer writer, Report report) 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          writer.write(buf.toString());
    29
    30          addViolations(writer, report, buf);
    31      }
    32
    33  	private void addViolations(Writer writer, Report report, StringBuffer buf) throws IOException {
    34  		int violationCount = 1;
    35  		IRuleViolation rv;
    36          for (Iterator 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 sb, String d) {
    54          sb.append('"').append(d).append('"');
    55      }
    56
    57      private void quoteAndCommify(StringBuffer sb, String d) {
    58      	quote(sb, d);
    59          sb.append(',');
    60      }
    61  }