/* ======================================================================
The Bodington System Software License, Version 1.0
  
Copyright (c) 2001 The University of Leeds.  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 acknowledgement:  "This product includes
software developed by the University of Leeds
(http://www.bodington.org/)."  Alternately, this acknowledgement may
appear in the software itself, if and wherever such third-party
acknowledgements normally appear.

4.  The names "Bodington", "Nathan Bodington", "Bodington System",
"Bodington Open Source Project", and "The University of Leeds" must not be
used to endorse or promote products derived from this software without
prior written permission. For written permission, please contact
d.gardner@leeds.ac.uk.

5.  The name "Bodington" may not appear in the name of products derived
from this software without prior written permission of the University of
Leeds.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO,  TITLE,  THE IMPLIED WARRANTIES 
OF QUALITY  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO 
EVENT SHALL THE UNIVERSITY OF LEEDS 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 was originally created by the University of Leeds and may contain voluntary 
contributions from others.  For more information on the Bodington Open Source Project, please 
see http://bodington.org/

====================================================================== */

package org.bodington.util;

import java.util.*;

/**
 *  Helper class to get over inadequacies of ResourceBundle Scheme.
 *  The inadequacy of the standard resource bundle is that only allows you
 *  to search two locales for the values, the specified locale and the default 
 *  locale. As when a web brower makes a request it can specify several locales
 *  (more than 2)
 *  in order of prefernce. This class allows that search to work.
 *  To work properly the default, top level resource bundle must be
 *  empty and the "en" locale must be complete.
 *
 * @author  jrm
 */
public class ResourceBundleHelper
{
    private static Hashtable instances = new Hashtable();
    
    private class AcceptableLocale
    {
        Locale locale;
        double q;
    }

    private class AcceptableLocaleComparator implements Comparator
    {
        public int compare(Object o1, Object o2)
        {
            if ( o1 == null )
                if ( o2 == null )
                    return 0;
                else
                    return 1;
            if ( o2 == null )
                return -1;
            
            if ( !(o1 instanceof AcceptableLocale) || !(o2 instanceof AcceptableLocale) )
                return 0;
            AcceptableLocale a1, a2;
            a1 = (AcceptableLocale)o1;
            a2 = (AcceptableLocale)o2;
            
            if ( a1.q < a2.q )
                return 1;
            if ( a1.q > a2.q )
                return -1;
            return 0;
        }
        
        public boolean equals( Object o )
        {
            return  o instanceof AcceptableLocaleComparator;
        }
    }

    String basename;
    private AcceptableLocale[] locales_in_order;
    
    
    /** Creates a new instance of ResourceBundleHelper */
    private ResourceBundleHelper( String basename, String accepts )
    {
        this.basename = basename;
        StringTokenizer tok = new StringTokenizer( accepts, "," );
        locales_in_order = new AcceptableLocale[ tok.countTokens() + 1 ];
        StringTokenizer spec, lspec;
        String qspec;
        int count, i;
        
        for ( i=0; tok.hasMoreTokens(); i++ )
        {
            locales_in_order[i] = new AcceptableLocale();
            
            spec = new StringTokenizer( tok.nextToken(), " ;" );
            lspec = new StringTokenizer( spec.nextToken(), "-" );
            count = lspec.countTokens();
            
            if ( count < 1 || count > 3 )
                throw new IllegalArgumentException( "Language spec must have one to three parts." );

            if ( count == 1 )
                locales_in_order[i].locale = new Locale( lspec.nextToken() );
            if ( count == 2 )
                locales_in_order[i].locale = new Locale( lspec.nextToken(), lspec.nextToken() );
            if ( count == 3 )
                locales_in_order[i].locale = new Locale( lspec.nextToken(), lspec.nextToken(), lspec.nextToken() );
            
            locales_in_order[i].q = 1.0;
            if ( spec.hasMoreTokens() )
            {
                qspec = spec.nextToken();
                if ( qspec.startsWith( "q=" ) )
                {
                    locales_in_order[i].q = Double.parseDouble( qspec.substring( 2 ) );
                }
            }
        }
        
        
        locales_in_order[i] = new AcceptableLocale();
        locales_in_order[i].locale = new Locale( "en" );
        locales_in_order[i].q = 0.0000000001;
        
        Arrays.sort( locales_in_order, new ResourceBundleHelper.AcceptableLocaleComparator() );
    }
    
    public static ResourceBundleHelper getResourceBundleHelper( String basename, String accepts )
    {
        synchronized ( instances )
        {
            if ( accepts == null )
                return getResourceBundleHelper( basename, "" );

            ResourceBundleHelper helper = (ResourceBundleHelper)instances.get( basename + ":" + accepts );
            if ( helper != null )
                return helper;

            try
            {
                helper = new ResourceBundleHelper( basename, accepts );
            }
            catch ( Exception e )
            {
                return null;
            }

            instances.put( basename + ":" + accepts, helper );
            
            return helper;
        }
    }
    

    public String getString( String key )
    {
        ResourceBundle bundle;
        String value;
        for ( int i=0; i<locales_in_order.length; i++ )
        {
            if ( locales_in_order[i] == null )
                continue;
            
            if ( locales_in_order[i].locale == null )
                continue;
            
            bundle = ResourceBundle.getBundle( basename, locales_in_order[i].locale );
            if ( bundle == null )
                continue;
            
            if ( !locales_in_order[i].locale.equals( bundle.getLocale() ) )
                continue;
            
            value = bundle.getString( key );
            if ( value == null )
                continue;
            
            return value;
        }
        
        return null;
    }
    
    public Locale getTopLocale()
    {
        return locales_in_order[0].locale;
    }
}
