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.checks.templates;
021
022 import org.apache.commons.io.IOUtils;
023 import org.sonar.api.profiles.RulesProfile;
024 import org.sonar.api.resources.Language;
025 import org.sonar.api.rules.*;
026 import org.sonar.check.IsoCategory;
027
028 import java.io.InputStream;
029 import java.util.*;
030
031 /**
032 * EXPERIMENTAL - will be used in version 2.3
033 *
034 * @since 2.1
035 */
036 public class CheckTemplateRepository implements RulesRepository {
037
038 private String key;
039 private Language language;
040 private List<CheckTemplate> templates;
041 private Map<String, CheckTemplate> templatesByKey;
042
043
044 public CheckTemplateRepository() {
045 }
046
047 public CheckTemplateRepository(String key) {
048 if (key == null) {
049 throw new IllegalArgumentException("Key can not be null");
050 }
051 this.key = key;
052 }
053
054 public String getKey() {
055 return key;
056 }
057
058 public CheckTemplateRepository setKey(String key) {
059 this.key = key;
060 return this;
061 }
062
063 public Language getLanguage() {
064 return language;
065 }
066
067 public CheckTemplateRepository setLanguage(Language l) {
068 this.language = l;
069 return this;
070 }
071
072 public List<CheckTemplate> getTemplates() {
073 if (templates == null) {
074 return Collections.emptyList();
075 }
076 return templates;
077 }
078
079 public CheckTemplateRepository setTemplates(List<CheckTemplate> c) {
080 this.templates = c;
081 return this;
082 }
083
084 public CheckTemplate getTemplate(String key) {
085 if (templatesByKey == null || templatesByKey.isEmpty()) {
086 templatesByKey = new HashMap<String, CheckTemplate>();
087 for (CheckTemplate template : templates) {
088 templatesByKey.put(template.getKey(), template);
089 }
090 }
091 return templatesByKey.get(key);
092 }
093
094 @Override
095 public boolean equals(Object o) {
096 if (this == o) {
097 return true;
098 }
099 if (o == null || getClass() != o.getClass()) {
100 return false;
101 }
102
103 CheckTemplateRepository that = (CheckTemplateRepository) o;
104 return key.equals(that.key);
105
106 }
107
108 @Override
109 public int hashCode() {
110 return key.hashCode();
111 }
112
113
114 public static CheckTemplateRepository createFromXml(String repositoryKey, Language language, String pathToXml) {
115 InputStream input = CheckTemplateRepository.class.getResourceAsStream(pathToXml);
116 try {
117 List<CheckTemplate> templates = new XmlCheckTemplateFactory().parse(input);
118 CheckTemplateRepository repository = new CheckTemplateRepository(repositoryKey);
119 repository.setTemplates(templates);
120 repository.setLanguage(language);
121 return repository;
122
123 } finally {
124 IOUtils.closeQuietly(input);
125 }
126 }
127
128 public static CheckTemplateRepository createFromAnnotatedClasses(String repositoryKey, Language language, Collection<Class> classes) {
129 AnnotationCheckTemplateFactory factory = new AnnotationCheckTemplateFactory(classes);
130 CheckTemplateRepository repository = new CheckTemplateRepository(repositoryKey);
131 repository.setTemplates(factory.create());
132 repository.setLanguage(language);
133 return repository;
134 }
135
136
137
138
139
140
141
142
143
144
145 /*
146
147 CODE FOR BACKWARD COMPATIBLITY
148 This class should not extend RulesRepository in next versions
149
150 */
151
152
153 public List<Rule> getInitialReferential() {
154 List<Rule> rules = new ArrayList<Rule>();
155 for (CheckTemplate checkTemplate : getTemplates()) {
156 rules.add(toRule(checkTemplate));
157 }
158 return rules;
159 }
160
161 private Rule toRule(CheckTemplate checkTemplate) {
162 Rule rule = new Rule(getKey(), checkTemplate.getKey());
163 rule.setDescription(checkTemplate.getDescription(Locale.ENGLISH));
164 rule.setName(checkTemplate.getTitle(Locale.ENGLISH));
165 rule.setPriority(RulePriority.fromCheckPriority(checkTemplate.getPriority()));
166 rule.setRulesCategory(toRuleCategory(checkTemplate.getIsoCategory()));
167 List<RuleParam> params = new ArrayList<RuleParam>();
168 for (CheckTemplateProperty checkTemplateProperty : checkTemplate.getProperties()) {
169 RuleParam param = new RuleParam();
170 param.setKey(checkTemplateProperty.getKey());
171 param.setDescription(checkTemplateProperty.getDescription(Locale.ENGLISH));
172 param.setRule(rule);
173 param.setType("s");
174 params.add(param);
175 }
176 rule.setParams(params);
177
178 return rule;
179 }
180
181 private RulesCategory toRuleCategory(IsoCategory isoCategory) {
182 if (isoCategory == IsoCategory.Reliability) {
183 return Iso9126RulesCategories.RELIABILITY;
184 }
185 if (isoCategory == IsoCategory.Efficiency) {
186 return Iso9126RulesCategories.EFFICIENCY;
187 }
188 if (isoCategory == IsoCategory.Maintainability) {
189 return Iso9126RulesCategories.MAINTAINABILITY;
190 }
191 if (isoCategory == IsoCategory.Portability) {
192 return Iso9126RulesCategories.PORTABILITY;
193 }
194 if (isoCategory == IsoCategory.Usability) {
195 return Iso9126RulesCategories.USABILITY;
196 }
197 return null;
198 }
199
200
201 public List<Rule> parseReferential(String fileContent) {
202 return Collections.emptyList();
203 }
204
205 public List<RulesProfile> getProvidedProfiles() {
206 return Collections.emptyList();
207 }
208 }