/*
 * The MIT License
 *
 * Copyright 2016 Fundacion Jala.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.fundacionjala.enforce.sonarqube.apex.rules;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.fundacionjala.enforce.sonarqube.apex.Apex;
import org.sonar.api.server.rule.RulesDefinition;
import org.sonar.api.server.rule.RulesDefinitionXmlLoader;

/**
 *
 */
public class ApexRulesDefinition implements RulesDefinition {

    protected static final String KEY = "apexlint";
    protected static final String NAME = "apexLint";

    protected String rulesDefinitionFilePath() {
        return "/rules.xml";
    }

    private void defineRulesForLanguage(Context context, String repositoryKey, String repositoryName, String languageKey) {
        NewRepository repository = context.createRepository(repositoryKey, languageKey).setName(repositoryName);

        InputStream rulesXml = this.getClass().getResourceAsStream(rulesDefinitionFilePath());
        if (rulesXml != null) {
            RulesDefinitionXmlLoader rulesLoader = new RulesDefinitionXmlLoader();
            rulesLoader.load(repository, rulesXml, StandardCharsets.UTF_8.name());
        }

        repository.done();
    }

    @Override
    public void define(Context context) {
        String repositoryKey = getRepositoryKeyForLanguage(Apex.KEY);
        String repositoryName = getRepositoryNameForLanguage(Apex.KEY);
        defineRulesForLanguage(context, repositoryKey, repositoryName, Apex.KEY);
    }

    public static String getRepositoryKeyForLanguage(String languageKey) {
        return languageKey.toLowerCase() + "-" + KEY;
    }

    public static String getRepositoryNameForLanguage(String languageKey) {
        return languageKey.toUpperCase() + " " + NAME;
    }

}

@@ -1199,69 +1199,53 @@
 ort 
-java.io.InputStream;%0Aimport java.nio.charset.StandardCharsets
+org.fundacionjala.enforce.sonarqube.apex.Apex
 ;%0Aim
@@ -1282,36 +1282,48 @@
 .sonarqube.apex.
-Apex
+checks.CheckList
 ;%0Aimport org.son
@@ -1375,32 +1375,55 @@
 g.sonar.
-api.server.rule.
+squidbridge.annotations.AnnotationBased
 RulesDef
@@ -1429,25 +1429,16 @@
 finition
-XmlLoader
 ;%0A%0A/**%0A 
@@ -1514,725 +1514,172 @@
     
-protected static final String KEY = %22apexlint%22;%0A    protected static final String NAME = %22apexLint%22;%0A%0A    protected String rulesDefinitionFilePath() %7B%0A        return %22/rules.xml%22;%0A    %7D%0A%0A    private void defineRulesForLanguage(Context context, String repositoryKey, String repositoryName, String languageKey) %7B%0A        NewRepository repository = context.createRepository(repositoryKey, languageKey).setName(repositoryName);%0A%0A        InputStream rulesXml = this.getClass().getResourceAsStream(rulesDefinitionFilePath());%0A        if (rulesXml != null) %7B%0A            RulesDefinitionXmlLoader rulesLoader = new RulesDefinitionXmlLoader();%0A            rulesLoader.load(repository, rulesXml, StandardCharsets.UTF_8.name());
+@Override%0A    public void define(Context context) %7B%0A        NewRepository repository = context%0A                .createRepository(CheckList.REPOSITORY_KEY, Apex.KEY)
 %0A   
@@ -1687,19 +1687,16 @@
     
-%7D%0A%0A
         
 repo
@@ -1695,168 +1695,54 @@
     
-repository.done();%0A    %7D%0A%0A    @Override%0A    public void define(Context context) %7B%0A        String repositoryKey = getRepositoryKeyForLanguage(Apex.KEY
+.setName(CheckList.REPOSITORY_NAME
 );%0A
+%0A
         
 Stri
@@ -1741,127 +1741,44 @@
     
-String repositoryName = getRepositoryNameForLanguage(Apex.KEY);%0A        defineRulesForLanguage(context, repositoryKey, 
+AnnotationBasedRulesDefinition.load(
 repo
@@ -1783,20 +1783,16 @@
 pository
-Name
 , Apex.K
@@ -1797,290 +1797,66 @@
 .KEY
-);%0A    %7D%0A%0A    public static String getRepositoryKeyForLanguage(String languageKey) %7B%0A        return languageKey.toLowerCase() + %22-%22 + KEY
+, CheckList.getChecks())
 ;%0A    
-%7D%0A%0A
     
-public static String getRepositoryNameForLanguage(String languageKey) %7B%0A        return languageKey.toUpperCase() + %22 %22 + NAME
+repository.done()
 ;%0A    %7D%0A
 %0A%7D%0A
@@ -1852,11 +1852,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Refactor ApexRulesDefinition