001/*
002 * Copyright 2003-2005 The Apache Software Foundation
003 * Copyright 2005 Stephen McConnell
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package net.dpml.cli.resource;
018
019import java.text.MessageFormat;
020
021import java.util.Locale;
022import java.util.MissingResourceException;
023import java.util.ResourceBundle;
024
025/**
026 * A utility class used to provide internationalisation support.
027 *
028 * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
029 * @version @PROJECT-VERSION@
030 */
031public final class ResourceHelper
032{
033    /** system property */
034    private static final String PROP_LOCALE = "net.dpml.cli.resource.bundle";
035
036    /** default package name */
037    private static final String DEFAULT_BUNDLE =
038        "net.dpml.cli.resource.CLIMessageBundle_en_US";
039        
040    private static ResourceHelper m_HELPER;
041
042    /** resource bundle */
043    private ResourceBundle m_bundle;
044
045    private String m_prop;
046    
047    /**
048     * Create a new ResourceHelper for the specified class.
049     */
050    private ResourceHelper() 
051    {
052        String bundleName = System.getProperty( PROP_LOCALE );
053
054        if( bundleName == null )
055        {
056            bundleName = DEFAULT_BUNDLE;
057        }
058
059        m_prop = bundleName;
060        
061        int firstUnderscore = bundleName.indexOf( '_' );
062        int secondUnderscore = bundleName.indexOf( '_', firstUnderscore + 1 );
063
064        Locale locale;
065        if( firstUnderscore != -1 )
066        { 
067            String language = bundleName.substring( firstUnderscore + 1, secondUnderscore );
068            String country = bundleName.substring( secondUnderscore + 1 );
069            locale = new Locale( language, country );
070        }
071        else 
072        {
073            locale = Locale.getDefault();
074        }
075        // initialize the bundle
076        try 
077        {
078            m_bundle = ResourceBundle.getBundle( bundleName, locale );
079        } 
080        catch( MissingResourceException exp )
081        {
082            m_bundle = ResourceBundle.getBundle( DEFAULT_BUNDLE, locale );
083        }
084    }
085
086   /**
087    * Return the resource bundle name.
088    * @return the name
089    */
090    public String getBundleName()
091    {
092        return m_prop;
093    }
094    
095    /**
096     * Gets the ResourceHelper appropriate to the specified class.
097     * @return a ResourceHelper
098     */
099    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}