1 /* 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.accessibility; 27 28 import java.util.Enumeration; 29 import java.util.Hashtable; 30 import java.util.Vector; 31 import java.util.Locale; 32 import java.util.MissingResourceException; 33 import java.util.ResourceBundle; 34 35 /** 36 * <p>Base class used to maintain a strongly typed enumeration. This is 37 * the superclass of {@link AccessibleState} and {@link AccessibleRole}. 38 * <p>The toDisplayString method allows you to obtain the localized string 39 * for a locale independent key from a predefined ResourceBundle for the 40 * keys defined in this class. This localized string is intended to be 41 * readable by humans. 42 * 43 * @see AccessibleRole 44 * @see AccessibleState 45 * 46 * @author Willie Walker 47 * @author Peter Korn 48 * @author Lynn Monsanto 49 */ 50 public abstract class AccessibleBundle { 51 52 private static Hashtable table = new Hashtable(); 53 private final String defaultResourceBundleName 54 = "com.sun.accessibility.internal.resources.accessibility"; 55 56 /** 57 * Construct an {@code AccessibleBundle}. 58 */ 59 public AccessibleBundle() { 60 } 61 62 /** 63 * The locale independent name of the state. This is a programmatic 64 * name that is not intended to be read by humans. 65 * @see #toDisplayString 66 */ 67 protected String key = null; 68 69 /** 70 * Obtains the key as a localized string. 71 * If a localized string cannot be found for the key, the 72 * locale independent key stored in the role will be returned. 73 * This method is intended to be used only by subclasses so that they 74 * can specify their own resource bundles which contain localized 75 * strings for their keys. 76 * @param resourceBundleName the name of the resource bundle to use for 77 * lookup 78 * @param locale the locale for which to obtain a localized string 79 * @return a localized String for the key. 80 */ 81 protected String toDisplayString(String resourceBundleName, 82 Locale locale) { 83 84 // loads the resource bundle if necessary 85 loadResourceBundle(resourceBundleName, locale); 86 87 // returns the localized string 88 Object o = table.get(locale); 89 if (o != null && o instanceof Hashtable) { 90 Hashtable resourceTable = (Hashtable) o; 91 o = resourceTable.get(key); 92 93 if (o != null && o instanceof String) { 94 return (String)o; 95 } 96 } 97 return key; 98 } 99 100 /** 101 * Obtains the key as a localized string. 102 * If a localized string cannot be found for the key, the 103 * locale independent key stored in the role will be returned. 104 * 105 * @param locale the locale for which to obtain a localized string 106 * @return a localized String for the key. 107 */ 108 public String toDisplayString(Locale locale) { 109 return toDisplayString(defaultResourceBundleName, locale); 110 } 111 112 /** 113 * Gets localized string describing the key using the default locale. 114 * @return a localized String describing the key for the default locale 115 */ 116 public String toDisplayString() { 117 return toDisplayString(Locale.getDefault()); 118 } 119 120 /** 121 * Gets localized string describing the key using the default locale. 122 * @return a localized String describing the key using the default locale 123 * @see #toDisplayString 124 */ 125 public String toString() { 126 return toDisplayString(); 127 } 128 129 /* 130 * Loads the Accessibility resource bundle if necessary. 131 */ 132 private void loadResourceBundle(String resourceBundleName, 133 Locale locale) { 134 if (! table.contains(locale)) { 135 136 try { 137 Hashtable resourceTable = new Hashtable(); 138 139 ResourceBundle bundle = ResourceBundle.getBundle(resourceBundleName, locale); 140 141 Enumeration iter = bundle.getKeys(); 142 while(iter.hasMoreElements()) { 143 String key = (String)iter.nextElement(); 144 resourceTable.put(key, bundle.getObject(key)); 145 } 146 147 table.put(locale, resourceTable); 148 } 149 catch (MissingResourceException e) { 150 System.err.println("loadResourceBundle: " + e); 151 // Just return so toDisplayString() returns the 152 // non-localized key. 153 return; 154 } 155 } 156 } 157 158 }