Search in sources :

Example 6 with SMInputCursor

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

the class XMLProfileParser method parse.

public RulesProfile parse(Reader reader, ValidationMessages messages) {
    RulesProfile profile = RulesProfile.create();
    SMInputFactory inputFactory = initStax();
    try {
        SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader);
        // <profile>
        rootC.advance();
        SMInputCursor cursor = rootC.childElementCursor();
        while (cursor.getNext() != null) {
            String nodeName = cursor.getLocalName();
            if (StringUtils.equals("rules", nodeName)) {
                SMInputCursor rulesCursor = cursor.childElementCursor("rule");
                processRules(rulesCursor, profile, messages);
            } else if (StringUtils.equals("name", nodeName)) {
                profile.setName(StringUtils.trim(cursor.collectDescendantText(false)));
            } else if (StringUtils.equals("language", nodeName)) {
                profile.setLanguage(StringUtils.trim(cursor.collectDescendantText(false)));
            }
        }
    } catch (XMLStreamException e) {
        messages.addErrorText("XML is not valid: " + e.getMessage());
    }
    checkProfile(profile, messages);
    return profile;
}
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 SMInputCursor

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

the class RulesDefinitionXmlLoader method load.

/**
   * Loads rules by reading the XML input stream. The reader is not closed by the method, so it
   * should be handled by the caller.
   * @since 4.3
   */
public void load(RulesDefinition.NewRepository repo, 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();
        SMInputCursor rulesC = rootC.childElementCursor("rule");
        while (rulesC.getNext() != null) {
            // <rule>
            processRule(repo, rulesC);
        }
    } catch (XMLStreamException e) {
        throw new IllegalStateException("XML is not valid", 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) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 8 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 9 with SMInputCursor

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

the class GenericTestExecutionReportParser 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) {
            numberOfUnknownFiles++;
            if (numberOfUnknownFiles <= MAX_STORED_UNKNOWN_FILE_PATHS) {
                firstUnknownFiles.add(filePath);
            }
            continue;
        }
        Preconditions.checkState(inputFile.language() != null, "Line %s of report refers to a file with an unknown language: %s", fileCursor.getCursorLocation().getLineNumber(), filePath);
        Preconditions.checkState(inputFile.type() != InputFile.Type.MAIN, "Line %s of report refers to a file which is not configured as a test file: %s", fileCursor.getCursorLocation().getLineNumber(), filePath);
        matchedFileKeys.add(inputFile.absolutePath());
        MutableTestPlan testPlan = testPlanBuilder.loadPerspective(MutableTestPlan.class, inputFile);
        SMInputCursor testCaseCursor = fileCursor.childElementCursor();
        while (testCaseCursor.getNext() != null) {
            parseTestCase(testCaseCursor, testPlan);
        }
    }
}
Also used : MutableTestPlan(org.sonar.api.test.MutableTestPlan) SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) InputFile(org.sonar.api.batch.fs.InputFile)

Example 10 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)

Aggregations

SMInputCursor (org.codehaus.staxmate.in.SMInputCursor)18 XMLStreamException (javax.xml.stream.XMLStreamException)7 SMInputFactory (org.codehaus.staxmate.SMInputFactory)6 SMHierarchicCursor (org.codehaus.staxmate.in.SMHierarchicCursor)6 SonarException (org.sonar.api.utils.SonarException)3 ArrayList (java.util.ArrayList)2 XMLInputFactory (javax.xml.stream.XMLInputFactory)2 InputFile (org.sonar.api.batch.fs.InputFile)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NewCoverage (org.sonar.api.batch.sensor.coverage.NewCoverage)1 RuleKey (org.sonar.api.rule.RuleKey)1 RuleStatus (org.sonar.api.rule.RuleStatus)1 ActiveRule (org.sonar.api.rules.ActiveRule)1 Rule (org.sonar.api.rules.Rule)1 RulePriority (org.sonar.api.rules.RulePriority)1 MutableTestCase (org.sonar.api.test.MutableTestCase)1 MutableTestPlan (org.sonar.api.test.MutableTestPlan)1