edu.hm.hafner.analysis.parser.ccm.CcmParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of analysis-model Show documentation
Show all versions of analysis-model Show documentation
This library provides a Java object model to read, aggregate, filter, and query static analysis reports.
It is used by Jenkins' warnings next generation plug-in to visualize the warnings of individual builds.
Additionally, this library is used by a GitHub action to autograde student software projects based on a given set of
metrics (unit tests, code and mutation coverage, static analysis warnings).
package edu.hm.hafner.analysis.parser.ccm;
import java.io.IOException;
import java.io.Reader;
import org.apache.commons.lang3.StringUtils;
import org.xml.sax.SAXException;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.IssueParser;
import edu.hm.hafner.analysis.ParsingException;
import edu.hm.hafner.analysis.ReaderFactory;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.SecureDigester;
import edu.hm.hafner.analysis.Severity;
/**
* A parser for CCM XML files.
*
* @author Bruno P. Kinoshita - http://www.kinoshita.eti.br
*/
public class CcmParser extends IssueParser {
private static final long serialVersionUID = -5172155190810975806L;
@Override
public Report parse(final ReaderFactory readerFactory) throws ParsingException {
Ccm report = parseCCMXmlFile(readerFactory);
return convert(report);
}
private Ccm parseCCMXmlFile(final ReaderFactory ccmXmlFile) {
SecureDigester digester = new SecureDigester(CcmParser.class);
String rootXPath = "ccm";
digester.addObjectCreate(rootXPath, Ccm.class);
digester.addSetProperties(rootXPath);
String fileMetric = "ccm/metric";
digester.addObjectCreate(fileMetric, Metric.class);
digester.addSetProperties(fileMetric);
digester.addBeanPropertySetter("ccm/metric/complexity");
digester.addBeanPropertySetter("ccm/metric/unit");
digester.addBeanPropertySetter("ccm/metric/classification");
digester.addBeanPropertySetter("ccm/metric/file");
digester.addBeanPropertySetter("ccm/metric/startLineNumber");
digester.addBeanPropertySetter("ccm/metric/endLineNumber");
digester.addSetNext(fileMetric, "addMetric", Metric.class.getName());
try (Reader reader = ccmXmlFile.create()) {
Ccm report = digester.parse(reader);
if (report == null) {
throw new ParsingException("Input stream is not a CCM file.");
}
return report;
}
catch (IOException | SAXException exception) {
throw new ParsingException(exception);
}
}
private Report convert(final Ccm collection) {
try (IssueBuilder issueBuilder = new IssueBuilder()) {
Report report = new Report();
for (Metric metric : collection.getMetrics()) {
Severity priority = calculateMetricPriority(metric);
String complexity = String.format("%s has a complexity of %d", metric.getUnit(),
metric.getComplexity());
issueBuilder.setSeverity(priority)
.setMessage(complexity)
.setCategory(metric.getClassification())
.setLineStart(metric.getStartLineNumber())
.setLineEnd(metric.getEndLineNumber())
.setFileName(metric.getFile());
report.add(issueBuilder.buildAndClean());
}
return report;
}
}
private Severity calculateMetricPriority(final Metric metric) {
if (isMetricHighPriority(metric)) {
return Severity.WARNING_HIGH;
}
else if (isMetricModeratePriority(metric)) {
return Severity.WARNING_NORMAL;
}
else {
return Severity.WARNING_LOW;
}
}
private boolean isMetricHighPriority(final Metric metric) {
String metricClassification = metric.getClassification();
if (StringUtils.contains(metricClassification, "high")) {
return true;
}
return "C".equals(metricClassification) || "D".equals(metricClassification)
|| "E".equals(metricClassification) || "F".equals(metricClassification);
}
private boolean isMetricModeratePriority(final Metric metric) {
String metricClassification = metric.getClassification();
if (StringUtils.contains(metricClassification, "moderate")) {
return true;
}
return "B".equals(metricClassification);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy