Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#95. jsonconfig #111

Merged
merged 5 commits into from
Oct 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ dependencies {
compile group: 'com.github.cfparser', name: 'cfml.parsing', version:'2.0.0'
compile group: 'com.github.cfparser', name: 'cfml.dictionary', version:'2.0.0'
compile group: 'junit', name: 'junit', version:'4.11'
compile group: 'com.googlecode.json-simple', name: 'json-simple', version:'1.1'
compile group: 'org.jdom', name: 'jdom', version:'1.1.3'
compile group: 'org.antlr', name: 'antlr4-runtime', version:'4.5'
compile group: 'org.antlr', name: 'stringtemplate', version:'4.0.2'
Expand All @@ -50,6 +49,8 @@ dependencies {
compile group: 'org.slf4j', name: 'slf4j-log4j12', version:'1.7.5'
compile group: 'ant', name: 'ant', version:'1.7.0'
compile group: 'com.sun.xml.bind', name: 'jaxb-impl', version:'2.1.8'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version:'2.6.3'
compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-jaxb-annotations', version:'2.6.3'
runtime group: 'commons-logging', name: 'commons-logging-api', version:'1.1'
runtime group: 'org.slf4j', name: 'slf4j-api', version:'1.7.5'
runtime group: 'org.javolution', name: 'javolution', version:'5.2.6'
Expand Down
21 changes: 13 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom</artifactId>
Expand Down Expand Up @@ -164,10 +157,22 @@
<version>2.1.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.6.3</version>
</dependency>


<!-- dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
</dependency-->
</dependencies>

<build>
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/cflint/CFLint.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ public class CFLint implements IErrorReporter {
ConfigRuntime configuration;
private Stack<Element> currentElement = new Stack<Element>();

public CFLint() {
public CFLint() throws IOException {
this((CFLintConfig)null);
}
public CFLint(CFLintConfig configFile) {
public CFLint(CFLintConfig configFile) throws IOException {
final CFLintPluginInfo pluginInfo = ConfigUtils.loadDefaultPluginInfo();
configuration = new ConfigRuntime(configFile,pluginInfo);
for(PluginInfoRule ruleInfo:configuration.getRules()){
Expand Down Expand Up @@ -145,8 +145,14 @@ public CFLint(ConfigRuntime configuration,final CFLintScanner... bugsScanners) {
}
}
}
final CFLintFilter filter = CFLintFilter.createFilter(verbose);
bugs = new BugList(filter);
CFLintFilter filter;
try {
filter = CFLintFilter.createFilter(verbose);
bugs = new BugList(filter);
} catch (IOException e1) {
e1.printStackTrace();
bugs = new BugList(null);
}
if(exceptionListeners.size() == 0){
addExceptionListener(new DefaultCFLintExceptionListener(bugs));
}
Expand Down
52 changes: 26 additions & 26 deletions src/main/java/com/cflint/JSONOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,48 @@
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;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

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();
JsonFactory jsonF = new JsonFactory();
JsonGenerator jg = jsonF.createGenerator(writer);
jg.writeStartArray();
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);
jg.writeStartObject();
jg.writeStringField("severity", notNull(bugEntry.getValue().get(0).getSeverity()));
jg.writeStringField("id", bugEntry.getValue().get(0).getMessageCode());
jg.writeStringField("message", bugEntry.getValue().get(0).getMessageCode());
jg.writeStringField("category", "CFLINT");
jg.writeStringField("abbrev", abbrev(bugEntry.getValue().get(0).getMessageCode()));
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);
jg.writeObjectFieldStart("location");
jg.writeStringField("file",notNull(bugInfo.getFilename()));
jg.writeStringField("fileName",filename(bugInfo.getFilename()));
jg.writeStringField("column",Integer.valueOf(bugInfo.getColumn()).toString());
jg.writeStringField("line",Integer.valueOf(bugInfo.getLine()).toString());
jg.writeStringField("message",notNull(bugInfo.getMessage()));
jg.writeStringField("variable",notNull(bugInfo.getVariable()));
jg.writeStringField("expression",notNull(bugInfo.getExpression()));
jg.writeEndObject();
prevbugInfo = bugInfo;
bugInfo = iterator.hasNext() ? iterator.next() : null;
} while (isGrouped(prevbugInfo, bugInfo));
issues.add(issue);
jg.writeEndObject();
}
}
issues.writeJSONString(writer);
jg.writeEndArray();
jg.close();
writer.close();
}

