Search in sources :

Example 1 with SMHierarchicCursor

use of org.codehaus.staxmate.in.SMHierarchicCursor 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 2 with SMHierarchicCursor

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

the class DuplicationsParser method parse.

public List<Block> parse(DbSession session, ComponentDto component, @Nullable String branch, @Nullable String pullRequest, @Nullable String duplicationsData) {
    Map<String, ComponentDto> componentsByKey = new LinkedHashMap<>();
    List<Block> blocks = new ArrayList<>();
    if (duplicationsData == null) {
        return blocks;
    }
    DuplicationComparator duplicationComparator = new DuplicationComparator(component.uuid(), component.projectUuid());
    try {
        SMInputFactory inputFactory = initStax();
        SMHierarchicCursor root = inputFactory.rootElementCursor(new StringReader(duplicationsData));
        // <duplications>
        root.advance();
        SMInputCursor cursor = root.childElementCursor("g");
        while (cursor.getNext() != null) {
            List<Duplication> duplications = new ArrayList<>();
            SMInputCursor bCursor = cursor.childElementCursor("b");
            while (bCursor.getNext() != null) {
                String from = bCursor.getAttrValue("s");
                String size = bCursor.getAttrValue("l");
                boolean disableLink = Boolean.parseBoolean(bCursor.getAttrValue("t"));
                String componentDbKey = bCursor.getAttrValue("r");
                if (from != null && size != null && componentDbKey != null) {
                    if (disableLink) {
                        // flag means that the target refers to an unchanged file in PRs that doesn't exist in DB.
                        // Display as text without a link or other details.
                        duplications.add(Duplication.newTextComponent(componentDbKey, Integer.valueOf(from), Integer.valueOf(size)));
                    } else {
                        duplications.add(createDuplication(componentsByKey, branch, pullRequest, from, size, componentDbKey, session));
                    }
                }
            }
            duplications.sort(duplicationComparator);
            blocks.add(new Block(duplications));
        }
        blocks.sort(BLOCK_COMPARATOR);
        return blocks;
    } catch (XMLStreamException e) {
        throw new IllegalStateException("XML is not valid", e);
    }
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) ArrayList(java.util.ArrayList) SMInputFactory(org.codehaus.staxmate.SMInputFactory) LinkedHashMap(java.util.LinkedHashMap) SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) SMHierarchicCursor(org.codehaus.staxmate.in.SMHierarchicCursor) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader)

Example 3 with SMHierarchicCursor

use of org.codehaus.staxmate.in.SMHierarchicCursor 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 4 with SMHierarchicCursor

use of org.codehaus.staxmate.in.SMHierarchicCursor 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 5 with SMHierarchicCursor

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

the class QProfileParser method readXml.

public ImportedQProfile readXml(Reader reader) {
    List<ImportedRule> rules = new ArrayList<>();
    String profileName = null;
    String profileLang = null;
    try {
        SMInputFactory inputFactory = initStax();
        SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
        // <profile>
        rootC.advance();
        if (!ATTRIBUTE_PROFILE.equals(rootC.getLocalName())) {
            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(ATTRIBUTE_NAME, nodeName)) {
                profileName = StringUtils.trim(cursor.collectDescendantText(false));
            } else if (StringUtils.equals(ATTRIBUTE_LANGUAGE, nodeName)) {
                profileLang = StringUtils.trim(cursor.collectDescendantText(false));
            } else if (StringUtils.equals(ATTRIBUTE_RULES, nodeName)) {
                SMInputCursor rulesCursor = cursor.childElementCursor("rule");
                rules = parseRuleActivations(rulesCursor);
            }
        }
    } catch (XMLStreamException e) {
        throw new IllegalArgumentException("Fail to restore Quality profile backup, XML document is not well formed", e);
    }
    return new ImportedQProfile(profileName, profileLang, rules);
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) SMHierarchicCursor(org.codehaus.staxmate.in.SMHierarchicCursor) XMLStreamException(javax.xml.stream.XMLStreamException) ArrayList(java.util.ArrayList) SMInputFactory(org.codehaus.staxmate.SMInputFactory)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)5 SMInputFactory (org.codehaus.staxmate.SMInputFactory)5 SMHierarchicCursor (org.codehaus.staxmate.in.SMHierarchicCursor)5 SMInputCursor (org.codehaus.staxmate.in.SMInputCursor)5 ArrayList (java.util.ArrayList)3 StringReader (java.io.StringReader)1 LinkedHashMap (java.util.LinkedHashMap)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 SonarException (org.sonar.api.utils.SonarException)1 ComponentDto (org.sonar.db.component.ComponentDto)1 RuleDebt (org.sonar.server.debt.DebtModelXMLExporter.RuleDebt)1