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

import com.adobe.agl.impl.ICUResourceBundle;
import com.adobe.agl.impl.ICUService;
import com.adobe.agl.impl.LocaleUtility;
import com.adobe.agl.util.ULocale;

import java.util.*;

public class ICULocaleService
extends ICUService {
    private ULocale fallbackLocale;
    private String fallbackLocaleName;

    public ICULocaleService(String name) {
        super(name);
    }

    public Object get(ULocale locale, ULocale[] actualReturn) {
        return this.get(locale, -1, actualReturn);
    }

    public Object get(ULocale locale, int kind, ULocale[] actualReturn) {
        ICUService.Key key = this.createKey(locale, kind);
        if (actualReturn == null) {
            return this.getKey(key);
        }
        String[] temp = new String[1];
        Object result = this.getKey(key, temp);
        if (result != null) {
            int n = temp[0].indexOf("/");
            if (n >= 0) {
                temp[0] = temp[0].substring(n + 1);
            }
            actualReturn[0] = new ULocale(temp[0]);
        }
        return result;
    }

    public ICUService.Factory registerObject(Object obj, ULocale locale) {
        return this.registerObject(obj, locale, -1, true);
    }

    public ICUService.Factory registerObject(Object obj, ULocale locale, int kind) {
        return this.registerObject(obj, locale, kind, true);
    }

    public ICUService.Factory registerObject(Object obj, ULocale locale, int kind, boolean visible) {
        SimpleLocaleKeyFactory factory = new SimpleLocaleKeyFactory(obj, locale, kind, visible);
        return this.registerFactory(factory);
    }

    public Locale[] getAvailableLocales() {
        Set visIDs = this.getVisibleIDs();
        Iterator iter = visIDs.iterator();
        Locale[] locales = new Locale[visIDs.size()];
        int n = 0;
        while (iter.hasNext()) {
            Locale loc = LocaleUtility.getLocaleFromName((String)iter.next());
            locales[n++] = loc;
        }
        return locales;
    }

    public ULocale[] getAvailableULocales() {
        Set visIDs = this.getVisibleIDs();
        Iterator iter = visIDs.iterator();
        ULocale[] locales = new ULocale[visIDs.size()];
        int n = 0;
        while (iter.hasNext()) {
            locales[n++] = new ULocale((String)iter.next());
        }
        return locales;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public String validateFallbackLocale() {
        ULocale loc = ULocale.getDefault();
        if (loc != this.fallbackLocale) {
            ICULocaleService iCULocaleService = this;
            synchronized (iCULocaleService) {
                if (loc != this.fallbackLocale) {
                    this.fallbackLocale = loc;
                    this.fallbackLocaleName = loc.getBaseName();
                    this.clearServiceCache();
                }
            }
        }
        return this.fallbackLocaleName;
    }

    public ICUService.Key createKey(String id) {
        return LocaleKey.createWithCanonicalFallback(id, this.validateFallbackLocale());
    }

    public ICUService.Key createKey(ULocale l, int kind) {
        return LocaleKey.createWithCanonical(l, this.validateFallbackLocale(), kind);
    }

    public static class ICUResourceBundleFactory
    extends LocaleKeyFactory {
        protected final String bundleName;

        public ICUResourceBundleFactory() {
            this("com/adobe/agl/impl/data/icudt40b");
        }

        public ICUResourceBundleFactory(String bundleName) {
            super(true);
            this.bundleName = bundleName;
        }

        protected Set getSupportedIDs() {
            return ICUResourceBundle.getFullLocaleNameSet(this.bundleName);
        }

        public void updateVisibleIDs(Map result) {
            Set visibleIDs = ICUResourceBundle.getAvailableLocaleNameSet(this.bundleName);
            Iterator iter = visibleIDs.iterator();
            while (iter.hasNext()) {
                String id = (String)iter.next();
                result.put(id, this);
            }
        }

        protected Object handleCreate(ULocale loc, int kind, ICUService service) {
            return ICUResourceBundle.getBundleInstance(this.bundleName, loc);
        }

        public String toString() {
            return super.toString() + ", bundle: " + this.bundleName;
        }
    }

    public static class SimpleLocaleKeyFactory
    extends LocaleKeyFactory {
        private final Object obj;
        private final String id;
        private final int kind;

        public SimpleLocaleKeyFactory(Object obj, ULocale locale, int kind, boolean visible) {
            this(obj, locale, kind, visible, null);
        }

        public SimpleLocaleKeyFactory(Object obj, ULocale locale, int kind, boolean visible, String name) {
            super(visible, name);
            this.obj = obj;
            this.id = locale.getBaseName();
            this.kind = kind;
        }

        public Object create(ICUService.Key key, ICUService service) {
            String keyID;
            LocaleKey lkey = (LocaleKey)key;
            if ((this.kind == -1 || this.kind == lkey.kind()) && this.id.equals(keyID = lkey.currentID())) {
                return this.obj;
            }
            return null;
        }

        public void updateVisibleIDs(Map result) {
            if (this.visible) {
                result.put(this.id, this);
            } else {
                result.remove(this.id);
            }
        }

        public String toString() {
            StringBuffer buf = new StringBuffer(super.toString());
            buf.append(", id: ");
            buf.append(this.id);
            buf.append(", kind: ");
            buf.append(this.kind);
            return buf.toString();
        }
    }

    public static abstract class LocaleKeyFactory
    implements ICUService.Factory {
        protected final String name;
        protected final boolean visible;

        protected LocaleKeyFactory(boolean visible) {
            this.visible = visible;
            this.name = null;
        }

        protected LocaleKeyFactory(boolean visible, String name) {
            this.visible = visible;
            this.name = name;
        }

        public Object create(ICUService.Key key, ICUService service) {
            if (this.handlesKey(key)) {
                LocaleKey lkey = (LocaleKey)key;
                int kind = lkey.kind();
                ULocale uloc = lkey.currentLocale();
                return this.handleCreate(uloc, kind, service);
            }
            return null;
        }

        protected boolean handlesKey(ICUService.Key key) {
            if (key != null) {
                String id = key.currentID();
                Set supported = this.getSupportedIDs();
                return supported.contains(id);
            }
            return false;
        }

        public void updateVisibleIDs(Map result) {
            Set cache = this.getSupportedIDs();
            Iterator iter = cache.iterator();
            while (iter.hasNext()) {
                String id = (String)iter.next();
                if (this.visible) {
                    result.put(id, this);
                    continue;
                }
                result.remove(id);
            }
        }

        public String getDisplayName(String id, ULocale locale) {
            if (locale == null) {
                return id;
            }
            ULocale loc = new ULocale(id);
            return loc.getDisplayName(locale);
        }

        protected Object handleCreate(ULocale loc, int kind, ICUService service) {
            return null;
        }

        protected Set getSupportedIDs() {
            return Collections.EMPTY_SET;
        }

        public String toString() {
            StringBuffer buf = new StringBuffer(super.toString());
            if (this.name != null) {
                buf.append(", name: ");
                buf.append(this.name);
            }
            buf.append(", visible: ");
            buf.append(this.visible);
            return buf.toString();
        }
    }

    public static class LocaleKey
    extends ICUService.Key {
        private int kind;
        private int varstart;
        private String primaryID;
        private String fallbackID;
        private String currentID;

        public static LocaleKey createWithCanonicalFallback(String primaryID, String canonicalFallbackID) {
            return LocaleKey.createWithCanonicalFallback(primaryID, canonicalFallbackID, -1);
        }

        public static LocaleKey createWithCanonicalFallback(String primaryID, String canonicalFallbackID, int kind) {
            if (primaryID == null) {
                return null;
            }
            if (primaryID.length() == 0) {
                primaryID = "root";
            }
            String canonicalPrimaryID = ULocale.getName(primaryID);
            return new LocaleKey(primaryID, canonicalPrimaryID, canonicalFallbackID, kind);
        }

        public static LocaleKey createWithCanonical(ULocale locale, String canonicalFallbackID, int kind) {
            if (locale == null) {
                return null;
            }
            String canonicalPrimaryID = locale.getName();
            return new LocaleKey(canonicalPrimaryID, canonicalPrimaryID, canonicalFallbackID, kind);
        }

        protected LocaleKey(String primaryID, String canonicalPrimaryID, String canonicalFallbackID, int kind) {
            super(primaryID);
            this.kind = kind;
            if (canonicalPrimaryID == null) {
                this.primaryID = "";
            } else {
                this.primaryID = canonicalPrimaryID;
                this.varstart = this.primaryID.indexOf(64);
            }
            this.fallbackID = this.primaryID.equals("") ? null : (canonicalFallbackID == null || this.primaryID.equals(canonicalFallbackID) ? "" : canonicalFallbackID);
            this.currentID = this.varstart == -1 ? this.primaryID : this.primaryID.substring(0, this.varstart);
        }

        public String prefix() {
            return this.kind == -1 ? null : Integer.toString(this.kind());
        }

        public int kind() {
            return this.kind;
        }

        public String canonicalID() {
            return this.primaryID;
        }

        public String currentID() {
            return this.currentID;
        }

        public String currentDescriptor() {
            String result = this.currentID();
            if (result != null) {
                StringBuffer buf = new StringBuffer();
                if (this.kind != -1) {
                    buf.append(this.prefix());
                }
                buf.append('/');
                buf.append(result);
                if (this.varstart != -1) {
                    buf.append(this.primaryID.substring(this.varstart, this.primaryID.length()));
                }
                result = buf.toString();
            }
            return result;
        }

        public ULocale currentLocale() {
            if (this.varstart == -1) {
                return new ULocale(this.currentID);
            }
            return new ULocale(this.currentID + this.primaryID.substring(this.varstart));
        }

        public boolean fallback() {
            int x = this.currentID.lastIndexOf(95);
            if (x != -1) {
                while (--x >= 0 && this.currentID.charAt(x) == '_') {
                }
                this.currentID = this.currentID.substring(0, x + 1);
                return true;
            }
            if (this.fallbackID != null) {
                if (this.fallbackID.length() == 0) {
                    this.currentID = "root";
                    this.fallbackID = null;
                } else {
                    this.currentID = this.fallbackID;
                    this.fallbackID = "";
                }
                return true;
            }
            this.currentID = null;
            return false;
        }

        public boolean isFallbackOf(String id) {
            return LocaleUtility.isFallbackOf(this.canonicalID(), id);
        }
    }

}