Search in sources :

Example 6 with SMInputFactory

use of org.codehaus.staxmate.SMInputFactory 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 7 with SMInputFactory

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

the class DebtRulesXMLImporter method initStax.

private static SMInputFactory initStax() {
    XMLInputFactory xmlFactory = XMLInputFactory2.newInstance();
    xmlFactory.setProperty(IS_COALESCING, TRUE);
    xmlFactory.setProperty(IS_NAMESPACE_AWARE, FALSE);
    xmlFactory.setProperty(SUPPORT_DTD, FALSE);
    xmlFactory.setProperty(IS_VALIDATING, FALSE);
    return new SMInputFactory(xmlFactory);
}
Also used : SMInputFactory(org.codehaus.staxmate.SMInputFactory) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 8 with SMInputFactory

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

the class DuplicationsParser method initStax.

private static SMInputFactory initStax() {
    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);
    return new SMInputFactory(xmlFactory);
}
Also used : SMInputFactory(org.codehaus.staxmate.SMInputFactory) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 9 with SMInputFactory

use of org.codehaus.staxmate.SMInputFactory 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 10 with SMInputFactory

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

the class XMLProfileParser method initStax.

private static SMInputFactory initStax() {
    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);
    return new SMInputFactory(xmlFactory);
}
Also used : SMInputFactory(org.codehaus.staxmate.SMInputFactory) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

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