se.bjurr.violations.lib.parsers.CodeNarcParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of violations-lib Show documentation
Show all versions of violations-lib Show documentation
Library for parsing report files from static code analyzis.
package se.bjurr.violations.lib.parsers;
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.reports.Parser.CODENARC;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getAttribute;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getIntegerAttribute;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 CodeNarcParser implements ViolationsParser {
private SEVERITY getSeverity(Integer from) {
if (from == 1) {
return ERROR;
}
if (from == 2) {
return WARN;
}
return INFO;
}
@Override
public List parseReportOutput(String string) throws Exception {
final List violations = new ArrayList<>();
final Map rules = new HashMap<>();
try (InputStream input = new ByteArrayInputStream(string.getBytes("UTF-8"))) {
final XMLInputFactory factory = XMLInputFactory.newInstance();
final XMLStreamReader xmlr = factory.createXMLStreamReader(input);
String name = null;
String description = null;
while (xmlr.hasNext()) {
final int eventType = xmlr.next();
if (eventType == START_ELEMENT) {
if (xmlr.getLocalName().equalsIgnoreCase("Rule")) {
name = getAttribute(xmlr, "name");
}
if (xmlr.getLocalName().equalsIgnoreCase("Description")) {
description = xmlr.getElementText().trim();
rules.put(name, description);
}
}
}
}
try (InputStream input = new ByteArrayInputStream(string.getBytes("UTF-8"))) {
final XMLInputFactory factory = XMLInputFactory.newInstance();
final XMLStreamReader xmlr = factory.createXMLStreamReader(input);
String path = null;
String name = null;
String ruleName = null;
Integer priority = null;
Integer lineNumber = null;
while (xmlr.hasNext()) {
final int eventType = xmlr.next();
if (eventType == START_ELEMENT) {
if (xmlr.getLocalName().equalsIgnoreCase("Package")) {
path = getAttribute(xmlr, "path");
}
if (xmlr.getLocalName().equalsIgnoreCase("File")) {
name = getAttribute(xmlr, "name");
}
if (xmlr.getLocalName().equalsIgnoreCase("Violation")) {
ruleName = getAttribute(xmlr, "ruleName");
priority = getIntegerAttribute(xmlr, "priority");
final String lineNumberString = getAttribute(xmlr, "lineNumber");
lineNumber = 1;
if (!lineNumberString.isEmpty()) {
lineNumber = Integer.parseInt(lineNumberString);
}
String message = rules.get(ruleName);
if (message == null) {
message = ruleName;
}
final Violation violation =
violationBuilder() //
.setParser(CODENARC) //
.setFile(path + "/" + name) //
.setMessage(message) //
.setRule(ruleName) //
.setSeverity(getSeverity(priority)) //
.setStartLine(lineNumber) //
.build();
violations.add(violation);
}
}
}
}
return violations;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy