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