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.

RuleI18nManager.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.core.i18n;
  21. import java.util.Locale;
  22. import javax.annotation.CheckForNull;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.sonar.api.ce.ComputeEngineSide;
  25. import org.sonar.api.i18n.RuleI18n;
  26. import org.sonar.api.rules.Rule;
  27. import org.sonar.api.server.ServerSide;
  28. /**
  29. * @deprecated in 4.1. Rules are not localized anymore. See http://jira.sonarsource.com/browse/SONAR-4885
  30. */
  31. @Deprecated
  32. @ServerSide
  33. @ComputeEngineSide
  34. public class RuleI18nManager implements RuleI18n {
  35. private static final String NAME_SUFFIX = ".name";
  36. private static final String RULE_PREFIX = "rule.";
  37. private DefaultI18n defaultI18n;
  38. public RuleI18nManager(DefaultI18n defaultI18n) {
  39. this.defaultI18n = defaultI18n;
  40. }
  41. /**
  42. * @deprecated in 4.1. Rules are not localized anymore. See http://jira.sonarsource.com/browse/SONAR-4885
  43. */
  44. @Override
  45. @Deprecated
  46. public String getName(String repositoryKey, String ruleKey, Locale locale) {
  47. return getName(repositoryKey, ruleKey);
  48. }
  49. /**
  50. * @deprecated in 4.1. Rules are not localized anymore. See http://jira.sonarsource.com/browse/SONAR-4885
  51. */
  52. @Override
  53. @Deprecated
  54. public String getName(Rule rule, Locale locale) {
  55. return getName(rule);
  56. }
  57. /**
  58. * @deprecated in 4.1. Rules are not localized anymore. See http://jira.sonarsource.com/browse/SONAR-4885
  59. */
  60. @Override
  61. @Deprecated
  62. public String getDescription(String repositoryKey, String ruleKey, Locale locale) {
  63. return getDescription(repositoryKey, ruleKey);
  64. }
  65. /**
  66. * @deprecated in 4.1. Rules are not localized anymore. See http://jira.sonarsource.com/browse/SONAR-4885
  67. */
  68. @Override
  69. @Deprecated
  70. public String getParamDescription(String repositoryKey, String ruleKey, String paramKey, Locale locale) {
  71. return getParamDescription(repositoryKey, ruleKey, paramKey);
  72. }
  73. @Override
  74. @CheckForNull
  75. public String getName(String repositoryKey, String ruleKey) {
  76. return message(repositoryKey, ruleKey, NAME_SUFFIX);
  77. }
  78. @Override
  79. @CheckForNull
  80. public String getName(Rule rule) {
  81. String name = message(rule.getRepositoryKey(), rule.getKey(), NAME_SUFFIX);
  82. return name != null ? name : rule.getName();
  83. }
  84. @Override
  85. public String getDescription(String repositoryKey, String ruleKey) {
  86. String relatedProperty = new StringBuilder().append(RULE_PREFIX).append(repositoryKey).append(".").append(ruleKey).append(NAME_SUFFIX).toString();
  87. String ruleDescriptionFilePath = "rules/" + repositoryKey + "/" + ruleKey + ".html";
  88. String description = defaultI18n.messageFromFile(Locale.ENGLISH, ruleDescriptionFilePath, relatedProperty);
  89. if (description == null) {
  90. // Following line is to ensure backward compatibility (SONAR-3319)
  91. description = lookUpDescriptionInFormerLocation(ruleKey, relatedProperty);
  92. }
  93. return description;
  94. }
  95. /*
  96. * Method used to ensure backward compatibility for language plugins that store HTML rule description files in the former
  97. * location (which was used prior to Sonar 3.0).
  98. *
  99. * See http://jira.sonarsource.com/browse/SONAR-3319
  100. */
  101. private String lookUpDescriptionInFormerLocation(String ruleKey, String relatedProperty) {
  102. return defaultI18n.messageFromFile(Locale.ENGLISH, ruleKey + ".html", relatedProperty);
  103. }
  104. @Override
  105. @CheckForNull
  106. public String getParamDescription(String repositoryKey, String ruleKey, String paramKey) {
  107. return message(repositoryKey, ruleKey, ".param." + paramKey);
  108. }
  109. @CheckForNull
  110. String message(String repositoryKey, String ruleKey, String suffix) {
  111. String propertyKey = new StringBuilder().append(RULE_PREFIX).append(repositoryKey).append(".").append(ruleKey).append(suffix).toString();
  112. return defaultI18n.message(Locale.ENGLISH, propertyKey, null);
  113. }
  114. static boolean isRuleProperty(String propertyKey) {
  115. return StringUtils.startsWith(propertyKey, RULE_PREFIX) && StringUtils.endsWith(propertyKey, NAME_SUFFIX) && !propertyKey.contains(".param.");
  116. }
  117. }