001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.api.server.rule;
021
022 import org.apache.commons.lang.StringUtils;
023 import org.sonar.api.ServerComponent;
024 import org.sonar.api.i18n.RuleI18n;
025
026 /**
027 * Loads the English bundles of rules (name, description and parameters) that are
028 * deprecated since 4.2. It can be useful when loading existing XML files that
029 * do not contain rule names and descriptions.
030 * <p/>
031 * This class must be executed after declaring rules on {@link RulesDefinition.NewRepository}.
032 * <p/>
033 * Note that localization of rules was dropped in version 4.2. All texts are English.
034 *
035 * @see org.sonar.api.server.rule.RulesDefinition for an example
036 * @since 4.3
037 */
038 public class RulesDefinitionI18nLoader implements ServerComponent {
039
040 private final RuleI18n i18n;
041
042 public RulesDefinitionI18nLoader(RuleI18n i18n) {
043 this.i18n = i18n;
044 }
045
046 /**
047 * Loads descriptions of rules and related rule parameters. Existing descriptions
048 * are overridden if English labels exist in bundles.
049 */
050 public void load(RulesDefinition.NewRepository repo) {
051 for (RulesDefinition.NewRule rule : repo.rules()) {
052 String name = i18n.getName(repo.key(), rule.key());
053 if (StringUtils.isNotBlank(name)) {
054 rule.setName(name);
055 }
056
057 String desc = i18n.getDescription(repo.key(), rule.key());
058 if (StringUtils.isNotBlank(desc)) {
059 rule.setHtmlDescription(desc);
060 }
061
062 for (RulesDefinition.NewParam param : rule.params()) {
063 String paramDesc = i18n.getParamDescription(repo.key(), rule.key(), param.key());
064 if (StringUtils.isNotBlank(paramDesc)) {
065 param.setDescription(paramDesc);
066 }
067 }
068 }
069 }
070 }