Expand Down Expand Up @@ -91,14 +91,14 @@ public void outputFindBugs(final BugList bugList, final Writer writer) throws IO
output(bugList, sw);
}

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

private CharSequence abbrev(final String messageCode) {
private String abbrev(final String messageCode) {
if (messageCode.length() <= 2) {
return messageCode;
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/cflint/ant/CFLintTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ public void execute() {
try {
CFLintConfig config = null;
if(configFile != null){
config = ConfigUtils.unmarshal(new FileInputStream(configFile), CFLintConfig.class);
if(configFile.getName().toLowerCase().endsWith(".xml")){
config = ConfigUtils.unmarshal(new FileInputStream(configFile), CFLintConfig.class);
}else{
config = ConfigUtils.unmarshalJson(new FileInputStream(configFile), CFLintConfig.class);
}
}

final CFLint cflint = new CFLint(config);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/cflint/config/CFLintPluginInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
import javax.xml.bind.annotation.XmlTransient;

import com.cflint.plugins.CFLintScanner;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

@XmlRootElement(name = "CFLint-Plugin")
@JsonInclude(Include.NON_NULL)
public class CFLintPluginInfo {

List<PluginInfoRule> rules = new ArrayList<CFLintPluginInfo.PluginInfoRule>();
Expand All @@ -33,6 +37,7 @@ public PluginInfoRule getRuleByName(String ruleName){
return null;
}

@JsonInclude(Include.NON_NULL)
public static class PluginInfoRule {

String name;
Expand Down
120 changes: 94 additions & 26 deletions src/main/java/com/cflint/config/ConfigUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.cflint.config;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;

Expand All @@ -13,69 +15,135 @@
import com.cflint.config.CFLintPluginInfo.PluginInfoRule;
import com.cflint.config.CFLintPluginInfo.PluginInfoRule.PluginParameter;
import com.cflint.plugins.CFLintScanner;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;

public class ConfigUtils {

static JAXBContext CFLintConfigContext = null;
public static Marshaller createMarshaller() throws JAXBException{
if(CFLintConfigContext == null){

public static Marshaller createMarshaller() throws JAXBException {
if (CFLintConfigContext == null) {
init();
}
return CFLintConfigContext.createMarshaller();
}
public static Unmarshaller createUnmarshaller() throws JAXBException{
if(CFLintConfigContext == null){

private static Unmarshaller createUnmarshaller() throws JAXBException {
if (CFLintConfigContext == null) {
init();
}
return CFLintConfigContext.createUnmarshaller();
}

protected static synchronized void init() throws JAXBException {
CFLintConfigContext = JAXBContext.newInstance(CFLintPluginInfo.class,CFLintConfig.class);
CFLintConfigContext = JAXBContext.newInstance(CFLintPluginInfo.class,
CFLintConfig.class);
}
public static String marshal(Object obj) throws JAXBException{

public static String marshal(Object obj) throws JAXBException {
StringWriter sw = new StringWriter();
createMarshaller().marshal(obj, sw);
return sw.toString();
}
public static String marshalQuietly(Object obj){
try{

public static String marshalQuietly(Object obj) {
try {
return marshal(obj);
}catch(JAXBException e){return null;}
} catch (JAXBException e) {
return null;
}
}

@SuppressWarnings("unchecked")
public static <E> E unmarshal(String xml,Class<E> expectedClass) throws JAXBException{
StringWriter sw = new StringWriter();
public static <E> E unmarshal(String xml, Class<E> expectedClass)
throws JAXBException {
return (E) createUnmarshaller().unmarshal(new StringReader(xml));
}

@SuppressWarnings("unchecked")
public static <E> E unmarshal(InputStream inputStream,Class<E> expectedClass) throws JAXBException{
public static <E> E unmarshal(InputStream inputStream,
Class<E> expectedClass) throws JAXBException {
return (E) createUnmarshaller().unmarshal(
new InputStreamReader(inputStream));
}

public static String marshalJson(Object obj) throws JsonGenerationException, JsonMappingException, IOException {
StringWriter sw = new StringWriter();
return (E) createUnmarshaller().unmarshal(new InputStreamReader(inputStream));
ObjectMapper objectMapper = new ObjectMapper();
JaxbAnnotationModule module = new JaxbAnnotationModule();
objectMapper.registerModule(module);
objectMapper.writeValue(sw, obj);
return sw.toString();
}

@SuppressWarnings("unchecked")
public static <E> E unmarshalJson(InputStream inputStream,
Class<E> expectedClass) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
JaxbAnnotationModule module = new JaxbAnnotationModule();
objectMapper.registerModule(module);
// AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
// mapper.setAnnotationIntrospector(introspector);
return objectMapper.readValue(inputStream, expectedClass);
}

public static CFLintPluginInfo loadDefaultPluginInfo(){
final InputStream inputStream = ConfigUtils.class.getResourceAsStream("/cflint.definition.xml");
if(inputStream != null){
try{
return unmarshal(inputStream,CFLintPluginInfo.class);
}catch(JAXBException e){
@SuppressWarnings("unchecked")
public static <E> E unmarshalJson(Reader reader,
Class<E> expectedClass) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper objectMapper = new ObjectMapper();
JaxbAnnotationModule module = new JaxbAnnotationModule();
objectMapper.registerModule(module);
return objectMapper.readValue(reader, expectedClass);
}

/**
* Load the plugin definitions.
* If it is available use the json definition file first.
*
* @return CFLintPluginInfo instance of plugin definitions
*/
public static CFLintPluginInfo loadDefaultPluginInfo() {
final InputStream jsonInputStream = ConfigUtils.class
.getResourceAsStream("/cflint.definition.json");
if (jsonInputStream != null) {
try {
return unmarshalJson(jsonInputStream, CFLintPluginInfo.class);
} catch (IOException e) {
e.printStackTrace(System.err);
}
}

final InputStream inputStream = ConfigUtils.class
.getResourceAsStream("/cflint.definition.xml");
if (inputStream != null) {
try {
return unmarshal(inputStream, CFLintPluginInfo.class);
} catch (JAXBException e) {
e.printStackTrace(System.err);
}
}
return new CFLintPluginInfo();
}

static final String PLUGIN_PACKAGE = "com.cflint.plugins.core";

public static CFLintScanner loadPlugin(final PluginInfoRule ruleInfo) {
final String shortClassName = ruleInfo.getClassName() != null && ruleInfo.getClassName().trim().length() > 0?
ruleInfo.getClassName():ruleInfo.getName();
final String shortClassName = ruleInfo.getClassName() != null
&& ruleInfo.getClassName().trim().length() > 0 ? ruleInfo
.getClassName() : ruleInfo.getName();
final String className = PLUGIN_PACKAGE + "." + shortClassName.trim();
try {
final Class<?> pluginClass = Class.forName(className);
final CFLintScanner plugin= (CFLintScanner)pluginClass.newInstance();
for(PluginParameter param: ruleInfo.getParameters()){
final CFLintScanner plugin = (CFLintScanner) pluginClass
.newInstance();
for (PluginParameter param : ruleInfo.getParameters()) {
plugin.setParameter(param.getName(), param.getValue());
}
ruleInfo.setPluginInstance(plugin);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/cflint/main/CFLintMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,12 @@ private void ui() {
private void execute() throws IOException, TransformerException, JAXBException {
CFLintConfig config = null;
if(configfile != null){
config = ConfigUtils.unmarshal(new FileInputStream(configfile), CFLintConfig.class);
if(configfile.toLowerCase().endsWith(".xml")){
config = ConfigUtils.unmarshal(new FileInputStream(configfile), CFLintConfig.class);
}else{
config = ConfigUtils.unmarshalJson(new FileInputStream(configfile), CFLintConfig.class);
}

}
final CFLint cflint = new CFLint(config);
cflint.setVerbose(verbose);
Expand Down
Loading