Search in sources :

Example 21 with SMInputCursor

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

the class QProfileParser method parseRuleActivations.

private static List<ImportedRule> parseRuleActivations(SMInputCursor rulesCursor) throws XMLStreamException {
    List<ImportedRule> activations = new ArrayList<>();
    Set<RuleKey> activatedKeys = new HashSet<>();
    List<RuleKey> duplicatedKeys = new ArrayList<>();
    while (rulesCursor.getNext() != null) {
        SMInputCursor ruleCursor = rulesCursor.childElementCursor();
        Map<String, String> parameters = new HashMap<>();
        String repositoryKey = null;
        String key = null;
        String templateKey = null;
        ImportedRule rule = new ImportedRule();
        while (ruleCursor.getNext() != null) {
            String nodeName = ruleCursor.getLocalName();
            if (StringUtils.equals(ATTRIBUTE_REPOSITORY_KEY, nodeName)) {
                repositoryKey = StringUtils.trim(ruleCursor.collectDescendantText(false));
            } else if (StringUtils.equals(ATTRIBUTE_KEY, nodeName)) {
                key = StringUtils.trim(ruleCursor.collectDescendantText(false));
            } else if (StringUtils.equals(ATTRIBUTE_TEMPLATE_KEY, nodeName)) {
                templateKey = StringUtils.trim(ruleCursor.collectDescendantText(false));
            } else if (StringUtils.equals(ATTRIBUTE_NAME, nodeName)) {
                rule.setName(StringUtils.trim(ruleCursor.collectDescendantText(false)));
            } else if (StringUtils.equals(ATTRIBUTE_TYPE, nodeName)) {
                rule.setType(StringUtils.trim(ruleCursor.collectDescendantText(false)));
            } else if (StringUtils.equals(ATTRIBUTE_DESCRIPTION, nodeName)) {
                rule.setDescription(StringUtils.trim(ruleCursor.collectDescendantText(false)));
            } else if (StringUtils.equals(ATTRIBUTE_PRIORITY, nodeName)) {
                rule.setSeverity(StringUtils.trim(ruleCursor.collectDescendantText(false)));
            } else if (StringUtils.equals(ATTRIBUTE_PARAMETERS, nodeName)) {
                SMInputCursor propsCursor = ruleCursor.childElementCursor(ATTRIBUTE_PARAMETER);
                readParameters(propsCursor, parameters);
                rule.setParameters(parameters);
            }
        }
        RuleKey ruleKey = RuleKey.of(repositoryKey, key);
        rule.setRuleKey(ruleKey);
        if (templateKey != null) {
            rule.setTemplateKey(RuleKey.of(repositoryKey, templateKey));
        }
        if (activatedKeys.contains(ruleKey)) {
            duplicatedKeys.add(ruleKey);
        }
        activatedKeys.add(ruleKey);
        activations.add(rule);
    }
    if (!duplicatedKeys.isEmpty()) {
        throw new IllegalArgumentException("The quality profile cannot be restored as it contains duplicates for the following rules: " + duplicatedKeys.stream().map(RuleKey::toString).filter(Objects::nonNull).collect(Collectors.joining(", ")));
    }
    return activations;
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) RuleKey(org.sonar.api.rule.RuleKey) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 22 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 || inputFile.language() == null) {
            numberOfUnknownFiles++;
            if (numberOfUnknownFiles <= MAX_STORED_UNKNOWN_FILE_PATHS) {
                firstUnknownFiles.add(filePath);
            }
            if (inputFile != null) {
                LOG.debug("Skipping file '{}' in the generic coverage report because it doesn't have a known language", filePath);
            }
            continue;
        }
        matchedFileKeys.add(inputFile.key());
        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 23 with SMInputCursor

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

the class QProfileBackuper method restore.

/**
   * @param reader     the XML backup
   * @param toProfileName the target profile. If <code>null</code>, then use the
   *                   lang and name declared in the backup
   */
