001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program 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 * This program 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 */
020package org.sonar.api.server.rule;
021
022import org.apache.commons.lang.StringUtils;
023import org.sonar.api.i18n.RuleI18n;
024
025/**
026 * Loads the English bundles of rules (name, description and parameters) that are
027 * deprecated since 4.2. It can be useful when loading existing XML files that
028 * do not contain rule names and descriptions.
029 * <br>
030 * This class must be executed after declaring rules on {@link RulesDefinition.NewRepository}.
031 * <br>
032 * Note that localization of rules was dropped in version 4.2. All texts are English.
033 *
034 * @see org.sonar.api.server.rule.RulesDefinition for an example
035 * @since 4.3
036 */
037public class RulesDefinitionI18nLoader {
038
039  private final RuleI18n i18n;
040
041  public RulesDefinitionI18nLoader(RuleI18n i18n) {
042    this.i18n = i18n;
043  }
044
045  /**
046   * Loads descriptions of rules and related rule parameters. Existing descriptions
047   * are overridden if English labels exist in bundles.
048   */
049  public void load(RulesDefinition.NewRepository repo) {
050    for (RulesDefinition.NewRule rule : repo.rules()) {
051      String name = i18n.getName(repo.key(), rule.key());
052      if (StringUtils.isNotBlank(name)) {
053        rule.setName(name);
054      }
055
056      String desc = i18n.getDescription(repo.key(), rule.key());
057      if (StringUtils.isNotBlank(desc)) {
058        rule.setHtmlDescription(desc);
059      }
060
061      for (RulesDefinition.NewParam param : rule.params()) {
062        String paramDesc = i18n.getParamDescription(repo.key(), rule.key(), param.key());
063        if (StringUtils.isNotBlank(paramDesc)) {
064          param.setDescription(paramDesc);
065        }
066      }
067    }
068  }
069}