You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by cr...@apache.org on 2002/12/29 00:29:49 UTC

cvs commit: jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl LocalStrings.properties

craigmcc    2002/12/28 15:29:49

  Modified:    resources/src/java/org/apache/commons/resources
                        Messages.java
               resources/src/java/org/apache/commons/resources/impl
                        ResourceBundleResources.java
               resources/src/test/org/apache/commons/resources
                        MessagesTestCase.java
  Added:       resources/src/java/org/apache/commons/resources
                        ResourcesKeyException.java
               resources/src/test/org/apache/commons/resources/impl
                        LocalStrings.properties
  Log:
  Add a static convenience method so that Java classes can easily gain access
  to internationalized message strings (from a LocalStrings resource in the
  same package) like this:
  
    protected static Messages messages =
      Messages.getMessages("com.mycompany.mypackage");
  
  Revision  Changes    Path
  1.2       +46 -4     jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/Messages.java
  
  Index: Messages.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/Messages.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Messages.java	28 Dec 2002 21:41:58 -0000	1.1
  +++ Messages.java	28 Dec 2002 23:29:48 -0000	1.2
  @@ -65,6 +65,10 @@
   import java.io.Serializable;
   import java.text.MessageFormat;
   import java.util.Locale;
  +import org.apache.commons.resources.Resources;
  +import org.apache.commons.resources.ResourcesException;
  +import org.apache.commons.resources.ResourcesFactory;
  +import org.apache.commons.resources.impl.ResourceBundleResourcesFactory;
   
   
   /**
  @@ -326,6 +330,16 @@
       }
   
   
  +    // ------------------------------------------------------- Static Variables
  +
  +
  +    /**
  +     * <p>The {@link ResourcesFactory} that will be used by the
  +     * <code>getMessages()</code> method.</p>
  +     */
  +    protected static ResourcesFactory factory = null;
  +
  +
       // --------------------------------------------------------- Static Methods
   
   
  @@ -563,6 +577,34 @@
           args[2] = arg2;
           args[3] = arg3;
           return (getMessage(resources, locale, key, args));
  +
  +    }
  +
  +
  +    /**
  +     * <p>Convenience factory method to create a {@link Messages} instance
  +     * that wraps a {@link Resources} instance that contains message resources
  +     * for the specified Java package.  It is expected that the resources
  +     * for each package will be in properties files that are nested in the
  +     * package directory, with the name <code>LocalStrings.properties</code>
  +     * for the default messages, and names like
  +     * <code>LocalStrings_en_US.properties</code> for messages localized to
  +     * a particular <code>Locale</code>.</p>
  +     *
  +     * @param name Package name of the Java package for which
  +     *  local message resources are desired
  +     */
  +    public static Messages getMessages(String name) {
  +
  +        if (factory == null) {
  +            factory = new ResourceBundleResourcesFactory();
  +        }
  +        try {
  +            Resources resources = factory.getResources(name + ".LocalStrings");
  +            return (new Messages(resources));
  +        } catch (ResourcesException e) {
  +            return (null);
  +        }
   
       }
   
  
  
  
  1.1                  jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourcesKeyException.java
  
  Index: ResourcesKeyException.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/ResourcesKeyException.java,v 1.1 2002/12/28 23:29:48 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2002/12/28 23:29:48 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  
  package org.apache.commons.resources;
  
  
  /**
   * <p>Specialized subclass of {@link ResourcesException} that is thrown
   * by a resource getter method of a {@link Resources} instance, if an
   * invalid key value is specified and the <code>returnNull</code> property
   * is <code>false</code>.</p>
   *
   * @author Craig R. McClanahan
   * @version $Revision: 1.1 $ $Date: 2002/12/28 23:29:48 $
   */
  public class ResourcesKeyException extends ResourcesException {
  
  
      /**
       * <p>Construct an exception instance documenting that the specified
       * key value was invalid.</p>
       *
       * @param key The invalid key value
       */
      public ResourcesKeyException(String key) {
          super(key);
      }
  
  
  }
  
  
  
  1.4       +44 -5     jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java
  
  Index: ResourceBundleResources.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/java/org/apache/commons/resources/impl/ResourceBundleResources.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ResourceBundleResources.java	28 Dec 2002 04:40:55 -0000	1.3
  +++ ResourceBundleResources.java	28 Dec 2002 23:29:48 -0000	1.4
  @@ -74,6 +74,8 @@
   import java.util.MissingResourceException;
   import java.util.ResourceBundle;
   import java.util.TimeZone;
  +import org.apache.commons.logging.Log;
  +import org.apache.commons.logging.LogFactory;
   import org.apache.commons.resources.Resources;
   import org.apache.commons.resources.ResourcesException;
   import org.apache.commons.resources.ResourcesKeyException;
  @@ -132,10 +134,33 @@
       protected Map bundles = new HashMap();
   
   
  +    /**
  +     * <p>The logging instance for this class.</p>
  +     */
  +    protected static Log log =
  +        LogFactory.getLog(ResourceBundleResources.class);
  +
  +
       // ------------------------------------------------------ Lifecycle Methods
   
   
       /**
  +     * <p>This must be called to initialize the data content of this
  +     * {@link Resources} instance, before any of the <code>getXxx()</code>
  +     * methods are called.</p>
  +     *
  +     * @exception ResourcesException if an error occurs during initialization
  +     */
  +    public void init() throws ResourcesException {
  +
  +        if (log.isDebugEnabled()) {
  +            log.debug("Initializing for base name '" + base + "'");
  +        }
  +
  +    }
  +
  +
  +    /**
        * <p>This method must be called when the manager of this resource
        * decides that it's no longer needed.  After this method is called,
        * no further calls to any of the <code>getXxx()</code> methods are
  @@ -145,6 +170,9 @@
        */
       public void destroy() throws ResourcesException {
   
  +        if (log.isDebugEnabled()) {
  +            log.debug("Finalizing for base name '" + base + "'");
  +        }
           synchronized (bundles) {
               Iterator loaders = bundles.keySet().iterator();
               while (loaders.hasNext()) {
  @@ -289,12 +317,23 @@
       public String getString(String key, Locale locale, TimeZone timeZone)
           throws ResourcesException {
   
  +        if (log.isTraceEnabled()) {
  +            log.trace("Retrieving message for key '" + key + "' for locale '"
  +                      + locale + "'");
  +        }
           try {
               ResourceBundle bundle = getBundle(locale, timeZone);
  -            return (bundle.getString(key));
  +            String message = bundle.getString(key);
  +            if (log.isTraceEnabled()) {
  +                log.trace("Retrieved message is '" + message + "'");
  +            }
  +            return (message);
           } catch (ClassCastException e) {
               throw new ResourcesException(e);
           } catch (MissingResourceException e) {
  +            if (log.isTraceEnabled()) {
  +                log.trace("No message found");
  +            }
               if (isReturnNull()) {
                   return (null);
               } else {
  
  
  
  1.2       +25 -4     jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/MessagesTestCase.java
  
  Index: MessagesTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/MessagesTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MessagesTestCase.java	28 Dec 2002 21:41:58 -0000	1.1
  +++ MessagesTestCase.java	28 Dec 2002 23:29:49 -0000	1.2
  @@ -143,6 +143,27 @@
       // ------------------------------------------------ Individual Test Methods
   
   
  +    // Test the use of the shortcut getMessages() method
  +    public void testLocal() {
  +        String message = null;
  +        Messages local =
  +            Messages.getMessages("org.apache.commons.resources.impl");
  +        assertNotNull("Local messages found", local);
  +        message = local.getMessage("local.message");
  +        assertEquals("Correct individual message",
  +                     "[Local] REPLACE {0} WITH {1}",
  +                     message);
  +        message = local.getMessage("local.message", "abc", "def");
  +        assertEquals("Correct replaced message",
  +                     "[Local] REPLACE abc WITH def",
  +                     message);
  +        message = local.getMessage("local.missing");
  +        assertEquals("Correct missing message",
  +                     "???local.missing???",
  +                     message);
  +    }
  +
  +
       // Test the ability to retrieve messages with replacements
       public void testMessages() {
           String message = null;
  
  
  
  1.1                  jakarta-commons-sandbox/resources/src/test/org/apache/commons/resources/impl/LocalStrings.properties
  
  Index: LocalStrings.properties
  ===================================================================
  local.message=[Local] REPLACE {0} WITH {1}
  local.test=[Local] TEST
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>