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