Skip to content

Commit

Permalink
#62
Browse files Browse the repository at this point in the history
json output added.
  • Loading branch information
ryaneberly committed Jun 16, 2015
1 parent 113a542 commit 35807ca
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 3 deletions.
120 changes: 120 additions & 0 deletions src/main/java/com/cflint/JSONOutput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.cflint;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamSource;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class JSONOutput {

@SuppressWarnings("unchecked")
public void output(final BugList bugList, final Writer writer) throws IOException {
// final StringBuilder sb = new StringBuilder();

JSONArray issues = new JSONArray();
for (final Entry<String, List<BugInfo>> bugEntry : bugList.getBugList().entrySet()) {
final Iterator<BugInfo> iterator = bugEntry.getValue().iterator();
BugInfo bugInfo = iterator.hasNext() ? iterator.next() : null;
BugInfo prevbugInfo = null;

while (bugInfo != null) {
JSONObject issue = new JSONObject();
issue.put("severity", notNull(bugEntry.getValue().get(0).getSeverity()));
issue.put("id", bugEntry.getValue().get(0).getMessageCode());
issue.put("message", bugEntry.getValue().get(0).getMessageCode());
issue.put("category", "CFLINT");
issue.put("abbrev", abbrev(bugEntry.getValue().get(0).getMessageCode()));
final JSONArray locations = new JSONArray();
issue.put("locations", locations);
do {
final JSONObject location = new JSONObject();
location.put("file",notNull(bugInfo.getFilename()));
location.put("fileName",filename(bugInfo.getFilename()));
location.put("column",Integer.valueOf(bugInfo.getColumn()).toString());
location.put("line",Integer.valueOf(bugInfo.getLine()).toString());
location.put("message",notNull(bugInfo.getMessage()));
location.put("variable",notNull(bugInfo.getVariable()));
location.put("expression",notNull(bugInfo.getExpression()));
locations.add(location);
prevbugInfo = bugInfo;
bugInfo = iterator.hasNext() ? iterator.next() : null;
} while (isGrouped(prevbugInfo, bugInfo));
issues.add(issue);
}
}
issues.writeJSONString(writer);
writer.close();
}

List<String> CODE_GROUPBY_FUNCTION = Arrays.asList("PARSE_ERROR");

private boolean isGrouped(final BugInfo prevbugInfo, final BugInfo bugInfo) {
if (prevbugInfo == null || bugInfo == null) {
return false;
}
// Different message types are not grouped
if (!safeEquals(prevbugInfo.getMessageCode(), bugInfo.getMessageCode())) {
return false;
}
// Different files are not grouped
if (!safeEquals(prevbugInfo.getFilename(), bugInfo.getFilename())) {
return false;
}
// Some message codes (such as parse error are grouped at the function
// level
if (CODE_GROUPBY_FUNCTION.contains(bugInfo.getMessageCode())) {
if (safeEquals(prevbugInfo.getFunction(), bugInfo.getFunction())) {
return true;
}
}
return false;
}

private boolean safeEquals(final String a, final String b) {
return a != null && b != null && a.equals(b);
}

public void outputFindBugs(final BugList bugList, final Writer writer) throws IOException, TransformerException {
final StringWriter sw = new StringWriter();
output(bugList, sw);
}

private CharSequence filename(final String filename) {
if(filename == null){
return "";
}
return filename.substring(Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'))+1);
}

private CharSequence abbrev(final String messageCode) {
if (messageCode.length() <= 2) {
return messageCode;
}
final String[] ms = messageCode.split("_");
if (ms.length >= 2) {
return (ms[0].substring(0, 1) + ms[1].substring(0, 1)).toUpperCase();
} else {
return ms[0].substring(0, 2).toUpperCase();
}
}

private String notNull(String value){
if (value == null){
return "";
}else{
return value;
}
}
}
5 changes: 4 additions & 1 deletion src/main/java/com/cflint/XMLOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ public void outputFindBugs(final BugList bugList, final Writer writer) throws IO
}

