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.ServerExtension;
024    import org.sonar.api.resources.Language;
025    
026    import java.io.InputStream;
027    import java.util.*;
028    
029    /**
030     * EXPERIMENTAL - will be used in version 2.2
031     *
032     * @since 2.1
033     */
034    public class CheckTemplateRepository implements ServerExtension {
035    
036      private String key;
037      private Language language;
038      private List<CheckTemplate> templates;
039      private Map<String, CheckTemplate> templatesByKey;
040    
041    
042      public CheckTemplateRepository() {
043      }
044    
045      public CheckTemplateRepository(String key) {
046        if (key == null) {
047          throw new IllegalArgumentException("Key can not be null");
048        }
049        this.key = key;
050      }
051    
052      public String getKey() {
053        return key;
054      }
055    
056      public CheckTemplateRepository setKey(String key) {
057        this.key = key;
058        return this;
059      }
060    
061      public Language getLanguage() {
062        return language;
063      }
064    
065      public CheckTemplateRepository setLanguage(Language l) {
066        this.language = l;
067        return this;
068      }
069    
070      public List<CheckTemplate> getTemplates() {
071        if (templates == null) {
072          return Collections.emptyList();
073        }
074        return templates;
075      }
076    
077      public CheckTemplateRepository setTemplates(List<CheckTemplate> c) {
078        this.templates = c;
079        return this;
080      }
081    
082      public CheckTemplate getTemplate(String key) {
083        if (templatesByKey == null || templatesByKey.isEmpty()) {
084          templatesByKey = new HashMap<String, CheckTemplate>();
085          for (CheckTemplate template : templates) {
086            templatesByKey.put(template.getKey(), template);
087          }
088        }
089        return templatesByKey.get(key);
090      }
091    
092      @Override
093      public boolean equals(Object o) {
094        if (this == o) {
095          return true;
096        }
097        if (o == null || getClass() != o.getClass()) {
098          return false;
099        }
100    
101        CheckTemplateRepository that = (CheckTemplateRepository) o;
102        return key.equals(that.key);
103    
104      }
105    
106      @Override
107      public int hashCode() {
108        return key.hashCode();
109      }
110    
111    
112      public static CheckTemplateRepository createFromXml(String repositoryKey, Language language, String pathToXml) {
113        InputStream input = CheckTemplateRepository.class.getResourceAsStream(pathToXml);
114        try {
115          List<CheckTemplate> templates = new XmlCheckTemplateFactory().parse(input);
116          CheckTemplateRepository repository = new CheckTemplateRepository(repositoryKey);
117          repository.setTemplates(templates);
118          repository.setLanguage(language);
119          return repository;
120    
121        } finally {
122          IOUtils.closeQuietly(input);
123        }
124      }
125    
126      public static CheckTemplateRepository createFromAnnotatedClasses(String repositoryKey, Language language, Collection<Class> classes) {
127        AnnotationCheckTemplateFactory factory = new AnnotationCheckTemplateFactory(classes);
128        CheckTemplateRepository repository = new CheckTemplateRepository(repositoryKey);
129        repository.setTemplates(factory.create());
130        repository.setLanguage(language);
131        return repository;
132      }
133    }