You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RulesDefinitionI18nLoader.java 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.api.server.rule;
  21. import org.apache.commons.lang.StringUtils;
  22. import org.sonar.api.i18n.RuleI18n;
  23. /**
  24. * Loads the English bundles of rules (name, description and parameters) that are
  25. * deprecated since 4.2. It can be useful when loading existing XML files that
  26. * do not contain rule names and descriptions.
  27. * <br>
  28. * This class must be executed after declaring rules on {@link RulesDefinition.NewRepository}.
  29. * <br>
  30. * Note that localization of rules was dropped in version 4.2. All texts are English.
  31. *
  32. * @see org.sonar.api.server.rule.RulesDefinition for an example
  33. * @since 4.3
  34. */
  35. public class RulesDefinitionI18nLoader {
  36. private final RuleI18n i18n;
  37. public RulesDefinitionI18nLoader(RuleI18n i18n) {
  38. this.i18n = i18n;
  39. }
  40. /**
  41. * Loads descriptions of rules and related rule parameters. Existing descriptions
  42. * are overridden if English labels exist in bundles.
  43. */
  44. public void load(RulesDefinition.NewRepository repo) {
  45. for (RulesDefinition.NewRule rule : repo.rules()) {
  46. String name = i18n.getName(repo.key(), rule.key());
  47. if (StringUtils.isNotBlank(name)) {
  48. rule.setName(name);
  49. }
  50. String desc = i18n.getDescription(repo.key(), rule.key());
  51. if (StringUtils.isNotBlank(desc)) {
  52. rule.setHtmlDescription(desc);
  53. }
  54. for (RulesDefinition.NewParam param : rule.params()) {
  55. String paramDesc = i18n.getParamDescription(repo.key(), rule.key(), param.key());
  56. if (StringUtils.isNotBlank(paramDesc)) {
  57. param.setDescription(paramDesc);
  58. }
  59. }
  60. }
  61. }
  62. }