Example #1
0
 private void importActiveRules(RulesDao rulesDao, RulesProfile profile) {
   for (Iterator<ActiveRule> iar = profile.getActiveRules(true).iterator(); iar.hasNext(); ) {
     ActiveRule activeRule = iar.next();
     Rule unMarshalledRule = activeRule.getRule();
     Rule matchingRuleInDb =
         rulesDao.getRuleByKey(unMarshalledRule.getRepositoryKey(), unMarshalledRule.getKey());
     if (matchingRuleInDb == null) {
       LoggerFactory.getLogger(getClass())
           .error(
               "Unable to find active rule "
                   + unMarshalledRule.getRepositoryKey()
                   + ":"
                   + unMarshalledRule.getKey());
       iar.remove();
       continue;
     }
     activeRule.setRule(matchingRuleInDb);
     activeRule.setRulesProfile(profile);
     activeRule.getActiveRuleParams();
     for (Iterator<ActiveRuleParam> irp = activeRule.getActiveRuleParams().iterator();
         irp.hasNext(); ) {
       ActiveRuleParam activeRuleParam = irp.next();
       RuleParam unMarshalledRP = activeRuleParam.getRuleParam();
       RuleParam matchingRPInDb = rulesDao.getRuleParam(matchingRuleInDb, unMarshalledRP.getKey());
       if (matchingRPInDb == null) {
         LoggerFactory.getLogger(getClass())
             .error("Unable to find active rule parameter " + unMarshalledRP.getKey());
         irp.remove();
         continue;
       }
       activeRuleParam.setActiveRule(activeRule);
       activeRuleParam.setRuleParam(matchingRPInDb);
     }
   }
 }
Example #2
0
 private void appendRuleParameters(ActiveRule activeRule, Writer writer) throws IOException {
   if (activeRule.getActiveRuleParams() != null && !activeRule.getActiveRuleParams().isEmpty()) {
     writer.append("<parameters>");
     for (ActiveRuleParam activeRuleParam : activeRule.getActiveRuleParams()) {
       appendRuleParameter(writer, activeRuleParam);
     }
     writer.append("</parameters>");
   }
 }
 /**
  * Materialize the current active rule set for the profile
  *
  * @param activeRules
  * @param profileName
  * @return
  */
 protected PmdRuleset createRuleset(List<ActiveRule> activeRules, String profileName) {
   PmdRuleset ruleset = new PmdRuleset(profileName);
   for (ActiveRule activeRule : activeRules) {
     if (activeRule
         .getRule()
         .getPluginName()
         .equals(PhpCodeSnifferRuleRepository.PHPCS_REPOSITORY_KEY)) {
       String configKey = activeRule.getRule().getKey();
       PmdRule rule = new PmdRule(configKey, mapper.to(activeRule.getPriority()));
       List<PmdProperty> properties = null;
       List<ActiveRuleParam> activeRuleParams = activeRule.getActiveRuleParams();
       if (activeRuleParams != null && !activeRuleParams.isEmpty()) {
         properties = new ArrayList<PmdProperty>();
         for (ActiveRuleParam activeRuleParam : activeRuleParams) {
           properties.add(
               new PmdProperty(
                   activeRuleParam.getRuleParam().getKey(), activeRuleParam.getValue()));
         }
       }
       rule.setProperties(properties);
       ruleset.addRule(rule);
       processXPathRule(activeRule.getRuleKey(), rule);
     }
   }
   return ruleset;
 }
  private List<ProfileDefinition> loadFromDeprecatedRepository(RulesRepository repository) {
    List<ProfileDefinition> result = new ArrayList<ProfileDefinition>();

    for (int index = 0; index < repository.getProvidedProfiles().size(); index++) {
      RulesProfile deprecated = (RulesProfile) repository.getProvidedProfiles().get(index);
      DefaultProfileDefinition providedProfile =
          DefaultProfileDefinition.create(deprecated.getName(), repository.getLanguage().getKey());
      for (ActiveRule deprecatedActiveRule : deprecated.getActiveRules(true)) {
        String repositoryKey = deprecatedActiveRule.getRepositoryKey();
        if (StringUtils.isBlank(repositoryKey)) {
          repositoryKey = getPluginKey(repository);
        }
        Rule rule = ruleFinder.findByKey(repositoryKey, deprecatedActiveRule.getRuleKey());
        if (rule != null) {
          ActiveRule activeRule =
              providedProfile.activateRule(rule, deprecatedActiveRule.getSeverity());
          for (ActiveRuleParam arp : deprecatedActiveRule.getActiveRuleParams()) {
            activeRule.setParameter(arp.getKey(), arp.getValue());
          }
        }
      }
      result.add(providedProfile);
    }
    return result;
  }
  @Test
  public void testImportingParameters() {
    Reader reader =
        new StringReader(
            TestUtils.getResourceContent("/org/sonar/plugins/php/pmd/simple-ruleset.xml"));
    RulesProfile profile = importer.importProfile(reader, messages);

    ActiveRule activeRule =
        profile.getActiveRuleByConfigKey(
            REPOSITORY_KEY, "rulesets/codesize.xml/CyclomaticComplexity");
    assertThat(activeRule.getActiveRuleParams().size(), is(1));
    assertThat(activeRule.getParameter("reportLevel"), is("30"));
  }
  @Test
  public void importParameters() {
    Reader reader =
        new StringReader(
            TestUtils.getResourceContent(
                "/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml"));
    RulesProfile profile = importer.importProfile(reader, messages);

    ActiveRule javadocCheck =
        profile.getActiveRuleByConfigKey("checkstyle", "Checker/JavadocPackage");
    assertThat(javadocCheck.getActiveRuleParams()).hasSize(2);
    assertThat(javadocCheck.getParameter("format")).isEqualTo("abcde");
    assertThat(javadocCheck.getParameter("ignore")).isEqualTo("true");
    assertThat(javadocCheck.getParameter("severity")).isNull(); // checkstyle internal parameter
  }
 private void configureFields(ActiveRule activeRule, Object check) {
   for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
     Field field = getField(check, param.getKey());
     if (field == null) {
       throw new SonarException(
           "The field "
               + param.getKey()
               + " does not exist or is not annotated with @RuleProperty in the class "
               + check.getClass().getName());
     }
     if (StringUtils.isNotBlank(param.getValue())) {
       configureField(check, field, param.getValue());
     }
   }
 }
Example #8
0
 /** Important : the ruby controller has already create the profile */
 public ValidationMessages importProfile(
     String profileName, String language, String importerKey, String profileDefinition) {
   ValidationMessages messages = ValidationMessages.create();
   ProfileImporter importer = getProfileImporter(importerKey);
   RulesProfile profile = importer.importProfile(new StringReader(profileDefinition), messages);
   if (!messages.hasErrors()) {
     DatabaseSession session = sessionFactory.getSession();
     RulesProfile persistedProfile =
         session.getSingleResult(RulesProfile.class, "name", profileName, "language", language);
     for (ActiveRule activeRule : profile.getActiveRules()) {
       ActiveRule persistedActiveRule =
           persistedProfile.activateRule(activeRule.getRule(), activeRule.getSeverity());
       for (ActiveRuleParam activeRuleParam : activeRule.getActiveRuleParams()) {
         persistedActiveRule.setParameter(activeRuleParam.getKey(), activeRuleParam.getValue());
       }
     }
     session.saveWithoutFlush(persistedProfile);
     session.commit();
   }
   return messages;
 }