3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.ce.task.projectanalysis.issue.commonrule;
22 import java.util.Optional;
23 import javax.annotation.CheckForNull;
24 import org.sonar.api.rule.RuleKey;
25 import org.sonar.ce.task.projectanalysis.component.Component;
26 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRule;
27 import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRulesHolder;
28 import org.sonar.core.issue.DefaultIssue;
30 import static org.apache.commons.lang.StringUtils.isNotBlank;
31 import static org.sonar.server.rule.CommonRuleKeys.commonRepositoryForLang;
33 public abstract class CommonRule {
35 private final ActiveRulesHolder activeRulesHolder;
36 private final String key;
38 public CommonRule(ActiveRulesHolder activeRulesHolder, String key) {
39 this.activeRulesHolder = activeRulesHolder;
44 public DefaultIssue processFile(Component file, String fileLanguage) {
45 DefaultIssue issue = null;
46 RuleKey ruleKey = RuleKey.of(commonRepositoryForLang(fileLanguage), key);
47 Optional<ActiveRule> activeRule = activeRulesHolder.get(ruleKey);
48 if (activeRule.isPresent()) {
49 CommonRuleIssue cri = doProcessFile(file, activeRule.get());
51 issue = new DefaultIssue();
52 issue.setGap(cri.effortToFix);
53 issue.setMessage(cri.message);
54 issue.setRuleKey(ruleKey);
55 issue.setSeverity(activeRule.get().getSeverity());
57 issue.setChecksum("");
58 issue.setIsFromExternalRuleEngine(false);
65 * Implementations must only set the effort to fix and message, so
66 * using the lite intermediary object {@link CommonRuleIssue}
69 protected abstract CommonRuleIssue doProcessFile(Component file, ActiveRule activeRule);
71 protected static class CommonRuleIssue {
72 private final double effortToFix;
73 // FIXME which format ? HTML or MD ?
74 private final String message;
76 public CommonRuleIssue(double effortToFix, String message) {
77 this.effortToFix = effortToFix;
78 this.message = message;
82 protected static double getMinDensityParam(ActiveRule activeRule, String paramKey) {
83 String s = activeRule.getParams().get(paramKey);
85 double d = Double.parseDouble(s);
86 if (d < 0.0 || d > 100.0) {
87 throw new IllegalStateException(String.format("Minimum density of rule [%s] is incorrect. Got [%s] but must be between 0 and 100.", activeRule.getRuleKey(), s));
91 throw new IllegalStateException(String.format("Required parameter [%s] is missing on rule [%s]", paramKey, activeRule.getRuleKey()));