[CVS spice] Refactored Resources to extend I18nResources.
pdonald-yCVjj/[email protected] 12 Jul 2004 01:02:47 -0000
| Newsgroups | gmane.comp.java.spice.cvs |
|---|---|
| Message-ID | <[email protected]> |
<html>
<head>
<style><!--
body {background-color:#ffffff;}
.file {border:1px solid #eeeeee;margin-top:1em;margin-bottom:1em;}
.pathname {font-family:monospace; float:right;}
.fileheader {margin-bottom:.5em;}
.diff {margin:0;}
.tasklist {padding:4px;border:1px dashed #000000;margin-top:1em;}
.tasklist ul {margin-top:0;margin-bottom:0;}
tr.alt {background-color:#eeeeee}
#added {background-color:#ddffdd;}
#addedchars {background-color:#99ff99;font-weight:bolder;}
tr.alt #added {background-color:#ccf7cc;}
#removed {background-color:#ffdddd;}
#removedchars {background-color:#ff9999;font-weight:bolder;}
tr.alt #removed {background-color:#f7cccc;}
#info {color:#888888;}
#context {background-color:#eeeeee;}
td {padding-left:.3em;padding-right:.3em;}
tr.head {border-bottom-width:1px;border-bottom-style:solid;}
tr.head td {padding:0;padding-top:.2em;}
.task {background-color:#ffff00;}
.comment {padding:4px;border:1px dashed #000000;background-color:#ffffdd}
.error {color:red;}
hr {border-width:0px;height:2px;background:black;}
--></style>
</head>
<body>
<table cellspacing="0" cellpadding="0" border="0" rules="cols">
<tr class="head"><td colspan="4">Commit in <b><tt>spice/components/salt</tt></b><span id="info"> on MAIN</span></td></tr>
<tr><td><tt><a href="#file1">salt.iml</a></tt></td><td align="right" id="added">+2</td><td align="right" id="removed">-1</td><td nowrap="nowrap" align="center">1.1 -> 1.2</td></tr>
<tr class="alt"><td><tt>src/java/org/codehaus/spice/salt/i18n/<a href="#file2"><span id="added">I18nResourceManager.java</span></a></tt></td><td align="right" id="added">+255</td><td></td><td nowrap="nowrap" align="right">added 1.1</td></tr>
<tr><td><tt> /<a href="#file3"><span id="added">I18nResources.java</span></a></tt></td><td align="right" id="added">+674</td><td></td><td nowrap="nowrap" align="right">added 1.1</td></tr>
<tr class="alt"><td><tt> /<a href="#file4">Resources.java</a></tt></td><td align="right" id="added">+3</td><td align="right" id="removed">-693</td><td nowrap="nowrap" align="center">1.1 -> 1.2</td></tr>
<tr><td></td><td align="right" id="added">+934</td><td align="right" id="removed">-694</td><td></td></tr>
</table>
<small id="info">2 added + 2 modified, total 4 files</small><br />
<pre class="comment">
Refactored Resources to extend I18nResources.
This was done so that a less generic classname could be used for
the object which can be a PITA if other frameworks use the same name.
Added in I18nResourceManager which is similar to ResourceManager except that it allows users to specify non-default Locale and cache on that.
</pre>
<hr /><a name="file1" /><div class="file">
<span class="pathname">spice/components/salt<br /></span>
<div class="fileheader"><big><b>salt.iml</b></big> <small id="info">1.1 -> 1.2</small></div>
<pre class="diff"><small id="info">diff -u -r1.1 -r1.2
--- salt.iml 21 Apr 2004 03:16:19 -0000 1.1
+++ salt.iml 12 Jul 2004 01:02:47 -0000 1.2
@@ -1,5 +1,5 @@
</small></pre><pre class="diff" id="context"> <?xml version="1.0" encoding="UTF-8"?>
</pre><pre class="diff" id="removed">-<module version="4" relativePaths="false">
</pre><pre class="diff" id="added">+<module version="4" relativePaths="false"<span id="addedchars"> type="JAVA_MODULE"</span>>
</pre><pre class="diff" id="context"> <component name="ModuleRootManager" />
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/target/classes" />
</pre><pre class="diff"><small id="info">@@ -10,6 +10,7 @@
</small></pre><pre class="diff" id="context"> </content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</pre><pre class="diff" id="added">+ <orderEntryProperties />
</pre><pre class="diff" id="context"> </component>
</module>
</pre></div>
<hr /><a name="file2" /><div class="file">
<span class="pathname" id="added">spice/components/salt/src/java/org/codehaus/spice/salt/i18n<br /></span>
<div class="fileheader" id="added"><big><b>I18nResourceManager.java</b></big> <small id="info">added at 1.1</small></div>
<pre class="diff"><small id="info">diff -N I18nResourceManager.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ I18nResourceManager.java 12 Jul 2004 01:02:47 -0000 1.1
@@ -0,0 +1,255 @@
</small></pre><pre class="diff" id="added">+/*
+ * Copyright (C) The Spice Group. All rights reserved.
+ *
+ * This software is published under the terms of the Spice
+ * Software License version 1.1, a copy of which has been included
+ * with this distribution in the LICENSE.txt file.
+ */
+package org.codehaus.spice.salt.i18n;
+
+import java.lang.ref.WeakReference;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.WeakHashMap;
+
+/**
+ * Manager for I18nResources objects.
+ * The manager caches the I18nResources object based on the ClassLoader
+ * and Locale that the I18nResources is loaded from.
+ *
+ * @author Peter Donald
+ */
+public class I18nResourceManager
+{
+ /** Postfix appended to base names to get names of Resource files. */
+ private static final String POSTFIX = "Resources";
+
+ /** Permission needed to clear complete cache. */
+ private static final RuntimePermission CLEAR_CACHE_PERMISSION = new RuntimePermission( "i18n.clearCompleteCache" );
+
+ /**
+ * This maps ClassLoader -> "Locale Map".
+ * The "Locale Map" is a map that maps Locale -> "Resource Map"
+ * The "Resource Map" is a map that maps baseName -> I18nResource
+ */
+ private static final Map c_resources = new WeakHashMap();
+
+ /**
+ * Clear the cache of all resources currently loaded into the system. This
+ * method is useful if you need to dump the complete cache and because part
+ * of the application is reloading and thus the resources may need to be
+ * reloaded.
+ *
+ * <p>Note that the caller must have been granted the
+ * "i18n.clearCompleteCache" {@link RuntimePermission} or else a security
+ * exception will be thrown.</p>
+ *
+ * @throws SecurityException if the caller does not have permission to clear
+ * cache
+ */
+ public static final synchronized void clearResourceCache()
+ throws SecurityException
+ {
+ final SecurityManager sm = System.getSecurityManager();
+ if( null != sm )
+ {
+ sm.checkPermission( CLEAR_CACHE_PERMISSION );
+ }
+
+ c_resources.clear();
+ }
+
+ /**
+ * Retrieve resource with specified basename.
+ *
+ * @param basename the basename
+ * @return the Resources
+ */
+ public static final I18nResources getBaseResources( final String basename,
+ final ClassLoader classLoader )
+ {
+ return getBaseResources( basename, Locale.getDefault(), classLoader );
+ }
+
+ /**
+ * Retrieve resource with specified basename in specified locale.
+ *
+ * @param basename the basename
+ * @return the Resources
+ */
+ public static final I18nResources getBaseResources( final String basename, final Locale locale,
+ final ClassLoader classLoader )
+ {
+ I18nResources resources = getCachedResource( basename, locale, classLoader );
+ if( null == resources )
+ {
+ resources = new I18nResources( basename, locale, classLoader );
+ putCachedResource( basename, locale, classLoader, resources );
+ }
+
+ return resources;
+ }
+
+ /**
+ * Retrieve resource for specified name. The baseName is determined by name
+ * postfixed with ".Resources".
+ *
+ * @param resource the base location
+ * @return the Resources
+ */
+ public static final I18nResources getResources( final String resource,
+ final ClassLoader classLoader )
+ {
+ return getResources( resource, Locale.getDefault(), classLoader );
+ }
+
+ /**
+ * Retrieve resource for specified name. The baseName is determined by name
+ * postfixed with ".Resources".
+ *
+ * @param resource the base location
+ * @return the Resources
+ */
+ public static final I18nResources getResources( final String resource, final Locale locale,
+ final ClassLoader classLoader )
+ {
+ return getBaseResources( resource + POSTFIX, locale, classLoader );
+ }
+
+ /**
+ * Retrieve resource for specified Classes package. The baseName is
+ * determined by name of classes package postfixed with ".Resources".
+ *
+ * @param clazz the Class
+ * @return the Resources
+ */
+ public static final I18nResources getPackageResources( final Class clazz )
+ {
+ return getPackageResources( clazz, Locale.getDefault() );
+ }
+
+ /**
+ * Retrieve resource for specified Classes package. The baseName is
+ * determined by name of classes package postfixed with ".Resources".
+ *
+ * @param clazz the Class
+ * @return the Resources
+ */
+ public static final I18nResources getPackageResources( final Class clazz, final Locale locale )
+ {
+ final String name = clazz.getName();
+ final int index = name.lastIndexOf( "." );
+ final String packageName;
+ if( -1 != index )
+ {
+ packageName = name.substring( 0, index ) + "." + POSTFIX;
+ }
+ else
+ {
+ packageName = POSTFIX;
+ }
+
+ return getBaseResources( packageName, locale, clazz.getClassLoader() );
+ }
+
+ /**
+ * Retrieve resource for specified Class. The baseName is determined by name
+ * of Class postfixed with ".Resources".
+ *
+ * @param clazz the Class
+ * @return the Resources
+ */
+ public static final I18nResources getClassResources( final Class clazz )
+ {
+ return getClassResources( clazz, Locale.getDefault() );
+ }
+
+ /**
+ * Retrieve resource for specified Class. The baseName is determined by name
+ * of Class postfixed with ".Resources".
+ *
+ * @param clazz the Class
+ * @return the Resources
+ */
+ public static final I18nResources getClassResources( final Class clazz, final Locale locale )
+ {
+ final String resource = clazz.getName() + POSTFIX;
+ return getBaseResources( resource, locale, clazz.getClassLoader() );
+ }
+
+ /**
+ * Cache specified resource in weak reference.
+ *
+ * @param baseName the resource key
+ * @param resources the resources object
+ */
+ private static final synchronized void putCachedResource( final String baseName,
+ final Locale locale,
+ final ClassLoader classLoader,
+ final I18nResources resources )
+ {
+
+ final Map map = getLocaleMap( classLoader, locale );
+ map.put( baseName, new WeakReference( resources ) );
+ }
+
+ /**
+ * Retrieve cached resource.
+ *
+ * @param baseName the resource key
+ * @return resources the resources object
+ */
+ private static final synchronized I18nResources getCachedResource( final String baseName,
+ final Locale locale,
+ final ClassLoader classLoader )
+ {
+ final Map map = getLocaleMap( classLoader, locale );
+ final WeakReference weakReference = (WeakReference) map.get( baseName );
+ if( null == weakReference )
+ {
+ return null;
+ }
+ else
+ {
+ return (I18nResources) weakReference.get();
+ }
+ }
+
+ /**
+ * Return the map associated with specified ClassLoader.
+ * Will create Map if it does not exist.
+ *
+ * @param classLoader the ClassLoader
+ * @return the Map.
+ */
+ private static final Map getLocaleMap( final ClassLoader classLoader, final Locale locale )
+ {
+ final Map loaderMap = getClassLoaderMap( classLoader );
+ Map map = (Map) loaderMap.get( locale );
+ if( null == map )
+ {
+ map = new HashMap();
+ loaderMap.put( locale, map );
+ }
+ return map;
+ }
+
+ /**
+ * Return the map associated with specified ClassLoader.
+ * Will create Map if it does not exist.
+ *
+ * @param classLoader the ClassLoader
+ * @return the Map.
+ */
+ private static final Map getClassLoaderMap( final ClassLoader classLoader )
+ {
+ Map map = (Map) c_resources.get( classLoader );
+ if( null == map )
+ {
+ map = new WeakHashMap();
+ c_resources.put( classLoader, map );
+ }
+ return map;
+ }
+}
</pre></div>
<hr /><a name="file3" /><div class="file">
<span class="pathname" id="added">spice/components/salt/src/java/org/codehaus/spice/salt/i18n<br /></span>
<div class="fileheader" id="added"><big><b>I18nResources.java</b></big> <small id="info">added at 1.1</small></div>
<pre class="diff"><small id="info">diff -N I18nResources.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ I18nResources.java 12 Jul 2004 01:02:47 -0000 1.1
@@ -0,0 +1,674 @@
</small></pre><pre class="diff" id="added">+/*
+ * Copyright (C) The Spice Group. All rights reserved.
+ *
+ * This software is published under the terms of the Spice
+ * Software License version 1.1, a copy of which has been included
+ * with this distribution in the LICENSE.txt file.
+ */
+package org.codehaus.spice.salt.i18n;
+
+import java.text.DateFormat;
+import java.text.MessageFormat;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+/**
+ * A class to simplify extracting localized strings, icons and other common
+ * resources from a ResourceBundle.
+ *
+ * @author Peter Donald
+ */
+public class I18nResources
+{
+ /** Local of Resources. */
+ protected final Locale m_locale;
+ /** Base name of resource bundle. */
+ protected final String m_baseName;
+ /** ClassLoader from which to load resources. */
+ protected final ClassLoader m_classLoader;
+ /** Resource bundle referenced by manager. */
+ private ResourceBundle m_bundle;
+
+ /**
+ * Constructor that builds a manager in specified locale.
+ *
+ * @param baseName the base name of ResourceBundle
+ * @param locale the Locale for resource bundle
+ * @param classLoader the classLoader to load ResourceBundle from
+ */
+ public I18nResources( final String baseName,
+ final Locale locale,
+ final ClassLoader classLoader )
+ {
+ if( null == baseName )
+ {
+ throw new NullPointerException( "baseName" );
+ }
+ if( null == locale )
+ {
+ throw new NullPointerException( "locale" );
+ }
+ if( null == classLoader )
+ {
+ throw new NullPointerException( "classLoader" );
+ }
+
+ m_baseName = baseName;
+ m_locale = locale;
+ m_classLoader = classLoader;
+ }
+
+ /**
+ * Retrieve a boolean from bundle.
+ *
+ * @param key the key of resource
+ * @param defaultValue the default value if key is missing
+ * @return the resource boolean
+ */
+ public boolean getBoolean( final String key, final boolean defaultValue )
+ {
+ try
+ {
+ return getBoolean( key );
+ }
+ catch( final MissingResourceException mre )
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * Retrieve a boolean from bundle.
+ *
+ * @param key the key of resource
+ * @return the resource boolean
+ */
+ public boolean getBoolean( final String key )
+ throws MissingResourceException
+ {
+ final ResourceBundle bundle = getBundle();
+ final String value = bundle.getString( key );
+ return value.equalsIgnoreCase( "true" );
+ }
+
+ /**
+ * Retrieve a byte from bundle.
+ *
+ * @param key the key of resource
+ * @param defaultValue the default value if key is missing
+ * @return the resource byte
+ */
+ public byte getByte( final String key, final byte defaultValue )
+ {
+ try
+ {
+ return getByte( key );
+ }
+ catch( final MissingResourceException mre )
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * Retrieve a byte from bundle.
+ *
+ * @param key the key of resource
+ * @return the resource byte
+ */
+ public byte getByte( final String key )
+ throws MissingResourceException
+ {
+ final ResourceBundle bundle = getBundle();
+ final String value = bundle.getString( key );
+ try
+ {
+ return Byte.parseByte( value );
+ }
+ catch( final NumberFormatException nfe )
+ {
+ throw new MissingResourceException( "Expecting a byte value but got " + value, "java.lang.String", key );
+ }
+ }
+
+ /**
+ * Retrieve a char from bundle.
+ *
+ * @param key the key of resource
+ * @param defaultValue the default value if key is missing
+ * @return the resource char
+ */
+ public char getChar( final String key, final char defaultValue )
+ {
+ try
+ {
+ return getChar( key );
+ }
+ catch( final MissingResourceException mre )
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * Retrieve a char from bundle.
+ *
+ * @param key the key of resource
+ * @return the resource char
+ */
+ public char getChar( final String key )
+ throws MissingResourceException
+ {
+ final ResourceBundle bundle = getBundle();
+ final String value = bundle.getString( key );
+
+ if( 1 == value.length() )
+ {
+ return value.charAt( 0 );
+ }
+ else
+ {
+ throw new MissingResourceException( "Expecting a char value but got " + value, "java.lang.String", key );
+ }
+ }
+
+ /**
+ * Retrieve a short from bundle.
+ *
+ * @param key the key of resource
+ * @param defaultValue the default value if key is missing
+ * @return the resource short
+ */
+ public short getShort( final String key, final short defaultValue )
+ {
+ try
+ {
+ return getShort( key );
+ }
+ catch( final MissingResourceException mre )
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * Retrieve a short from bundle.
+ *
+ * @param key the key of resource
+ * @return the resource short
+ */
+ public short getShort( final String key )
+ throws MissingResourceException
+ {
+ final ResourceBundle bundle = getBundle();
+ final String value = bundle.getString( key );
+ try
+ {
+ return Short.parseShort( value );
+ }
+ catch( final NumberFormatException nfe )
+ {
+ throw new MissingResourceException( "Expecting a short value but got " + value, "java.lang.String", key );
+ }
+ }
+
+ /**
+ * Retrieve a integer from bundle.
+ *
+ * @param key the key of resource
+ * @param defaultValue the default value if key is missing
+ * @return the resource integer
+ */
+ public int getInteger( final String key, final int defaultValue )
+ {
+ try
+ {
+ return getInteger( key );
+ }
+ catch( final MissingResourceException mre )
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * Retrieve a integer from bundle.
+ *
+ * @param key the key of resource
+ * @return the resource integer
+ */
+ public int getInteger( final String key )
+ throws MissingResourceException
+ {
+ final ResourceBundle bundle = getBundle();
+ final String value = bundle.getString( key );
+ try
+ {
+ return Integer.parseInt( value );
+ }
+ catch( final NumberFormatException nfe )
+ {
+ throw new MissingResourceException( "Expecting a integer value but got " + value, "java.lang.String", key );
+ }
+ }
+
+ /**
+ * Retrieve a long from bundle.
+ *
+ * @param key the key of resource
+ * @param defaultValue the default value if key is missing
+ * @return the resource long
+ */
+ public long getLong( final String key, final long defaultValue )
+ {
+ try
+ {
+ return getLong( key );
+ }
+ catch( final MissingResourceException mre )
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * Retrieve a long from bundle.
+ *
+ * @param key the key of resource
+ * @return the resource long
+ */
+ public long getLong( final String key )
+ throws MissingResourceException
+ {
+ final ResourceBundle bundle = getBundle();
+ final String value = bundle.getString( key );
+ try
+ {
+ return Long.parseLong( value );
+ }
+ catch( final NumberFormatException nfe )
+ {
+ throw new MissingResourceException( "Expecting a long value but got " + value, "java.lang.String", key );
+ }
+ }
+
+ /**
+ * Retrieve a float from bundle.
+ *
+ * @param key the key of resource
+ * @param defaultValue the default value if key is missing
+ * @return the resource float
+ */
+ public float getFloat( final String key, final float defaultValue )
+ {
+ try
+ {
+ return getFloat( key );
+ }
+ catch( final MissingResourceException mre )
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * Retrieve a float from bundle.
+ *
+ * @param key the key of resource
+ * @return the resource float
+ */
+ public float getFloat( final String key )
+ throws MissingResourceException
+ {
+ final ResourceBundle bundle = getBundle();
+ final String value = bundle.getString( key );
+ try
+ {
+ return Float.parseFloat( value );
+ }
+ catch( final NumberFormatException nfe )
+ {
+ throw new MissingResourceException( "Expecting a float value but got " + value, "java.lang.String", key );
+ }
+ }
+
+ /**
+ * Retrieve a double from bundle.
+ *
+ * @param key the key of resource
+ * @param defaultValue the default value if key is missing
+ * @return the resource double
+ */
+ public double getDouble( final String key, final double defaultValue )
+ {
+ try
+ {
+ return getDouble( key );
+ }
+ catch( final MissingResourceException mre )
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * Retrieve a double from bundle.
+ *
+ * @param key the key of resource
+ * @return the resource double
+ */
+ public double getDouble( final String key )
+ throws MissingResourceException
+ {
+ final ResourceBundle bundle = getBundle();
+ final String value = bundle.getString( key );
+ try
+ {
+ return Double.parseDouble( value );
+ }
+ catch( final NumberFormatException nfe )
+ {
+ throw new MissingResourceException( "Expecting a double value but got " + value, "java.lang.String", key );
+ }
+ }
+
+ /**
+ * Retrieve a date from bundle.
+ *
+ * @param key the key of resource
+ * @param defaultValue the default value if key is missing
+ * @return the resource date
+ */
+ public Date getDate( final String key, final Date defaultValue )
+ {
+ try
+ {
+ return getDate( key );
+ }
+ catch( final MissingResourceException mre )
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * Retrieve a date from bundle.
+ *
+ * @param key the key of resource
+ * @return the resource date
+ */
+ public Date getDate( final String key )
+ throws MissingResourceException
+ {
+ final ResourceBundle bundle = getBundle();
+ final String value = bundle.getString( key );
+ try
+ {
+ final DateFormat format = DateFormat.getDateInstance( DateFormat.DEFAULT, m_locale );
+ return format.parse( value );
+ }
+ catch( final ParseException pe )
+ {
+ throw new MissingResourceException( "Expecting a date value but got " + value, "java.lang.String", key );
+ }
+ }
+
+ /**
+ * Retrieve a time from bundle.
+ *
+ * @param key the key of resource
+ * @param defaultValue the default value if key is missing
+ * @return the resource time
+ */
+ public Date getTime( final String key, final Date defaultValue )
+ {
+ try
+ {
+ return getTime( key );
+ }
+ catch( final MissingResourceException mre )
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * Retrieve a time from bundle.
+ *
+ * @param key the key of resource
+ * @return the resource time
+ */
+ public Date getTime( final String key )
+ throws MissingResourceException
+ {
+ final ResourceBundle bundle = getBundle();
+ final String value = bundle.getString( key );
+ try
+ {
+ final DateFormat format = DateFormat.getTimeInstance( DateFormat.DEFAULT, m_locale );
+ return format.parse( value );
+ }
+ catch( final ParseException pe )
+ {
+ throw new MissingResourceException( "Expecting a time value but got " + value, "java.lang.String", key );
+ }
+ }
+
+ /**
+ * Retrieve a time from bundle.
+ *
+ * @param key the key of resource
+ * @param defaultValue the default value if key is missing
+ * @return the resource time
+ */
+ public Date getDateTime( final String key, final Date defaultValue )
+ {
+ try
+ {
+ return getDateTime( key );
+ }
+ catch( final MissingResourceException mre )
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * Retrieve a date + time from bundle.
+ *
+ * @param key the key of resource
+ * @return the resource date + time
+ */
+ public Date getDateTime( final String key )
+ throws MissingResourceException
+ {
+ final ResourceBundle bundle = getBundle();
+ final String value = bundle.getString( key );
+ try
+ {
+ final DateFormat format = DateFormat.getDateTimeInstance( DateFormat.DEFAULT, DateFormat.DEFAULT, m_locale );
+ return format.parse( value );
+ }
+ catch( final ParseException pe )
+ {
+ throw new MissingResourceException( "Expecting a time value but got " + value, "java.lang.String", key );
+ }
+ }
+
+ /**
+ * Retrieve a raw string from bundle.
+ *
+ * @param key the key of resource
+ * @return the resource string
+ */
+ public String getString( final String key )
+ throws MissingResourceException
+ {
+ final ResourceBundle bundle = getBundle();
+ return bundle.getString( key );
+ }
+
+ /**
+ * Retrieve a raw string from bundle.
+ *
+ * @param key the key of resource
+ * @param defaultValue the default value if key is missing
+ * @return the resource string
+ */
+ public String getString( final String key, final String defaultValue )
+ {
+ try
+ {
+ return getString( key );
+ }
+ catch( final MissingResourceException mre )
+ {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * Retrieve a string from resource bundle and format it with specified
+ * args.
+ *
+ * @param key the key for resource
+ * @param arg1 an arg
+ * @return the formatted string
+ */
+ public String format( final String key, final Object arg1 )
+ {
+ final Object[] args = new Object[]{arg1};
+ return format( key, args );
+ }
+
+ /**
+ * Retrieve a string from resource bundle and format it with specified
+ * args.
+ *
+ * @param key the key for resource
+ * @param arg1 an arg
+ * @param arg2 an arg
+ * @return the formatted string
+ */
+ public String format( final String key, final Object arg1, final Object arg2 )
+ {
+ final Object[] args = new Object[]{arg1, arg2};
+ return format( key, args );
+ }
+
+ /**
+ * Retrieve a string from resource bundle and format it with specified
+ * args.
+ *
+ * @param key the key for resource
+ * @param arg1 an arg
+ * @param arg2 an arg
+ * @param arg3 an arg
+ * @return the formatted string
+ */
+ public String format( final String key, final Object arg1, final Object arg2,
+ final Object arg3 )
+ {
+ final Object[] args = new Object[]{arg1, arg2, arg3};
+ return format( key, args );
+ }
+
+ /**
+ * Retrieve a string from resource bundle and format it with specified
+ * args.
+ *
+ * @param key the key for resource
+ * @param arg1 an arg
+ * @param arg2 an arg
+ * @param arg3 an arg
+ * @param arg4 an arg
+ * @return the formatted string
+ */
+ public String format( final String key, final Object arg1, final Object arg2,
+ final Object arg3, final Object arg4 )
+ {
+ final Object[] args = new Object[]{arg1, arg2, arg3, arg4};
+ return format( key, args );
+ }
+
+ /**
+ * Retrieve a string from resource bundle and format it with specified
+ * args.
+ *
+ * @param key the key for resource
+ * @param arg1 an arg
+ * @param arg2 an arg
+ * @param arg3 an arg
+ * @param arg4 an arg
+ * @param arg5 an arg
+ * @return the formatted string
+ */
+ public String format( final String key, final Object arg1, final Object arg2,
+ final Object arg3, final Object arg4, final Object arg5 )
+ {
+ final Object[] args = new Object[]{arg1, arg2, arg3, arg4, arg5};
+ return format( key, args );
+ }
+
+ /**
+ * Retrieve a string from resource bundle and format it with specified
+ * args.
+ *
+ * @param key the key for resource
+ * @param args an array of args
+ * @return the formatted string
+ */
+ public String format( final String key, final Object[] args )
+ {
+ try
+ {
+ final String pattern = getString( key );
+ return MessageFormat.format( pattern, args );
+ }
+ catch( final MissingResourceException mre )
+ {
+ final StringBuffer sb = new StringBuffer();
+ sb.append( "Unknown resource. Bundle: '" );
+ sb.append( m_baseName );
+ sb.append( "' Key: '" );
+ sb.append( key );
+ sb.append( "' Args: '" );
+
+ for( int i = 0; i < args.length; i++ )
+ {
+ if( 0 != i )
+ {
+ sb.append( "', '" );
+ }
+ sb.append( args[i] );
+ }
+
+ sb.append( "' Reason: " );
+ sb.append( mre );
+
+ return sb.toString();
+ }
+ }
+
+ /**
+ * Retrieve underlying ResourceBundle. If bundle has not been loaded it will
+ * be loaded by this method. Access is given in case other resources need to
+ * be extracted that this Manager does not provide simplified access to.
+ *
+ * @return the ResourceBundle
+ * @throws MissingResourceException if an error occurs
+ */
+ public final synchronized ResourceBundle getBundle()
+ throws MissingResourceException
+ {
+ if( null == m_bundle )
+ {
+ // bundle wasn't cached, so load it, cache it, and return it.
+ m_bundle = ResourceBundle.getBundle( m_baseName, m_locale, m_classLoader );
+ }
+ return m_bundle;
+ }
+}
</pre></div>
<hr /><a name="file4" /><div class="file">
<span class="pathname">spice/components/salt/src/java/org/codehaus/spice/salt/i18n<br /></span>
<div class="fileheader"><big><b>Resources.java</b></big> <small id="info">1.1 -> 1.2</small></div>
<pre class="diff"><small id="info">diff -u -r1.1 -r1.2
--- Resources.java 2 Dec 2003 02:15:04 -0000 1.1
+++ Resources.java 12 Jul 2004 01:02:47 -0000 1.2
@@ -7,35 +7,16 @@
</small></pre><pre class="diff" id="context"> */
package org.codehaus.spice.salt.i18n;
</pre><pre class="diff" id="removed">-import java.text.DateFormat;
-import java.text.MessageFormat;
-import java.text.ParseException;
-import java.util.Date;
</pre><pre class="diff" id="context"> import java.util.Locale;
</pre><pre class="diff" id="removed">-import java.util.MissingResourceException;
-import java.util.ResourceBundle;
</pre><pre class="diff" id="context">
/**
* A class to simplify extracting localized strings, icons and other common
* resources from a ResourceBundle.
*
* @author Peter Donald
</pre><pre class="diff" id="removed">- * @version $Revision: 1.1 $ $Date: 2003/12/02 02:15:04 $
</pre><pre class="diff" id="context"> */
</pre><pre class="diff" id="removed">-public class Resources
</pre><pre class="diff" id="added">+public class Resources extends I18nResources
</pre><pre class="diff" id="context"> {
</pre><pre class="diff" id="removed">- /** Local of Resources. */
- private final Locale m_locale;
-
- /** Base name of resource bundle. */
- private final String m_baseName;
-
- /** ClassLoader from which to load resources. */
- private final ClassLoader m_classLoader;
-
- /** Resource bundle referenced by manager. */
- private ResourceBundle m_bundle;
-
</pre><pre class="diff" id="context"> /**
* Constructor that builds a manager in default locale using specified
* ClassLoader.
</pre><pre class="diff"><small id="info">@@ -45,7 +26,7 @@
</small></pre><pre class="diff" id="context"> */
public Resources( final String baseName, final ClassLoader classLoader )
{
</pre><pre class="diff" id="removed">- <span id="removedchars">this</span>( baseName, Locale.getDefault(), classLoader );
</pre><pre class="diff" id="added">+ <span id="addedchars">super</span>( baseName, Locale.getDefault(), classLoader );
</pre><pre class="diff" id="context"> }
/**
</pre><pre class="diff"><small id="info">@@ -59,677 +40,6 @@
</small></pre><pre class="diff" id="context"> final Locale locale,
final ClassLoader classLoader )
{
</pre><pre class="diff" id="removed">- if( null == baseName )
- {
- throw new NullPointerException( "baseName" );
- }
- if( null == locale )
- {
- throw new NullPointerException( "locale" );
- }
- if( null == classLoader )
- {
- throw new NullPointerException( "classLoader" );
- }
-
- m_baseName = baseName;
- m_locale = locale;
- m_classLoader = classLoader;
- }
-
- /**
- * Retrieve a boolean from bundle.
- *
- * @param key the key of resource
- * @param defaultValue the default value if key is missing
- * @return the resource boolean
- */
- public boolean getBoolean( final String key, final boolean defaultValue )
- {
- try
- {
- return getBoolean( key );
- }
- catch( final MissingResourceException mre )
- {
- return defaultValue;
- }
- }
-
- /**
- * Retrieve a boolean from bundle.
- *
- * @param key the key of resource
- * @return the resource boolean
- */
- public boolean getBoolean( final String key )
- throws MissingResourceException
- {
- final ResourceBundle bundle = getBundle();
- final String value = bundle.getString( key );
- return value.equalsIgnoreCase( "true" );
- }
-
- /**
- * Retrieve a byte from bundle.
- *
- * @param key the key of resource
- * @param defaultValue the default value if key is missing
- * @return the resource byte
- */
- public byte getByte( final String key, final byte defaultValue )
- {
- try
- {
- return getByte( key );
- }
- catch( final MissingResourceException mre )
- {
- return defaultValue;
- }
- }
-
- /**
- * Retrieve a byte from bundle.
- *
- * @param key the key of resource
- * @return the resource byte
- */
- public byte getByte( final String key )
- throws MissingResourceException
- {
- final ResourceBundle bundle = getBundle();
- final String value = bundle.getString( key );
- try
- {
- return Byte.parseByte( value );
- }
- catch( final NumberFormatException nfe )
- {
- throw new MissingResourceException(
- "Expecting a byte value but got " + value,
- "java.lang.String",
- key );
- }
- }
-
- /**
- * Retrieve a char from bundle.
- *
- * @param key the key of resource
- * @param defaultValue the default value if key is missing
- * @return the resource char
- */
- public char getChar( final String key, final char defaultValue )
- {
- try
- {
- return getChar( key );
- }
- catch( final MissingResourceException mre )
- {
- return defaultValue;
- }
- }
-
- /**
- * Retrieve a char from bundle.
- *
- * @param key the key of resource
- * @return the resource char
- */
- public char getChar( final String key )
- throws MissingResourceException
- {
- final ResourceBundle bundle = getBundle();
- final String value = bundle.getString( key );
-
- if( 1 == value.length() )
- {
- return value.charAt( 0 );
- }
- else
- {
- throw new MissingResourceException(
- "Expecting a char value but got " + value,
- "java.lang.String",
- key );
- }
- }
-
- /**
- * Retrieve a short from bundle.
- *
- * @param key the key of resource
- * @param defaultValue the default value if key is missing
- * @return the resource short
- */
- public short getShort( final String key, final short defaultValue )
- {
- try
- {
- return getShort( key );
- }
- catch( final MissingResourceException mre )
- {
- return defaultValue;
- }
- }
-
- /**
- * Retrieve a short from bundle.
- *
- * @param key the key of resource
- * @return the resource short
- */
- public short getShort( final String key )
- throws MissingResourceException
- {
- final ResourceBundle bundle = getBundle();
- final String value = bundle.getString( key );
- try
- {
- return Short.parseShort( value );
- }
- catch( final NumberFormatException nfe )
- {
- throw new MissingResourceException(
- "Expecting a short value but got " + value,
- "java.lang.String",
- key );
- }
- }
-
- /**
- * Retrieve a integer from bundle.
- *
- * @param key the key of resource
- * @param defaultValue the default value if key is missing
- * @return the resource integer
- */
- public int getInteger( final String key, final int defaultValue )
- {
- try
- {
- return getInteger( key );
- }
- catch( final MissingResourceException mre )
- {
- return defaultValue;
- }
- }
-
- /**
- * Retrieve a integer from bundle.
- *
- * @param key the key of resource
- * @return the resource integer
- */
- public int getInteger( final String key )
- throws MissingResourceException
- {
- final ResourceBundle bundle = getBundle();
- final String value = bundle.getString( key );
- try
- {
- return Integer.parseInt( value );
- }
- catch( final NumberFormatException nfe )
- {
- throw new MissingResourceException(
- "Expecting a integer value but got " + value,
- "java.lang.String",
- key );
- }
- }
-
- /**
- * Retrieve a long from bundle.
- *
- * @param key the key of resource
- * @param defaultValue the default value if key is missing
- * @return the resource long
- */
- public long getLong( final String key, final long defaultValue )
- {
- try
- {
- return getLong( key );
- }
- catch( final MissingResourceException mre )
- {
- return defaultValue;
- }
- }
-
- /**
- * Retrieve a long from bundle.
- *
- * @param key the key of resource
- * @return the resource long
- */
- public long getLong( final String key )
- throws MissingResourceException
- {
- final ResourceBundle bundle = getBundle();
- final String value = bundle.getString( key );
- try
- {
- return Long.parseLong( value );
- }
- catch( final NumberFormatException nfe )
- {
- throw new MissingResourceException(
- "Expecting a long value but got " + value,
- "java.lang.String",
- key );
- }
- }
-
- /**
- * Retrieve a float from bundle.
- *
- * @param key the key of resource
- * @param defaultValue the default value if key is missing
- * @return the resource float
- */
- public float getFloat( final String key, final float defaultValue )
- {
- try
- {
- return getFloat( key );
- }
- catch( final MissingResourceException mre )
- {
- return defaultValue;
- }
- }
-
- /**
- * Retrieve a float from bundle.
- *
- * @param key the key of resource
- * @return the resource float
- */
- public float getFloat( final String key )
- throws MissingResourceException
- {
- final ResourceBundle bundle = getBundle();
- final String value = bundle.getString( key );
- try
- {
- return Float.parseFloat( value );
- }
- catch( final NumberFormatException nfe )
- {
- throw new MissingResourceException(
- "Expecting a float value but got " + value,
- "java.lang.String",
- key );
- }
- }
-
- /**
- * Retrieve a double from bundle.
- *
- * @param key the key of resource
- * @param defaultValue the default value if key is missing
- * @return the resource double
- */
- public double getDouble( final String key, final double defaultValue )
- {
- try
- {
- return getDouble( key );
- }
- catch( final MissingResourceException mre )
- {
- return defaultValue;
- }
- }
-
- /**
- * Retrieve a double from bundle.
- *
- * @param key the key of resource
- * @return the resource double
- */
- public double getDouble( final String key )
- throws MissingResourceException
- {
- final ResourceBundle bundle = getBundle();
- final String value = bundle.getString( key );
- try
- {
- return Double.parseDouble( value );
- }
- catch( final NumberFormatException nfe )
- {
- throw new MissingResourceException(
- "Expecting a double value but got " + value,
- "java.lang.String",
- key );
- }
- }
-
- /**
- * Retrieve a date from bundle.
- *
- * @param key the key of resource
- * @param defaultValue the default value if key is missing
- * @return the resource date
- */
- public Date getDate( final String key, final Date defaultValue )
- {
- try
- {
- return getDate( key );
- }
- catch( final MissingResourceException mre )
- {
- return defaultValue;
- }
- }
-
- /**
- * Retrieve a date from bundle.
- *
- * @param key the key of resource
- * @return the resource date
- */
- public Date getDate( final String key )
- throws MissingResourceException
- {
- final ResourceBundle bundle = getBundle();
- final String value = bundle.getString( key );
- try
- {
- final DateFormat format =
- DateFormat.getDateInstance( DateFormat.DEFAULT, m_locale );
- return format.parse( value );
- }
- catch( final ParseException pe )
- {
- throw new MissingResourceException(
- "Expecting a date value but got " + value,
- "java.lang.String",
- key );
- }
- }
-
- /**
- * Retrieve a time from bundle.
- *
- * @param key the key of resource
- * @param defaultValue the default value if key is missing
- * @return the resource time
- */
- public Date getTime( final String key, final Date defaultValue )
- {
- try
- {
- return getTime( key );
- }
- catch( final MissingResourceException mre )
- {
- return defaultValue;
- }
- }
-
- /**
- * Retrieve a time from bundle.
- *
- * @param key the key of resource
- * @return the resource time
- */
- public Date getTime( final String key )
- throws MissingResourceException
- {
- final ResourceBundle bundle = getBundle();
- final String value = bundle.getString( key );
- try
- {
- final DateFormat format =
- DateFormat.getTimeInstance( DateFormat.DEFAULT, m_locale );
- return format.parse( value );
- }
- catch( final ParseException pe )
- {
- throw new MissingResourceException(
- "Expecting a time value but got " + value,
- "java.lang.String",
- key );
- }
- }
-
- /**
- * Retrieve a time from bundle.
- *
- * @param key the key of resource
- * @param defaultValue the default value if key is missing
- * @return the resource time
- */
- public Date getDateTime( final String key, final Date defaultValue )
- {
- try
- {
- return getDateTime( key );
- }
- catch( final MissingResourceException mre )
- {
- return defaultValue;
- }
- }
-
- /**
- * Retrieve a date + time from bundle.
- *
- * @param key the key of resource
- * @return the resource date + time
- */
- public Date getDateTime( final String key )
- throws MissingResourceException
- {
- final ResourceBundle bundle = getBundle();
- final String value = bundle.getString( key );
- try
- {
- final DateFormat format =
- DateFormat.getDateTimeInstance( DateFormat.DEFAULT,
- DateFormat.DEFAULT,
- m_locale );
- return format.parse( value );
- }
- catch( final ParseException pe )
- {
- throw new MissingResourceException(
- "Expecting a time value but got " + value,
- "java.lang.String",
- key );
- }
- }
-
- /**
- * Retrieve a raw string from bundle.
- *
- * @param key the key of resource
- * @return the resource string
- */
- public String getString( final String key )
- throws MissingResourceException
- {
- final ResourceBundle bundle = getBundle();
- return bundle.getString( key );
- }
-
- /**
- * Retrieve a raw string from bundle.
- *
- * @param key the key of resource
- * @param defaultValue the default value if key is missing
- * @return the resource string
- */
- public String getString( final String key, final String defaultValue )
- {
- try
- {
- return getString( key );
- }
- catch( final MissingResourceException mre )
- {
- return defaultValue;
- }
- }
-
- /**
- * Retrieve a string from resource bundle and format it with specified
- * args.
- *
- * @param key the key for resource
- * @param arg1 an arg
- * @return the formatted string
- */
- public String format( final String key, final Object arg1 )
- {
- final Object[] args = new Object[]{arg1};
- return format( key, args );
- }
-
- /**
- * Retrieve a string from resource bundle and format it with specified
- * args.
- *
- * @param key the key for resource
- * @param arg1 an arg
- * @param arg2 an arg
- * @return the formatted string
- */
- public String format( final String key,
- final Object arg1,
- final Object arg2 )
- {
- final Object[] args = new Object[]{arg1, arg2};
- return format( key, args );
- }
-
- /**
- * Retrieve a string from resource bundle and format it with specified
- * args.
- *
- * @param key the key for resource
- * @param arg1 an arg
- * @param arg2 an arg
- * @param arg3 an arg
- * @return the formatted string
- */
- public String format( final String key,
- final Object arg1,
- final Object arg2,
- final Object arg3 )
- {
- final Object[] args = new Object[]{arg1, arg2, arg3};
- return format( key, args );
- }
-
- /**
- * Retrieve a string from resource bundle and format it with specified
- * args.
- *
- * @param key the key for resource
- * @param arg1 an arg
- * @param arg2 an arg
- * @param arg3 an arg
- * @param arg4 an arg
- * @return the formatted string
- */
- public String format( final String key,
- final Object arg1,
- final Object arg2,
- final Object arg3,
- final Object arg4 )
- {
- final Object[] args = new Object[]{arg1, arg2, arg3, arg4};
- return format( key, args );
- }
-
- /**
- * Retrieve a string from resource bundle and format it with specified
- * args.
- *
- * @param key the key for resource
- * @param arg1 an arg
- * @param arg2 an arg
- * @param arg3 an arg
- * @param arg4 an arg
- * @param arg5 an arg
- * @return the formatted string
- */
- public String format( final String key,
- final Object arg1,
- final Object arg2,
- final Object arg3,
- final Object arg4,
- final Object arg5 )
- {
- final Object[] args = new Object[]{arg1, arg2, arg3, arg4, arg5};
- return format( key, args );
- }
-
- /**
- * Retrieve a string from resource bundle and format it with specified
- * args.
- *
- * @param key the key for resource
- * @param args an array of args
- * @return the formatted string
- */
- public String format( final String key, final Object[] args )
- {
- try
- {
- final String pattern = getString( key );
- return MessageFormat.format( pattern, args );
- }
- catch( final MissingResourceException mre )
- {
- final StringBuffer sb = new StringBuffer();
- sb.append( "Unknown resource. Bundle: '" );
- sb.append( m_baseName );
- sb.append( "' Key: '" );
- sb.append( key );
- sb.append( "' Args: '" );
-
- for( int i = 0; i < args.length; i++ )
- {
- if( 0 != i )
- sb.append( "', '" );
- sb.append( args[ i ] );
- }
-
- sb.append( "' Reason: " );
- sb.append( mre );
-
- return sb.toString();
- }
- }
-
- /**
- * Retrieve underlying ResourceBundle. If bundle has not been loaded it will
- * be loaded by this method. Access is given in case other resources need to
- * be extracted that this Manager does not provide simplified access to.
- *
- * @return the ResourceBundle
- * @throws MissingResourceException if an error occurs
- */
- public synchronized final ResourceBundle getBundle()
- throws MissingResourceException
- {
- if( null == m_bundle )
- {
- // bundle wasn't cached, so load it, cache it, and return it.
- m_bundle = ResourceBundle.
- getBundle( m_baseName, m_locale, m_classLoader );
- }
- return m_bundle;
</pre><pre class="diff" id="added">+ super( baseName, locale, classLoader );
</pre><pre class="diff" id="context"> }
}
</pre></div>
<center><small><a href="http://www.badgers-in-foil.co.uk/projects/cvsspam/" title="commit -> email">CVSspam</a> 0.2.8</small></center>
</body></html>