org.sonar.plugins.checkstyle.CheckstyleProfileExporter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sonar-checkstyle-plugin Show documentation
Show all versions of sonar-checkstyle-plugin Show documentation
Checkstyle is a code analyser to help programmers write Java code that adheres to a coding standard.
The newest version!
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2008-2012 SonarSource
* 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.plugins.checkstyle;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import org.apache.commons.configuration.BaseConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.profiles.ProfileExporter;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.Java;
import org.sonar.api.rules.ActiveRule;
import org.sonar.api.rules.RuleParam;
import org.sonar.api.utils.SonarException;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
public class CheckstyleProfileExporter extends ProfileExporter {
static final String DOCTYPE_DECLARATION = "";
private Configuration conf;
public CheckstyleProfileExporter(Configuration conf) {
super(CheckstyleConstants.REPOSITORY_KEY, CheckstyleConstants.PLUGIN_NAME);
this.conf = conf;
setSupportedLanguages(Java.KEY);
setMimeType("application/xml");
}
/**
* for unit tests
*/
CheckstyleProfileExporter() {
this(new BaseConfiguration());
}
@Override
public void exportProfile(RulesProfile profile, Writer writer) {
try {
ListMultimap activeRulesByConfigKey = arrangeByConfigKey(profile.getActiveRulesByRepository(CheckstyleConstants.REPOSITORY_KEY));
generateXML(writer, activeRulesByConfigKey);
} catch (IOException e) {
throw new SonarException("Fail to export the profile " + profile, e);
}
}
private void generateXML(Writer writer, ListMultimap activeRulesByConfigKey) throws IOException {
appendXmlHeader(writer);
appendCustomFilters(writer);
appendCheckerModules(writer, activeRulesByConfigKey);
appendTreeWalker(writer, activeRulesByConfigKey);
appendXmlFooter(writer);
}
private void appendXmlHeader(Writer writer) throws IOException {
writer.append(""
+ DOCTYPE_DECLARATION
+ ""
+ "");
}
private void appendCustomFilters(Writer writer) throws IOException {
String filtersXML = conf.getString(CheckstyleConstants.FILTERS_KEY, CheckstyleConstants.FILTERS_DEFAULT_VALUE);
if (StringUtils.isNotBlank(filtersXML)) {
writer.append(filtersXML);
}
}
private void appendCheckerModules(Writer writer, ListMultimap activeRulesByConfigKey) throws IOException {
for (String configKey : activeRulesByConfigKey.keySet()) {
if (!isInTreeWalker(configKey)) {
List activeRules = activeRulesByConfigKey.get(configKey);
for (ActiveRule activeRule : activeRules) {
appendModule(writer, activeRule);
}
}
}
}
private void appendTreeWalker(Writer writer, ListMultimap activeRulesByConfigKey) throws IOException {
writer.append("");
writer.append(" ");
for (String configKey : activeRulesByConfigKey.keySet()) {
if (isInTreeWalker(configKey)) {
List activeRules = activeRulesByConfigKey.get(configKey);
for (ActiveRule activeRule : activeRules) {
appendModule(writer, activeRule);
}
}
}
writer.append(" ");
}
private void appendXmlFooter(Writer writer) throws IOException {
writer.append(" ");
}
static boolean isInTreeWalker(String configKey) {
return StringUtils.startsWithIgnoreCase(configKey, "Checker/TreeWalker/");
}
private static ListMultimap arrangeByConfigKey(List activeRules) {
ListMultimap result = ArrayListMultimap.create();
if (activeRules != null) {
for (ActiveRule activeRule : activeRules) {
result.put(activeRule.getConfigKey(), activeRule);
}
}
return result;
}
private void appendModule(Writer writer, ActiveRule activeRule) throws IOException {
String moduleName = StringUtils.substringAfterLast(activeRule.getConfigKey(), "/");
writer.append("");
if (activeRule.getRule().getParent() != null) {
appendModuleProperty(writer, "id", activeRule.getRuleKey());
}
appendModuleProperty(writer, "severity", CheckstyleSeverityUtils.toSeverity(activeRule.getSeverity()));
appendRuleParameters(writer, activeRule);
writer.append(" ");
}
private void appendRuleParameters(Writer writer, ActiveRule activeRule) throws IOException {
for (RuleParam ruleParam : activeRule.getRule().getParams()) {
String value = activeRule.getParameter(ruleParam.getKey());
if (StringUtils.isNotBlank(value)) {
appendModuleProperty(writer, ruleParam.getKey(), value);
}
}
}
private void appendModuleProperty(Writer writer, String propertyKey, String propertyValue) throws IOException {
if (StringUtils.isNotBlank(propertyValue)) {
writer.append(" ");
}
}
}