Skip to content

Commit

Permalink
Refactoring, adding ViolationsParser interface
Browse files Browse the repository at this point in the history
 * Also preparing for FxCop.
  • Loading branch information
tomasbjerre committed Oct 1, 2016
1 parent 4f54260 commit adef3f6
Show file tree
Hide file tree
Showing 21 changed files with 564 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.findIntegerAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getChunks;
import static se.bjurr.violations.lib.reports.Reporter.ANDROIDLINT;

import java.io.File;
Expand All @@ -17,7 +20,7 @@
import com.google.common.base.Optional;
import com.google.common.io.Files;

public class AndroidLintParser extends ViolationsParser {
public class AndroidLintParser implements ViolationsParser {

@Override
public List<Violation> parseFile(File file) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getChunks;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getIntegerAttribute;
import static se.bjurr.violations.lib.reports.Reporter.CPPCHECK;

import java.io.File;
Expand All @@ -16,7 +19,7 @@
import com.google.common.base.Charsets;
import com.google.common.io.Files;

public class CPPCheckParser extends ViolationsParser {
public class CPPCheckParser implements ViolationsParser {

@Override
public List<Violation> parseFile(File file) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.findIntegerAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getChunks;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getIntegerAttribute;
import static se.bjurr.violations.lib.reports.Reporter.CSSLINT;

import java.io.File;
Expand All @@ -17,7 +21,7 @@
import com.google.common.base.Optional;
import com.google.common.io.Files;

public class CSSLintParser extends ViolationsParser {
public class CSSLintParser implements ViolationsParser {

@Override
public List<Violation> parseFile(File file) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.findIntegerAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getChunks;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getIntegerAttribute;
import static se.bjurr.violations.lib.reports.Reporter.CHECKSTYLE;

import java.io.File;
Expand All @@ -17,7 +21,7 @@
import com.google.common.base.Optional;
import com.google.common.io.Files;

public class CheckStyleParser extends ViolationsParser {
public class CheckStyleParser implements ViolationsParser {

@Override
public List<Violation> parseFile(File file) throws Exception {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/se/bjurr/violations/lib/parsers/CppLintParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
/**
* PyLint. Format used by Flake8.
*/
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getLines;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getParts;
import static se.bjurr.violations.lib.reports.Reporter.CPPLINT;

import java.io.File;
Expand All @@ -17,10 +22,7 @@

import com.google.common.io.Files;

/**
* PyLint. Format used by Flake8.
*/
public class CppLintParser extends ViolationsParser {
public class CppLintParser implements ViolationsParser {

@Override
public List<Violation> parseFile(File file) throws Exception {
Expand Down
123 changes: 64 additions & 59 deletions src/main/java/se/bjurr/violations/lib/parsers/FindbugsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
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 static se.bjurr.violations.lib.parsers.ViolationParserUtils.findIntegerAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getChunks;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getContent;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getIntegerAttribute;
import static se.bjurr.violations.lib.reports.Reporter.FINDBUGS;

import java.io.File;
Expand All @@ -16,19 +21,18 @@
import java.util.List;
import java.util.Map;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

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;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;

public class FindbugsParser extends ViolationsParser {
public class FindbugsParser implements ViolationsParser {

/**
* Severity rank.
Expand All @@ -45,14 +49,14 @@ public List<Violation> parseFile(File file) throws Exception {
List<Violation> violations = newArrayList();
Map<String, String> messagesPerType = getMessagesPerType();

try(InputStream input = new FileInputStream(file)) {
try (InputStream input = new FileInputStream(file)) {

XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader xmlr = factory.createXMLStreamReader(input);

while (xmlr.hasNext()) {
int eventType = xmlr.next();
if (eventType == XMLEvent.START_ELEMENT) {
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (xmlr.getLocalName().equals("BugInstance")) {
parseBugInstance(xmlr, violations, messagesPerType);
}
Expand All @@ -62,21 +66,41 @@ public List<Violation> parseFile(File file) throws Exception {
return violations;
}

private void parseBugInstance(XMLStreamReader xmlr, List<Violation> violations, Map<String, String> messagesPerType) throws XMLStreamException {
String type = getAttribute(xmlr,"type");
Integer rank = getIntegerAttribute(xmlr,"rank");
String message = messagesPerType.get(type);
if (message == null) {
message = type;
private Map<String, String> getMessagesPerType() {
Map<String, String> messagesPerType = newHashMap();
try {
if (isNullOrEmpty(findbugsMessagesXml)) {
findbugsMessagesXml = Resources.toString(getResource("findbugs/messages.xml"), UTF_8);
}
List<String> bugPatterns = getChunks(findbugsMessagesXml, "<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);
}
SEVERITY severity = toSeverity(rank);
} catch (IOException e) {
propagate(e);
}
return messagesPerType;
}

private void parseBugInstance(XMLStreamReader xmlr, List<Violation> violations, Map<String, String> messagesPerType)
throws XMLStreamException {
String type = getAttribute(xmlr, "type");
Integer rank = getIntegerAttribute(xmlr, "rank");
String message = messagesPerType.get(type);
if (message == null) {
message = type;
}
SEVERITY severity = toSeverity(rank);

List<Violation> candidates = newArrayList();

while(xmlr.hasNext()) {
while (xmlr.hasNext()) {
int eventType = xmlr.next();
if( eventType == XMLEvent.START_ELEMENT) {
if( xmlr.getLocalName().equals("SourceLine")) {
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (xmlr.getLocalName().equals("SourceLine")) {
Optional<Integer> startLine = findIntegerAttribute(xmlr, "start");
Optional<Integer> endLine = findIntegerAttribute(xmlr, "end");
if (!startLine.isPresent() || !endLine.isPresent()) {
Expand All @@ -85,35 +109,35 @@ private void parseBugInstance(XMLStreamReader xmlr, List<Violation> violations,
String filename = getAttribute(xmlr, "sourcepath");
String classname = getAttribute(xmlr, "classname");
candidates.add(//
violationBuilder()//
.setReporter(FINDBUGS)//
.setMessage(message)//
.setFile(filename)//
.setStartLine(startLine.get())//
.setEndLine(endLine.get())//
.setRule(type)//
.setSeverity(severity)//
.setSource(classname)//
.setSpecific(FINDBUGS_SPECIFIC_RANK, rank)//
.build()//
);
violationBuilder()//
.setReporter(FINDBUGS)//
.setMessage(message)//
.setFile(filename)//
.setStartLine(startLine.get())//
.setEndLine(endLine.get())//
.setRule(type)//
.setSeverity(severity)//
.setSource(classname)//
.setSpecific(FINDBUGS_SPECIFIC_RANK, rank)//
.build()//
);
}
}
if( eventType == XMLEvent.END_ELEMENT) {
if( xmlr.getLocalName().equals("BugInstance") ) {
if (eventType == XMLStreamConstants.END_ELEMENT) {
if (xmlr.getLocalName().equals("BugInstance")) {
// End of the bug instance.
break;
}
}
}

if (!candidates.isEmpty()) {
/**
* Last one is the most specific, first 2 may be class and method when the
* third is source line.
*/
violations.add(candidates.get(candidates.size() - 1));
}
if (!candidates.isEmpty()) {
/**
* Last one is the most specific, first 2 may be class and method when the
* third is source line.
*/
violations.add(candidates.get(candidates.size() - 1));
}

}

Expand All @@ -131,23 +155,4 @@ private SEVERITY toSeverity(Integer rank) {
return SEVERITY.INFO;
}

private Map<String, String> getMessagesPerType() {
Map<String, String> messagesPerType = newHashMap();
try {
if (isNullOrEmpty(findbugsMessagesXml)) {
findbugsMessagesXml = Resources.toString(getResource("findbugs/messages.xml"), UTF_8);
}
List<String> bugPatterns = getChunks(findbugsMessagesXml, "<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 @@ -8,6 +8,7 @@
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getLines;
import static se.bjurr.violations.lib.reports.Reporter.FLAKE8;

import java.io.File;
Expand All @@ -24,7 +25,7 @@
* msg-template='{path}:{line}:{column} [{msg_id}] {msg}'
* </code>
*/
public class Flake8Parser extends ViolationsParser {
public class Flake8Parser implements ViolationsParser {

@Override
public List<Violation> parseFile(File file) throws Exception {
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/se/bjurr/violations/lib/parsers/FxCopParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package se.bjurr.violations.lib.parsers;

import static com.google.common.collect.Lists.newArrayList;

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

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

public class FxCopParser implements ViolationsParser {

@Override
public List<Violation> parseFile(File file) throws Exception {
List<Violation> violations = newArrayList();
return violations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getChunks;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getIntegerAttribute;
import static se.bjurr.violations.lib.reports.Reporter.JSHINT;

import java.io.File;
Expand All @@ -16,7 +19,7 @@
import com.google.common.base.Charsets;
import com.google.common.io.Files;

public class JSHintParser extends ViolationsParser {
public class JSHintParser implements ViolationsParser {

@Override
public List<Violation> parseFile(File file) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.findIntegerAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getChunks;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getIntegerAttribute;
import static se.bjurr.violations.lib.reports.Reporter.LINT;

import java.io.File;
Expand All @@ -17,7 +21,7 @@
import com.google.common.base.Optional;
import com.google.common.io.Files;

public class LintParser extends ViolationsParser {
public class LintParser implements ViolationsParser {

@Override
public List<Violation> parseFile(File file) throws Exception {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/se/bjurr/violations/lib/parsers/PMDParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getChunks;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getIntegerAttribute;
import static se.bjurr.violations.lib.reports.Reporter.PMD;

import java.io.File;
Expand All @@ -16,7 +19,7 @@
import com.google.common.base.Charsets;
import com.google.common.io.Files;

public class PMDParser extends ViolationsParser {
public class PMDParser implements ViolationsParser {

@Override
public List<Violation> parseFile(File file) throws Exception {
Expand Down
Loading

0 comments on commit adef3f6

Please sign in to comment.