001: /*
002: *
003: * @(#)ListResourceBundle.java 1.23 06/10/10
004: *
005: * Portions Copyright 2000-2006 Sun Microsystems, Inc. All Rights
006: * Reserved. Use is subject to license terms.
007: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License version
011: * 2 only, as published by the Free Software Foundation.
012: *
013: * This program is distributed in the hope that it will be useful, but
014: * WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License version 2 for more details (a copy is
017: * included at /legal/license.txt).
018: *
019: * You should have received a copy of the GNU General Public License
020: * version 2 along with this work; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022: * 02110-1301 USA
023: *
024: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
025: * Clara, CA 95054 or visit www.sun.com if you need additional
026: * information or have any questions.
027: */
028:
029: /*
030: * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
031: * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
032: *
033: * The original version of this source code and documentation
034: * is copyrighted and owned by Taligent, Inc., a wholly-owned
035: * subsidiary of IBM. These materials are provided under terms
036: * of a License Agreement between Taligent and Sun. This technology
037: * is protected by multiple US and International patents.
038: *
039: * This notice and attribution to Taligent may not be removed.
040: * Taligent is a registered trademark of Taligent, Inc.
041: *
042: */
043:
044: package java.util;
045:
046: /**
047: * <code>ListResourceBundle</code> is an abstract subclass of
048: * <code>ResourceBundle</code> that manages resources for a locale
049: * in a convenient and easy to use list. See <code>ResourceBundle</code> for
050: * more information about resource bundles in general.
051: *
052: * <P>
053: * Subclasses must override <code>getContents</code> and provide an array,
054: * where each item in the array is a pair of objects.
055: * The first element of each pair is the key, which must be a
056: * <code>String</code>, and the second element is the value associated with
057: * that key.
058: *
059: * <p>
060: * The following <a name="sample">example</a> shows two members of a resource
061: * bundle family with the base name "MyResources".
062: * "MyResources" is the default member of the bundle family, and
063: * "MyResources_fr" is the French member.
064: * These members are based on <code>ListResourceBundle</code>
065: * (a related <a href="PropertyResourceBundle.html#sample">example</a> shows
066: * how you can add a bundle to this family that's based on a properties file).
067: * The keys in this example are of the form "s1" etc. The actual
068: * keys are entirely up to your choice, so long as they are the same as
069: * the keys you use in your program to retrieve the objects from the bundle.
070: * Keys are case-sensitive.
071: * <blockquote>
072: * <pre>
073: *
074: * public class MyResources extends ListResourceBundle {
075: * public Object[][] getContents() {
076: * return contents;
077: * }
078: * static final Object[][] contents = {
079: * // LOCALIZE THIS
080: * {"s1", "The disk \"{1}\" contains {0}."}, // MessageFormat pattern
081: * {"s2", "1"}, // location of {0} in pattern
082: * {"s3", "My Disk"}, // sample disk name
083: * {"s4", "no files"}, // first ChoiceFormat choice
084: * {"s5", "one file"}, // second ChoiceFormat choice
085: * {"s6", "{0,number} files"}, // third ChoiceFormat choice
086: * {"s7", "3 Mar 96"}, // sample date
087: * {"s8", new Dimension(1,5)} // real object, not just string
088: * // END OF MATERIAL TO LOCALIZE
089: * };
090: * }
091: *
092: * public class MyResources_fr extends ListResourceBundle {
093: * public Object[][] getContents() {
094: * return contents;
095: * }
096: * static final Object[][] contents = {
097: * // LOCALIZE THIS
098: * {"s1", "Le disque \"{1}\" {0}."}, // MessageFormat pattern
099: * {"s2", "1"}, // location of {0} in pattern
100: * {"s3", "Mon disque"}, // sample disk name
101: * {"s4", "ne contient pas de fichiers"}, // first ChoiceFormat choice
102: * {"s5", "contient un fichier"}, // second ChoiceFormat choice
103: * {"s6", "contient {0,number} fichiers"}, // third ChoiceFormat choice
104: * {"s7", "3 mars 1996"}, // sample date
105: * {"s8", new Dimension(1,3)} // real object, not just string
106: * // END OF MATERIAL TO LOCALIZE
107: * };
108: * }
109: * </pre>
110: * </blockquote>
111: * @see ResourceBundle
112: * @see PropertyResourceBundle
113: * @since JDK1.1
114: */
115: public abstract class ListResourceBundle extends ResourceBundle {
116: /**
117: * Sole constructor. (For invocation by subclass constructors, typically
118: * implicit.)
119: */
120: public ListResourceBundle() {
121: }
122:
123: // Implements java.util.ResourceBundle.handleGetObject; inherits javadoc specification.
124: public final Object handleGetObject(String key) {
125: // lazily load the lookup hashtable.
126: if (lookup == null) {
127: loadLookup();
128: }
129: if (key == null) {
130: throw new NullPointerException();
131: }
132: return lookup.get(key); // this class ignores locales
133: }
134:
135: /**
136: * Implementation of ResourceBundle.getKeys.
137: */
138: public Enumeration getKeys() {
139: // lazily load the lookup hashtable.
140: if (lookup == null) {
141: loadLookup();
142: }
143:
144: ResourceBundle parent = this .parent;
145: return new ResourceBundleEnumeration(lookup.keySet(),
146: (parent != null) ? parent.getKeys() : null);
147: }
148:
149: /**
150: * See class description.
151: */
152: abstract protected Object[][] getContents();
153:
154: // ==================privates====================
155:
156: /**
157: * We lazily load the lookup hashtable. This function does the
158: * loading.
159: */
160: private synchronized void loadLookup() {
161: if (lookup != null)
162: return;
163:
164: Object[][] contents = getContents();
165: HashMap temp = new HashMap(contents.length);
166: for (int i = 0; i < contents.length; ++i) {
167: // key must be non-null String, value must be non-null
168: String key = (String) contents[i][0];
169: Object value = contents[i][1];
170: if (key == null || value == null) {
171: throw new NullPointerException();
172: }
173: temp.put(key, value);
174: }
175: lookup = temp;
176: }
177:
178: private Map lookup = null;
179: }
|