Search in sources :

Example 6 with ActiveRule

use of org.sonar.api.rules.ActiveRule in project sonarqube by SonarSource.

the class XMLProfileParser method processRules.

private void processRules(SMInputCursor rulesCursor, RulesProfile profile, ValidationMessages messages) throws XMLStreamException {
    Map<String, String> parameters = new HashMap<>();
    while (rulesCursor.getNext() != null) {
        SMInputCursor ruleCursor = rulesCursor.childElementCursor();
        String repositoryKey = null;
        String key = null;
        RulePriority priority = null;
        parameters.clear();
        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)) {
                priority = RulePriority.valueOf(StringUtils.trim(ruleCursor.collectDescendantText(false)));
            } else if (StringUtils.equals("parameters", nodeName)) {
                SMInputCursor propsCursor = ruleCursor.childElementCursor("parameter");
                processParameters(propsCursor, parameters);
            }
        }
        Rule rule = ruleFinder.findByKey(repositoryKey, key);
        if (rule == null) {
            messages.addWarningText("Rule not found: " + ruleToString(repositoryKey, key));
        } else {
            ActiveRule activeRule = profile.activateRule(rule, priority);
            for (Map.Entry<String, String> entry : parameters.entrySet()) {
                if (rule.getParam(entry.getKey()) == null) {
                    messages.addWarningText("The parameter '" + entry.getKey() + "' does not exist in the rule: " + ruleToString(repositoryKey, key));
                } else {
                    activeRule.setParameter(entry.getKey(), entry.getValue());
                }
            }
        }
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) HashMap(java.util.HashMap) ActiveRule(org.sonar.api.rules.ActiveRule) RulePriority(org.sonar.api.rules.RulePriority) Rule(org.sonar.api.rules.Rule) ActiveRule(org.sonar.api.rules.ActiveRule) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with ActiveRule

use of org.sonar.api.rules.ActiveRule in project sonarqube by SonarSource.

the class XMLProfileParserTest method importProfileWithRuleParameters.

@Test
public void importProfileWithRuleParameters() {
    ValidationMessages validation = ValidationMessages.create();
    RulesProfile profile = parse("importProfileWithRuleParameters.xml", validation);
    assertThat(validation.hasErrors()).isFalse();
    assertThat(validation.hasWarnings()).isFalse();
    ActiveRule rule = profile.getActiveRule("checkstyle", "IllegalRegexp");
    assertThat(rule.getParameter("format")).isEqualTo("foo");
    assertThat(rule.getParameter("message")).isEqualTo("with special characters < > &");
}
Also used : ActiveRule(org.sonar.api.rules.ActiveRule) ValidationMessages(org.sonar.api.utils.ValidationMessages) Test(org.junit.Test)

Example 8 with ActiveRule

use of org.sonar.api.rules.ActiveRule in project sonarqube by SonarSource.

the class RulesProfileProvider method select.

private static RulesProfile select(QProfile qProfile, ActiveRules activeRules) {
    RulesProfile deprecatedProfile = new RulesProfile();
    deprecatedProfile.setName(qProfile.getName());
    deprecatedProfile.setLanguage(qProfile.getLanguage());
    for (org.sonar.api.batch.rule.ActiveRule activeRule : activeRules.findByLanguage(qProfile.getLanguage())) {
        Rule rule = Rule.create(activeRule.ruleKey().repository(), activeRule.ruleKey().rule());
        rule.setConfigKey(activeRule.internalKey());
        // SONAR-6706
        if (activeRule.templateRuleKey() != null) {
            rule.setTemplate(Rule.create(activeRule.ruleKey().repository(), activeRule.templateRuleKey()));
        }
        ActiveRule deprecatedActiveRule = deprecatedProfile.activateRule(rule, RulePriority.valueOf(activeRule.severity()));
        for (Map.Entry<String, String> param : activeRule.params().entrySet()) {
            rule.createParameter(param.getKey());
            deprecatedActiveRule.setParameter(param.getKey(), param.getValue());
        }
    }
    return deprecatedProfile;
}
Also used : RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRule(org.sonar.api.rules.ActiveRule) Rule(org.sonar.api.rules.Rule) ActiveRule(org.sonar.api.rules.ActiveRule) Map(java.util.Map)

Aggregations

ActiveRule (org.sonar.api.rules.ActiveRule)8 Map (java.util.Map)3 Test (org.junit.Test)3 Rule (org.sonar.api.rules.Rule)3 RulesProfile (org.sonar.api.profiles.RulesProfile)2 ValidationMessages (org.sonar.api.utils.ValidationMessages)2 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 SMInputCursor (org.codehaus.staxmate.in.SMInputCursor)1 ActiveRuleParam (org.sonar.api.rules.ActiveRuleParam)1 RulePriority (org.sonar.api.rules.RulePriority)1 DbSession (org.sonar.db.DbSession)1 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)1 RuleParamDto (org.sonar.db.rule.RuleParamDto)1