Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/accessibility/AccessibleBundle.java
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.accessibility;2627import java.util.Enumeration;28import java.util.Hashtable;29import java.util.Vector;30import java.util.Locale;31import java.util.MissingResourceException;32import java.util.ResourceBundle;3334/**35* <p>Base class used to maintain a strongly typed enumeration. This is36* the superclass of {@link AccessibleState} and {@link AccessibleRole}.37* <p>The toDisplayString method allows you to obtain the localized string38* for a locale independent key from a predefined ResourceBundle for the39* keys defined in this class. This localized string is intended to be40* readable by humans.41*42* @see AccessibleRole43* @see AccessibleState44*45* @author Willie Walker46* @author Peter Korn47* @author Lynn Monsanto48*/49public abstract class AccessibleBundle {5051private static Hashtable table = new Hashtable();52private final String defaultResourceBundleName53= "com.sun.accessibility.internal.resources.accessibility";5455/**56* Construct an {@code AccessibleBundle}.57*/58public AccessibleBundle() {59}6061/**62* The locale independent name of the state. This is a programmatic63* name that is not intended to be read by humans.64* @see #toDisplayString65*/66protected String key = null;6768/**69* Obtains the key as a localized string.70* If a localized string cannot be found for the key, the71* locale independent key stored in the role will be returned.72* This method is intended to be used only by subclasses so that they73* can specify their own resource bundles which contain localized74* strings for their keys.75* @param resourceBundleName the name of the resource bundle to use for76* lookup77* @param locale the locale for which to obtain a localized string78* @return a localized String for the key.79*/80protected String toDisplayString(String resourceBundleName,81Locale locale) {8283// loads the resource bundle if necessary84loadResourceBundle(resourceBundleName, locale);8586// returns the localized string87Object o = table.get(locale);88if (o != null && o instanceof Hashtable) {89Hashtable resourceTable = (Hashtable) o;90o = resourceTable.get(key);9192if (o != null && o instanceof String) {93return (String)o;94}95}96return key;97}9899/**100* Obtains the key as a localized string.101* If a localized string cannot be found for the key, the102* locale independent key stored in the role will be returned.103*104* @param locale the locale for which to obtain a localized string105* @return a localized String for the key.106*/107public String toDisplayString(Locale locale) {108return toDisplayString(defaultResourceBundleName, locale);109}110111/**112* Gets localized string describing the key using the default locale.113* @return a localized String describing the key for the default locale114*/115public String toDisplayString() {116return toDisplayString(Locale.getDefault());117}118119/**120* Gets localized string describing the key using the default locale.121* @return a localized String describing the key using the default locale122* @see #toDisplayString123*/124public String toString() {125return toDisplayString();126}127128/*129* Loads the Accessibility resource bundle if necessary.130*/131private void loadResourceBundle(String resourceBundleName,132Locale locale) {133if (! table.contains(locale)) {134135try {136Hashtable resourceTable = new Hashtable();137138ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName, locale);139140Enumeration iter = bundle.getKeys();141while(iter.hasMoreElements()) {142String key = (String)iter.nextElement();143resourceTable.put(key, bundle.getObject(key));144}145146table.put(locale, resourceTable);147}148catch (MissingResourceException e) {149System.err.println("loadResourceBundle: " + e);150// Just return so toDisplayString() returns the151// non-localized key.152return;153}154}155}156157}158159160