/* * 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 org.sonar.api.CoreProperties; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.Java; import org.sonar.api.rules.*; import org.sonar.plugins.checkstyle.xml.Module; import org.sonar.plugins.checkstyle.xml.Property; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class CheckstyleRulesRepository extends AbstractImportableRulesRepository implements ConfigurationExportable { public CheckstyleRulesRepository(Java java) { super(java, new CheckstyleRulePriorityMapper()); } @Override public String getRepositoryResourcesBase() { return "org/sonar/plugins/checkstyle"; } @Override public Map getBuiltInProfiles() { Map defaults = new HashMap(); defaults.put(RulesProfile.SONAR_WAY_NAME, "profile-sonar-way.xml"); defaults.put(RulesProfile.SONAR_WAY_FINDBUGS_NAME, "profile-sonar-way.xml"); defaults.put(RulesProfile.SUN_CONVENTIONS_NAME, "profile-sun-conventions.xml"); return defaults; } public String exportConfiguration(RulesProfile activeProfile) { Module module = toXStream(activeProfile.getActiveRulesByPlugin(CoreProperties.CHECKSTYLE_PLUGIN)); module.getOrCreateChild("TreeWalker" + Module.MODULE_SEPARATOR + "FileContentsHolder"); module.getOrCreateChild("SuppressionCommentFilter"); return addXmlHeader(module.toXml()); } public List importConfiguration(String xml, List rules) { List activeRules = new ArrayList(); Module module = Module.fromXml(xml); buildActiveRulesFromXStream(module, "", activeRules, rules); return activeRules; } protected Module toXStream(List activeRules) { Module root = new Module(""); for (ActiveRule activeRule : activeRules) { if (activeRule.getRule().getPluginName().equals(CoreProperties.CHECKSTYLE_PLUGIN)) { String configKey = activeRule.getRule().getConfigKey(); Module module = root.getOrCreateChild(configKey); List properties = new ArrayList(); if(!activeRule.getRuleKey().equals("com.puppycrawl.tools.checkstyle.filter.SuppressionCommentFilter")) { properties.add(new Property("severity", getRulePriorityMapper().to(activeRule.getPriority()))); } for (ActiveRuleParam activeRuleParam : activeRule.getActiveRuleParams()) { if (activeRuleParam.getValue() != null && activeRuleParam.getValue().length() != 0) { properties.add(new Property(activeRuleParam.getRuleParam().getKey(), activeRuleParam.getValue())); } } module.setProperties(properties); } } if (root.getChildren().isEmpty()) { return new Module("Checker", null); } return root.getChildren().get(0); } protected String addXmlHeader(String xml) { return new StringBuilder() .append("\n") .append("") .append("\n") .append(xml) .toString(); } protected void buildActiveRulesFromXStream(Module module, String modulePath, List activeRules, List rules) { if (module.getChildren() == null || module.getChildren().isEmpty()) { for (Rule rule : rules) { if (rule.getConfigKey().equals(modulePath)) { RulePriority rulePriority = getRulePriority(module); ActiveRule activeRule = new ActiveRule(null, rule, rulePriority); activeRule.setActiveRuleParams(getActiveRuleParams(module, rule, activeRule)); activeRules.add(activeRule); return; } } } else { String baseModulePath = modulePath.length() == 0 ? module.getName() + Module.MODULE_SEPARATOR : modulePath + Module.MODULE_SEPARATOR; for (Module child : module.getChildren()) { buildActiveRulesFromXStream(child, baseModulePath + child.getName(), activeRules, rules); } } } private RulePriority getRulePriority(Module module) { if (module.getProperties() != null) { for (Property property : module.getProperties()) { if ("severity".equals(property.getName())) { return getRulePriorityMapper().from(property.getValue()); } } } return null; } private List getActiveRuleParams(Module module, Rule rule, ActiveRule activeRule) { List activeRuleParams = new ArrayList(); if (module.getProperties() != null) { for (Property property : module.getProperties()) { if (rule.getParams() != null) { for (RuleParam ruleParam : rule.getParams()) { if (ruleParam.getKey().equals(property.getName())) { activeRuleParams.add(new ActiveRuleParam(activeRule, ruleParam, property.getValue())); } } } } } return activeRuleParams; } }