Search in sources :

Example 16 with SMInputCursor

use of org.codehaus.staxmate.in.SMInputCursor in project sonarqube by SonarSource.

the class XMLProfileParser method processRules.

private void processRules(SMInputCursor rulesCursor, RulesProfile profile, ValidationMessages messages) throws XMLStreamException {
    Map<String, String> parameters = new HashMap<>();
    while (rulesCursor.getNext() != null) {
        SMInputCursor ruleCursor = rulesCursor.childElementCursor();
        String repositoryKey = null;
        String key = null;
        RulePriority priority = null;
        parameters.clear();
        while (ruleCursor.getNext() != null) {
            String nodeName = ruleCursor.getLocalName();
            if (StringUtils.equals("repositoryKey", nodeName)) {
                repositoryKey = StringUtils.trim(ruleCursor.collectDescendantText(false));
            } else if (StringUtils.equals("key", nodeName)) {
                key = StringUtils.trim(ruleCursor.collectDescendantText(false));
            } else if (StringUtils.equals("priority", nodeName)) {
                priority = RulePriority.valueOf(StringUtils.trim(ruleCursor.collectDescendantText(false)));
            } else if (StringUtils.equals("parameters", nodeName)) {
                SMInputCursor propsCursor = ruleCursor.childElementCursor("parameter");
                processParameters(propsCursor, parameters);
            }
        }
        Rule rule = ruleFinder.findByKey(repositoryKey, key);
        if (rule == null) {
            messages.addWarningText("Rule not found: " + ruleToString(repositoryKey, key));
        } else {
            ActiveRule activeRule = profile.activateRule(rule, priority);
            for (Map.Entry<String, String> entry : parameters.entrySet()) {
                if (rule.getParam(entry.getKey()) == null) {
                    messages.addWarningText("The parameter '" + entry.getKey() + "' does not exist in the rule: " + ruleToString(repositoryKey, key));
                } else {
                    activeRule.setParameter(entry.getKey(), entry.getValue());
                }
            }
        }
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) HashMap(java.util.HashMap) ActiveRule(org.sonar.api.rules.ActiveRule) RulePriority(org.sonar.api.rules.RulePriority) Rule(org.sonar.api.rules.Rule) ActiveRule(org.sonar.api.rules.ActiveRule) HashMap(java.util.HashMap) Map(java.util.Map)

Example 17 with SMInputCursor

use of org.codehaus.staxmate.in.SMInputCursor in project sonarqube by SonarSource.

the class GenericCoverageReportParser method parseFiles.

private void parseFiles(SMInputCursor fileCursor, SensorContext context) throws XMLStreamException {
    while (fileCursor.getNext() != null) {
        checkElementName(fileCursor, "file");
        String filePath = mandatoryAttribute(fileCursor, "path");
        InputFile inputFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasPath(filePath));
        if (inputFile == null) {
            numberOfUnknownFiles++;
            if (numberOfUnknownFiles <= MAX_STORED_UNKNOWN_FILE_PATHS) {
                firstUnknownFiles.add(filePath);
            }
            continue;
        }
        Preconditions.checkState(inputFile.language() != null, "Line %s of report refers to a file with an unknown language: %s", fileCursor.getCursorLocation().getLineNumber(), filePath);
        matchedFileKeys.add(inputFile.absolutePath());
        NewCoverage newCoverage = context.newCoverage().onFile(inputFile);
        SMInputCursor lineToCoverCursor = fileCursor.childElementCursor();
        while (lineToCoverCursor.getNext() != null) {
            parseLineToCover(lineToCoverCursor, newCoverage);
        }
        newCoverage.save();
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) NewCoverage(org.sonar.api.batch.sensor.coverage.NewCoverage) InputFile(org.sonar.api.batch.fs.InputFile)

Example 18 with SMInputCursor

use of org.codehaus.staxmate.in.SMInputCursor in project sonarqube by SonarSource.

the class GenericTestExecutionReportParser method parseTestCase.

private void parseTestCase(SMInputCursor cursor, MutableTestPlan testPlan) throws XMLStreamException {
    checkElementName(cursor, "testCase");
    MutableTestCase testCase = testPlan.addTestCase(mandatoryAttribute(cursor, NAME_ATTR));
    TestCase.Status status = TestCase.Status.OK;
    testCase.setDurationInMs(longValue(mandatoryAttribute(cursor, DURATION_ATTR), cursor, DURATION_ATTR, 0));
    SMInputCursor child = cursor.descendantElementCursor();
    if (child.getNext() != null) {
        String elementName = child.getLocalName();
        if (SKIPPED.equals(elementName)) {
            status = TestCase.Status.SKIPPED;
        } else if (FAILURE.equals(elementName)) {
            status = TestCase.Status.FAILURE;
        } else if (ERROR.equals(elementName)) {
            status = TestCase.Status.ERROR;
        }
        if (TestCase.Status.OK != status) {
            testCase.setMessage(mandatoryAttribute(child, MESSAGE_ATTR));
            testCase.setStackTrace(child.collectDescendantText());
        }
    }
    testCase.setStatus(status);
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) MutableTestCase(org.sonar.api.test.MutableTestCase) MutableTestCase(org.sonar.api.test.MutableTestCase) TestCase(org.sonar.api.test.TestCase)

Aggregations

SMInputCursor (org.codehaus.staxmate.in.SMInputCursor)18 XMLStreamException (javax.xml.stream.XMLStreamException)7 SMInputFactory (org.codehaus.staxmate.SMInputFactory)6 SMHierarchicCursor (org.codehaus.staxmate.in.SMHierarchicCursor)6 SonarException (org.sonar.api.utils.SonarException)3 ArrayList (java.util.ArrayList)2 XMLInputFactory (javax.xml.stream.XMLInputFactory)2 InputFile (org.sonar.api.batch.fs.InputFile)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NewCoverage (org.sonar.api.batch.sensor.coverage.NewCoverage)1 RuleKey (org.sonar.api.rule.RuleKey)1 RuleStatus (org.sonar.api.rule.RuleStatus)1 ActiveRule (org.sonar.api.rules.ActiveRule)1 Rule (org.sonar.api.rules.Rule)1 RulePriority (org.sonar.api.rules.RulePriority)1 MutableTestCase (org.sonar.api.test.MutableTestCase)1 MutableTestPlan (org.sonar.api.test.MutableTestPlan)1