001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
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.Rule;
026    import org.sonar.api.rules.RuleParam;
027    import org.sonar.api.rules.RulePriority;
028    import org.sonar.api.rules.RulesRepository;
029    
030    import java.io.InputStream;
031    import java.util.*;
032    
033    /**
034     * @since 2.1 (experimental)
035     * @deprecated since 2.3
036     */
037    @Deprecated
038    public class CheckTemplateRepository implements RulesRepository {
039    
040      private String key;
041      private Language language;
042      private List<CheckTemplate> templates;
043      private Map<String, CheckTemplate> templatesByKey;
044    
045      public CheckTemplateRepository() {
046      }
047    
048      public CheckTemplateRepository(String key) {
049        if (key == null) {
050          throw new IllegalArgumentException("Key can not be null");
051        }
052        this.key = key;
053      }
054    
055      public String getKey() {
056        return key;
057      }
058    
059      public CheckTemplateRepository setKey(String key) {
060        this.key = key;
061        return this;
062      }
063    
064      public Language getLanguage() {
065        return language;
066      }
067    
068      public CheckTemplateRepository setLanguage(Language l) {
069        this.language = l;
070        return this;
071      }
072    
073      public List<CheckTemplate> getTemplates() {
074        if (templates == null) {
075          return Collections.emptyList();
076        }
077        return templates;
078      }
079    
080      public CheckTemplateRepository setTemplates(List<CheckTemplate> c) {
081        this.templates = c;
082        return this;
083      }
084    
085      public CheckTemplate getTemplate(String key) {
086        if (templatesByKey == null || templatesByKey.isEmpty()) {
087          templatesByKey = new HashMap<String, CheckTemplate>();
088          for (CheckTemplate template : templates) {
089            templatesByKey.put(template.getKey(), template);
090          }
091        }
092        return templatesByKey.get(key);
093      }
094    
095      @Override
096      public boolean equals(Object o) {
097        if (this == o) {
098          return true;
099        }
100        if (o == null || getClass() != o.getClass()) {
101          return false;
102        }
103    
104        CheckTemplateRepository that = (CheckTemplateRepository) o;
105        return key.equals(that.key);
106    
107      }
108    
109      @Override
110      public int hashCode() {
111        return key.hashCode();
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       * CODE FOR BACKWARD COMPATIBLITY
138       * This class should not extend RulesRepository in next versions
139       */
140    
141      public List<Rule> getInitialReferential() {
142        List<Rule> rules = new ArrayList<Rule>();
143        for (CheckTemplate checkTemplate : getTemplates()) {
144          rules.add(toRule(checkTemplate));
145        }
146        return rules;
147      }
148    
149      private Rule toRule(CheckTemplate checkTemplate) {
150        Rule rule = new Rule(getKey(), checkTemplate.getKey());
151        rule.setDescription(checkTemplate.getDescription(Locale.ENGLISH));
152        rule.setName(checkTemplate.getTitle(Locale.ENGLISH));
153        rule.setSeverity(RulePriority.fromCheckPriority(checkTemplate.getPriority()));
154        for (CheckTemplateProperty checkTemplateProperty : checkTemplate.getProperties()) {
155          RuleParam param = rule.createParameter(checkTemplateProperty.getKey());
156          param.setDescription(checkTemplateProperty.getDescription(Locale.ENGLISH));
157          param.setType("s");
158        }
159    
160        return rule;
161      }
162    
163      public List<Rule> parseReferential(String fileContent) {
164        return Collections.emptyList();
165      }
166    
167      public List<RulesProfile> getProvidedProfiles() {
168        return Collections.emptyList();
169      }
170    }