FileDocCategorySizeDatePackage
LocalStringsManager.javaAPI DocGlassfish v2 API8559Fri May 04 22:34:10 BST 2007com.sun.enterprise.admin.util

LocalStringsManager

public class LocalStringsManager extends Object
Implementation of a local string manager. Provides access to i18n messages for classes that need them. Now emulates the proper sun.enterprise.util.LocalStringManagerImpl with few essential differences:
1. It uses package hierarchy, but not classes hierarchy for resource files searching(it allows to place resources separatly from source files)
2. You can choose different resource file names (not only LocalName.properties);
3. New optional mode - with fixedResourceBundle allows to exclude bundle file search repeating during the subsequent getString() calls;

Fields Summary
static final String
DEFAULT_PROPERTY_FILE_NAME
private String
m_propertyFileName
private String
m_basePackage
private ResourceBundle
m_FixedResourceBundle
Constructors Summary
public LocalStringsManager(String basePackage, String propertyFileName)
Create a string manager that looks for LocalStrings.properties in the package of the basePackage.

param
basePackage Most high level package Class path with localized strings

    
                                  
        
    
        m_basePackage       = basePackage;
        m_propertyFileName  = propertyFileName;
    
public LocalStringsManager(String basePackage)

        this(basePackage, DEFAULT_PROPERTY_FILE_NAME);
    
Methods Summary
public java.lang.StringgetString(java.lang.String startPackage, java.lang.String key, java.lang.String defaultValue)
Get a localized string.

param
startPackage The package name which defines the starting directory for resource bundle serach.
param
key The name of the resource to fetch
param
defaultValue The default return value if not found
return
The localized value for the resource

        if(m_FixedResourceBundle!=null)
        {
            String value = m_FixedResourceBundle.getString(key);
            if(value!=null)
               return value;
            return defaultValue;
        }
               
        
        String stopPackage = m_basePackage;
        if(startPackage==null)
            startPackage = stopPackage;
        else
            if(!startPackage.startsWith(m_basePackage))
               stopPackage = startPackage; //maybe  = ""
        ResourceBundle resources  = null;

        while (startPackage.length()>0 && startPackage.length()>=stopPackage.length())
        {
            try
            {
                String resFileName = startPackage+"."+m_propertyFileName;
                startPackage = startPackage.substring(0, startPackage.lastIndexOf("."));
                resources = ResourceBundle.getBundle(resFileName);
                if ( resources != null )
                {
                    String value = resources.getString(key);
                    if ( value != null )
                        return value;
                }
            } catch (Exception ex)
            {
            }
        }
        
        // Look for a global resource (defined by defaultClass)
        if ( !stopPackage.equals(m_basePackage) )
        {
            return getString(null, key, defaultValue);
        }

        return defaultValue;
    
public java.lang.StringgetString(java.lang.String key, java.lang.String defaultValue)
Get a localized string from the base package.

param
key The name of the resource to fetch
param
defaultValue The default return value if not found
return
The localized string

        return getString(null, key, defaultValue);
    
public java.lang.StringgetString(java.lang.String startPackage, java.lang.String key, java.lang.String defaultFormat, java.lang.Object[] arguments)
Get a local string for the caller and format the arguments accordingly.

param
startPackage The package name which defines the starting directory for resource bundle serach.
param
key The key to the local format string
param
fmt The default format if not found in the resources
param
arguments The set of arguments to provide to the formatter
return
A formatted localized string

        MessageFormat f = new MessageFormat(
        getString(startPackage, key, defaultFormat));
        for (int i = 0; i < arguments.length; i++)
        {
            if ( arguments[i] == null )
            {
                arguments[i] = "null";
            } else if  ( !(arguments[i] instanceof String) &&
            !(arguments[i] instanceof Number) &&
            !(arguments[i] instanceof java.util.Date))
            {
                arguments[i] = arguments[i].toString();
            }
        }
        return f.format(arguments);
    
public java.lang.StringgetString(java.lang.String key, java.lang.String defaultFormat, java.lang.Object[] arguments)
Get a local string from the base package and format the arguments accordingly.

param
key The key to the local format string
param
defaultFormat The default format if not found in the resources
param
arguments The set of arguments to provide to the formatter
return
A formatted localized string

        return getString(null, key, defaultFormat, arguments);
    
protected voidsetFixedResourceBundle(java.lang.String startPackage)

        if(startPackage==null)
        {
            m_FixedResourceBundle = null;
            return;
        }
        
        String stopPackage = m_basePackage;
        if(startPackage==null)
            startPackage = stopPackage;
        else
            if(!startPackage.startsWith(m_basePackage))
               stopPackage = startPackage; //maybe  = ""
        ResourceBundle resources  = null;

        while (startPackage.length()>0 && startPackage.length()>=stopPackage.length())
        {
            try
            {
                String resFileName = startPackage+"."+m_propertyFileName;
                startPackage = startPackage.substring(0, startPackage.lastIndexOf("."));
                resources = ResourceBundle.getBundle(resFileName);
                if ( resources != null )
                {
                   m_FixedResourceBundle = resources;
                   return;
                }
            } catch (Exception ex)
            {
            }
        }
        
        // Look for a global resource (defined by defaultClass)
        if ( !stopPackage.equals(m_basePackage) )
        {
            setFixedResourceBundle(m_basePackage);
        }