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.rules;
021    
022    import org.apache.commons.io.IOUtils;
023    import org.apache.commons.lang.CharEncoding;
024    import org.sonar.api.profiles.RulesProfile;
025    import org.sonar.api.resources.Language;
026    import org.sonar.api.utils.SonarException;
027    
028    import java.io.IOException;
029    import java.io.InputStream;
030    import java.util.ArrayList;
031    import java.util.List;
032    import java.util.Map;
033    import java.util.TreeMap;
034    
035    @Deprecated
036    public abstract class AbstractImportableRulesRepository<LANG extends Language, MAPPER extends RulePriorityMapper<?, ?>> extends AbstractRulesRepository<LANG, MAPPER> implements ConfigurationImportable {
037    
038      public AbstractImportableRulesRepository(LANG language, MAPPER mapper) {
039        super(language, mapper);
040      }
041    
042      /**
043       * A map a of profiles to import, The profile name as key, and the xml profile file name in the classpath
044       *
045       * @return
046       */
047      public abstract Map<String, String> getBuiltInProfiles();
048    
049      public final List<RulesProfile> getProvidedProfiles() {
050        List<RulesProfile> profiles = new ArrayList<RulesProfile>();
051    
052        Map<String, String> defaultProfiles = new TreeMap<String, String>(getBuiltInProfiles());
053        for (Map.Entry<String, String> entry : defaultProfiles.entrySet()) {
054          profiles.add(loadProvidedProfile(entry.getKey(), getCheckResourcesBase() + entry.getValue()));
055        }
056        return profiles;
057      }
058    
059      public final RulesProfile loadProvidedProfile(String name, String fileName) {
060        InputStream input = null;
061        try {
062          input = getClass().getResourceAsStream(fileName);
063          RulesProfile profile = new RulesProfile(name, getLanguage().getKey());
064          profile.setActiveRules(importConfiguration(IOUtils.toString(input, CharEncoding.UTF_8), getInitialReferential()));
065          return profile;
066    
067        } catch (IOException e) {
068          throw new SonarException("Configuration file not found for the profile : " + name, e);
069    
070        } finally {
071          IOUtils.closeQuietly(input);
072        }
073      }
074    
075    }