se.bjurr.violations.lib.parsers.CheckStyleParser 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.CHECKSTYLE;
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 static se.bjurr.violations.lib.util.ViolationParserUtils.getChunks;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getIntegerAttribute;
import java.util.ArrayList;
import java.util.List;
import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;
import se.bjurr.violations.lib.util.Optional;
public class CheckStyleParser implements ViolationsParser {
@Override
public List parseReportOutput(String string) throws Exception {
final List violations = new ArrayList<>();
final List files = getChunks(string, "");
for (final String fileChunk : files) {
final String filename = getAttribute(fileChunk, "name");
final List errors = getChunks(fileChunk, " ");
for (final String errorChunk : errors) {
final Integer line = getIntegerAttribute(errorChunk, "line");
final Optional column = findIntegerAttribute(errorChunk, "column");
final String severity = getAttribute(errorChunk, "severity");
final String message = getAttribute(errorChunk, "message");
final String rule = findAttribute(errorChunk, "source").orNull();
violations.add( //
violationBuilder() //
.setParser(CHECKSTYLE) //
.setStartLine(line) //
.setColumn(column.orNull()) //
.setFile(filename) //
.setSeverity(toSeverity(severity)) //
.setMessage(message) //
.setRule(rule) //
.build() //
);
}
}
return violations;
}
public SEVERITY toSeverity(String severity) {
if (severity.equalsIgnoreCase("ERROR")) {
return ERROR;
}
if (severity.equalsIgnoreCase("WARNING")) {
return WARN;
}
return INFO;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy