Skip to content

Commit

Permalink
Findbugs parser
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Feb 21, 2016
1 parent d6e0920 commit 395483a
Show file tree
Hide file tree
Showing 6 changed files with 8,860 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/main/java/se/bjurr/violations/lib/model/Violation.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public ViolationBuilder setSpecific(String specificsKey, String specificsValue)
return this;
}

public ViolationBuilder setSpecific(String specificsKey, Integer specificsValue) {
this.specifics.put(specificsKey, Integer.toString(specificsValue));
return this;
}

public ViolationBuilder setEndLine(Integer endLine) {
this.endLine = endLine;
return this;
Expand Down Expand Up @@ -87,6 +92,7 @@ public ViolationBuilder setStartLine(Integer startLine) {
public Violation build() {
return new Violation(this);
}

}

private final Integer startLine;
Expand Down
101 changes: 101 additions & 0 deletions src/main/java/se/bjurr/violations/lib/parsers/FindbugsParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package se.bjurr.violations.lib.parsers;

import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newHashMap;
import static com.google.common.io.Resources.getResource;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;

import com.google.common.base.Optional;
import com.google.common.io.Files;
import com.google.common.io.Resources;

public class FindbugsParser extends ViolationsParser {

/**
* Severity rank.
*/
public static final String FINDBUGS_SPECIFIC_RANK = "RANK";

@Override
public List<Violation> parseFile(File file) throws Exception {
List<Violation> violations = newArrayList();
Map<String, String> messagesPerType = getMessagesPerType();
String string = Files.toString(file, UTF_8);
List<String> bugInstances = getChunks(string, "<BugInstance", "</BugInstance>");
for (String bugInstanceChunk : bugInstances) {
String type = getAttribute(bugInstanceChunk, "type");
Integer rank = getIntegerAttribute(bugInstanceChunk, "rank");
String message = messagesPerType.get(type);
if (message == null) {
message = type;
}
SEVERITY severity = toSeverity(rank);

List<String> sourceLineChunks = getChunks(bugInstanceChunk, "<SourceLine", "/>");
for (String sourceLineChunk : sourceLineChunks) {
Optional<Integer> startLine = findIntegerAttribute(sourceLineChunk, "start");
Optional<Integer> endLine = findIntegerAttribute(sourceLineChunk, "end");
if (!startLine.isPresent() || !endLine.isPresent()) {
continue;
}
String filename = getAttribute(sourceLineChunk, "sourcepath");
String classname = getAttribute(sourceLineChunk, "classname");
violations.add(//
violationBuilder()//
.setMessage(message)//
.setFile(filename)//
.setStartLine(startLine.get())//
.setEndLine(endLine.get())//
.setRule(type)//
.setSeverity(severity)//
.setSource(classname)//
.setSpecific(FINDBUGS_SPECIFIC_RANK, rank)//
.build()//
);
}
}
return violations;
}

/**
* Bugs are given a rank 1-20, and grouped into the categories scariest (rank
* 1-4), scary (rank 5-9), troubling (rank 10-14), and of concern (rank 15-20).
*/
private SEVERITY toSeverity(Integer rank) {
if (rank <= 9) {
return SEVERITY.ERROR;
}
if (rank <= 14) {
return SEVERITY.WARN;
}
return SEVERITY.INFO;
}

private Map<String, String> getMessagesPerType() {
Map<String, String> messagesPerType = newHashMap();
try {
String string = Resources.toString(getResource("findbugs/messages.xml"), UTF_8);
List<String> bugPatterns = getChunks(string, "<BugPattern", "</BugPattern>");
for (String bugPattern : bugPatterns) {
String type = getAttribute(bugPattern, "type");
String shortDescription = getContent(bugPattern, "ShortDescription");
String details = getContent(bugPattern, "Details");
messagesPerType.put(type, shortDescription + "\n\n" + details);
}
} catch (IOException e) {
propagate(e);
}
return messagesPerType;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ public static List<String> getChunks(String in, String includingStart, String in
return chunks;
}

public static String getContent(String in, String tag) {
Pattern pattern = Pattern.compile("<" + tag + ">[^<]*<!\\[CDATA\\[(" + ".+?" + ")\\]\\]>[^<]*</" + tag + ">", DOTALL);
Matcher matcher = pattern.matcher(in);
if (matcher.find()) {
return matcher.group(1);
}
pattern = Pattern.compile("<" + tag + ">(" + ".+?" + ")</" + tag + ">", DOTALL);
matcher = pattern.matcher(in);
if (matcher.find()) {
return matcher.group(1);
}
throw new RuntimeException("\"" + tag + "\" not found in " + in);
}

public abstract List<Violation> parseFile(File file) throws Exception;

}
4 changes: 3 additions & 1 deletion src/main/java/se/bjurr/violations/lib/reports/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
import se.bjurr.violations.lib.model.Violation;
import se.bjurr.violations.lib.parsers.CSSLintParser;
import se.bjurr.violations.lib.parsers.CheckStyleParser;
import se.bjurr.violations.lib.parsers.FindbugsParser;
import se.bjurr.violations.lib.parsers.ViolationsParser;

public enum Reporter {
CHECKSTYLE(new CheckStyleParser()), //
CSSLINT(new CSSLintParser());
CSSLINT(new CSSLintParser()), //
FINDBUGS(new FindbugsParser());

private static Logger LOG = Logger.getLogger(Reporter.class.getSimpleName());
private ViolationsParser violationsParser;
Expand Down
Loading

0 comments on commit 395483a

Please sign in to comment.