UResourceBundle.java 13.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.agl.util;

import com.adobe.agl.impl.ICUResourceBundle;
import com.adobe.agl.impl.ICUResourceBundleReader;
import com.adobe.agl.impl.ResourceBundleWrapper;
import com.adobe.agl.util.ULocale;
import com.adobe.agl.util.UResourceBundleIterator;
import com.adobe.agl.util.UResourceTypeMismatchException;

import java.lang.ref.SoftReference;
import java.nio.ByteBuffer;
import java.util.*;

public abstract class UResourceBundle
extends ResourceBundle {
    private static SoftReference BUNDLE_CACHE;
    private static final ResourceCacheKey cacheKey;
    private static SoftReference ROOT_CACHE;
    private Vector keys = null;
    protected String key;
    protected int size = 1;
    protected long resource = -1;
    protected boolean isTopLevel = false;

    public static UResourceBundle getBundleInstance(String baseName, String localeName) {
        return UResourceBundle.getBundleInstance(baseName, localeName, ICUResourceBundle.ICU_DATA_CLASS_LOADER, false);
    }

    public static UResourceBundle getBundleInstance(String baseName, String localeName, ClassLoader root) {
        return UResourceBundle.getBundleInstance(baseName, localeName, root, false);
    }

    protected static UResourceBundle getBundleInstance(String baseName, String localeName, ClassLoader root, boolean disableFallback) {
        return UResourceBundle.instantiateBundle(baseName, localeName, root, disableFallback);
    }

    public static UResourceBundle getBundleInstance(String baseName, ULocale locale) {
        if (baseName == null) {
            baseName = "com/adobe/agl/impl/data/icudt40b";
        }
        if (locale == null) {
            locale = ULocale.getDefault();
        }
        return UResourceBundle.getBundleInstance(baseName, locale.toString(), ICUResourceBundle.ICU_DATA_CLASS_LOADER, false);
    }

    public abstract ULocale getULocale();

    protected abstract String getLocaleID();

    protected abstract String getBaseName();

    protected abstract UResourceBundle getParent();

    public Locale getLocale() {
        return this.getULocale().toLocale();
    }

    private static void addToCache(ResourceCacheKey key, UResourceBundle b) {
        HashMap<ResourceCacheKey, UResourceBundle> m = null;
        if (BUNDLE_CACHE != null) {
            m = (HashMap<ResourceCacheKey, UResourceBundle>)BUNDLE_CACHE.get();
        }
        if (m == null) {
            m = new HashMap<ResourceCacheKey, UResourceBundle>();
            BUNDLE_CACHE = new SoftReference<HashMap<K, V>>(m);
        }
        m.put(key, b);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected static void addToCache(ClassLoader cl, String fullName, ULocale defaultLocale, UResourceBundle b) {
        ResourceCacheKey resourceCacheKey = cacheKey;
        synchronized (resourceCacheKey) {
            cacheKey.setKeyValues(cl, fullName, defaultLocale);
            UResourceBundle.addToCache((ResourceCacheKey)cacheKey.clone(), b);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected static UResourceBundle loadFromCache(ClassLoader cl, String fullName, ULocale defaultLocale) {
        ResourceCacheKey resourceCacheKey = cacheKey;
        synchronized (resourceCacheKey) {
            cacheKey.setKeyValues(cl, fullName, defaultLocale);
            return UResourceBundle.loadFromCache(cacheKey);
        }
    }

    private static UResourceBundle loadFromCache(ResourceCacheKey key) {
        Map m;
        if (BUNDLE_CACHE != null && (m = (Map)BUNDLE_CACHE.get()) != null) {
            return (UResourceBundle)m.get(key);
        }
        return null;
    }

    private static int getRootType(String baseName, ClassLoader root) {
        Integer rootType;
        HashMap<String, Integer> m = null;
        if (ROOT_CACHE != null) {
            m = (HashMap<String, Integer>)ROOT_CACHE.get();
        }
        if (m == null) {
            m = new HashMap<String, Integer>();
            ROOT_CACHE = new SoftReference<HashMap<K, V>>(m);
        }
        if ((rootType = (Integer)m.get(baseName)) == null) {
            String rootLocale = baseName.indexOf(46) == -1 ? "root" : "";
            int rt = 0;
            try {
                ICUResourceBundle.getBundleInstance(baseName, rootLocale, root, true);
                rt = 1;
            }
            catch (MissingResourceException ex) {
                try {
                    ResourceBundleWrapper.getBundleInstance(baseName, rootLocale, root, true);
                    rt = 2;
                }
                catch (MissingResourceException e) {
                    // empty catch block
                }
            }
            rootType = new Integer(rt);
            m.put(baseName, rootType);
        }
        return rootType;
    }

    private static void setRootType(String baseName, int rootType) {
        Integer rt = new Integer(rootType);
        HashMap<String, Integer> m = null;
        if (ROOT_CACHE != null) {
            m = (HashMap<String, Integer>)ROOT_CACHE.get();
        } else {
            m = new HashMap<String, Integer>();
            ROOT_CACHE = new SoftReference<HashMap<K, V>>(m);
        }
        m.put(baseName, rt);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected static UResourceBundle instantiateBundle(String baseName, String localeName, ClassLoader root, boolean disableFallback) {
        UResourceBundle b = null;
        int rootType = UResourceBundle.getRootType(baseName, root);
        ULocale defaultLocale = ULocale.getDefault();
        switch (rootType) {
            case 1: {
                if (disableFallback) {
                    String fullName = ICUResourceBundleReader.getFullName(baseName, localeName);
                    ResourceCacheKey resourceCacheKey = cacheKey;
                    synchronized (resourceCacheKey) {
                        cacheKey.setKeyValues(root, fullName, defaultLocale);
                        b = UResourceBundle.loadFromCache(cacheKey);
                    }
                    if (b == null) {
                        b = ICUResourceBundle.getBundleInstance(baseName, localeName, root, disableFallback);
                        UResourceBundle.addToCache(cacheKey, b);
                    }
                } else {
                    b = ICUResourceBundle.getBundleInstance(baseName, localeName, root, disableFallback);
                }
                return b;
            }
            case 2: {
                return ResourceBundleWrapper.getBundleInstance(baseName, localeName, root, disableFallback);
            }
        }
        try {
            b = ICUResourceBundle.getBundleInstance(baseName, localeName, root, disableFallback);
            UResourceBundle.setRootType(baseName, 1);
        }
        catch (MissingResourceException ex) {
            b = ResourceBundleWrapper.getBundleInstance(baseName, localeName, root, disableFallback);
            UResourceBundle.setRootType(baseName, 2);
        }
        return b;
    }

    public ByteBuffer getBinary() {
        throw new UResourceTypeMismatchException("");
    }

    public String getString() {
        throw new UResourceTypeMismatchException("");
    }

    public int[] getIntVector() {
        throw new UResourceTypeMismatchException("");
    }

    public UResourceBundle get(String aKey) {
        UResourceBundle obj = this.handleGet(aKey, null, this);
        if (obj == null) {
            UResourceBundle res = this;
            while ((res = res.getParent()) != null && obj == null) {
                obj = res.handleGet(aKey, null, this);
            }
            if (obj == null) {
                String fullName = ICUResourceBundleReader.getFullName(this.getBaseName(), this.getLocaleID());
                throw new MissingResourceException("Can't find resource for bundle " + fullName + ", key " + aKey, this.getClass().getName(), aKey);
            }
        }
        ((ICUResourceBundle)obj).setLoadingStatus(this.getLocaleID());
        return obj;
    }

    public String getString(int index) {
        ICUResourceBundle temp = (ICUResourceBundle)this.get(index);
        if (temp.getType() == 0) {
            return temp.getString();
        }
        throw new UResourceTypeMismatchException("");
    }

    public UResourceBundle get(int index) {
        UResourceBundle obj = this.handleGet(index, null, this);
        if (obj == null) {
            obj = (ICUResourceBundle)this.getParent();
            if (obj != null) {
                obj = obj.get(index);
            }
            if (obj == null) {
                throw new MissingResourceException("Can't find resource for bundle " + this.getClass().getName() + ", key " + this.getKey(), this.getClass().getName(), this.getKey());
            }
        }
        ((ICUResourceBundle)obj).setLoadingStatus(this.getLocaleID());
        return obj;
    }

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

    private synchronized void initKeysVector() {
        if (this.keys != null) {
            return;
        }
        this.keys = new Vector<E>();
        Enumeration e = this.handleGetKeys();
        while (e.hasMoreElements()) {
            String elem = (String)e.nextElement();
            if (this.keys.contains(elem)) continue;
            this.keys.add(elem);
        }
    }

    public int getSize() {
        return this.size;
    }

    public int getType() {
        int type = ICUResourceBundle.RES_GET_TYPE(this.resource);
        if (type == 4) {
            return 2;
        }
        return type;
    }

    public UResourceBundleIterator getIterator() {
        return new UResourceBundleIterator(this);
    }

    public String getKey() {
        return this.key;
    }

    protected UResourceBundle handleGet(String aKey, HashMap table, UResourceBundle requested) {
        return null;
    }

    protected UResourceBundle handleGet(int index, HashMap table, UResourceBundle requested) {
        return null;
    }

    protected String[] handleGetStringArray() {
        return null;
    }

    protected Enumeration handleGetKeys() {
        Vector<String> resKeys = new Vector<String>();
        UResourceBundle item = null;
        for (int i = 0; i < this.size; ++i) {
            item = this.get(i);
            resKeys.add(item.getKey());
        }
        return resKeys.elements();
    }

    protected Object handleGetObject(String aKey) {
        return this.handleGetObjectImpl(aKey, this);
    }

    private Object handleGetObjectImpl(String aKey, UResourceBundle requested) {
        Object obj = this.resolveObject(aKey, requested);
        if (obj == null) {
            UResourceBundle parentBundle = this.getParent();
            if (parentBundle != null) {
                obj = parentBundle.handleGetObjectImpl(aKey, requested);
            }
            if (obj == null) {
                throw new MissingResourceException("Can't find resource for bundle " + this.getClass().getName() + ", key " + aKey, this.getClass().getName(), aKey);
            }
        }
        return obj;
    }

    private Object resolveObject(String aKey, UResourceBundle requested) {
        if (this.getType() == 0) {
            return this.getString();
        }
        UResourceBundle obj = this.handleGet(aKey, null, requested);
        if (obj != null) {
            if (obj.getType() == 0) {
                return obj.getString();
            }
            try {
                if (obj.getType() == 8) {
                    return obj.handleGetStringArray();
                }
            }
            catch (UResourceTypeMismatchException ex) {
                return obj;
            }
        }
        return obj;
    }

    static {
        cacheKey = new ResourceCacheKey();
    }

    private static final class ResourceCacheKey
    implements Cloneable {
        private SoftReference loaderRef;
        private String searchName;
        private ULocale defaultLocale;
        private int hashCodeCache;

        private ResourceCacheKey() {
        }

        public boolean equals(Object other) {
            if (this == other) {
                return true;
            }
            try {
                ResourceCacheKey otherEntry = (ResourceCacheKey)other;
                if (this.hashCodeCache != otherEntry.hashCodeCache) {
                    return false;
                }
                if (!this.searchName.equals(otherEntry.searchName)) {
                    return false;
                }
                if (this.defaultLocale == null ? otherEntry.defaultLocale != null : !this.defaultLocale.equals(otherEntry.defaultLocale)) {
                    return false;
                }
                if (this.loaderRef == null) {
                    return otherEntry.loaderRef == null;
                }
                return otherEntry.loaderRef != null && this.loaderRef.get() == otherEntry.loaderRef.get();
            }
            catch (NullPointerException e) {
                return false;
            }
            catch (ClassCastException e) {
                return false;
            }
        }

        public int hashCode() {
            return this.hashCodeCache;
        }

        public Object clone() {
            try {
                return super.clone();
            }
            catch (CloneNotSupportedException e) {
                throw new IllegalStateException();
            }
        }

        private synchronized void setKeyValues(ClassLoader root, String searchName, ULocale defaultLocale) {
            this.searchName = searchName;
            this.hashCodeCache = searchName.hashCode();
            this.defaultLocale = defaultLocale;
            if (defaultLocale != null) {
                this.hashCodeCache ^= defaultLocale.hashCode();
            }
            if (root == null) {
                this.loaderRef = null;
            } else {
                this.loaderRef = new SoftReference<ClassLoader>(root);
                this.hashCodeCache ^= root.hashCode();
            }
        }
    }

}