01: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
02: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
03: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
04: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
05: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
06: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
07: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
08: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
09: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
11: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
12: // POSSIBILITY OF SUCH DAMAGE.
13: //
14: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
15: package com.metaboss.util;
16:
17: import java.util.Locale;
18: import java.util.MissingResourceException;
19: import java.util.ResourceBundle;
20:
21: /** Useful utilities dealing with resource bundles */
22: public class ResourceUtils {
23: /** Loads resource bundle associated with the given class. Returns value of the string
24: * with given id or specified default string if nothing was found. This version of the method uses
25: * default locale.
26: * @param pResourceOwnerClass the class we have to load resource for
27: * @param pStringId - the identifier of the string
28: * @param pDefaultStringValue - the default value to use if resource or string within resource was not found
29: */
30: public static String getClassResourceString(
31: Class pResourceOwnerClass, String pStringId,
32: String pDefaultStringValue) {
33: return getLocalisedClassResourceString(Locale.getDefault(),
34: pResourceOwnerClass, pStringId, pDefaultStringValue);
35: }
36:
37: /** Loads resource bundle associated with the given class. Returns value of the string
38: * with given id or specified default string if nothing was found. This version of the method uses
39: * specified locale.
40: * @param pDesiredLocale the locale we would like to try to load resources for
41: * @param pResourceOwnerClass the class we have to load resource for
42: * @param pStringId - the identifier of the string
43: * @param pDefaultStringValue - the default value to use if resource or string within resource was not found
44: */
45: public static String getLocalisedClassResourceString(
46: Locale pDesiredLocale, Class pResourceOwnerClass,
47: String pStringId, String pDefaultStringValue) {
48: try {
49: ResourceBundle lBundle = ResourceBundle.getBundle(
50: pResourceOwnerClass.getName(), pDesiredLocale);
51: if (lBundle == null)
52: return pDefaultStringValue; // Ignore absence of the bundle
53: String lResourceString = lBundle.getString(pStringId);
54: if (lResourceString == null)
55: return pDefaultStringValue; // Ignore absence of the string in the bundle
56: return lResourceString;
57: } catch (MissingResourceException e) {
58: return pDefaultStringValue; // Ignore absence of the bundle
59: }
60: }
61: }
|