001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
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.api.checks.templates;
021
022 import org.slf4j.Logger;
023 import org.slf4j.LoggerFactory;
024
025 import java.util.Locale;
026 import java.util.MissingResourceException;
027 import java.util.ResourceBundle;
028
029 /**
030 * Internationalized check template. Translations are loaded from resource bundles (properties files in the classpath)
031 *
032 * @since 2.1
033 */
034 public class BundleCheckTemplate extends CheckTemplate {
035 private static final Logger LOG = LoggerFactory.getLogger(BundleCheckTemplate.class);
036
037 private String bundleBaseName;
038 private String defaultTitle;
039 private String defaultDescription;
040
041 protected BundleCheckTemplate(String key, String bundleBaseName) {
042 super(key);
043 this.bundleBaseName = bundleBaseName;
044 }
045
046 protected BundleCheckTemplate(String key, Class bundleClass) {
047 this(key, bundleClass.getCanonicalName());
048 }
049
050 protected String getDefaultTitle() {
051 if (defaultTitle == null || "".equals(defaultTitle)) {
052 return getKey();
053 }
054 return defaultTitle;
055 }
056
057 protected void setDefaultTitle(String defaultTitle) {
058 this.defaultTitle = defaultTitle;
059 }
060
061 protected String getDefaultDescription() {
062 return defaultDescription;
063 }
064
065 protected void setDefaultDescription(String defaultDescription) {
066 this.defaultDescription = defaultDescription;
067 }
068
069 @Override
070 public String getTitle(Locale locale) {
071 return getText("title", locale, getDefaultTitle());
072 }
073
074 @Override
075 public String getDescription(Locale locale) {
076 return getText("description", locale, getDefaultDescription());
077 }
078
079 @Override
080 public String getMessage(Locale locale, String key, Object... params) {
081 return null;
082 }
083
084 protected String getText(String key, Locale locale, String defaultValue) {
085 String result = null;
086 ResourceBundle bundle = getBundle(locale);
087 if (bundle != null) {
088 try {
089 result = bundle.getString(key);
090 } catch (MissingResourceException e) {
091 LOG.debug(e.getMessage());
092 }
093 }
094 if (result == null) {
095 result = defaultValue;
096 }
097 return result;
098 }
099
100 protected ResourceBundle getBundle(Locale locale) {
101 try {
102 if (locale != null) {
103 return ResourceBundle.getBundle(bundleBaseName, locale);
104 }
105 } catch (MissingResourceException e) {
106 // do nothing : use the default values
107 }
108 return null;
109 }
110 }