se.bjurr.violations.lib.parsers.ResharperParser 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.RESHARPER;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;
import se.bjurr.violations.lib.util.Optional;
public class ResharperParser implements ViolationsParser {
@Override
public List parseReportOutput(String string) throws Exception {
List violations = new ArrayList<>();
List issueTypeChunks = getChunks(string, " ");
Map> issueTypesPerTypeId = new HashMap<>();
for (String issueTypesChunk : issueTypeChunks) {
Map issueType = new HashMap<>();
String id = getAttribute(issueTypesChunk, "Id");
issueType.put("category", getAttribute(issueTypesChunk, "Category"));
Optional description = findAttribute(issueTypesChunk, "Description");
issueType.put("description", description.or(id));
issueType.put("severity", getAttribute(issueTypesChunk, "Severity"));
issueType.put("url", findAttribute(issueTypesChunk, "WikiUrl").orNull());
issueTypesPerTypeId.put(id, issueType);
}
List issueChunks = getChunks(string, " ");
for (String issueChunk : issueChunks) {
String typeId = getAttribute(issueChunk, "TypeId");
String filename = getAttribute(issueChunk, "File");
String url = issueTypesPerTypeId.get(typeId).get("url");
String message =
getAttribute(issueChunk, "Message")
+ ". "
+ issueTypesPerTypeId.get(typeId).get("category")
+ ". "
+ issueTypesPerTypeId.get(typeId).get("description")
+ (url != null ? ". For more info, visit " + url : "");
Integer line = findIntegerAttribute(issueChunk, "Line").or(0);
String severity = issueTypesPerTypeId.get(typeId).get("severity");
violations.add( //
violationBuilder() //
.setParser(RESHARPER) //
.setStartLine(line) //
.setFile(filename) //
.setSeverity(toSeverity(severity)) //
.setMessage(message) //
.setRule(typeId) //
.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