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.apache.commons.io.IOUtils;
023    import org.sonar.api.resources.Language;
024    import org.sonar.api.utils.SonarException;
025    
026    import java.io.InputStream;
027    import java.util.List;
028    
029    public abstract class AbstractRulesRepository<LANG extends Language, MAPPER extends RulePriorityMapper<?, ?>> implements RulesRepository<LANG> {
030    
031      private MAPPER priorityMapper;
032      private LANG language;
033    
034      public AbstractRulesRepository(LANG language, MAPPER priorityMapper) {
035        super();
036        this.priorityMapper = priorityMapper;
037        this.language = language;
038      }
039    
040      public LANG getLanguage() {
041        return language;
042      }
043    
044      public abstract String getRepositoryResourcesBase();
045    
046      public final List<Rule> getInitialReferential() {
047        String baseCP = getCheckResourcesBase();
048        InputStream input = getClass().getResourceAsStream(baseCP + "rules.xml");
049        if (input == null) {
050          throw new SonarException("Resource not found : " + baseCP + "rules.xml");
051        }
052        try {
053          return new StandardRulesXmlParser().parse(input);
054        }
055        finally {
056          IOUtils.closeQuietly(input);
057        }
058      }
059    
060      public List<Rule> parseReferential(String fileContent) {
061        return new StandardRulesXmlParser().parse(fileContent);
062      }
063    
064      public MAPPER getRulePriorityMapper() {
065        return priorityMapper;
066      }
067    
068      protected String getCheckResourcesBase() {
069        String base = getRepositoryResourcesBase();
070        base = base.startsWith("/") ? base : "/" + base;
071        base = base.endsWith("/") ? base : base + "/";
072        return base;
073      }
074    
075    }