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