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 * @since 2.1 (experimental) 033 * @deprecated since 2.3 034 */ 035 @Deprecated 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 for (CheckTemplateProperty checkTemplateProperty : checkTemplate.getProperties()) { 168 RuleParam param = rule.createParameter(checkTemplateProperty.getKey()); 169 param.setDescription(checkTemplateProperty.getDescription(Locale.ENGLISH)); 170 param.setType("s"); 171 } 172 173 return rule; 174 } 175 176 private RulesCategory toRuleCategory(IsoCategory isoCategory) { 177 if (isoCategory == IsoCategory.Reliability) { 178 return Iso9126RulesCategories.RELIABILITY; 179 } 180 if (isoCategory == IsoCategory.Efficiency) { 181 return Iso9126RulesCategories.EFFICIENCY; 182 } 183 if (isoCategory == IsoCategory.Maintainability) { 184 return Iso9126RulesCategories.MAINTAINABILITY; 185 } 186 if (isoCategory == IsoCategory.Portability) { 187 return Iso9126RulesCategories.PORTABILITY; 188 } 189 if (isoCategory == IsoCategory.Usability) { 190 return Iso9126RulesCategories.USABILITY; 191 } 192 return null; 193 } 194 195 196 public List<Rule> parseReferential(String fileContent) { 197 return Collections.emptyList(); 198 } 199 200 public List<RulesProfile> getProvidedProfiles() { 201 return Collections.emptyList(); 202 } 203 }