// Path: src/main/java/org/sonar/plugins/checkstyle/rule/ActiveRuleWrapper.java
// public interface ActiveRuleWrapper {
//     String getInternalKey();
// 
//     String getRuleKey();
// 
//     String getTemplateRuleKey();
// 
//     String getSeverity();
// 
//     Map<String, String> getParams();
// }
// 
// Path: src/main/java/org/sonar/plugins/checkstyle/rule/ActiveRuleWrapperScannerImpl.java
// public class ActiveRuleWrapperScannerImpl implements ActiveRuleWrapper {
//     private final ActiveRule activeRule;
// 
//     public ActiveRuleWrapperScannerImpl(ActiveRule activeRule) {
//         this.activeRule = activeRule;
//     }
// 
//     @Override
//     public String getInternalKey() {
//         return activeRule.internalKey();
//     }
// 
//     @Override
//     public String getRuleKey() {
//         return activeRule.ruleKey().rule();
//     }
// 
//     @Override
//     public String getTemplateRuleKey() {
//         return activeRule.templateRuleKey();
//     }
// 
//     @Override
//     public String getSeverity() {
//         return CheckstyleSeverityUtils.toSeverity(activeRule.severity());
//     }
// 
//     @Override
//     public Map<String, String> getParams() {
//         return activeRule.params();
//     }
// }
// 
// Path: src/main/java/org/sonar/plugins/checkstyle/rule/ActiveRuleWrapperServerImpl.java
// public class ActiveRuleWrapperServerImpl implements ActiveRuleWrapper {
//     private final ActiveRule activeRule;
// 
//     public ActiveRuleWrapperServerImpl(ActiveRule activeRule) {
//         this.activeRule = activeRule;
//     }
// 
//     @Override
//     public String getInternalKey() {
//         return activeRule.getConfigKey();
//     }
// 
//     @Override
//     public String getRuleKey() {
//         return activeRule.getRuleKey();
//     }
// 
//     @Override
//     public String getTemplateRuleKey() {
//         String result = null;
//         if (Objects.nonNull(activeRule.getRule().getTemplate())) {
//             result = activeRule.getRule().getTemplate().getKey();
//         }
//         return result;
//     }
// 
//     @Override
//     public String getSeverity() {
//         return CheckstyleSeverityUtils.toSeverity(activeRule.getSeverity());
//     }
// 
//     @Override
//     public Map<String, String> getParams() {
//         final Map<String, String> result = new HashMap<>();
//         for (RuleParam param : activeRule.getRule().getParams()) {
//             final String value = activeRule.getParameter(param.getKey());
//             if (StringUtils.isNotBlank(value)) {
//                 result.put(param.getKey(), value);
//             }
//         }
//         return result;
//     }
// }


import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.ExtensionPoint;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.config.Configuration;
import org.sonar.api.profiles.ProfileExporter;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.rules.ActiveRule;
import org.sonar.plugins.checkstyle.rule.ActiveRuleWrapper;
import org.sonar.plugins.checkstyle.rule.ActiveRuleWrapperScannerImpl;
import org.sonar.plugins.checkstyle.rule.ActiveRuleWrapperServerImpl;
import com.google.common.annotations.VisibleForTesting;
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2022 the original author or authors.
//
// This library 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.
//
// This library 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 this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////

package org.sonar.plugins.checkstyle;





@ExtensionPoint
@ScannerSide
public class CheckstyleProfileExporter extends ProfileExporter {

    public static final String DOCTYPE_DECLARATION =
        "<!DOCTYPE module PUBLIC \"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN\" "
        + "\"https://checkstyle.org/dtds/configuration_1_3.dtd\">";
    private static final String CLOSE_MODULE = "</module>";

    private final Configuration configuration;

    public CheckstyleProfileExporter(Configuration configuration) {
        super(CheckstyleConstants.REPOSITORY_KEY, CheckstyleConstants.PLUGIN_NAME);
        this.configuration = configuration;
        setSupportedLanguages(CheckstyleConstants.JAVA_KEY);
        setMimeType("application/xml");
    }

