| PropertyResourceBundle.java |
1 /*
2 * %W% %E%
3 *
4 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
5 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6 */
7
8 /*
9 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
10 * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
11 *
12 * The original version of this source code and documentation
13 * is copyrighted and owned by Taligent, Inc., a wholly-owned
14 * subsidiary of IBM. These materials are provided under terms
15 * of a License Agreement between Taligent and Sun. This technology
16 * is protected by multiple US and International patents.
17 *
18 * This notice and attribution to Taligent may not be removed.
19 * Taligent is a registered trademark of Taligent, Inc.
20 */
21
22 package java.util;
23
24 import java.io.InputStream;
25 import java.io.Reader;
26 import java.io.IOException;
27 import sun.util.ResourceBundleEnumeration;
28
29 /**
30 * <code>PropertyResourceBundle</code> is a concrete subclass of
31 * <code>ResourceBundle</code> that manages resources for a locale
32 * using a set of static strings from a property file. See
33 * {@link ResourceBundle ResourceBundle} for more information about resource
34 * bundles.
35 *
36 * <p>
37 * Unlike other types of resource bundle, you don't subclass
38 * <code>PropertyResourceBundle</code>. Instead, you supply properties
39 * files containing the resource data. <code>ResourceBundle.getBundle</code>
40 * will automatically look for the appropriate properties file and create a
41 * <code>PropertyResourceBundle</code> that refers to it. See
42 * {@link ResourceBundle#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) ResourceBundle.getBundle}
43 * for a complete description of the search and instantiation strategy.
44 *
45 * <p>
46 * The following <a name="sample">example</a> shows a member of a resource
47 * bundle family with the base name "MyResources".
48 * The text defines the bundle "MyResources_de",
49 * the German member of the bundle family.
50 * This member is based on <code>PropertyResourceBundle</code>, and the text
51 * therefore is the content of the file "MyResources_de.properties"
52 * (a related <a href="ListResourceBundle.html#sample">example</a> shows
53 * how you can add bundles to this family that are implemented as subclasses
54 * of <code>ListResourceBundle</code>).
55 * The keys in this example are of the form "s1" etc. The actual
56 * keys are entirely up to your choice, so long as they are the same as
57 * the keys you use in your program to retrieve the objects from the bundle.
58 * Keys are case-sensitive.
59 * <blockquote>
60 * <pre>
61 * # MessageFormat pattern
62 * s1=Die Platte \"{1}\" enthält {0}.
63 *
64 * # location of {0} in pattern
65 * s2=1
66 *
67 * # sample disk name
68 * s3=Meine Platte
69 *
70 * # first ChoiceFormat choice
71 * s4=keine Dateien
72 *
73 * # second ChoiceFormat choice
74 * s5=eine Datei
75 *
76 * # third ChoiceFormat choice
77 * s6={0,number} Dateien
78 *
79 * # sample date
80 * s7=3. März 1996
81 * </pre>
82 * </blockquote>
83 *
84 * <p>
85 * <strong>Note:</strong> PropertyResourceBundle can be constructed either
86 * from an InputStream or a Reader, which represents a property file.
87 * Constructing a PropertyResourceBundle instance from an InputStream requires
88 * that the input stream be encoded in ISO-8859-1. In that case, characters
89 * that cannot be represented in ISO-8859-1 encoding must be represented by
90 * <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.3">Unicode Escapes</a>,
91 * whereas the other constructor which takes a Reader does not have that limitation.
92 *
93 * @see ResourceBundle
94 * @see ListResourceBundle
95 * @see Properties
96 * @since JDK1.1
97 */
98 public class PropertyResourceBundle extends ResourceBundle {
99 /**
100 * Creates a property resource bundle from an {@link java.io.InputStream
101 * InputStream}. The property file read with this constructor
102 * must be encoded in ISO-8859-1.
103 *
104 * @param stream an InputStream that represents a property file
105 * to read from.
106 * @throws IOException if an I/O error occurs
107 * @throws NullPointerException if <code>stream</code> is null
108 */
109 public PropertyResourceBundle (InputStream stream) throws IOException {
110 Properties properties = new Properties();
111 properties.load(stream);
112 lookup = new HashMap(properties);
113 }
114
115 /**
116 * Creates a property resource bundle from a {@link java.io.Reader
117 * Reader}. Unlike the constructor
118 * {@link #PropertyResourceBundle(java.io.InputStream) PropertyResourceBundle(InputStream)},
119 * there is no limitation as to the encoding of the input property file.
120 *
121 * @param reader a Reader that represents a property file to
122 * read from.
123 * @throws IOException if an I/O error occurs
124 * @throws NullPointerException if <code>reader</code> is null
125 * @since 1.6
126 */
127 public PropertyResourceBundle (Reader reader) throws IOException {
128 Properties properties = new Properties();
129 properties.load(reader);
130 lookup = new HashMap(properties);
131 }
132
133 // Implements java.util.ResourceBundle.handleGetObject; inherits javadoc specification.
134 public Object handleGetObject(String key) {
135 if (key == null) {
136 throw new NullPointerException();
137 }
138 return lookup.get(key);
139 }
140
141 /**
142 * Returns an <code>Enumeration</code> of the keys contained in
143 * this <code>ResourceBundle</code> and its parent bundles.
144 *
145 * @return an <code>Enumeration</code> of the keys contained in
146 * this <code>ResourceBundle</code> and its parent bundles.
147 * @see #keySet()
148 */
149 public Enumeration<String> getKeys() {
150 ResourceBundle parent = this.parent;
151 return new ResourceBundleEnumeration(lookup.keySet(),
152 (parent != null) ? parent.getKeys() : null);
153 }
154
155 /**
156 * Returns a <code>Set</code> of the keys contained
157 * <em>only</em> in this <code>ResourceBundle</code>.
158 *
159 * @return a <code>Set</code> of the keys contained only in this
160 * <code>ResourceBundle</code>
161 * @since 1.6
162 * @see #keySet()
163 */
164 protected Set<String> handleKeySet() {
165 return lookup.keySet();
166 }
167
168 // ==================privates====================
169
170 private Map<String,Object> lookup;
171 }
172