aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/java/org/sonar/server/rules/DeprecatedRuleBridge.java
blob: d92184e98056edb209399aeb1fd95e901b683082 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2009 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * Sonar 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.
 *
 * Sonar 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 Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.server.rules;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.CharEncoding;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleParam;
import org.sonar.api.rules.RuleRepository;
import org.sonar.api.rules.RulesRepository;
import org.sonar.api.utils.SonarException;
import org.sonar.server.platform.DefaultServerFileSystem;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Bridge for the deprecated extension "org.sonar.api.rules.RulesRepository"
 */
public final class DeprecatedRuleBridge extends RuleRepository {

  private RulesRepository deprecatedRepository;
  private DefaultServerFileSystem fileSystem;

  public DeprecatedRuleBridge(String repositoryKey, String repositoryName, RulesRepository deprecatedRepository, DefaultServerFileSystem fileSystem) {
    super(repositoryKey, deprecatedRepository.getLanguage().getKey());
    this.deprecatedRepository = deprecatedRepository;
    this.fileSystem = fileSystem;
    setName(repositoryName);
  }

  @Override
  public List<Rule> createRules() {
    List<Rule> rules = new ArrayList<Rule>();
    registerRules(rules);
    registerRuleExtensions(rules);
    return rules;
  }

  private void registerRules(List<Rule> rules) {
    List<Rule> deprecatedRules = deprecatedRepository.getInitialReferential();
    if (deprecatedRules != null) {
      for (Rule deprecatedRule : deprecatedRules) {
        rules.add(cloneRule(deprecatedRule));
      }
    }
  }

  private void registerRuleExtensions(List<Rule> rules) {
    for (File file : fileSystem.getPluginExtensionXml(getKey())) {
      try {
        String fileContent = FileUtils.readFileToString(file, CharEncoding.UTF_8);
        List<Rule> deprecatedRules = deprecatedRepository.parseReferential(fileContent);
        if (deprecatedRules != null) {
          for (Rule deprecatedRule : deprecatedRules) {
            rules.add(cloneRule(deprecatedRule));
          }
        }
      } catch (IOException e) {
        throw new SonarException("Can not read the file: " + file.getPath(), e);
      }
    }
  }

  private Rule cloneRule(Rule deprecatedRule) {
    Rule rule = Rule.create(getKey(), deprecatedRule.getKey(), deprecatedRule.getName());
    rule.setRulesCategory(deprecatedRule.getRulesCategory());
    rule.setConfigKey(deprecatedRule.getConfigKey());
    rule.setPriority(deprecatedRule.getPriority());
    rule.setDescription(deprecatedRule.getDescription());
    rule.setEnabled(true);
    if (deprecatedRule.getParams() != null) {
      for (RuleParam deprecatedParam : deprecatedRule.getParams()) {
        rule.createParameter(deprecatedParam.getKey())
            .setDescription(deprecatedParam.getDescription())
            .setType(deprecatedParam.getType());
      }
    }
    return rule;
  }
}