001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2008-2011 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar 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 * Sonar 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 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.core.i18n; 021 022 import com.google.common.collect.Lists; 023 import org.apache.commons.lang.StringUtils; 024 import org.sonar.api.ServerComponent; 025 026 import java.util.List; 027 import java.util.Locale; 028 029 public class RuleI18nManager implements ServerComponent { 030 031 private static final String NAME_SUFFIX = ".name"; 032 private static final String RULE_PREFIX = "rule."; 033 034 private I18nManager i18nManager; 035 private RuleKey[] ruleKeys; 036 037 public RuleI18nManager(I18nManager i18nManager) { 038 this.i18nManager = i18nManager; 039 } 040 041 public void start() { 042 List<RuleKey> list = Lists.newArrayList(); 043 for (String propertyKey : i18nManager.getPropertyKeys()) { 044 if (isRuleProperty(propertyKey)) { 045 list.add(extractRuleKey(propertyKey)); 046 } 047 } 048 this.ruleKeys = list.toArray(new RuleKey[list.size()]); 049 } 050 051 public String getName(String repositoryKey, String ruleKey, Locale locale) { 052 return message(repositoryKey, ruleKey, locale, NAME_SUFFIX); 053 } 054 055 public String getDescription(String repositoryKey, String ruleKey, Locale locale) { 056 String relatedProperty = new StringBuilder().append(RULE_PREFIX).append(repositoryKey).append(".").append(ruleKey).append(NAME_SUFFIX).toString(); 057 058 Locale localeWithoutCountry = (locale.getCountry()==null ? locale : new Locale(locale.getLanguage())); 059 String description = i18nManager.messageFromFile(localeWithoutCountry, ruleKey + ".html", relatedProperty, true); 060 if (description == null && !"en".equals(localeWithoutCountry.getLanguage())) { 061 description = i18nManager.messageFromFile(Locale.ENGLISH, ruleKey + ".html", relatedProperty, true); 062 } 063 return description; 064 } 065 066 public String getParamDescription(String repositoryKey, String ruleKey, String paramKey, Locale locale) { 067 return message(repositoryKey, ruleKey, locale, ".param." + paramKey); 068 } 069 070 String message(String repositoryKey, String ruleKey, Locale locale, String suffix) { 071 String propertyKey = new StringBuilder().append(RULE_PREFIX).append(repositoryKey).append(".").append(ruleKey).append(suffix).toString(); 072 return i18nManager.message(locale, propertyKey, null); 073 } 074 075 public List<RuleKey> searchNames(String search, Locale locale) { 076 List<RuleKey> result = Lists.newArrayList(); 077 for (RuleKey ruleKey : ruleKeys) { 078 String name = i18nManager.message(locale, ruleKey.getNameProperty(), null); 079 if (name != null && StringUtils.indexOfIgnoreCase(name, search) >= 0) { 080 result.add(ruleKey); 081 } 082 } 083 return result; 084 } 085 086 RuleKey[] getRuleKeys() { 087 return ruleKeys; 088 } 089 090 static RuleKey extractRuleKey(String propertyKey) { 091 String s = StringUtils.substringBetween(propertyKey, RULE_PREFIX, NAME_SUFFIX); 092 String ruleKey = StringUtils.substringAfter(s, "."); 093 String repository = StringUtils.substringBefore(s, "."); 094 return new RuleKey(repository, ruleKey); 095 } 096 097 static boolean isRuleProperty(String propertyKey) { 098 return StringUtils.startsWith(propertyKey, RULE_PREFIX) && StringUtils.endsWith(propertyKey, NAME_SUFFIX) && !propertyKey.contains(".param."); 099 } 100 101 public static class RuleKey { 102 private String repositoryKey; 103 private String key; 104 105 RuleKey(String repositoryKey, String key) { 106 this.repositoryKey = repositoryKey; 107 this.key = key; 108 } 109 110 public String getRepositoryKey() { 111 return repositoryKey; 112 } 113 114 public String getKey() { 115 return key; 116 } 117 118 public String getNameProperty() { 119 return new StringBuilder().append(RULE_PREFIX).append(repositoryKey).append(".").append(key).append(NAME_SUFFIX).toString(); 120 } 121 122 @Override 123 public boolean equals(Object o) { 124 if (this == o) { 125 return true; 126 } 127 if (o == null || getClass() != o.getClass()) { 128 return false; 129 } 130 RuleKey ruleKey = (RuleKey) o; 131 if (!key.equals(ruleKey.key)) { 132 return false; 133 } 134 if (!repositoryKey.equals(ruleKey.repositoryKey)) { 135 return false; 136 } 137 return true; 138 } 139 140 @Override 141 public int hashCode() { 142 int result = repositoryKey.hashCode(); 143 result = 31 * result + key.hashCode(); 144 return result; 145 } 146 147 @Override 148 public String toString() { 149 return new StringBuilder().append(repositoryKey).append(":").append(key).toString(); 150 } 151 } 152 }