You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by ni...@apache.org on 2004/03/08 12:23:05 UTC

cvs commit: avalon/util/i18n/src/test/org/apache/avalon/util/i18n/test ResourceManagerTestCase.java ResourceManagerTestCaseResources.properties Resources.properties

niclas      2004/03/08 03:23:05

  Added:       util/i18n maven.xml project.xml
               util/i18n/src/java/org/apache/avalon/util/i18n
                        ResourceManager.java Resources.java package.html
               util/i18n/src/test/org/apache/avalon/util/i18n/test
                        ResourceManagerTestCase.java
                        ResourceManagerTestCaseResources.properties
                        Resources.properties
  Log:
  I18N utility to further remove excalibur dependencies in Merlin.
  
  Revision  Changes    Path
  1.1                  avalon/util/i18n/maven.xml
  
  Index: maven.xml
  ===================================================================
  <project default="jar:install">
  </project>
  
  
  
  1.1                  avalon/util/i18n/project.xml
  
  Index: project.xml
  ===================================================================
  <?xml version="1.0" encoding="ISO-8859-1"?>
  
  <project>
  
    <extend>${basedir}/../project.xml</extend>
  
    <groupId>avalon-util</groupId>
    <id>avalon-util-i18n</id>
    <name>Avalon Util Internationalization</name>
    <package>org.apache.avalon.util.i18n</package>
    <currentVersion>1.0</currentVersion>
  
    <inceptionYear>2004</inceptionYear>
    <shortDescription>Internationalization and Localization routines.</shortDescription>
  
  </project>
  
  
  
  1.1                  avalon/util/i18n/src/java/org/apache/avalon/util/i18n/ResourceManager.java
  
  Index: ResourceManager.java
  ===================================================================
  /* 
   * Copyright 1999-2004 The Apache Software Foundation
   * Licensed  under the  Apache License,  Version 2.0  (the "License");
   * you may not use  this file  except in  compliance with the License.
   * You may obtain a copy of the License at 
   * 
   *   http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed  under the  License is distributed on an "AS IS" BASIS,
   * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
   * implied.
   * 
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.avalon.util.i18n;
  
  import java.lang.ref.WeakReference;
  import java.util.HashMap;
  
  /**
   * Manager for resources.
   *
   * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
   */
  public class ResourceManager
  {
      /**
       * Permission needed to clear complete cache.
       */
      private static final RuntimePermission CLEAR_CACHE_PERMISSION =
          new RuntimePermission( "i18n.clearCompleteCache" );
      private static final HashMap c_resources = new HashMap();
  
      /**
       * Retrieve resource with specified basename.
       *
       * @param baseName the basename
       * @return the Resources
       */
      public static final Resources getBaseResources( final String baseName )
      {
          return getBaseResources( baseName, null );
      }
  
      /**
       * Retrieve resource with specified basename.
       *
       * @param baseName the basename
       * @param classLoader the classLoader to load resources from
       * @return the Resources
       */
      public synchronized static final Resources getBaseResources( final String baseName,
                                                                   final ClassLoader classLoader )
      {
          Resources resources = getCachedResource( baseName );
          if( null == resources )
          {
              resources = new Resources( baseName, classLoader );
              putCachedResource( baseName, resources );
          }
  
          return resources;
      }
  
      /**
       * 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 synchronized static final void clearResourceCache()
          throws SecurityException
      {
          final SecurityManager sm = System.getSecurityManager();
          if( null != sm )
          {
              sm.checkPermission( CLEAR_CACHE_PERMISSION );
          }
  
          c_resources.clear();
      }
  
      /**
       * Cache specified resource in weak reference.
       *
       * @param baseName the resource key
       * @param resources the resources object
       */
      private synchronized static final void putCachedResource( final String baseName,
                                                                final Resources resources )
      {
          c_resources.put( baseName,
                           new WeakReference( resources ) );
      }
  
      /**
       * Retrieve cached resource.
       *
       * @param baseName the resource key
       * @return resources the resources object
       */
      private synchronized static final Resources getCachedResource( final String baseName )
      {
          final WeakReference weakReference =
              (WeakReference)c_resources.get( baseName );
          if( null == weakReference )
          {
              return null;
          }
          else
          {
              return (Resources)weakReference.get();
          }
      }
  
      /**
       * Retrieve resource for specified name.
       * The basename is determined by name postfixed with ".Resources".
       *
       * @param name the name to use when looking up resources
       * @return the Resources
       */
      public static final Resources getResources( final String name )
      {
          return getBaseResources( name + ".Resources" );
      }
  
      /**
       * 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 Resources getPackageResources( final Class clazz )
      {
          return getBaseResources( getPackageResourcesBaseName( clazz ), 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 Resources getClassResources( final Class clazz )
      {
          return getBaseResources( getClassResourcesBaseName( clazz ), clazz.getClassLoader() );
      }
  
      /**
       * Retrieve resource basename for specified Classes package.
       * The basename is determined by name of classes package
       * postfixed with ".Resources".
       *
       * @param clazz the Class
       * @return the resource basename
       */
      public static final String getPackageResourcesBaseName( final Class clazz )
      {
          final Package pkg = clazz.getPackage();
  
          String baseName;
          if( null == pkg )
          {
              final String name = clazz.getName();
              if( -1 == name.lastIndexOf( "." ) )
              {
                  baseName = "Resources";
              }
              else
              {
                  baseName = name.substring( 0, name.lastIndexOf( "." ) ) + ".Resources";
              }
          }
          else
          {
              baseName = pkg.getName() + ".Resources";
          }
  
          return baseName;
      }
  
      /**
       * Retrieve resource basename for specified Class.
       * The basename is determined by name of Class
       * postfixed with "Resources".
       *
       * @param clazz the Class
       * @return the resource basename
       */
      public static final String getClassResourcesBaseName( final Class clazz )
      {
          return clazz.getName() + "Resources";
      }
  
      /**
       * Private Constructor to block instantiation.
       */
      private ResourceManager()
      {
      }
  }
  
  
  
  1.1                  avalon/util/i18n/src/java/org/apache/avalon/util/i18n/Resources.java
  
  Index: Resources.java
  ===================================================================
  /* 
   * Copyright 1999-2004 The Apache Software Foundation
   * Licensed  under the  Apache License,  Version 2.0  (the "License");
   * you may not use  this file  except in  compliance with the License.
   * You may obtain a copy of the License at 
   * 
   *   http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed  under the  License is distributed on an "AS IS" BASIS,
   * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
   * implied.
   * 
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.avalon.util.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.Random;
  import java.util.ResourceBundle;
  
  /**
   * A class to simplify extracting localized strings, icons
   * and other common resources from a ResourceBundle.
   *
   * Reworked to mirror behaviour of StringManager from Tomcat (format() to getString()).
   *
   * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
   */
  public class Resources
  {
      private static final Random RANDOM = new Random();
  
      ///Local of Resources
      private final Locale m_locale;
  
      ///Resource bundle referenced by manager
      private ResourceBundle m_bundle;
  
      ///Base name of resource bundle
      private String m_baseName;
  
      ///ClassLoader from which to load resources
      private ClassLoader m_classLoader;
  
      /**
       * Constructor that builds a manager in default locale.
       *
       * @param baseName the base name of ResourceBundle
       */
      public Resources( final String baseName )
      {
          this( baseName, Locale.getDefault(), null );
      }
  
      /**
       * Constructor that builds a manager in default locale
       * using specified ClassLoader.
       *
       * @param baseName the base name of ResourceBundle
       * @param classLoader the classLoader to load ResourceBundle from
       */
      public Resources( final String baseName, final ClassLoader classLoader )
      {
          this( baseName, Locale.getDefault(), classLoader );
      }
  
      /**
       * Constructor that builds a manager in specified locale.
       *
       * @param baseName the base name of ResourceBundle
       * @param locale the Locale for resource bundle
       */
      public Resources( final String baseName, final Locale locale )
      {
          this( baseName, locale, null );
      }
  
      /**
       * 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 Resources( final String baseName,
                        final Locale locale,
                        final ClassLoader classLoader )
      {
          if( null == baseName )
          {
              throw new NullPointerException( "baseName property is null" );
          }
          if( null == locale )
          {
              throw new NullPointerException( "locale property is null" );
          }
          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 )
          throws MissingResourceException
      {
          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 )
          throws MissingResourceException
      {
          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 )
          throws MissingResourceException
      {
          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 )
          throws MissingResourceException
      {
          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 )
          throws MissingResourceException
      {
          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 )
          throws MissingResourceException
      {
          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 )
          throws MissingResourceException
      {
          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 )
          throws MissingResourceException
      {
          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 )
          throws MissingResourceException
      {
          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 )
          throws MissingResourceException
      {
          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 )
          throws MissingResourceException
      {
          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 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 getString( 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 getString( 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 getString( 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 getString( 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 getString( 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 arg1 an arg
       * @param arg2 an arg
       * @param arg3 an arg
       * @param arg4 an arg
       * @param arg5 an arg
       * @param arg6 an arg
       * @return the formatted string
       */
      public String getString( final String key,
                               final Object arg1,
                               final Object arg2,
                               final Object arg3,
                               final Object arg4,
                               final Object arg5,
                               final Object arg6 )
      {
          final Object[] args = new Object[]{arg1, arg2, arg3, arg4, arg5, arg6};
          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
       * @param arg6 an arg
       * @param arg7 an arg
       * @return the formatted string
       */
      public String getString( final String key,
                               final Object arg1,
                               final Object arg2,
                               final Object arg3,
                               final Object arg4,
                               final Object arg5,
                               final Object arg6,
                               final Object arg7 )
      {
          final Object[] args = new Object[]{arg1, arg2, arg3, arg4, arg5, arg6, arg7};
          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 = getPatternString( 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 ResourceBundle getBundle()
          throws MissingResourceException
      {
          if( null == m_bundle )
          {
              // bundle wasn't cached, so load it, cache it, and return it.
              ClassLoader classLoader = m_classLoader;
              if( null == classLoader )
              {
                  classLoader = Thread.currentThread().getContextClassLoader();
              }
              if( null != classLoader )
              {
                  m_bundle = ResourceBundle.getBundle( m_baseName, m_locale, classLoader );
              }
              else
              {
                  m_bundle = ResourceBundle.getBundle( m_baseName, m_locale );
              }
          }
          return m_bundle;
      }
  
      /**
       * Utility method to retrieve a string from ResourceBundle.
       * If the key is a single string then that will be returned.
       * If key refers to string array then a random string will be chosen.
       * Other types cause an exception.
       *
       * @param key the key to resource
       * @return the string resource
       * @throws MissingResourceException if an error occurs
       */
      private String getPatternString( final String key )
          throws MissingResourceException
      {
          final ResourceBundle bundle = getBundle();
          final Object object = bundle.getObject( key );
  
          // is the resource a single string
          if( object instanceof String )
          {
              return (String)object;
          }
          else if( object instanceof String[] )
          {
              //if string array then randomly pick one
              final String[] strings = (String[])object;
              return strings[ RANDOM.nextInt( strings.length ) ];
          }
          else
          {
              throw new MissingResourceException( "Unable to find resource of appropriate type.",
                                                  "java.lang.String",
                                                  key );
          }
      }
  }
  
  
  
  1.1                  avalon/util/i18n/src/java/org/apache/avalon/util/i18n/package.html
  
  Index: package.html
  ===================================================================
  <html>
  <body>
  Resources supporting internationalization.
  </body>
  </html>
  
  
  
  1.1                  avalon/util/i18n/src/test/org/apache/avalon/util/i18n/test/ResourceManagerTestCase.java
  
  Index: ResourceManagerTestCase.java
  ===================================================================
  /* 
   * Copyright 1999-2004 The Apache Software Foundation
   * Licensed  under the  Apache License,  Version 2.0  (the "License");
   * you may not use  this file  except in  compliance with the License.
   * You may obtain a copy of the License at 
   * 
   *   http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed  under the  License is distributed on an "AS IS" BASIS,
   * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
   * implied.
   * 
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.avalon.util.i18n.test;
  
  import java.util.MissingResourceException;
  
  import junit.framework.TestCase;
  
  import org.apache.avalon.util.i18n.ResourceManager;
  import org.apache.avalon.util.i18n.Resources;
  
  /**
   * TestCase for ResourceManager.
   *
   * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
   */
  public class ResourceManagerTestCase
      extends TestCase
  {
      public ResourceManagerTestCase( final String name )
      {
          super( name );
      }
  
      public void testClassResources()
      {
          try
          {
              final Resources resources =
                  ResourceManager.getClassResources( getClass() );
  
              resources.getBundle();
          }
          catch( final MissingResourceException mre )
          {
              fail( "Unable to find class resource for class " + getClass() );
          }
      }
  
      public void testPackageResources()
      {
          try
          {
              final Resources resources =
                  ResourceManager.getPackageResources( getClass() );
  
              resources.getBundle();
          }
          catch( final MissingResourceException mre )
          {
              fail( "Unable to find package resources for class " + getClass() );
          }
      }
  }
  
  
  
  1.1                  avalon/util/i18n/src/test/org/apache/avalon/util/i18n/test/ResourceManagerTestCaseResources.properties
  
  	<<Binary file>>
  
  
  1.1                  avalon/util/i18n/src/test/org/apache/avalon/util/i18n/test/Resources.properties
  
  	<<Binary file>>
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org