Search in sources :

Example 1 with SMInputCursor

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

the class XMLProfileParser method processParameters.

private static void processParameters(SMInputCursor propsCursor, Map<String, String> parameters) throws XMLStreamException {
    while (propsCursor.getNext() != null) {
        SMInputCursor propCursor = propsCursor.childElementCursor();
        String key = null;
        String value = null;
        while (propCursor.getNext() != null) {
            String nodeName = propCursor.getLocalName();
            if (StringUtils.equals("key", nodeName)) {
                key = StringUtils.trim(propCursor.collectDescendantText(false));
            } else if (StringUtils.equals("value", nodeName)) {
                value = StringUtils.trim(propCursor.collectDescendantText(false));
            }
        }
        if (key != null) {
            parameters.put(key, value);
        }
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor)

Example 2 with SMInputCursor

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

the class RulesDefinitionXmlLoader method processParameter.

private static ParamStruct processParameter(SMInputCursor ruleC) throws XMLStreamException {
    ParamStruct param = new ParamStruct();
    // BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT
    String keyAttribute = ruleC.getAttrValue("key");
    if (isNotBlank(keyAttribute)) {
        param.key = trim(keyAttribute);
    }
    // BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT
    String typeAttribute = ruleC.getAttrValue("type");
    if (isNotBlank(typeAttribute)) {
        param.type = RuleParamType.parse(typeAttribute);
    }
    SMInputCursor paramC = ruleC.childElementCursor();
    while (paramC.getNext() != null) {
        String propNodeName = paramC.getLocalName();
        String propText = nodeValue(paramC);
        if (equalsIgnoreCase("key", propNodeName)) {
            param.key = propText;
        } else if (equalsIgnoreCase("description", propNodeName)) {
            param.description = propText;
        } else if (equalsIgnoreCase("type", propNodeName)) {
            param.type = RuleParamType.parse(propText);
        } else if (equalsIgnoreCase("defaultValue", propNodeName)) {
            param.defaultValue = propText;
        }
    }
    return param;
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor)

Example 3 with SMInputCursor

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

the class DebtRulesXMLImporter method importXML.

public List<RuleDebt> importXML(Reader xml, ValidationMessages validationMessages) {
    List<RuleDebt> ruleDebts = newArrayList();
    try {
        SMInputFactory inputFactory = initStax();
        SMHierarchicCursor cursor = inputFactory.rootElementCursor(xml);
        // advance to <sqale>
        cursor.advance();
        SMInputCursor rootCursor = cursor.childElementCursor(CHARACTERISTIC);
        while (rootCursor.getNext() != null) {
            process(ruleDebts, validationMessages, rootCursor);
        }
        cursor.getStreamReader().closeCompletely();
    } catch (XMLStreamException e) {
        throw new IllegalStateException("XML is not valid", e);
    }
    return ruleDebts;
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) SMHierarchicCursor(org.codehaus.staxmate.in.SMHierarchicCursor) XMLStreamException(javax.xml.stream.XMLStreamException) SMInputFactory(org.codehaus.staxmate.SMInputFactory) RuleDebt(org.sonar.server.debt.DebtModelXMLExporter.RuleDebt)

Example 4 with SMInputCursor

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

the class QProfileBackuper method parseRuleActivations.

private List<RuleActivation> parseRuleActivations(SMInputCursor rulesCursor) throws XMLStreamException {
    List<RuleActivation> activations = Lists.newArrayList();
    Set<RuleKey> activatedKeys = Sets.newHashSet();
    List<RuleKey> duplicatedKeys = Lists.newArrayList();
    while (rulesCursor.getNext() != null) {
        SMInputCursor ruleCursor = rulesCursor.childElementCursor();
        String repositoryKey = null;
        String key = null;
        String severity = null;
        Map<String, String> parameters = Maps.newHashMap();
        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)) {
                severity = StringUtils.trim(ruleCursor.collectDescendantText(false));
            } else if (StringUtils.equals("parameters", nodeName)) {
                SMInputCursor propsCursor = ruleCursor.childElementCursor("parameter");
                readParameters(propsCursor, parameters);
            }
        }
        RuleKey ruleKey = RuleKey.of(repositoryKey, key);
        if (activatedKeys.contains(ruleKey)) {
            duplicatedKeys.add(ruleKey);
        }
        activatedKeys.add(ruleKey);
        RuleActivation activation = new RuleActivation(ruleKey);
        activation.setSeverity(severity);
        activation.setParameters(parameters);
        activations.add(activation);
    }
    if (!duplicatedKeys.isEmpty()) {
        throw new IllegalArgumentException("The quality profile cannot be restored as it contains duplicates for the following rules: " + RULE_KEY_JOINER.join(duplicatedKeys));
    }
    return activations;
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) RuleKey(org.sonar.api.rule.RuleKey)

Example 5 with SMInputCursor

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

the class QProfileBackuper method readParameters.

private void readParameters(SMInputCursor propsCursor, Map<String, String> parameters) throws XMLStreamException {
    while (propsCursor.getNext() != null) {
        SMInputCursor propCursor = propsCursor.childElementCursor();
        String key = null;
        String value = null;
        while (propCursor.getNext() != null) {
            String nodeName = propCursor.getLocalName();
            if (StringUtils.equals("key", nodeName)) {
                key = StringUtils.trim(propCursor.collectDescendantText(false));
            } else if (StringUtils.equals("value", nodeName)) {
                value = StringUtils.trim(propCursor.collectDescendantText(false));
            }
        }
        if (key != null) {
            parameters.put(key, value);
        }
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor)

Aggregations

SMInputCursor (org.codehaus.staxmate.in.SMInputCursor)22 XMLStreamException (javax.xml.stream.XMLStreamException)6 ArrayList (java.util.ArrayList)5 SMInputFactory (org.codehaus.staxmate.SMInputFactory)5 SMHierarchicCursor (org.codehaus.staxmate.in.SMHierarchicCursor)5 SonarException (org.sonar.api.utils.SonarException)3 HashMap (java.util.HashMap)2 InputFile (org.sonar.api.batch.fs.InputFile)2 RuleKey (org.sonar.api.rule.RuleKey)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 ElementFilter (org.codehaus.staxmate.in.ElementFilter)1 SMEvent (org.codehaus.staxmate.in.SMEvent)1 NewCoverage (org.sonar.api.batch.sensor.coverage.NewCoverage)1 RuleStatus (org.sonar.api.rule.RuleStatus)1 ActiveRule (org.sonar.api.rules.ActiveRule)1