-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ac58cc
commit 60fa8a9
Showing
15 changed files
with
686 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/java/se/bjurr/violations/lib/parsers/JCReportParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package se.bjurr.violations.lib.parsers; | ||
|
||
import static com.google.common.collect.Lists.newArrayList; | ||
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; | ||
import static se.bjurr.violations.lib.model.SEVERITY.ERROR; | ||
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.getIntegerAttribute; | ||
import static se.bjurr.violations.lib.reports.Reporter.JCREPORT; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
import javax.xml.stream.XMLInputFactory; | ||
import javax.xml.stream.XMLStreamReader; | ||
|
||
import se.bjurr.violations.lib.model.SEVERITY; | ||
import se.bjurr.violations.lib.model.Violation; | ||
|
||
public class JCReportParser implements ViolationsParser { | ||
|
||
@Override | ||
public List<Violation> parseFile(String string) throws Exception { | ||
List<Violation> violations = newArrayList(); | ||
|
||
try (InputStream input = new ByteArrayInputStream(string.getBytes())) { | ||
|
||
XMLInputFactory factory = XMLInputFactory.newInstance(); | ||
XMLStreamReader xmlr = factory.createXMLStreamReader(input); | ||
|
||
String name = null; | ||
String findingType = null; | ||
Integer line = null; | ||
String message = null; | ||
String origin = null; | ||
String severity = null; | ||
while (xmlr.hasNext()) { | ||
int eventType = xmlr.next(); | ||
if (eventType == START_ELEMENT) { | ||
if (xmlr.getLocalName().equals("file")) { | ||
name = getAttribute(xmlr, "name"); | ||
} | ||
if (xmlr.getLocalName().equals("item")) { | ||
findingType = getAttribute(xmlr, "finding-type"); | ||
line = getIntegerAttribute(xmlr, "line"); | ||
message = getAttribute(xmlr, "message"); | ||
origin = getAttribute(xmlr, "origin"); | ||
severity = getAttribute(xmlr, "severity"); | ||
Violation violation = violationBuilder()// | ||
.setReporter(JCREPORT)// | ||
.setFile(name)// | ||
.setMessage(message)// | ||
.setRule(findingType + "(" + origin + ")")// | ||
.setSeverity(toSeverity(severity))// | ||
.setStartLine(line)// | ||
.build(); | ||
violations.add(violation); | ||
} | ||
} | ||
} | ||
} | ||
return violations; | ||
} | ||
|
||
private SEVERITY toSeverity(String severity) { | ||
if (severity.equals("error")) { | ||
return ERROR; | ||
} | ||
if (severity.equals("cpd")) { | ||
return ERROR; | ||
} | ||
if (severity.equals("warning")) { | ||
return WARN; | ||
} | ||
if (severity.equals("design")) { | ||
return WARN; | ||
} | ||
if (severity.equals("code-style")) { | ||
return INFO; | ||
} | ||
return INFO; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/se/bjurr/violations/lib/parsers/SimianParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package se.bjurr.violations.lib.parsers; | ||
|
||
import static com.google.common.collect.Lists.newArrayList; | ||
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; | ||
import static se.bjurr.violations.lib.model.SEVERITY.ERROR; | ||
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.getIntegerAttribute; | ||
import static se.bjurr.violations.lib.reports.Reporter.SIMIAN; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
import javax.xml.stream.XMLInputFactory; | ||
import javax.xml.stream.XMLStreamReader; | ||
|
||
import se.bjurr.violations.lib.model.SEVERITY; | ||
import se.bjurr.violations.lib.model.Violation; | ||
|
||
public class SimianParser implements ViolationsParser { | ||
|
||
@Override | ||
public List<Violation> parseFile(String string) throws Exception { | ||
List<Violation> violations = newArrayList(); | ||
|
||
try (InputStream input = new ByteArrayInputStream(string.getBytes())) { | ||
|
||
XMLInputFactory factory = XMLInputFactory.newInstance(); | ||
XMLStreamReader xmlr = factory.createXMLStreamReader(input); | ||
|
||
String sourceFile = null; | ||
Integer lineCount = null; | ||
Integer startLineNumber = null; | ||
Integer endLineNumber = null; | ||
while (xmlr.hasNext()) { | ||
int eventType = xmlr.next(); | ||
if (eventType == START_ELEMENT) { | ||
if (xmlr.getLocalName().equals("set")) { | ||
lineCount = getIntegerAttribute(xmlr, "lineCount"); | ||
} | ||
if (xmlr.getLocalName().equals("block")) { | ||
sourceFile = getAttribute(xmlr, "sourceFile"); | ||
startLineNumber = getIntegerAttribute(xmlr, "startLineNumber"); | ||
endLineNumber = getIntegerAttribute(xmlr, "endLineNumber"); | ||
|
||
Violation violation = violationBuilder()// | ||
.setReporter(SIMIAN)// | ||
.setFile(sourceFile)// | ||
.setMessage("Duplication")// | ||
.setRule("DUPLICATION")// | ||
.setSeverity(toSeverity(lineCount))// | ||
.setStartLine(startLineNumber)// | ||
.setEndLine(endLineNumber)// | ||
.build(); | ||
violations.add(violation); | ||
} | ||
} | ||
} | ||
} | ||
return violations; | ||
} | ||
|
||
private SEVERITY toSeverity(Integer lineCount) { | ||
if (lineCount < 10) { | ||
return INFO; | ||
} | ||
if (lineCount < 50) { | ||
return WARN; | ||
} | ||
return ERROR; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/se/bjurr/violations/lib/parsers/ZPTLintParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package se.bjurr.violations.lib.parsers; | ||
|
||
import static com.google.common.collect.Lists.newArrayList; | ||
import static se.bjurr.violations.lib.model.SEVERITY.ERROR; | ||
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.ZPTLINT; | ||
|
||
import java.util.List; | ||
|
||
import se.bjurr.violations.lib.model.Violation; | ||
|
||
public class ZPTLintParser implements ViolationsParser { | ||
|
||
@Override | ||
public List<Violation> parseFile(String string) throws Exception { | ||
List<Violation> violations = newArrayList(); | ||
for (List<String> parts : getLines(string, "[ ]+Error in: (.*) (.*) , at line (\\d+).*")) { | ||
if (parts.size() < 3) { | ||
continue; | ||
} | ||
Integer lineInFile = Integer.parseInt(parts.get(3)); | ||
String message = parts.get(2); | ||
String fileName = parts.get(1); | ||
Violation violation = violationBuilder()// | ||
.setReporter(ZPTLINT)// | ||
.setFile(fileName)// | ||
.setMessage(message)// | ||
.setRule("ZPT")// | ||
.setSeverity(ERROR)// | ||
.setStartLine(lineInFile)// | ||
.build(); | ||
violations.add(violation); | ||
} | ||
return violations; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package se.bjurr.violations.lib; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static se.bjurr.violations.lib.TestUtils.getRootFolder; | ||
import static se.bjurr.violations.lib.ViolationsReporterApi.violationsReporterApi; | ||
import static se.bjurr.violations.lib.model.SEVERITY.INFO; | ||
import static se.bjurr.violations.lib.reports.Reporter.JCREPORT; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
import se.bjurr.violations.lib.model.Violation; | ||
|
||
public class JCReportTest { | ||
@Test | ||
public void testThatViolationsCanBeParsed() { | ||
String rootFolder = getRootFolder(); | ||
|
||
List<Violation> actual = violationsReporterApi() // | ||
.withPattern(".*/jcreport/.*\\.xml$") // | ||
.inFolder(rootFolder) // | ||
.findAll(JCREPORT) // | ||
.violations(); | ||
|
||
assertThat(actual)// | ||
.hasSize(54); | ||
|
||
assertThat(actual.get(0).getMessage())// | ||
.isEqualTo("Type Javadoc comment is missing an @author tag."); | ||
assertThat(actual.get(0).getFile())// | ||
.isEqualTo("D:/projects/fawkez/test/java/org/jcoderz/commons/logging/XmlPrinterTest.java"); | ||
assertThat(actual.get(0).getSeverity())// | ||
.isEqualTo(INFO); | ||
assertThat(actual.get(0).getRule().get())// | ||
.isEqualTo("CS_MISSING_TAG(Checkstyle)"); | ||
assertThat(actual.get(0).getStartLine())// | ||
.isEqualTo(50); | ||
assertThat(actual.get(0).getEndLine())// | ||
.isEqualTo(50); | ||
|
||
assertThat(actual.get(1).getMessage())// | ||
.isEqualTo("Class LogElementHandler should be declared as final."); | ||
} | ||
|
||
} |
Oops, something went wrong.