]> source.dussan.org Git - sonarqube.git/commitdiff
API: apply the same pattern between rules and profiles API. The extension point to...
authorsimonbrandhof <simon.brandhof@gmail.com>
Fri, 8 Oct 2010 15:48:07 +0000 (15:48 +0000)
committersimonbrandhof <simon.brandhof@gmail.com>
Fri, 8 Oct 2010 15:48:07 +0000 (15:48 +0000)
14 files changed:
plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepository.java
plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepositoryTest.java
plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsRuleRepository.java
plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleFinder.java
plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleRepositoryTest.java
plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsTests.java
plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdRuleRepository.java
plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java
plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRuleRepositoryTest.java
sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleRepository.java [deleted file]
sonar-plugin-api/src/main/java/org/sonar/api/rules/XMLRuleParser.java
sonar-plugin-api/src/test/java/org/sonar/api/rules/XMLRuleParserTest.java
sonar-server/src/main/java/org/sonar/server/platform/Platform.java

index ee533a1cffa993ac2d27b768743ba99d224fe479..02dd0ad885c3c4c46f6e393e217c05775c845457 100644 (file)
@@ -33,19 +33,21 @@ public final class CheckstyleRuleRepository extends RuleRepository {
 
   // for user extensions
   private ServerFileSystem fileSystem;
+  private XMLRuleParser xmlRuleParser;
 
-  public CheckstyleRuleRepository(ServerFileSystem fileSystem) {
+  public CheckstyleRuleRepository(ServerFileSystem fileSystem, XMLRuleParser xmlRuleParser) {
     super(CheckstyleConstants.REPOSITORY_KEY, Java.KEY);
     setName(CheckstyleConstants.REPOSITORY_NAME);
     this.fileSystem = fileSystem;
+    this.xmlRuleParser = xmlRuleParser;
   }
 
   @Override
   public List<Rule> createRules() {
     List<Rule> rules = new ArrayList<Rule>();
-    rules.addAll(XMLRuleParser.parseXML(getClass().getResourceAsStream("/org/sonar/plugins/checkstyle/rules.xml")));
+    rules.addAll(xmlRuleParser.parse(getClass().getResourceAsStream("/org/sonar/plugins/checkstyle/rules.xml")));
     for (File userExtensionXml : fileSystem.getExtensions(CheckstyleConstants.REPOSITORY_KEY, "xml")) {
-      rules.addAll(XMLRuleParser.parseXML(userExtensionXml));
+      rules.addAll(xmlRuleParser.parse(userExtensionXml));
     }
     return rules;
   }
index 71d29f5c572ecf82659cd184d32724de38529682..a501feb7308265b9fcc6d00a624d64bfaa358c7a 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.plugins.checkstyle;
 import org.junit.Test;
 import org.sonar.api.platform.ServerFileSystem;
 import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.XMLRuleParser;
 
 import java.util.List;
 
@@ -34,7 +35,7 @@ public class CheckstyleRuleRepositoryTest {
   @Test
   public void loadRepositoryFromXml() {
     ServerFileSystem fileSystem = mock(ServerFileSystem.class);
-    CheckstyleRuleRepository repository = new CheckstyleRuleRepository(fileSystem);
+    CheckstyleRuleRepository repository = new CheckstyleRuleRepository(fileSystem, new XMLRuleParser());
     List<Rule> rules = repository.createRules();
     assertThat(rules.size(), greaterThan(100));
   }
index 0f43b2cd4d290f19a8752a7658305b46827d89b9..38150ecead9051387a360ef37073811bc1144e34 100644 (file)
@@ -28,13 +28,16 @@ import java.util.List;
 
 public final class FindbugsRuleRepository extends RuleRepository {
 
-  public FindbugsRuleRepository() {
+  private XMLRuleParser xmlRuleParser;
+
+  public FindbugsRuleRepository(XMLRuleParser xmlRuleParser) {
     super(FindbugsConstants.REPOSITORY_KEY, Java.KEY);
     setName(FindbugsConstants.REPOSITORY_NAME);
+    this.xmlRuleParser = xmlRuleParser;
   }
 
   @Override
   public List<Rule> createRules() {
-    return XMLRuleParser.parseXML(getClass().getResourceAsStream("/org/sonar/plugins/findbugs/rules.xml"));
+    return xmlRuleParser.parse(getClass().getResourceAsStream("/org/sonar/plugins/findbugs/rules.xml"));
   }
 }
index 44c279b4f6e4574b20eb01422318985f05047ce9..ab3af9d93a6b4daea3281651b56af26abbf2b779 100644 (file)
@@ -25,13 +25,14 @@ import java.util.List;
 import org.sonar.api.rules.Rule;
 import org.sonar.api.rules.RuleFinder;
 import org.sonar.api.rules.RuleQuery;
+import org.sonar.api.rules.XMLRuleParser;
 
 public class FindbugsRuleFinder implements RuleFinder {
 
   private final List<Rule> findbugsRules;
 
   public FindbugsRuleFinder() {
-    FindbugsRuleRepository repo = new FindbugsRuleRepository();
+    FindbugsRuleRepository repo = new FindbugsRuleRepository(new XMLRuleParser());
     findbugsRules = repo.createRules();
     for(Rule rule : findbugsRules){
       rule.setRepositoryKey(FindbugsConstants.REPOSITORY_KEY);
index d8c9ff8a32c2cbf9512fbd9c64f819c28febdf3c..511232b7a6d62564a46a1983f86796b425346087 100644 (file)
@@ -27,12 +27,13 @@ import java.util.List;
 
 import org.junit.Test;
 import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.XMLRuleParser;
 
 public class FindbugsRuleRepositoryTest {
 
   @Test
   public void testLoadRepositoryFromXml() {
-    FindbugsRuleRepository repository = new FindbugsRuleRepository();
+    FindbugsRuleRepository repository = new FindbugsRuleRepository(new XMLRuleParser());
     List<Rule> rules = repository.createRules();
     assertThat(rules.size(), greaterThan(300));
     for (Rule rule : rules) {
index e051e7c52a7ce355673f2395dcd0d4d6a719f04f..f805a005459d1841ca977201f53936417a6d85ce 100644 (file)
@@ -35,11 +35,7 @@ import org.mockito.stubbing.Answer;
 import org.sonar.api.CoreProperties;\r
 import org.sonar.api.profiles.RulesProfile;\r
 import org.sonar.api.resources.Java;\r
-import org.sonar.api.rules.ActiveRule;\r
-import org.sonar.api.rules.Rule;\r
-import org.sonar.api.rules.RulePriority;\r
-import org.sonar.api.rules.RuleQuery;\r
-import org.sonar.api.rules.RulesManager;\r
+import org.sonar.api.rules.*;\r
 import org.sonar.test.TestUtils;\r
 import org.xml.sax.SAXException;\r
 \r
@@ -93,7 +89,7 @@ public abstract class FindbugsTests {
     RulesProfile profile = RulesProfile.create();\r
     profile.setName(RulesProfile.SONAR_WAY_FINDBUGS_NAME);\r
     profile.setLanguage(Java.KEY);\r
-    for (Rule rule : new FindbugsRuleRepository().createRules()) {\r
+    for (Rule rule : new FindbugsRuleRepository(new XMLRuleParser()).createRules()) {\r
       rule.setRepositoryKey(FindbugsConstants.REPOSITORY_KEY);\r
       profile.activateRule(rule, null);\r
     }\r
index 19214409b08ba4e72967ed798ce0659942e9ec87..04fe1fdfe7699b9d12b60810729f09049c3ac043 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.plugins.pmd;
 
+import com.google.common.collect.Lists;
 import org.sonar.api.platform.ServerFileSystem;
 import org.sonar.api.resources.Java;
 import org.sonar.api.rules.Rule;
@@ -26,26 +27,27 @@ import org.sonar.api.rules.RuleRepository;
 import org.sonar.api.rules.XMLRuleParser;
 
 import java.io.File;
-import java.util.ArrayList;
 import java.util.List;
 
 public final class PmdRuleRepository extends RuleRepository {
 
   // for user extensions
   private ServerFileSystem fileSystem;
+  private XMLRuleParser xmlRuleParser;
 
-  public PmdRuleRepository(ServerFileSystem fileSystem) {
+  public PmdRuleRepository(ServerFileSystem fileSystem, XMLRuleParser xmlRuleParser) {
     super(PmdConstants.REPOSITORY_KEY, Java.KEY);
     setName(PmdConstants.REPOSITORY_NAME);
     this.fileSystem = fileSystem;
+    this.xmlRuleParser = xmlRuleParser;
   }
 
   @Override
   public List<Rule> createRules() {
-    List<Rule> rules = new ArrayList<Rule>();
-    rules.addAll(XMLRuleParser.parseXML(getClass().getResourceAsStream("/org/sonar/plugins/pmd/rules.xml")));
+    List<Rule> rules = Lists.newArrayList();
+    rules.addAll(xmlRuleParser.parse(getClass().getResourceAsStream("/org/sonar/plugins/pmd/rules.xml")));
     for (File userExtensionXml : fileSystem.getExtensions(PmdConstants.REPOSITORY_KEY, "xml")) {
-      rules.addAll(XMLRuleParser.parseXML(userExtensionXml));
+      rules.addAll(xmlRuleParser.parse(userExtensionXml));
     }
     return rules;
   }
index d6189e1319677f9969977b80d59d9cf888398d22..438bb9d3c4d13af139733854dd5a83f4e6895dee 100644 (file)
@@ -17,10 +17,7 @@ import org.apache.commons.lang.StringUtils;
 import org.junit.Test;
 import org.sonar.api.platform.ServerFileSystem;
 import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.rules.ActiveRule;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleFinder;
-import org.sonar.api.rules.RuleQuery;
+import org.sonar.api.rules.*;
 import org.sonar.api.utils.ValidationMessages;
 import org.sonar.plugins.pmd.xml.PmdProperty;
 import org.sonar.plugins.pmd.xml.PmdRule;
@@ -34,7 +31,7 @@ public class PmdProfileExporterTest {
   @Test
   public void testExportProfile() throws IOException, SAXException {
     ServerFileSystem fileSystem = mock(ServerFileSystem.class);
-    PmdRuleRepository repository = new PmdRuleRepository(fileSystem);
+    PmdRuleRepository repository = new PmdRuleRepository(fileSystem, new XMLRuleParser());
     List<Rule> rules = repository.createRules();
 
     RuleFinder ruleFinder = new PmdRuleFinder(rules);
index ac55ffc35f98eadef5c7dcfb873feb808091a9fe..d726c28564e2db9de54b717bb16039c1382fd589 100644 (file)
@@ -28,13 +28,14 @@ import java.util.List;
 import org.junit.Test;
 import org.sonar.api.platform.ServerFileSystem;
 import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.XMLRuleParser;
 
 public class PmdRuleRepositoryTest {
 
   @Test
   public void testLoadRepositoryFromXml() {
     ServerFileSystem fileSystem = mock(ServerFileSystem.class);
-    PmdRuleRepository repository = new PmdRuleRepository(fileSystem);
+    PmdRuleRepository repository = new PmdRuleRepository(fileSystem, new XMLRuleParser());
     List<Rule> rules = repository.createRules();
     assertThat(rules.size(), greaterThan(100));
   }
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java
new file mode 100644 (file)
index 0000000..28c7679
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.api.rules;
+
+import com.google.common.collect.Lists;
+import org.apache.commons.lang.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.ServerComponent;
+import org.sonar.api.utils.AnnotationUtils;
+import org.sonar.check.Check;
+
+import java.lang.reflect.Field;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * @since 2.3
+ */
+public final class AnnotationRuleParser implements ServerComponent {
+
+  private static final Logger LOG = LoggerFactory.getLogger(AnnotationRuleParser.class);
+
+  public List<Rule> parse(String repositoryKey, Collection<Class> annotatedClasses) {
+    List<Rule> rules = Lists.newArrayList();
+    for (Class annotatedClass : annotatedClasses) {
+      rules.add(create(repositoryKey, annotatedClass));
+    }
+    return rules;
+  }
+
+  private Rule create(String repositoryKey, Class annotatedClass) {
+    org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getClassAnnotation(annotatedClass, org.sonar.check.Rule.class);
+    if (ruleAnnotation != null) {
+      return toRule(repositoryKey, annotatedClass, ruleAnnotation);
+    }
+    Check checkAnnotation = AnnotationUtils.getClassAnnotation(annotatedClass, Check.class);
+    if (checkAnnotation != null) {
+      return toRule(repositoryKey, annotatedClass, checkAnnotation);
+    }
+    LOG.warn("The class " + annotatedClass.getCanonicalName() + " should be annotated with " + Rule.class);
+    return null;
+  }
+
+  private Rule toRule(String repositoryKey, Class clazz, org.sonar.check.Rule ruleAnnotation) {
+    String ruleKey = StringUtils.defaultIfEmpty(ruleAnnotation.key(), clazz.getCanonicalName());
+    Rule rule = Rule.create(repositoryKey, ruleKey, ruleAnnotation.name());
+    rule.setDescription(ruleAnnotation.description());
+    rule.setRulesCategory(RulesCategory.fromIsoCategory(ruleAnnotation.isoCategory()));
+    rule.setPriority(RulePriority.fromCheckPriority(ruleAnnotation.priority()));
+
+    Field[] fields = clazz.getDeclaredFields();
+    if (fields != null) {
+      for (Field field : fields) {
+        addRuleProperty(rule, field);
+      }
+    }
+
+    return rule;
+  }
+
+  private Rule toRule(String repositoryKey, Class clazz, Check checkAnnotation) {
+    String ruleKey = StringUtils.defaultIfEmpty(checkAnnotation.key(), clazz.getCanonicalName());
+    Rule rule = Rule.create(repositoryKey, ruleKey, checkAnnotation.title());
+    rule.setDescription(checkAnnotation.description());
+    rule.setRulesCategory(RulesCategory.fromIsoCategory(checkAnnotation.isoCategory()));
+    rule.setPriority(RulePriority.fromCheckPriority(checkAnnotation.priority()));
+
+    Field[] fields = clazz.getDeclaredFields();
+    if (fields != null) {
+      for (Field field : fields) {
+        addCheckProperty(rule, field);
+      }
+    }
+    return rule;
+  }
+
+  private void addRuleProperty(Rule rule, Field field) {
+    org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class);
+    if (propertyAnnotation != null) {
+      String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName());
+      RuleParam param = rule.createParameter(fieldKey);
+      param.setDescription(propertyAnnotation.description());
+      param.setDefaultValue(propertyAnnotation.defaultValue());
+    }
+  }
+
+  private void addCheckProperty(Rule rule, Field field) {
+    org.sonar.check.CheckProperty propertyAnnotation = field.getAnnotation(org.sonar.check.CheckProperty.class);
+    if (propertyAnnotation != null) {
+      String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName());
+      RuleParam param = rule.createParameter(fieldKey);
+      param.setDescription(propertyAnnotation.description());
+    }
+  }
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleRepository.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/AnnotationRuleRepository.java
deleted file mode 100644 (file)
index ed8292a..0000000
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2009 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.api.rules;
-
-import com.google.common.collect.Lists;
-import org.apache.commons.lang.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.sonar.api.utils.AnnotationUtils;
-import org.sonar.check.Check;
-
-import java.lang.reflect.Field;
-import java.util.Collection;
-import java.util.List;
-
-/**
- * @since 2.3
- */
-public final class AnnotationRuleRepository extends RuleRepository {
-
-  private static final Logger LOG = LoggerFactory.getLogger(AnnotationRuleRepository.class);
-
-  private Collection<Class> annotatedClasses;
-
-  /**
-   * Use the factory method create()
-   */
-  private AnnotationRuleRepository(String key, String language, String name, Collection<Class> annotatedClasses) {
-    super(key, language);
-    setName(name);
-    this.annotatedClasses = annotatedClasses;
-  }
-
-  public static AnnotationRuleRepository create(String key, String language, String name, Collection<Class> annotatedClasses) {
-    return new AnnotationRuleRepository(key, language, name, annotatedClasses);
-  }
-
-  @Override
-  public List<Rule> createRules() {
-    List<Rule> rules = Lists.newArrayList();
-    for (Class annotatedClass : annotatedClasses) {
-      rules.add(create(annotatedClass));
-    }
-    return rules;
-  }
-
-  private Rule create(Class annotatedClass) {
-    org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getClassAnnotation(annotatedClass, org.sonar.check.Rule.class);
-    if (ruleAnnotation != null) {
-      return toRule(annotatedClass, ruleAnnotation);
-    }
-    Check checkAnnotation = AnnotationUtils.getClassAnnotation(annotatedClass, Check.class);
-    if (checkAnnotation != null) {
-      return toRule(annotatedClass, checkAnnotation);
-    }
-    LOG.warn("The class " + annotatedClass.getCanonicalName() + " is not a check template. It should be annotated with " + Rule.class);
-    return null;
-  }
-
-  private Rule toRule(Class clazz, org.sonar.check.Rule ruleAnnotation) {
-    String key = StringUtils.defaultIfEmpty(ruleAnnotation.key(), clazz.getCanonicalName());
-    Rule rule = Rule.create(getKey(), key, ruleAnnotation.name());
-    rule.setDescription(ruleAnnotation.description());
-    rule.setRulesCategory(RulesCategory.fromIsoCategory(ruleAnnotation.isoCategory()));
-    rule.setPriority(RulePriority.fromCheckPriority(ruleAnnotation.priority()));
-
-    Field[] fields = clazz.getDeclaredFields();
-    if (fields != null) {
-      for (Field field : fields) {
-        addRuleProperty(rule, field);
-      }
-    }
-
-    return rule;
-  }
-
-  private Rule toRule(Class clazz, Check checkAnnotation) {
-    String key = StringUtils.defaultIfEmpty(checkAnnotation.key(), clazz.getCanonicalName());
-    Rule rule = Rule.create(getKey(), key, checkAnnotation.title());
-    rule.setDescription(checkAnnotation.description());
-    rule.setRulesCategory(RulesCategory.fromIsoCategory(checkAnnotation.isoCategory()));
-    rule.setPriority(RulePriority.fromCheckPriority(checkAnnotation.priority()));
-
-    Field[] fields = clazz.getDeclaredFields();
-    if (fields != null) {
-      for (Field field : fields) {
-        addCheckProperty(rule, field);
-      }
-    }
-    return rule;
-  }
-
-  private void addRuleProperty(Rule rule, Field field) {
-    org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class);
-    if (propertyAnnotation != null) {
-      String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName());
-      RuleParam param = rule.createParameter(fieldKey);
-      param.setDescription(propertyAnnotation.description());
-      param.setDefaultValue(propertyAnnotation.defaultValue());
-    }
-  }
-
-  private void addCheckProperty(Rule rule, Field field) {
-    org.sonar.check.CheckProperty propertyAnnotation = field.getAnnotation(org.sonar.check.CheckProperty.class);
-    if (propertyAnnotation != null) {
-      String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName());
-      RuleParam param = rule.createParameter(fieldKey);
-      param.setDescription(propertyAnnotation.description());
-    }
-  }
-}
index 4537da54b2758e37ec1a3212ee5dd80e61dce643..60090731ca2c0cef7887b238b3a363846ad701bd 100644 (file)
@@ -27,6 +27,7 @@ import org.codehaus.stax2.XMLInputFactory2;
 import org.codehaus.staxmate.SMInputFactory;
 import org.codehaus.staxmate.in.SMHierarchicCursor;
 import org.codehaus.staxmate.in.SMInputCursor;
+import org.sonar.api.ServerComponent;
 import org.sonar.api.utils.SonarException;
 
 import javax.xml.stream.XMLInputFactory;
@@ -38,17 +39,13 @@ import java.util.List;
 /**
  * @since 2.3
  */
-public final class XMLRuleParser {
+public final class XMLRuleParser implements ServerComponent {
 
-  private XMLRuleParser() {
-    // only static methods
-  }
-
-  public static List<Rule> parseXML(File file) {
+  public List<Rule> parse(File file) {
     Reader reader = null;
     try {
       reader = new InputStreamReader(FileUtils.openInputStream(file), CharEncoding.UTF_8);
-      return parseXML(reader);
+      return parse(reader);
 
     } catch (IOException e) {
       throw new SonarException("Fail to load the file: " + file, e);
@@ -61,11 +58,11 @@ public final class XMLRuleParser {
   /**
    * Warning : the input stream is closed in this method
    */
-  public static List<Rule> parseXML(InputStream input) {
+  public List<Rule> parse(InputStream input) {
     Reader reader = null;
     try {
       reader = new InputStreamReader(input, CharEncoding.UTF_8);
-      return parseXML(reader);
+      return parse(reader);
 
     } catch (IOException e) {
       throw new SonarException("Fail to load the xml stream", e);
@@ -75,7 +72,7 @@ public final class XMLRuleParser {
     }
   }
 
-  public static List<Rule> parseXML(Reader reader) {
+  public List<Rule> parse(Reader reader) {
     XMLInputFactory xmlFactory = XMLInputFactory2.newInstance();
     xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
     xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
index 73f41a460e305f32e075834c337c92ba1c9ac680..ebe150ff997599708e6e1438b85f0b05cd411b53 100644 (file)
@@ -35,7 +35,7 @@ public class XMLRuleParserTest {
 
   @Test
   public void parseXml() {
-    List<Rule> rules = XMLRuleParser.parseXML(getClass().getResourceAsStream("/org/sonar/api/rules/XMLRuleParserTest/rules.xml"));
+    List<Rule> rules = new XMLRuleParser().parse(getClass().getResourceAsStream("/org/sonar/api/rules/XMLRuleParserTest/rules.xml"));
     assertThat(rules.size(), is(2));
 
     Rule rule = rules.get(0);
@@ -59,17 +59,17 @@ public class XMLRuleParserTest {
 
   @Test(expected = SonarException.class)
   public void failIfMissingRuleKey() {
-    XMLRuleParser.parseXML(new StringReader("<rules><rule><name>Foo</name></rule></rules>"));
+    new XMLRuleParser().parse(new StringReader("<rules><rule><name>Foo</name></rule></rules>"));
   }
 
   @Test(expected = SonarException.class)
   public void failIfMissingPropertyKey() {
-    XMLRuleParser.parseXML(new StringReader("<rules><rule><key>foo</key><name>Foo</name><param></param></rule></rules>"));
+    new XMLRuleParser().parse(new StringReader("<rules><rule><key>foo</key><name>Foo</name><param></param></rule></rules>"));
   }
 
   @Test
   public void utf8Encoding() {
-    List<Rule> rules = XMLRuleParser.parseXML(getClass().getResourceAsStream("/org/sonar/api/rules/XMLRuleParserTest/utf8.xml"));
+    List<Rule> rules = new XMLRuleParser().parse(getClass().getResourceAsStream("/org/sonar/api/rules/XMLRuleParserTest/utf8.xml"));
     assertThat(rules.size(), is(1));
     Rule rule = rules.get(0);
     assertThat(rule.getKey(), is("com.puppycrawl.tools.checkstyle.checks.naming.LocalVariableNameCheck"));
@@ -82,7 +82,7 @@ public class XMLRuleParserTest {
   @Test
   public void supportDeprecatedFormat() {
     // the deprecated format uses some attributes instead of nodes
-    List<Rule> rules = XMLRuleParser.parseXML(getClass().getResourceAsStream("/org/sonar/api/rules/XMLRuleParserTest/deprecated.xml"));
+    List<Rule> rules = new XMLRuleParser().parse(getClass().getResourceAsStream("/org/sonar/api/rules/XMLRuleParserTest/deprecated.xml"));
     assertThat(rules.size(), is(1));
     Rule rule = rules.get(0);
     assertThat(rule.getPriority(), Is.is(RulePriority.CRITICAL));
index 48c321c0c0254d5cf49aa26fd061f8fb060a1faa..cfbcda01422cd5ce3eab1a97029c508846e04971 100644 (file)
@@ -33,6 +33,7 @@ import org.sonar.api.profiles.XMLProfileParser;
 import org.sonar.api.profiles.XMLProfileSerializer;
 import org.sonar.api.resources.Languages;
 import org.sonar.api.rules.DefaultRulesManager;
+import org.sonar.api.rules.XMLRuleParser;
 import org.sonar.api.utils.HttpDownloader;
 import org.sonar.api.utils.IocContainer;
 import org.sonar.api.utils.TimeProfiler;
@@ -175,6 +176,7 @@ public final class Platform {
     servicesContainer.as(Characteristics.CACHE).addComponent(AnnotationProfileParser.class);
     servicesContainer.as(Characteristics.CACHE).addComponent(XMLProfileParser.class);
     servicesContainer.as(Characteristics.CACHE).addComponent(XMLProfileSerializer.class);
+    servicesContainer.as(Characteristics.CACHE).addComponent(XMLRuleParser.class);
     servicesContainer.as(Characteristics.CACHE).addComponent(DefaultRuleFinder.class);
     servicesContainer.as(Characteristics.CACHE).addComponent(DeprecatedRuleRepositories.class);
     servicesContainer.as(Characteristics.CACHE).addComponent(DeprecatedProfiles.class);