// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   LocalizableSupport.java

package org.apache.batik.i18n;

import java.text.MessageFormat;
import java.util.*;

// Referenced classes of package org.apache.batik.i18n:
//            Localizable, LocaleGroup

public class LocalizableSupport
    implements Localizable
{

    public LocalizableSupport(String s, Class cls)
    {
        this(s, cls, null);
    }

    public LocalizableSupport(String s, Class cls, ClassLoader cl)
    {
        localeGroup = LocaleGroup.DEFAULT;
        resourceBundles = new ArrayList();
        bundleName = s;
        this.cls = cls;
        classLoader = cl;
    }

    public LocalizableSupport(String s)
    {
        this(s, (ClassLoader)null);
    }

    public LocalizableSupport(String s, ClassLoader cl)
    {
        localeGroup = LocaleGroup.DEFAULT;
        resourceBundles = new ArrayList();
        bundleName = s;
        classLoader = cl;
    }

    public void setLocale(Locale l)
    {
        if(locale != l)
        {
            locale = l;
            resourceBundles.clear();
            lastResourceClass = null;
        }
    }

    public Locale getLocale()
    {
        return locale;
    }

    public void setLocaleGroup(LocaleGroup lg)
    {
        localeGroup = lg;
    }

    public LocaleGroup getLocaleGroup()
    {
        return localeGroup;
    }

    public void setDefaultLocale(Locale l)
    {
        localeGroup.setLocale(l);
    }

    public Locale getDefaultLocale()
    {
        return localeGroup.getLocale();
    }

    public String formatMessage(String key, Object args[])
    {
        return MessageFormat.format(getString(key), args);
    }

    protected Locale getCurrentLocale()
    {
        if(locale != null)
            return locale;
        Locale l = localeGroup.getLocale();
        if(l != null)
            return l;
        else
            return Locale.getDefault();
    }

    protected boolean setUsedLocale()
    {
        Locale l = getCurrentLocale();
        if(usedLocale == l)
        {
            return false;
        } else
        {
            usedLocale = l;
            resourceBundles.clear();
            lastResourceClass = null;
            return true;
        }
    }

    public ResourceBundle getResourceBundle()
    {
        return getResourceBundle(0);
    }

    protected boolean hasNextResourceBundle(int i)
    {
        if(i == 0)
            return true;
        if(i < resourceBundles.size())
            return true;
        if(lastResourceClass == null)
            return false;
        return lastResourceClass != (java.lang.Object.class);
    }

    protected ResourceBundle lookupResourceBundle(String bundle, Class theClass)
    {
        ClassLoader cl = classLoader;
        ResourceBundle rb = null;
        if(cl != null)
        {
            try
            {
                rb = ResourceBundle.getBundle(bundle, usedLocale, cl);
            }
            catch(MissingResourceException mre) { }
            if(rb != null)
                return rb;
        }
        if(theClass != null)
            try
            {
                cl = theClass.getClassLoader();
            }
            catch(SecurityException se) { }
        if(cl == null)
            cl = getClass().getClassLoader();
        try
        {
            rb = ResourceBundle.getBundle(bundle, usedLocale, cl);
        }
        catch(MissingResourceException mre) { }
        return rb;
    }

    protected ResourceBundle getResourceBundle(int i)
    {
        setUsedLocale();
        ResourceBundle rb = null;
        if(cls == null)
        {
            if(resourceBundles.size() == 0)
            {
                rb = lookupResourceBundle(bundleName, null);
                resourceBundles.add(rb);
            }
            return (ResourceBundle)resourceBundles.get(0);
        }
        Class cl;
        String bundle;
        for(; i >= resourceBundles.size(); resourceBundles.add(lookupResourceBundle(bundle, cl)))
        {
            if(lastResourceClass == (java.lang.Object.class))
                return null;
            if(lastResourceClass == null)
                lastResourceClass = cls;
            else
                lastResourceClass = lastResourceClass.getSuperclass();
            cl = lastResourceClass;
            bundle = cl.getPackage().getName() + "." + bundleName;
        }

        return (ResourceBundle)resourceBundles.get(i);
    }

    public String getString(String key)
        throws MissingResourceException
    {
        setUsedLocale();
        for(int i = 0; hasNextResourceBundle(i); i++)
        {
            ResourceBundle rb = getResourceBundle(i);
            if(rb != null)
                try
                {
                    String ret = rb.getString(key);
                    if(ret != null)
                        return ret;
                }
                catch(MissingResourceException mre) { }
        }

        String classStr = cls == null ? bundleName : cls.toString();
        throw new MissingResourceException("Unable to find resource: " + key, classStr, key);
    }

    public int getInteger(String key)
        throws MissingResourceException
    {
        String i = getString(key);
        try
        {
            return Integer.parseInt(i);
        }
        catch(NumberFormatException e)
        {
            throw new MissingResourceException("Malformed integer", bundleName, key);
        }
    }

    public int getCharacter(String key)
        throws MissingResourceException
    {
        String s = getString(key);
        if(s == null || s.length() == 0)
            throw new MissingResourceException("Malformed character", bundleName, key);
        else
            return s.charAt(0);
    }

    protected LocaleGroup localeGroup;
    protected String bundleName;
    protected ClassLoader classLoader;
    protected Locale locale;
    protected Locale usedLocale;
    List resourceBundles;
    Class lastResourceClass;
    Class cls;
}
