View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   
5   package net.sourceforge.pmd.renderers;
6   
7   import com.google.gson.Gson;
8   import net.sourceforge.pmd.Rule;
9   import net.sourceforge.pmd.RuleViolation;
10  
11  import java.io.IOException;
12  import java.io.Writer;
13  import java.util.Iterator;
14  
15  /**
16   * Renderer for Code Climate JSON format
17   */
18  public class CodeClimateRenderer extends AbstractIncrementingRenderer {
19      public static final String NAME = "codeclimate";
20  
21      protected static final String EOL = System.getProperty("line.separator", "\n");
22  
23      public CodeClimateRenderer() {
24          super(NAME, "Code Climate integration.");
25      }
26  
27      /**
28       * {@inheritDoc}
29       */
30      @Override
31      public void renderFileViolations(Iterator<RuleViolation> violations) throws IOException {
32          Writer writer = getWriter();
33          Gson gson = new Gson();
34          while (violations.hasNext()) {
35              RuleViolation rv = violations.next();
36              writer.write(gson.toJson(makeIssue(rv)) + EOL);
37          }
38      }
39  
40      /**
41       * Generate a CodeClimateIssue suitable for processing into JSON from the given RuleViolation.
42       * @param rv RuleViolation to convert.
43       * @return The generated issue.
44       */
45      private CodeClimateIssue makeIssue(RuleViolation rv) {
46          CodeClimateIssue issue = new CodeClimateIssue();
47          Rule rule = rv.getRule();
48          issue.check_name = rule.getName();
49          issue.description = rv.getDescription();
50          issue.content = new CodeClimateIssue.Content(rule.getDescription());
51          issue.location = new CodeClimateIssue.Location(rv.getFilename(), rv.getBeginLine(), rv.getEndLine());
52          switch(rule.getPriority()) {
53              case HIGH:
54                  issue.severity = "critical";
55                  break;
56              case MEDIUM_HIGH:
57              case MEDIUM:
58              case MEDIUM_LOW:
59                  issue.severity = "normal";
60                  break;
61              case LOW:
62                  issue.severity = "info";
63                  break;
64          }
65          return issue;
66      }
67  
68      @Override
69      public String defaultFileExtension() {
70          return "json";
71      }
72  }