se.bjurr.violations.lib.parsers.CSSLintParser 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 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.CSSLINT;
import static se.bjurr.violations.lib.util.ViolationParserUtils.findAttribute;
import static se.bjurr.violations.lib.util.ViolationParserUtils.findIntegerAttribute;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getAttribute;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import se.bjurr.violations.lib.ViolationsLogger;
import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;
import se.bjurr.violations.lib.util.ViolationParserUtils;
public class CSSLintParser implements ViolationsParser {
@Override
public Set parseReportOutput(
final String content, final ViolationsLogger violationsLogger) throws Exception {
final Set violations = new TreeSet<>();
try (InputStream input = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8))) {
final XMLStreamReader xmlr = ViolationParserUtils.createXmlReader(input);
String filename = null;
Integer line = null;
Optional charAttrib = null;
String severity = null;
String message = null;
String evidence = null;
while (xmlr.hasNext()) {
final int eventType = xmlr.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (xmlr.getLocalName().equalsIgnoreCase("file")) {
filename = getAttribute(xmlr, "name");
}
if (xmlr.getLocalName().equalsIgnoreCase("issue")) {
line = findIntegerAttribute(xmlr, "line").orElse(1);
charAttrib = findIntegerAttribute(xmlr, "char");
severity = getAttribute(xmlr, "severity");
message = getAttribute(xmlr, "reason");
evidence = findAttribute(xmlr, "evidence").orElse("").trim();
final Violation violation =
violationBuilder() //
.setParser(CSSLINT) //
.setStartLine(line) //
.setColumn(charAttrib.orElse(null)) //
.setFile(filename) //
.setSeverity(this.toSeverity(severity)) //
.setMessage(message + ": " + evidence) //
.build();
violations.add(violation);
}
}
}
}
return violations;
}
public SEVERITY toSeverity(final String severity) {
if (severity.equalsIgnoreCase("ERROR")) {
return ERROR;
}
if (severity.equalsIgnoreCase("WARNING")) {
return WARN;
}
return INFO;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy