All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.flex.flexpmd.FlexPmdRulesRepository Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
/*
 * Sonar Flex Plugin
 * Copyright (C) 2010 SonarSource
 * [email protected]
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */

package org.sonar.plugins.flex.flexpmd;

import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.rules.*;
import org.sonar.plugins.flex.Flex;
import org.sonar.plugins.flex.FlexPlugin;
import org.sonar.plugins.flex.flexpmd.xml.Ruleset;
import org.sonar.plugins.flex.flexpmd.xml.FlexRule;
import org.sonar.plugins.flex.flexpmd.xml.Property;
import org.sonar.plugins.flex.flexpmd.xml.FlexRulesUtils;

import java.util.List;
import java.util.ArrayList;

public class FlexPmdRulesRepository implements RulesRepository, ConfigurationExportable, ConfigurationImportable {
  private FlexPmdRulePriorityMapper priorityMapper = new FlexPmdRulePriorityMapper();
  private String resourcePath = "/org/sonar/plugins/flex/flexpmd/";
  private Flex flex;

  public FlexPmdRulesRepository(Flex flex) {
    this.flex = flex;
  }

  public Flex getLanguage() {
    return flex;
  }

  public final List getInitialReferential() {
    return parseReferential(resourcePath + "rules.xml");
  }

  public List parseReferential(String path) {
    Ruleset ruleset = FlexRulesUtils.buildRuleSetFromXml(FlexRulesUtils.getConfigurationFromFile(path));
    List rulesRepository = new ArrayList();
    for (FlexRule fRule : ruleset.getRules()) {
      rulesRepository.add(createRepositoryRule(fRule));
    }
    return rulesRepository;
  }

  public final List getProvidedProfiles() {
    List profiles = new ArrayList();
    profiles.add(buildProfile("Default Flex Profile", resourcePath + "default-flex-profile.xml"));
    return profiles;
  }

  public final RulesProfile buildProfile(String name, String path) {
    RulesProfile profile = new RulesProfile(name, Flex.KEY);
    List activeRules = importConfiguration(FlexRulesUtils.getConfigurationFromFile(path), getInitialReferential());
    profile.setActiveRules(activeRules);
    return profile;
  }

  public List importConfiguration(String configuration, List rulesRepository) {
    Ruleset ruleset = FlexRulesUtils.buildRuleSetFromXml(configuration);
    List activeRules = new ArrayList();
    for (FlexRule fRule : ruleset.getRules()) {
      ActiveRule activeRule = createActiveRule(fRule, rulesRepository);
      if (activeRule != null) {
        activeRules.add(activeRule);
      }
    }
    return activeRules;
  }

  public String exportConfiguration(RulesProfile activeProfile) {
    Ruleset tree = buildRulesetFromActiveProfile(activeProfile.getActiveRulesByPlugin(FlexPlugin.FLEXPMD_PLUGIN));
    return FlexRulesUtils.buildXmlFromRuleset(tree);
  }

  private Rule createRepositoryRule(FlexRule fRule) {
    RulesCategory category = FlexRulesUtils.matchRuleCategory(fRule.getCategory());
    RulePriority priority = priorityMapper.from(fRule.getPriority());
    Rule rule = new Rule(FlexPlugin.FLEXPMD_PLUGIN, fRule.getClazz(), fRule.getMessage(), category, priority);
    rule.setDescription(fRule.getDescription());
    List ruleParams = new ArrayList();
    if (fRule.getProperties() != null) {
      for (Property property : fRule.getProperties()) {
        RuleParam param = new RuleParam(rule, property.getName(), property.getName(), "i");
        ruleParams.add(param);
      }
    }
    rule.setParams(ruleParams);
    return rule;
  }

  private ActiveRule createActiveRule(FlexRule fRule, List rulesRepository) {
    String clazz = fRule.getClazz();
    RulePriority fRulePriority = priorityMapper.from(fRule.getPriority());
    for (Rule rule : rulesRepository) {
      if (rule.getKey().equals(clazz)) {
        RulePriority priority = fRulePriority != null ? fRulePriority : rule.getPriority();
        ActiveRule activeRule = new ActiveRule(null, rule, priority);
        activeRule.setActiveRuleParams(buildActiveRuleParams(fRule, rule, activeRule));
        return activeRule;
      }
    }
    return null;
  }

  protected List buildActiveRuleParams(FlexRule flexRule, Rule repositoryRule, ActiveRule activeRule) {
    List activeRuleParams = new ArrayList();
    if (flexRule.getProperties() != null) {
      for (Property property : flexRule.getProperties()) {
        if (repositoryRule.getParams() != null) {
          for (RuleParam ruleParam : repositoryRule.getParams()) {
            if (ruleParam.getKey().equals(property.getName())) {
              activeRuleParams.add(new ActiveRuleParam(activeRule, ruleParam, property.getValue()));
            }
          }
        }
      }
    }
    return activeRuleParams;
  }

  protected Ruleset buildRulesetFromActiveProfile(List activeRules) {
    Ruleset ruleset = new Ruleset();
    for (ActiveRule activeRule : activeRules) {
      if (activeRule.getRule().getPluginName().equals(FlexPlugin.FLEXPMD_PLUGIN)) {
        String key = activeRule.getRule().getKey();
        String priority = priorityMapper.to(activeRule.getPriority());
        FlexRule flexRule = new FlexRule(key, priority);
        List properties = new ArrayList();
        for (ActiveRuleParam activeRuleParam : activeRule.getActiveRuleParams()) {
          properties.add(new Property(activeRuleParam.getRuleParam().getKey(), activeRuleParam.getValue()));
        }
        flexRule.setProperties(properties);
        flexRule.setMessage(activeRule.getRule().getName());
        ruleset.addRule(flexRule);
      }
    }
    return ruleset;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy