Source for net.dpml.cli.resource.ResourceHelper

   1: /*
   2:  * Copyright 2003-2005 The Apache Software Foundation
   3:  * Copyright 2005 Stephen McConnell
   4:  *
   5:  * Licensed under the Apache License, Version 2.0 (the "License");
   6:  * you may not use this file except in compliance with the License.
   7:  * You may obtain a copy of the License at
   8:  *
   9:  *     http://www.apache.org/licenses/LICENSE-2.0
  10:  *
  11:  * Unless required by applicable law or agreed to in writing, software
  12:  * distributed under the License is distributed on an "AS IS" BASIS,
  13:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14:  * See the License for the specific language governing permissions and
  15:  * limitations under the License.
  16:  */
  17: package net.dpml.cli.resource;
  18: 
  19: import java.text.MessageFormat;
  20: 
  21: import java.util.Locale;
  22: import java.util.MissingResourceException;
  23: import java.util.ResourceBundle;
  24: 
  25: /**
  26:  * A utility class used to provide internationalisation support.
  27:  *
  28:  * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
  29:  * @version @PROJECT-VERSION@
  30:  */
  31: public final class ResourceHelper
  32: {
  33:     /** system property */
  34:     private static final String PROP_LOCALE = "net.dpml.cli.resource.bundle";
  35: 
  36:     /** default package name */
  37:     private static final String DEFAULT_BUNDLE =
  38:         "net.dpml.cli.resource.CLIMessageBundle_en_US";
  39:         
  40:     private static ResourceHelper m_HELPER;
  41: 
  42:     /** resource bundle */
  43:     private ResourceBundle m_bundle;
  44: 
  45:     private String m_prop;
  46:     
  47:     /**
  48:      * Create a new ResourceHelper for the specified class.
  49:      */
  50:     private ResourceHelper() 
  51:     {
  52:         String bundleName = System.getProperty( PROP_LOCALE );
  53: 
  54:         if( bundleName == null )
  55:         {
  56:             bundleName = DEFAULT_BUNDLE;
  57:         }
  58: 
  59:         m_prop = bundleName;
  60:         
  61:         int firstUnderscore = bundleName.indexOf( '_' );
  62:         int secondUnderscore = bundleName.indexOf( '_', firstUnderscore + 1 );
  63: 
  64:         Locale locale;
  65:         if( firstUnderscore != -1 )
  66:         { 
  67:             String language = bundleName.substring( firstUnderscore + 1, secondUnderscore );
  68:             String country = bundleName.substring( secondUnderscore + 1 );
  69:             locale = new Locale( language, country );
  70:         }
  71:         else 
  72:         {
  73:             locale = Locale.getDefault();
  74:         }
  75:         // initialize the bundle
  76:         try 
  77:         {
  78:             m_bundle = ResourceBundle.getBundle( bundleName, locale );
  79:         } 
  80:         catch( MissingResourceException exp )
  81:         {
  82:             m_bundle = ResourceBundle.getBundle( DEFAULT_BUNDLE, locale );
  83:         }
  84:     }
  85: 
  86:    /**
  87:     * Return the resource bundle name.
  88:     * @return the name
  89:     */
  90:     public String getBundleName()
  91:     {
  92:         return m_prop;
  93:     }
  94:     
  95:     /**
  96:      * Gets the ResourceHelper appropriate to the specified class.
  97:      * @return a ResourceHelper
  98:      */
  99:     public static ResourceHelper getResourceHelper()
 100:     {
 101:         String bundleName = System.getProperty( PROP_LOCALE );
 102:         if( m_HELPER == null || !m_HELPER.getBundleName().equals( bundleName ) )
 103:         {
 104:             m_HELPER = new ResourceHelper();
 105:         }
 106:         return m_HELPER;
 107:     }
 108: 
 109:     /**
 110:      * Returns the message for the specified key.
 111:      *
 112:      * @param key the unique identifier of the message
 113:      * @return String the formatted String
 114:      */
 115:     public String getMessage( final String key )
 116:     {
 117:         return getMessage( key, new Object[0] );
 118:     }
 119: 
 120:     /**
 121:      * Returns the message for the specified key and argument.
 122:      *
 123:      * @param key the unique identifier of the message
 124:      * @param value the argument value
 125:      * @return String the formatted String
 126:      */
 127:     public String getMessage( final String key, final Object value )
 128:     {
 129:         return getMessage( key, new Object[]{value} );
 130:     }
 131: 
 132:     /**
 133:      * Returns the message for the specified key and arguments.
 134:      *
 135:      * @param key the unique identifier of the message
 136:      * @param value1 an argument value
 137:      * @param value2 an argument value
 138:      * @return String the formatted String
 139:      */
 140:     public String getMessage(
 141:       final String key, final Object value1, final Object value2 )
 142:     {
 143:         return getMessage( key, new Object[]{value1, value2} );
 144:     }
 145: 
 146:     /**
 147:      * Returns the message for the specified key and arguments.
 148:      *
 149:      * @param key the unique identifier of the message
 150:      * @param value1 an argument value
 151:      * @param value2 an argument value
 152:      * @param value3 an argument value
 153:      *
 154:      * @return String the formatted String
 155:      */
 156:     public String getMessage(
 157:       final String key, final Object value1, final Object value2, final Object value3 )
 158:     {
 159:         return getMessage( key, new Object[]{value1, value2, value3} );
 160:     }
 161: 
 162:     /**
 163:      * Returns the message for the specified key and arguments.
 164:      *
 165:      * @param key the unique identifier of the message
 166:      * @param values argument values
 167:      * @return String the formatted String
 168:      */
 169:     public String getMessage( final String key, final Object[] values )
 170:     {
 171:         final String msgFormatStr = m_bundle.getString( key );
 172:         final MessageFormat msgFormat = new MessageFormat( msgFormatStr );
 173:         return msgFormat.format( values );
 174:     }
 175: }