Search in sources :

Example 1 with StaxParser

use of org.sonar.cxx.sensors.utils.StaxParser in project sonar-cxx by SonarOpenCommunity.

the class CxxOtherSensor method processReport.

@Override
public void processReport(File report) {
    try {
        var parser = new StaxParser((SMHierarchicCursor rootCursor) -> {
            rootCursor.advance();
            SMInputCursor errorCursor = rootCursor.childElementCursor("error");
            while (errorCursor.getNext() != null) {
                String file = errorCursor.getAttrValue("file");
                String line = errorCursor.getAttrValue("line");
                String column = errorCursor.getAttrValue("column");
                String id = errorCursor.getAttrValue("id");
                String msg = errorCursor.getAttrValue("msg");
                var issue = new CxxReportIssue(id, file, line, column, msg);
                saveUniqueViolation(issue);
            }
        });
        parser.parse(report);
    } catch (XMLStreamException e) {
        throw new InvalidReportException("The 'other' report is invalid", e);
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) SMHierarchicCursor(org.codehaus.staxmate.in.SMHierarchicCursor) XMLStreamException(javax.xml.stream.XMLStreamException) StaxParser(org.sonar.cxx.sensors.utils.StaxParser) InvalidReportException(org.sonar.cxx.sensors.utils.InvalidReportException) CxxReportIssue(org.sonar.cxx.utils.CxxReportIssue)

Example 2 with StaxParser

use of org.sonar.cxx.sensors.utils.StaxParser in project sonar-cxx by SonarOpenCommunity.

the class CoberturaParser method parse.

/**
 * {@inheritDoc}
 */
@Override
public Map<String, CoverageMeasures> parse(File report) {
    var coverageData = new HashMap<String, CoverageMeasures>();
    try {
        baseDir = Paths.get(".");
        var sourceParser = new StaxParser((SMHierarchicCursor rootCursor) -> {
            try {
                rootCursor.advance();
            } catch (com.ctc.wstx.exc.WstxEOFException e) {
                throw new EmptyReportException("Coverage report " + report + " result is empty (parsed by " + this + ")", e);
            }
            readBaseDir(rootCursor.descendantElementCursor("source"));
        });
        sourceParser.parse(report);
        var packageParser = new StaxParser((SMHierarchicCursor rootCursor) -> {
            rootCursor.advance();
            collectPackageMeasures(rootCursor.descendantElementCursor("package"), coverageData);
        });
        packageParser.parse(report);
    } catch (XMLStreamException e) {
        throw new InvalidReportException("Cobertura coverage report '" + report + "' cannot be parsed.", e);
    }
    return coverageData;
}
Also used : SMHierarchicCursor(org.codehaus.staxmate.in.SMHierarchicCursor) EmptyReportException(org.sonar.cxx.sensors.utils.EmptyReportException) XMLStreamException(javax.xml.stream.XMLStreamException) HashMap(java.util.HashMap) StaxParser(org.sonar.cxx.sensors.utils.StaxParser) InvalidReportException(org.sonar.cxx.sensors.utils.InvalidReportException)

Example 3 with StaxParser

use of org.sonar.cxx.sensors.utils.StaxParser in project sonar-cxx by SonarOpenCommunity.

the class VisualStudioParser method parse.

/**
 * {@inheritDoc}
 */
@Override
public Map<String, CoverageMeasures> parse(File report) {
    var coverageData = new HashMap<String, CoverageMeasures>();
    try {
        var parser = new StaxParser((SMHierarchicCursor rootCursor) -> {
            try {
                rootCursor.advance();
            } catch (com.ctc.wstx.exc.WstxEOFException e) {
                throw new EmptyReportException("Coverage report " + report + " result is empty (parsed by " + this + ")", e);
            }
            collectModuleMeasures(rootCursor.descendantElementCursor("module"), coverageData);
        });
        parser.parse(report);
    } catch (XMLStreamException e) {
        throw new InvalidReportException("Visual Studio coverage report '" + report + "' cannot be parsed.", e);
    }
    return coverageData;
}
Also used : SMHierarchicCursor(org.codehaus.staxmate.in.SMHierarchicCursor) EmptyReportException(org.sonar.cxx.sensors.utils.EmptyReportException) XMLStreamException(javax.xml.stream.XMLStreamException) HashMap(java.util.HashMap) StaxParser(org.sonar.cxx.sensors.utils.StaxParser) InvalidReportException(org.sonar.cxx.sensors.utils.InvalidReportException)

Example 4 with StaxParser

use of org.sonar.cxx.sensors.utils.StaxParser in project sonar-cxx by SonarOpenCommunity.

the class BullseyeParser method parse.

/**
 * {@inheritDoc}
 */
@Override
public Map<String, CoverageMeasures> parse(File report) {
    var coverageData = new HashMap<String, CoverageMeasures>();
    try {
        var topLevelparser = new StaxParser((SMHierarchicCursor rootCursor) -> {
            try {
                rootCursor.advance();
            } catch (com.ctc.wstx.exc.WstxEOFException e) {
                throw new EmptyReportException("Coverage report " + report + " result is empty (parsed by " + this + ")", e);
            }
            collectCoverageLeafNodes(rootCursor.getAttrValue("dir"), rootCursor.childElementCursor("src"), coverageData);
        });
        var parser = new StaxParser((SMHierarchicCursor rootCursor) -> {
            rootCursor.advance();
            collectCoverage2(rootCursor.getAttrValue("dir"), rootCursor.childElementCursor("folder"), coverageData);
        });
        topLevelparser.parse(report);
        parser.parse(report);
    } catch (XMLStreamException e) {
        throw new InvalidReportException("Bullseye coverage report '" + report + "' cannot be parsed.", e);
    }
    return coverageData;
}
Also used : SMHierarchicCursor(org.codehaus.staxmate.in.SMHierarchicCursor) EmptyReportException(org.sonar.cxx.sensors.utils.EmptyReportException) XMLStreamException(javax.xml.stream.XMLStreamException) HashMap(java.util.HashMap) StaxParser(org.sonar.cxx.sensors.utils.StaxParser) InvalidReportException(org.sonar.cxx.sensors.utils.InvalidReportException)

Example 5 with StaxParser

use of org.sonar.cxx.sensors.utils.StaxParser in project sonar-cxx by SonarOpenCommunity.

the class XunitReportParserTest method shouldThrowWhenGivenInvalidTime.

@Test(expected = javax.xml.stream.XMLStreamException.class)
public void shouldThrowWhenGivenInvalidTime() throws javax.xml.stream.XMLStreamException {
    parserHandler = new XunitReportParser("");
    parser = new StaxParser(parserHandler, false);
    File report = TestUtils.loadResource(pathPrefix + "invalid-time-xunit-report.xml");
    parser.parse(report);
}
Also used : StaxParser(org.sonar.cxx.sensors.utils.StaxParser) File(java.io.File) Test(org.junit.Test)

Aggregations

StaxParser (org.sonar.cxx.sensors.utils.StaxParser)10 XMLStreamException (javax.xml.stream.XMLStreamException)6 SMHierarchicCursor (org.codehaus.staxmate.in.SMHierarchicCursor)6 EmptyReportException (org.sonar.cxx.sensors.utils.EmptyReportException)5 InvalidReportException (org.sonar.cxx.sensors.utils.InvalidReportException)5 File (java.io.File)3 HashMap (java.util.HashMap)3 SMInputCursor (org.codehaus.staxmate.in.SMInputCursor)3 Test (org.junit.Test)3 CxxReportIssue (org.sonar.cxx.utils.CxxReportIssue)3 TreeMap (java.util.TreeMap)2 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Strings (org.assertj.core.util.Strings)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 TestUtils (org.sonar.cxx.sensors.utils.TestUtils)1