Search in sources :

Example 26 with SMInputCursor

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

the class XMLRuleParser method processParameter.

private static void processParameter(Rule rule, SMInputCursor ruleC) throws XMLStreamException {
    RuleParam param = rule.createParameter();
    String keyAttribute = ruleC.getAttrValue("key");
    if (StringUtils.isNotBlank(keyAttribute)) {
        /* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */
        param.setKey(StringUtils.trim(keyAttribute));
    }
    String typeAttribute = ruleC.getAttrValue("type");
    if (StringUtils.isNotBlank(typeAttribute)) {
        /* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */
        param.setType(type(StringUtils.trim(typeAttribute)));
    }
    SMInputCursor paramC = ruleC.childElementCursor();
    while (paramC.getNext() != null) {
        String propNodeName = paramC.getLocalName();
        String propText = StringUtils.trim(paramC.collectDescendantText(false));
        if (StringUtils.equalsIgnoreCase("key", propNodeName)) {
            param.setKey(propText);
        } else if (StringUtils.equalsIgnoreCase("description", propNodeName)) {
            param.setDescription(propText);
        } else if (StringUtils.equalsIgnoreCase("type", propNodeName)) {
            param.setType(type(propText));
        } else if (StringUtils.equalsIgnoreCase("defaultValue", propNodeName)) {
            param.setDefaultValue(propText);
        }
    }
    if (Strings.isNullOrEmpty(param.getKey())) {
        throw new SonarException("Node <key> is missing in <param>");
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) SonarException(org.sonar.api.utils.SonarException)

Example 27 with SMInputCursor

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

the class XMLRuleParser method parse.

public List<Rule> parse(Reader reader) {
    XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
    xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
    xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
    // just so it won't try to load DTD in if there's DOCTYPE
    xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
    SMInputFactory inputFactory = new SMInputFactory(xmlFactory);
    try {
        SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
        // <rules>
        rootC.advance();
        List<Rule> rules = new ArrayList<>();
        SMInputCursor rulesC = rootC.childElementCursor("rule");
        while (rulesC.getNext() != null) {
            // <rule>
            Rule rule = Rule.create();
            rules.add(rule);
            processRule(rule, rulesC);
        }
        return rules;
    } catch (XMLStreamException e) {
        throw new SonarException("XML is not valid", e);
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) SMHierarchicCursor(org.codehaus.staxmate.in.SMHierarchicCursor) XMLStreamException(javax.xml.stream.XMLStreamException) SonarException(org.sonar.api.utils.SonarException) SMInputFactory(org.codehaus.staxmate.SMInputFactory) ArrayList(java.util.ArrayList) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 28 with SMInputCursor

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

the class RulesDefinitionXmlLoader method processRule.

private static void processRule(RulesDefinition.NewRepository repo, SMInputCursor ruleC) throws XMLStreamException {
    String key = null;
    String name = null;
    String description = null;
    // enum is not used as variable type as we want to raise an exception with the rule key when format is not supported
    String descriptionFormat = DescriptionFormat.HTML.name();
    String internalKey = null;
    String severity = Severity.defaultSeverity();
    String type = null;
    RuleStatus status = RuleStatus.defaultStatus();
    boolean template = false;
    String gapDescription = null;
    String debtRemediationFunction = null;
    String debtRemediationFunctionGapMultiplier = null;
    String debtRemediationFunctionBaseEffort = null;
    List<ParamStruct> params = new ArrayList<>();
    List<String> tags = new ArrayList<>();
    /* BACKWARD COMPATIBILITY WITH VERY OLD FORMAT */
    String keyAttribute = ruleC.getAttrValue("key");
    if (isNotBlank(keyAttribute)) {
        key = trim(keyAttribute);
    }
    String priorityAttribute = ruleC.getAttrValue("priority");
    if (isNotBlank(priorityAttribute)) {
        severity = trim(priorityAttribute);
    }
    SMInputCursor cursor = ruleC.childElementCursor();
    while (cursor.getNext() != null) {
        String nodeName = cursor.getLocalName();
        if (equalsIgnoreCase("name", nodeName)) {
            name = nodeValue(cursor);
        } else if (equalsIgnoreCase("type", nodeName)) {
            type = nodeValue(cursor);
        } else if (equalsIgnoreCase("description", nodeName)) {
            description = nodeValue(cursor);
        } else if (equalsIgnoreCase("descriptionFormat", nodeName)) {
            descriptionFormat = nodeValue(cursor);
        } else if (equalsIgnoreCase("key", nodeName)) {
            key = nodeValue(cursor);
        } else if (equalsIgnoreCase("configKey", nodeName)) {
            // deprecated field, replaced by internalKey
            internalKey = nodeValue(cursor);
        } else if (equalsIgnoreCase("internalKey", nodeName)) {
            internalKey = nodeValue(cursor);
        } else if (equalsIgnoreCase("priority", nodeName) || equalsIgnoreCase("severity", nodeName)) {
            // "priority" is deprecated field and has been replaced by "severity"
            severity = nodeValue(cursor);
        } else if (equalsIgnoreCase("cardinality", nodeName)) {
            template = Cardinality.MULTIPLE == Cardinality.valueOf(nodeValue(cursor));
        } else if (equalsIgnoreCase("gapDescription", nodeName) || equalsIgnoreCase("effortToFixDescription", nodeName)) {
            gapDescription = nodeValue(cursor);
        } else if (equalsIgnoreCase("remediationFunction", nodeName) || equalsIgnoreCase("debtRemediationFunction", nodeName)) {
            debtRemediationFunction = nodeValue(cursor);
        } else if (equalsIgnoreCase("remediationFunctionBaseEffort", nodeName) || equalsIgnoreCase("debtRemediationFunctionOffset", nodeName)) {
            debtRemediationFunctionGapMultiplier = nodeValue(cursor);
        } else if (equalsIgnoreCase("remediationFunctionGapMultiplier", nodeName) || equalsIgnoreCase("debtRemediationFunctionCoefficient", nodeName)) {
            debtRemediationFunctionBaseEffort = nodeValue(cursor);
        } else if (equalsIgnoreCase("status", nodeName)) {
            String s = nodeValue(cursor);
            if (s != null) {
                status = RuleStatus.valueOf(s);
            }
        } else if (equalsIgnoreCase("param", nodeName)) {
            params.add(processParameter(cursor));
        } else if (equalsIgnoreCase("tag", nodeName)) {
            tags.add(nodeValue(cursor));
        }
    }
    try {
        RulesDefinition.NewRule rule = repo.createRule(key).setSeverity(severity).setName(name).setInternalKey(internalKey).setTags(tags.toArray(new String[tags.size()])).setTemplate(template).setStatus(status).setGapDescription(gapDescription);
        if (type != null) {
            rule.setType(RuleType.valueOf(type));
        }
        fillDescription(rule, descriptionFormat, description);
        fillRemediationFunction(rule, debtRemediationFunction, debtRemediationFunctionGapMultiplier, debtRemediationFunctionBaseEffort);
        fillParams(rule, params);
    } catch (Exception e) {
        throw new IllegalStateException(format("Fail to load the rule with key [%s:%s]", repo.key(), key), e);
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) ArrayList(java.util.ArrayList) RuleStatus(org.sonar.api.rule.RuleStatus) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException)

Example 29 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 30 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)42 XMLStreamException (javax.xml.stream.XMLStreamException)9 SMHierarchicCursor (org.codehaus.staxmate.in.SMHierarchicCursor)8 ArrayList (java.util.ArrayList)7 SMInputFactory (org.codehaus.staxmate.SMInputFactory)5 InputFile (org.sonar.api.batch.fs.InputFile)5 File (java.io.File)3 ElementFilter (org.codehaus.staxmate.in.ElementFilter)3 SonarException (org.sonar.api.utils.SonarException)3 StaxParser (org.sonar.cxx.sensors.utils.StaxParser)3 CxxReportIssue (org.sonar.cxx.utils.CxxReportIssue)3 HashMap (java.util.HashMap)2 RuleKey (org.sonar.api.rule.RuleKey)2 CoverageMeasures (org.sonar.cxx.sensors.coverage.CoverageMeasures)2 EmptyReportException (org.sonar.cxx.sensors.utils.EmptyReportException)2 InvalidReportException (org.sonar.cxx.sensors.utils.InvalidReportException)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1