commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adammurd...@apache.org
Subject cvs commit: jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/util Messages.java
Date Tue, 22 Oct 2002 13:01:58 GMT
adammurdoch    2002/10/22 06:01:57

  Modified:    vfs/src/java/org/apache/commons/vfs/util Messages.java
  Added:       vfs/src/java/org/apache/commons/vfs Resources.properties
  Removed:     vfs/src/java/org/apache/commons/vfs/impl
                        Resources.properties
               vfs/src/java/org/apache/commons/vfs/provider
                        Resources.properties
               vfs/src/java/org/apache/commons/vfs/provider/ftp
                        Resources.properties
               vfs/src/java/org/apache/commons/vfs/provider/jar
                        Resources.properties
               vfs/src/java/org/apache/commons/vfs/provider/local
                        Resources.properties
               vfs/src/java/org/apache/commons/vfs/provider/smb
                        Resources.properties
               vfs/src/java/org/apache/commons/vfs/provider/zip
                        Resources.properties
  Log:
  - Combined all messages into a single properties file.
  - Changed Messages.getString() to use this combined file.
  
  Revision  Changes    Path
  1.3       +112 -2    jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/Resources.properties
  
  
  
  
  1.2       +42 -11    jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/util/Messages.java
  
  Index: Messages.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/util/Messages.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Messages.java	22 Oct 2002 11:41:49 -0000	1.1
  +++ Messages.java	22 Oct 2002 13:01:57 -0000	1.2
  @@ -7,6 +7,12 @@
    */
   package org.apache.commons.vfs.util;
   
  +import java.util.ResourceBundle;
  +import java.util.MissingResourceException;
  +import java.util.Map;
  +import java.util.HashMap;
  +import java.text.MessageFormat;
  +
   /**
    * Formats messages.
    *
  @@ -15,6 +21,10 @@
    */
   public class Messages
   {
  +    /** Map from message code to MessageFormat object for the message. */
  +    private static final Map messages = new HashMap();
  +    private static ResourceBundle resources;
  +
       private Messages()
       {
       }
  @@ -51,17 +61,38 @@
        */
       public static String getString( final String code, final Object[] params )
       {
  -        StringBuffer sb = new StringBuffer( code );
  -        sb.append( '{' );
  -        if ( params != null )
  +        try
  +        {
  +            final MessageFormat msg = findMessage( code );
  +            return msg.format( params );
  +        }
  +        catch ( final MissingResourceException mre )
  +        {
  +            return "Unknown message with code \"" + code + "\".";
  +        }
  +    }
  +
  +    /**
  +     * Locates a message by its code.
  +     */
  +    private static synchronized MessageFormat findMessage( final String code )
  +        throws MissingResourceException
  +    {
  +        // Check if the message is cached
  +        MessageFormat msg = (MessageFormat)messages.get( code );
  +        if ( msg != null )
  +        {
  +            return msg;
  +        }
  +
  +        // Locate the message
  +        if ( resources == null )
           {
  -            for ( int i = 0; i < params.length; i++ )
  -            {
  -                sb.append( params[ i ] );
  -                sb.append( ',' );
  -            }
  +            resources = ResourceBundle.getBundle( "org.apache.commons.vfs.Resources" );
           }
  -        sb.append( '}' );
  -        return sb.toString();
  +        final String msgText = resources.getString( code );
  +        msg = new MessageFormat( msgText );
  +        messages.put( code, msg );
  +        return msg;
       }
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message