001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.api.rules;
021
022 import org.sonar.api.checks.*;
023 import org.sonar.api.checks.CheckTemplateProperty;
024 import org.sonar.api.checks.CheckTemplateRepository;
025 import org.sonar.api.checks.CheckTemplate;
026 import org.sonar.api.profiles.RulesProfile;
027 import org.sonar.api.resources.Language;
028
029 import java.util.ArrayList;
030 import java.util.Collections;
031 import java.util.List;
032 import java.util.Locale;
033
034 public abstract class CheckToRuleRepositoryBridge implements RulesRepository {
035
036 private Language language = null;
037 private CheckTemplateRepository checkTemplateRepository = null;
038
039 public CheckToRuleRepositoryBridge(Language language, CheckTemplateRepository checkTemplateRepository) {
040 this.language = language;
041 this.checkTemplateRepository = checkTemplateRepository;
042 }
043
044 public Language getLanguage() {
045 return language;
046 }
047
048 public List<Rule> getInitialReferential() {
049 List<Rule> rules = new ArrayList<Rule>();
050 for (CheckTemplate checkTemplate : checkTemplateRepository.getTemplates()) {
051 rules.add(toRule(checkTemplate));
052 }
053 return rules;
054 }
055
056 private Rule toRule(CheckTemplate checkTemplate) {
057 Rule rule = new Rule(checkTemplateRepository.getKey(), checkTemplate.getKey());
058 rule.setDescription(checkTemplate.getDescription(Locale.ENGLISH));
059 rule.setName(checkTemplate.getTitle(Locale.ENGLISH));
060 rule.setPriority(toPriority(checkTemplate.getPriority()));
061 rule.setRulesCategory(toRuleCategory(checkTemplate.getIsoCategory()));
062 List<RuleParam> params = new ArrayList<RuleParam>();
063 for (CheckTemplateProperty checkTemplateProperty : checkTemplate.getProperties()) {
064 RuleParam param = new RuleParam();
065 param.setKey(checkTemplateProperty.getKey());
066 param.setDescription(checkTemplateProperty.getDescription(Locale.ENGLISH));
067 param.setRule(rule);
068 param.setType("s");
069 params.add(param);
070 }
071 rule.setParams(params);
072
073 return rule;
074 }
075
076 private RulesCategory toRuleCategory(IsoCategory isoCategory) {
077 if (isoCategory == IsoCategory.Reliability) {
078 return Iso9126RulesCategories.RELIABILITY;
079 }
080 if (isoCategory == IsoCategory.Efficiency) {
081 return Iso9126RulesCategories.EFFICIENCY;
082 }
083 if (isoCategory == IsoCategory.Maintainability) {
084 return Iso9126RulesCategories.MAINTAINABILITY;
085 }
086 if (isoCategory == IsoCategory.Portability) {
087 return Iso9126RulesCategories.PORTABILITY;
088 }
089 if (isoCategory == IsoCategory.Usability) {
090 return Iso9126RulesCategories.USABILITY;
091 }
092 return null;
093 }
094
095 private RulePriority toPriority(Priority checkPriority) {
096 if (checkPriority == Priority.BLOCKER) {
097 return RulePriority.BLOCKER;
098 }
099 if (checkPriority == Priority.CRITICAL) {
100 return RulePriority.CRITICAL;
101 }
102 if (checkPriority == Priority.MAJOR) {
103 return RulePriority.MAJOR;
104 }
105 if (checkPriority == Priority.MINOR) {
106 return RulePriority.MINOR;
107 }
108 if (checkPriority == Priority.INFO) {
109 return RulePriority.INFO;
110 }
111 return null;
112 }
113
114 public List<Rule> parseReferential(String fileContent) {
115 return Collections.emptyList();
116 }
117
118 public List<RulesProfile> getProvidedProfiles() {
119 return Collections.emptyList();
120 }
121 }