src/core/net/sf/basedb/util/resources/ResourceBundleWrapper.java

Code
Comments
Other
Rev Date Author Line
5409 16 Sep 10 nicklas 1 /**
5409 16 Sep 10 nicklas 2   $Id$
5409 16 Sep 10 nicklas 3
5409 16 Sep 10 nicklas 4   Copyright (C) 2010 Nicklas Nordborg
5409 16 Sep 10 nicklas 5
5409 16 Sep 10 nicklas 6   This file is part of BASE - BioArray Software Environment.
5409 16 Sep 10 nicklas 7   Available at http://base.thep.lu.se/
5409 16 Sep 10 nicklas 8
5409 16 Sep 10 nicklas 9   BASE is free software; you can redistribute it and/or
5409 16 Sep 10 nicklas 10   modify it under the terms of the GNU General Public License
5409 16 Sep 10 nicklas 11   as published by the Free Software Foundation; either version 3
5409 16 Sep 10 nicklas 12   of the License, or (at your option) any later version.
5409 16 Sep 10 nicklas 13
5409 16 Sep 10 nicklas 14   BASE is distributed in the hope that it will be useful,
5409 16 Sep 10 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5409 16 Sep 10 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5409 16 Sep 10 nicklas 17   GNU General Public License for more details.
5409 16 Sep 10 nicklas 18
5409 16 Sep 10 nicklas 19   You should have received a copy of the GNU General Public License
5409 16 Sep 10 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5409 16 Sep 10 nicklas 21 */
5409 16 Sep 10 nicklas 22 package net.sf.basedb.util.resources;
5409 16 Sep 10 nicklas 23
5409 16 Sep 10 nicklas 24
5409 16 Sep 10 nicklas 25 import java.util.Locale;
5409 16 Sep 10 nicklas 26 import java.util.MissingResourceException;
5409 16 Sep 10 nicklas 27 import java.util.ResourceBundle;
5409 16 Sep 10 nicklas 28
5409 16 Sep 10 nicklas 29 /**
5409 16 Sep 10 nicklas 30   A wrapper around {@link ResourceBundle} objects. We use the wrapper because we don't
5409 16 Sep 10 nicklas 31   want to throw exceptions if a key is missing. Instead, we return the key as the value
5409 16 Sep 10 nicklas 32   and log a message to system log. There are also some other useful variants for retreiving
5409 16 Sep 10 nicklas 33   values.
5409 16 Sep 10 nicklas 34   
5409 16 Sep 10 nicklas 35   @author Nicklas
5409 16 Sep 10 nicklas 36   @since 2.16
5409 16 Sep 10 nicklas 37   @base.modified $Date$
5409 16 Sep 10 nicklas 38 */
5409 16 Sep 10 nicklas 39 public class ResourceBundleWrapper
5409 16 Sep 10 nicklas 40 {
5409 16 Sep 10 nicklas 41
6444 09 Apr 14 nicklas 42   private static final org.slf4j.Logger log = 
6444 09 Apr 14 nicklas 43     org.slf4j.LoggerFactory.getLogger(ResourceBundleWrapper.class);
5409 16 Sep 10 nicklas 44
5409 16 Sep 10 nicklas 45   private final String name;
5409 16 Sep 10 nicklas 46   private final ResourceBundle parent;
5409 16 Sep 10 nicklas 47   
5409 16 Sep 10 nicklas 48   private boolean returnKeys;
5409 16 Sep 10 nicklas 49
5409 16 Sep 10 nicklas 50   /**
5409 16 Sep 10 nicklas 51     The wrapper is created by {@link ResourceBundleFactory}.
5409 16 Sep 10 nicklas 52     @param name The base name of the bundle
5409 16 Sep 10 nicklas 53     @param parent The underlying {@link ResourceBundle}
5409 16 Sep 10 nicklas 54   */
5409 16 Sep 10 nicklas 55   public ResourceBundleWrapper(String name, ResourceBundle parent)
5409 16 Sep 10 nicklas 56   {
5409 16 Sep 10 nicklas 57     this.name = name;
5409 16 Sep 10 nicklas 58     this.parent = parent;
5409 16 Sep 10 nicklas 59     this.returnKeys = false;
5409 16 Sep 10 nicklas 60   }
5409 16 Sep 10 nicklas 61
5409 16 Sep 10 nicklas 62   /**
5409 16 Sep 10 nicklas 63     Get the base name of the resource bundle.
5409 16 Sep 10 nicklas 64   */
5409 16 Sep 10 nicklas 65   public String getName()
5409 16 Sep 10 nicklas 66   {
5409 16 Sep 10 nicklas 67     return name;
5409 16 Sep 10 nicklas 68   }
5409 16 Sep 10 nicklas 69   
5409 16 Sep 10 nicklas 70   /**
5409 16 Sep 10 nicklas 71     Get the actual locale used by the bundle, which may be a different
5409 16 Sep 10 nicklas 72     locale from the one that was used to load it.
5409 16 Sep 10 nicklas 73   */
5409 16 Sep 10 nicklas 74   public Locale getLocale()
5409 16 Sep 10 nicklas 75   {
5409 16 Sep 10 nicklas 76     return parent.getLocale();
5409 16 Sep 10 nicklas 77   }
5409 16 Sep 10 nicklas 78   
5409 16 Sep 10 nicklas 79   /**
5409 16 Sep 10 nicklas 80     Set a flag indicating that the various <code>getString()</code> methods
5409 16 Sep 10 nicklas 81     should always return the key instead of trying to find a value for it.
5409 16 Sep 10 nicklas 82     
5409 16 Sep 10 nicklas 83     @param returnKeys TRUE to always return the key, FALSE to try to find values
5409 16 Sep 10 nicklas 84   */
5409 16 Sep 10 nicklas 85   public void setReturnKeys(boolean returnKeys)
5409 16 Sep 10 nicklas 86   {
5409 16 Sep 10 nicklas 87     this.returnKeys = returnKeys;
5409 16 Sep 10 nicklas 88   }
5409 16 Sep 10 nicklas 89
5409 16 Sep 10 nicklas 90   /**
5409 16 Sep 10 nicklas 91     Get access to the underlying resource bundle.
5409 16 Sep 10 nicklas 92   */
5409 16 Sep 10 nicklas 93   public ResourceBundle getResourceBundle()
5409 16 Sep 10 nicklas 94   {
5409 16 Sep 10 nicklas 95     return parent;
5409 16 Sep 10 nicklas 96   }
5409 16 Sep 10 nicklas 97   
5409 16 Sep 10 nicklas 98   /**
5409 16 Sep 10 nicklas 99     Utility method for returning the value for the key given
5409 16 Sep 10 nicklas 100     by <code>key.0</code> (variant=false) or <code>key.1</code> 
5409 16 Sep 10 nicklas 101     (variant=true) depending on the value of the variant parameter.
5409 16 Sep 10 nicklas 102     @see #getString(String, String...)
5409 16 Sep 10 nicklas 103   */
5409 16 Sep 10 nicklas 104   public String getString(String key, boolean variant, String... replacements)
5409 16 Sep 10 nicklas 105   {
5409 16 Sep 10 nicklas 106     return getString(key + (variant ? ".1" : ".0"), replacements);
5409 16 Sep 10 nicklas 107   }
5409 16 Sep 10 nicklas 108   
5409 16 Sep 10 nicklas 109   /**
5409 16 Sep 10 nicklas 110      Utility method for returning the value for the key given by:
5409 16 Sep 10 nicklas 111      <code>key.index</code>.
5409 16 Sep 10 nicklas 112      @see #getString(String, String...)
5409 16 Sep 10 nicklas 113   */
5409 16 Sep 10 nicklas 114   public String getString(String key, int index, String... replacements)
5409 16 Sep 10 nicklas 115   {
5409 16 Sep 10 nicklas 116     return getString(key + "." + index, replacements);
5409 16 Sep 10 nicklas 117   }
5409 16 Sep 10 nicklas 118   
5409 16 Sep 10 nicklas 119   /**
5409 16 Sep 10 nicklas 120     Get a string value from the bundle. The value may contain placeholders
5409 16 Sep 10 nicklas 121     of the form <code>{index}</code> which will be replaced by the 
5409 16 Sep 10 nicklas 122     corresponding value from the replacements array. Index values
5409 16 Sep 10 nicklas 123     that are outside the range of the array are not replaced.
5409 16 Sep 10 nicklas 124     <p>
5409 16 Sep 10 nicklas 125     If no value is found for the key or if the {@link #setReturnKeys(boolean)}
5409 16 Sep 10 nicklas 126     has been enabled the key is returned.
5409 16 Sep 10 nicklas 127     
5409 16 Sep 10 nicklas 128     @param key The key for the value
5409 16 Sep 10 nicklas 129     @param replacements An optional array with string replacments
5409 16 Sep 10 nicklas 130     @return The value found for the key
5409 16 Sep 10 nicklas 131   */
5409 16 Sep 10 nicklas 132   public String getString(String key, String... replacements)
5409 16 Sep 10 nicklas 133   {
5409 16 Sep 10 nicklas 134     if (returnKeys) return key;
5409 16 Sep 10 nicklas 135     String value = key;
5409 16 Sep 10 nicklas 136     try
5409 16 Sep 10 nicklas 137     {
5409 16 Sep 10 nicklas 138       value = parent.getString(key);
5409 16 Sep 10 nicklas 139     }
5409 16 Sep 10 nicklas 140     catch (MissingResourceException ex)
5409 16 Sep 10 nicklas 141     {
5409 16 Sep 10 nicklas 142       String msg = "Missing value for key '" + key + "' in bundle '" + getName() + "'";
5409 16 Sep 10 nicklas 143       if (log.isDebugEnabled())
5409 16 Sep 10 nicklas 144       {
5409 16 Sep 10 nicklas 145         log.debug(msg, ex);
5409 16 Sep 10 nicklas 146       }
5409 16 Sep 10 nicklas 147       else
5409 16 Sep 10 nicklas 148       {
5409 16 Sep 10 nicklas 149         log.warn(msg);
5409 16 Sep 10 nicklas 150       }
5409 16 Sep 10 nicklas 151     }
5409 16 Sep 10 nicklas 152     
5409 16 Sep 10 nicklas 153     if (replacements != null && replacements.length > 0)
5409 16 Sep 10 nicklas 154     {
5409 16 Sep 10 nicklas 155       for (int i = 0; i < replacements.length; ++i)
5409 16 Sep 10 nicklas 156       {
5409 16 Sep 10 nicklas 157         value = value.replace("{" + i + "}", replacements[i]);
5409 16 Sep 10 nicklas 158       }
5409 16 Sep 10 nicklas 159     }
5409 16 Sep 10 nicklas 160     return value;
5409 16 Sep 10 nicklas 161   }
5409 16 Sep 10 nicklas 162   
5409 16 Sep 10 nicklas 163   @Override
5409 16 Sep 10 nicklas 164   public String toString()
5409 16 Sep 10 nicklas 165   {
5409 16 Sep 10 nicklas 166     return "ResourceBundleWrapper[name=" + getName() + "; locale=" + getLocale() + "; parent=" + parent.toString();
5409 16 Sep 10 nicklas 167   }
5409 16 Sep 10 nicklas 168   
5409 16 Sep 10 nicklas 169   
5409 16 Sep 10 nicklas 170 }