ResourceBundleWrapper.java 8.17 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.agl.impl;

import com.adobe.agl.impl.ICUDebug;
import com.adobe.agl.util.ULocale;
import com.adobe.agl.util.UResourceBundle;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;

public class ResourceBundleWrapper
extends UResourceBundle {
    private ResourceBundle bundle = null;
    private String localeID = null;
    private String baseName = null;
    private Vector keys = null;
    private static final boolean DEBUG = ICUDebug.enabled("resourceBundleWrapper");

    private ResourceBundleWrapper(ResourceBundle bundle) {
        this.bundle = bundle;
    }

    protected Object handleGetObject(String aKey) {
        Object obj = null;
        for (ResourceBundleWrapper current = this; current != null; current = (ResourceBundleWrapper)current.getParent()) {
            try {
                obj = current.bundle.getObject(aKey);
                break;
            }
            catch (MissingResourceException ex) {
                continue;
            }
        }
        if (obj == null) {
            throw new MissingResourceException("Can't find resource for bundle " + this.baseName + ", key " + aKey, this.getClass().getName(), aKey);
        }
        return obj;
    }

    public Enumeration getKeys() {
        return this.keys.elements();
    }

    private void initKeysVector() {
        this.keys = new Vector();
        for (ResourceBundleWrapper current = this; current != null; current = (ResourceBundleWrapper)current.getParent()) {
            Enumeration<String> e = current.bundle.getKeys();
            while (e.hasMoreElements()) {
                String elem = e.nextElement();
                if (this.keys.contains(elem)) continue;
                this.keys.add(elem);
            }
        }
    }

    protected String getLocaleID() {
        return this.localeID;
    }

    protected String getBaseName() {
        return this.bundle.getClass().getName().replace('.', '/');
    }

    public ULocale getULocale() {
        return new ULocale(this.localeID);
    }

    public UResourceBundle getParent() {
        return (UResourceBundle)this.parent;
    }

    public static UResourceBundle getBundleInstance(String baseName, String localeID, ClassLoader root, boolean disableFallback) {
        UResourceBundle b = ResourceBundleWrapper.instantiateBundle(baseName, localeID, root, disableFallback);
        if (b == null) {
            String separator = "_";
            if (baseName.indexOf(47) >= 0) {
                separator = "/";
            }
            throw new MissingResourceException("Could not find the bundle " + baseName + separator + localeID, "", "");
        }
        return b;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Loose catch block
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     * Lifted jumps to return sites
     */
    protected static synchronized UResourceBundle instantiateBundle(String baseName, String localeID, ClassLoader root, boolean disableFallback) {
        ResourceBundleWrapper b;
        if (root == null) {
            root = ClassLoader.getSystemClassLoader();
        }
        final ClassLoader cl = root;
        String name = baseName;
        ULocale defaultLocale = ULocale.getDefault();
        if (localeID.length() != 0) {
            name = name + "_" + localeID;
        }
        if ((b = (ResourceBundleWrapper)ResourceBundleWrapper.loadFromCache(cl, name, defaultLocale)) == null) {
            block29 : {
                ResourceBundleWrapper parent = null;
                int i = localeID.lastIndexOf(95);
                if (i != -1) {
                    String locName = localeID.substring(0, i);
                    parent = (ResourceBundleWrapper)ResourceBundleWrapper.loadFromCache(cl, baseName + "_" + locName, defaultLocale);
                    if (parent == null) {
                        parent = (ResourceBundleWrapper)ResourceBundleWrapper.instantiateBundle(baseName, locName, cl, disableFallback);
                    }
                } else if (localeID.length() > 0 && (parent = (ResourceBundleWrapper)ResourceBundleWrapper.loadFromCache(cl, baseName, defaultLocale)) == null) {
                    parent = (ResourceBundleWrapper)ResourceBundleWrapper.instantiateBundle(baseName, "", cl, disableFallback);
                }
                try {
                    Class cls = cl.loadClass(name);
                    ResourceBundle bx = (ResourceBundle)cls.newInstance();
                    b = new ResourceBundleWrapper(bx);
                    if (parent != null) {
                        b.setParent(parent);
                    }
                    b.baseName = baseName;
                    b.localeID = localeID;
                }
                catch (ClassNotFoundException e) {
                    block28 : {
                        final String resName = name.replace('.', '/') + ".properties";
                        InputStream stream = (InputStream)AccessController.doPrivileged(new PrivilegedAction(){

                            public Object run() {
                                if (cl != null) {
                                    return cl.getResourceAsStream(resName);
                                }
                                return ClassLoader.getSystemResourceAsStream(resName);
                            }
                        });
                        if (stream != null) {
                            stream = new BufferedInputStream(stream);
                            b = new ResourceBundleWrapper(new PropertyResourceBundle(stream));
                            if (parent != null) {
                                b.setParent(parent);
                            }
                            b.baseName = baseName;
                            b.localeID = localeID;
                            try {
                                stream.close();
                            }
                            catch (Exception ex) {}
                            break block28;
                            catch (Exception ex) {
                                try {
                                    stream.close();
                                }
                                catch (Exception ex) {}
                                break block28;
                                catch (Throwable throwable) {
                                    try {
                                        stream.close();
                                        throw throwable;
                                    }
                                    catch (Exception ex) {
                                        // empty catch block
                                    }
                                    throw throwable;
                                }
                            }
                        }
                    }
                    if (b == null) {
                        String defaultName = defaultLocale.toString();
                        if (localeID.length() > 0 && localeID.indexOf(95) < 0 && defaultName.indexOf(localeID) == -1 && (b = (ResourceBundleWrapper)ResourceBundleWrapper.loadFromCache(cl, baseName + "_" + defaultName, defaultLocale)) == null) {
                            b = (ResourceBundleWrapper)ResourceBundleWrapper.instantiateBundle(baseName, defaultName, cl, disableFallback);
                        }
                    }
                    if (b == null) {
                        b = parent;
                    }
                }
                catch (Exception e) {
                    if (DEBUG) {
                        System.out.println("failure");
                    }
                    if (!DEBUG) break block29;
                    System.out.println(e);
                }
            }
            ResourceBundleWrapper.addToCache(cl, name, defaultLocale, b);
        }
        if (b != null) {
            b.initKeysVector();
            return b;
        }
        if (!DEBUG) return b;
        System.out.println("Returning null for " + baseName + "_" + localeID);
        return b;
    }

}