public BulkChangeResult restore(Reader reader, @Nullable QProfileName toProfileName) {
    try {
        String profileLang = null;
        String profileName = null;
        List<RuleActivation> ruleActivations = Lists.newArrayList();
        SMInputFactory inputFactory = initStax();
        SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
        // <profile>
        rootC.advance();
        if (!rootC.getLocalName().equals("profile")) {
            throw new IllegalArgumentException("Backup XML is not valid. Root element must be <profile>.");
        }
        SMInputCursor cursor = rootC.childElementCursor();
        while (cursor.getNext() != null) {
            String nodeName = cursor.getLocalName();
            if (StringUtils.equals("name", nodeName)) {
                profileName = StringUtils.trim(cursor.collectDescendantText(false));
            } else if (StringUtils.equals("language", nodeName)) {
                profileLang = StringUtils.trim(cursor.collectDescendantText(false));
            } else if (StringUtils.equals("rules", nodeName)) {
                SMInputCursor rulesCursor = cursor.childElementCursor("rule");
                ruleActivations = parseRuleActivations(rulesCursor);
            }
        }
        QProfileName target = (QProfileName) ObjectUtils.defaultIfNull(toProfileName, new QProfileName(profileLang, profileName));
        return reset.reset(target, ruleActivations);
    } catch (XMLStreamException e) {
        throw new IllegalStateException("Fail to restore Quality profile backup", e);
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) SMHierarchicCursor(org.codehaus.staxmate.in.SMHierarchicCursor) XMLStreamException(javax.xml.stream.XMLStreamException) SMInputFactory(org.codehaus.staxmate.SMInputFactory)

Example 24 with SMInputCursor

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

the class DebtRulesXMLImporter method processProperty.

private static Property processProperty(ValidationMessages validationMessages, SMInputCursor cursor) throws XMLStreamException {
    SMInputCursor c = cursor.childElementCursor();
    String key = null;
    int value = 0;
    String textValue = null;
    while (c.getNext() != null) {
        String node = c.getLocalName();
        if (StringUtils.equals(node, PROPERTY_KEY)) {
            key = c.collectDescendantText().trim();
        } else if (StringUtils.equals(node, PROPERTY_VALUE)) {
            String s = c.collectDescendantText().trim();
            try {
                Double valueDouble = NumberUtils.createDouble(s);
                value = valueDouble.intValue();
            } catch (NumberFormatException ex) {
                validationMessages.addErrorText(String.format("Cannot import value '%s' for field %s - Expected a numeric value instead", s, key));
            }
        } else if (StringUtils.equals(node, PROPERTY_TEXT_VALUE)) {
            textValue = c.collectDescendantText().trim();
            textValue = "mn".equals(textValue) ? MINUTE : textValue;
        }
    }
    return new Property(key, value, textValue);
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor)

Example 25 with SMInputCursor

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

the class XMLRuleParser method processRule.

private static void processRule(Rule rule, SMInputCursor ruleC) throws XMLStreamException {
    /* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */
    String keyAttribute = ruleC.getAttrValue("key");
    if (StringUtils.isNotBlank(keyAttribute)) {
        rule.setKey(StringUtils.trim(keyAttribute));
    }
    /* BACKWARD COMPATIBILITY WITH DEPRECATED FORMAT */
    String priorityAttribute = ruleC.getAttrValue("priority");
    if (StringUtils.isNotBlank(priorityAttribute)) {
        rule.setSeverity(RulePriority.valueOf(StringUtils.trim(priorityAttribute)));
    }
    List<String> tags = Lists.newArrayList();
    SMInputCursor cursor = ruleC.childElementCursor();
    while (cursor.getNext() != null) {
        String nodeName = cursor.getLocalName();
        if (StringUtils.equalsIgnoreCase("name", nodeName)) {
            rule.setName(StringUtils.trim(cursor.collectDescendantText(false)));
        } else if (StringUtils.equalsIgnoreCase("description", nodeName)) {
            rule.setDescription(StringUtils.trim(cursor.collectDescendantText(false)));
        } else if (StringUtils.equalsIgnoreCase("key", nodeName)) {
            rule.setKey(StringUtils.trim(cursor.collectDescendantText(false)));
        } else if (StringUtils.equalsIgnoreCase("configKey", nodeName)) {
            rule.setConfigKey(StringUtils.trim(cursor.collectDescendantText(false)));
        } else if (StringUtils.equalsIgnoreCase("priority", nodeName)) {
            rule.setSeverity(RulePriority.valueOf(StringUtils.trim(cursor.collectDescendantText(false))));
        } else if (StringUtils.equalsIgnoreCase("cardinality", nodeName)) {
            rule.setCardinality(Cardinality.valueOf(StringUtils.trim(cursor.collectDescendantText(false))));
        } else if (StringUtils.equalsIgnoreCase("status", nodeName)) {
            rule.setStatus(StringUtils.trim(cursor.collectDescendantText(false)));
        } else if (StringUtils.equalsIgnoreCase("param", nodeName)) {
            processParameter(rule, cursor);
        } else if (StringUtils.equalsIgnoreCase("tag", nodeName)) {
            tags.add(StringUtils.trim(cursor.collectDescendantText(false)));
        }
    }
    if (Strings.isNullOrEmpty(rule.getKey())) {
        throw new SonarException("Node <key> is missing in <rule>");
    }
    rule.setTags(tags.toArray(new String[tags.size()]));
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) SonarException(org.sonar.api.utils.SonarException)

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