private CharSequence filename(final String filename) {
return filename.substring(Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\')));
if(filename == null){
return "";
}
return filename.substring(Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'))+1);
}

private CharSequence abbrev(final String messageCode) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/cflint/config/CFLintConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static class ConfigOutput {
OutputText text;
OutputXML html;
OutputXML xml;
OutputText json;

public String getName() {
return name;
Expand All @@ -72,11 +73,18 @@ public void setName(String name) {
public OutputText getText() {
return text;
}
public OutputText getJSON() {
return json;
}

@XmlElement(name="text")
public void setText(OutputText text) {
this.text = text;
}
@XmlElement(name="json")
public void setJSON(OutputText text) {
this.text = text;
}

public OutputXML getHtml() {
return html;
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/com/cflint/main/CFLintMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.cflint.CFLint;
import com.cflint.HTMLOutput;
import com.cflint.JSONOutput;
import com.cflint.TextOutput;
import com.cflint.Version;
import com.cflint.XMLOutput;
Expand All @@ -41,12 +42,14 @@ public class CFLintMain {
boolean logerror = false;
boolean quiet = false;
boolean xmlOutput = false;
boolean jsonOutput = false;
boolean htmlOutput = true;
boolean textOutput = false;
String xmlOutFile = "cflint-result.xml";
String xmlstyle = "cflint";
String htmlOutFile = "cflint-result.html";
String htmlStyle = "plain.xsl";
String jsonOutFile = "cflint-result.json";
String textOutFile = null;
String[] includeCodes = null;
String[] excludeCodes = null;
Expand Down Expand Up @@ -86,6 +89,8 @@ public static void main(final String[] args) throws ParseException, IOException,
options.addOption("html", false, "output in html format (default)");
options.addOption("htmlfile", true, "specify the output html file (default: cflint-results.html)");
options.addOption("htmlstyle", true, "default,plain");// fancy,fancy-hist,summary
options.addOption("json", false, "output in json format");
options.addOption("jsonfile", true, "specify the output json file (default: cflint-results.json)");
options.addOption("text", false, "output in plain text");
options.addOption("textfile", true, "specify the output text file (default: cflint-results.txt)");
options.addOption("extensions", true, "specify the extensions of the CF source files (default: .cfm,.cfc)");
Expand All @@ -108,6 +113,7 @@ public static void main(final String[] args) throws ParseException, IOException,
main.quiet = (cmd.hasOption('q') || cmd.hasOption("quiet"));
main.logerror = (cmd.hasOption('e') || cmd.hasOption("logerror"));
main.xmlOutput = cmd.hasOption("xml") || cmd.hasOption("xmlstyle") || cmd.hasOption("xmlfile");
main.jsonOutput = cmd.hasOption("json") || cmd.hasOption("jsonfile");
main.textOutput = cmd.hasOption("text") || cmd.hasOption("textfile");
if (cmd.hasOption("ui")) {
main.ui();
Expand Down Expand Up @@ -138,6 +144,9 @@ public static void main(final String[] args) throws ParseException, IOException,
if (cmd.hasOption("xmlfile")) {
main.xmlOutFile = cmd.getOptionValue("xmlfile");
}
if (cmd.hasOption("jsonfile")) {
main.jsonOutFile = cmd.getOptionValue("jsonfile");
}
if (cmd.hasOption("configfile")) {
main.configfile = cmd.getOptionValue("configfile");
}
Expand Down Expand Up @@ -188,6 +197,10 @@ private void open() throws IOException {
Desktop.getDesktop().open(new File(htmlOutFile));
return;
}
if (jsonOutput) {
Desktop.getDesktop().open(new File(htmlOutFile));
return;
}
}

private void ui() {
Expand All @@ -201,7 +214,7 @@ private void ui() {
return;
}

final String[] slist = new String[] { "xml", "html", "text" };
final String[] slist = new String[] { "xml", "html", "text","txt","json" };
final JList list = new JList(slist);
JOptionPane.showMessageDialog(null, list, "Output Type", JOptionPane.PLAIN_MESSAGE);

Expand All @@ -214,9 +227,13 @@ private void ui() {
if (indx == 1) {
htmlOutput = true;
}
if (indx == 2) {
if (indx == 2||indx == 3) {
textOutput = true;
}
if (indx == 4) {
jsonOutput = true;
}

}
}

Expand Down Expand Up @@ -298,6 +315,12 @@ private void execute() throws IOException, TransformerException, JAXBException {
throw new IOException(e);
}
}
if (jsonOutput) {
if(verbose){
System.out.println("Writing " + jsonOutFile);
}
new JSONOutput().output(cflint.getBugs(), new FileWriter(jsonOutFile));
}
if (includeCodes != null) {
cflint.getBugs().getFilter().includeCode(includeCodes);
}
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/com/cflint/TestJSONOutput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.cflint;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

import org.junit.Before;
import org.junit.Test;

import com.cflint.tools.CFLintFilter;

public class TestJSONOutput {

private JSONOutput outputer;
private BugList bugList;
private Writer writer;
@Before
public void setUp(){
outputer = new JSONOutput();
bugList = new BugList(null);
writer = new StringWriter();
}
@Test
public void testOutput1() throws IOException {
BugInfo bugInfo = new BugInfo.BugInfoBuilder().setFunction("testf").setMessageCode("PARSE_ERROR").setFilename("c:\\temp\\test.cfc").build();
bugList.add(bugInfo);
outputer.output(bugList, writer);
assertEquals("[]",writer.toString());
}
}

0 comments on commit 35807ca

Please sign in to comment.