    @Override
    public void exportProfile(RulesProfile profile, Writer writer) {
        try {
            final List<ActiveRule> activeRules = profile
                    .getActiveRulesByRepository(CheckstyleConstants.REPOSITORY_KEY);
            if (activeRules != null) {
                final List<ActiveRuleWrapper> activeRuleWrappers = new ArrayList<>();
                for (ActiveRule activeRule : activeRules) {
// Path: src/main/java/org/sonar/plugins/checkstyle/rule/ActiveRuleWrapper.java
// public interface ActiveRuleWrapper {
//     String getInternalKey();
// 
//     String getRuleKey();
// 
//     String getTemplateRuleKey();
// 
//     String getSeverity();
// 
//     Map<String, String> getParams();
// }
// 
// Path: src/main/java/org/sonar/plugins/checkstyle/rule/ActiveRuleWrapperScannerImpl.java
// public class ActiveRuleWrapperScannerImpl implements ActiveRuleWrapper {
//     private final ActiveRule activeRule;
// 
//     public ActiveRuleWrapperScannerImpl(ActiveRule activeRule) {
//         this.activeRule = activeRule;
//     }
// 
//     @Override
//     public String getInternalKey() {
//         return activeRule.internalKey();
//     }
// 
//     @Override
//     public String getRuleKey() {
//         return activeRule.ruleKey().rule();
//     }
// 
//     @Override
//     public String getTemplateRuleKey() {
//         return activeRule.templateRuleKey();
//     }
// 
//     @Override
//     public String getSeverity() {
//         return CheckstyleSeverityUtils.toSeverity(activeRule.severity());
//     }
// 
//     @Override
//     public Map<String, String> getParams() {
//         return activeRule.params();
//     }
// }
// 
// Path: src/main/java/org/sonar/plugins/checkstyle/rule/ActiveRuleWrapperServerImpl.java
// public class ActiveRuleWrapperServerImpl implements ActiveRuleWrapper {
//     private final ActiveRule activeRule;
// 
//     public ActiveRuleWrapperServerImpl(ActiveRule activeRule) {
//         this.activeRule = activeRule;
//     }
// 
//     @Override
//     public String getInternalKey() {
//         return activeRule.getConfigKey();
//     }
// 
//     @Override
//     public String getRuleKey() {
//         return activeRule.getRuleKey();
//     }
// 
//     @Override
//     public String getTemplateRuleKey() {
//         String result = null;
//         if (Objects.nonNull(activeRule.getRule().getTemplate())) {
//             result = activeRule.getRule().getTemplate().getKey();
//         }
//         return result;
//     }
// 
//     @Override
//     public String getSeverity() {
//         return CheckstyleSeverityUtils.toSeverity(activeRule.getSeverity());
//     }
// 
//     @Override
//     public Map<String, String> getParams() {
//         final Map<String, String> result = new HashMap<>();
//         for (RuleParam param : activeRule.getRule().getParams()) {
//             final String value = activeRule.getParameter(param.getKey());
//             if (StringUtils.isNotBlank(value)) {
//                 result.put(param.getKey(), value);
//             }
//         }
//         return result;
//     }
// }


// Path: src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.ExtensionPoint;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.config.Configuration;
import org.sonar.api.profiles.ProfileExporter;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.rules.ActiveRule;
import org.sonar.plugins.checkstyle.rule.ActiveRuleWrapper;
import org.sonar.plugins.checkstyle.rule.ActiveRuleWrapperScannerImpl;
import org.sonar.plugins.checkstyle.rule.ActiveRuleWrapperServerImpl;
import com.google.common.annotations.VisibleForTesting;

////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2022 the original author or authors.
//
// This library 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.
//
// This library 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 this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////

package org.sonar.plugins.checkstyle;





@ExtensionPoint
@ScannerSide
public class CheckstyleProfileExporter extends ProfileExporter {

    public static final String DOCTYPE_DECLARATION =
        "<!DOCTYPE module PUBLIC \"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN\" "
        + "\"https://checkstyle.org/dtds/configuration_1_3.dtd\">";
    private static final String CLOSE_MODULE = "</module>";

    private final Configuration configuration;

    public CheckstyleProfileExporter(Configuration configuration) {
        super(CheckstyleConstants.REPOSITORY_KEY, CheckstyleConstants.PLUGIN_NAME);
        this.configuration = configuration;
        setSupportedLanguages(CheckstyleConstants.JAVA_KEY);
        setMimeType("application/xml");
    }

    @Override
    public void exportProfile(RulesProfile profile, Writer writer) {
        try {
            final List<ActiveRule> activeRules = profile
                    .getActiveRulesByRepository(CheckstyleConstants.REPOSITORY_KEY);
            if (activeRules != null) {
                final List<ActiveRuleWrapper> activeRuleWrappers = new ArrayList<>();
                for (ActiveRule activeRule : activeRules) {
                    activeRuleWrappers.add(new ActiveRuleWrapperServerImpl(activeRule));