001/*
002 * SonarQube
003 * Copyright (C) 2009-2016 SonarSource SA
004 * mailto:contact 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.server.ServerSide;
024import 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@ServerSide
039public class RulesDefinitionI18nLoader {
040
041  private final RuleI18n i18n;
042
043  public RulesDefinitionI18nLoader(RuleI18n i18n) {
044    this.i18n = i18n;
045  }
046
047  /**
048   * Loads descriptions of rules and related rule parameters. Existing descriptions
049   * are overridden if English labels exist in bundles.
050   */
051  public void load(RulesDefinition.NewRepository repo) {
052    for (RulesDefinition.NewRule rule : repo.rules()) {
053      String name = i18n.getName(repo.key(), rule.key());
054      if (StringUtils.isNotBlank(name)) {
055        rule.setName(name);
056      }
057
058      String desc = i18n.getDescription(repo.key(), rule.key());
059      if (StringUtils.isNotBlank(desc)) {
060        rule.setHtmlDescription(desc);
061      }
062
063      for (RulesDefinition.NewParam param : rule.params()) {
064        String paramDesc = i18n.getParamDescription(repo.key(), rule.key(), param.key());
065        if (StringUtils.isNotBlank(paramDesc)) {
066          param.setDescription(paramDesc);
067        }
068      }
069    }
070  }
071}