All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonarsource.dotnet.shared.plugins.AbstractSonarLintProfileExporter Maven / Gradle / Ivy

There is a newer version: 9.32.0.97167
Show newest version
/*
 * SonarSource :: .NET :: Shared library
 * Copyright (C) 2014-2024 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program 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 program 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 program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonarsource.dotnet.shared.plugins;

import java.io.IOException;
import java.io.Writer;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import org.sonar.api.profiles.ProfileExporter;
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;

public abstract class AbstractSonarLintProfileExporter extends ProfileExporter {
  private final String analyzerName;
  private final String repositoryKey;
  private final RuleFinder ruleFinder;

  public AbstractSonarLintProfileExporter(String profileKey, String profileName, String languageKey, String analyzerName, String repositoryKey, RuleFinder ruleFinder) {
    super(profileKey, profileName);
    this.ruleFinder = ruleFinder;
    setSupportedLanguages(languageKey);
    this.analyzerName = analyzerName;
    this.repositoryKey = repositoryKey;
  }

  @Override
  public void exportProfile(RulesProfile ruleProfile, Writer writer) {
    Set allRuleKeys = ruleFinder.findAll(RuleQuery.create().withRepositoryKey(repositoryKey))
      .stream()
      .map(Rule::getKey)
      .collect(Collectors.toSet());
    Set disabledRuleKeys = new HashSet<>(allRuleKeys);

    appendLine(writer, "");
    appendLine(writer, "");
    appendLine(writer, "  ");

    for (ActiveRule activeRule : ruleProfile.getActiveRulesByRepository(repositoryKey)) {
      Rule rule = activeRule.getRule();
      disabledRuleKeys.remove(rule.getKey());

      appendLine(writer, "    ");
    }

    for (String disableRuleKey : disabledRuleKeys) {
      appendLine(writer, "    ");
    }

    appendLine(writer, "  ");
    appendLine(writer, "");
  }

  private static void appendLine(Writer writer, String line) {
    try {
      writer.write(line);
      writer.write("\r\n");
    } catch (IOException e) {
      throw new IllegalStateException(e);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy