123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- /*
- * 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.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.ActiveRuleParam;
- 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 {
-
- 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());
- }
-
- public void exportProfile(RulesProfile profile, Writer writer) {
- try {
- ListMultimap<String, ActiveRule> 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<String, ActiveRule> activeRulesByConfigKey) throws IOException {
- appendXmlHeader(writer);
- appendCustomFilters(writer);
- appendCheckerModules(writer, activeRulesByConfigKey);
- appendTreeWalker(writer, activeRulesByConfigKey);
- appendXmlFooter(writer);
- }
-
- private void appendXmlHeader(Writer writer) throws IOException {
- writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
- + "<!DOCTYPE module PUBLIC \"-//Puppy Crawl//DTD Check Configuration 1.2//EN\" \"http://www.puppycrawl.com/dtds/configuration_1_2.dtd\">"
- + "<!-- Generated by Sonar -->"
- + "<module name=\"Checker\">");
- }
-
- 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<String, ActiveRule> activeRulesByConfigKey) throws IOException {
- for (String configKey : activeRulesByConfigKey.keySet()) {
- if (!isInTreeWalker(configKey)) {
- List<ActiveRule> activeRules = activeRulesByConfigKey.get(configKey);
- for (ActiveRule activeRule : activeRules) {
- appendModule(writer, activeRule, activeRules.size() > 1);
- }
- }
- }
- }
-
- private void appendTreeWalker(Writer writer, ListMultimap<String, ActiveRule> activeRulesByConfigKey) throws IOException {
- writer.append("<module name=\"TreeWalker\">");
- writer.append("<module name=\"FileContentsHolder\"/> ");
- for (String configKey : activeRulesByConfigKey.keySet()) {
- if (isInTreeWalker(configKey)) {
- List<ActiveRule> activeRules = activeRulesByConfigKey.get(configKey);
- for (ActiveRule activeRule : activeRules) {
- appendModule(writer, activeRule, activeRules.size() > 1);
- }
- }
- }
- writer.append("</module>");
- }
-
- private void appendXmlFooter(Writer writer) throws IOException {
- writer.append("</module>");
- }
-
- static boolean isInTreeWalker(String configKey) {
- return StringUtils.startsWithIgnoreCase(configKey, "Checker/TreeWalker/");
- }
-
- private static ListMultimap<String, ActiveRule> arrangeByConfigKey(List<ActiveRule> activeRules) {
- ListMultimap<String, ActiveRule> result = ArrayListMultimap.create();
- if (activeRules != null) {
- for (ActiveRule activeRule : activeRules) {
- result.put(activeRule.getConfigKey(), activeRule);
- }
- }
- return result;
- }
-
- private void appendModule(Writer writer, ActiveRule activeRule, boolean manyInstances) throws IOException {
- String moduleName = StringUtils.substringAfterLast(activeRule.getConfigKey(), "/");
- writer.append("<module name=\"");
- StringEscapeUtils.escapeXml(writer, moduleName);
- writer.append("\">");
- if (manyInstances) {
- appendModuleProperty(writer, "id", activeRule.getRuleKey());
- }
- appendModuleProperty(writer, "severity", CheckstyleSeverityUtils.toSeverity(activeRule.getPriority()));
- appendRuleParameters(writer, activeRule);
- writer.append("</module>");
- }
-
- 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("<property name=\"");
- StringEscapeUtils.escapeXml(writer, propertyKey);
- writer.append("\" value=\"");
- StringEscapeUtils.escapeXml(writer, propertyValue);
- writer.append("\"/>");
- }
- }
-
-
- }
|