jtulach@1334
|
1 |
/*
|
jtulach@1334
|
2 |
* Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
|
jtulach@1334
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jtulach@1334
|
4 |
*
|
jtulach@1334
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jtulach@1334
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jtulach@1334
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jtulach@1334
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jtulach@1334
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jtulach@1334
|
10 |
*
|
jtulach@1334
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jtulach@1334
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jtulach@1334
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jtulach@1334
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jtulach@1334
|
15 |
* accompanied this code).
|
jtulach@1334
|
16 |
*
|
jtulach@1334
|
17 |
* You should have received a copy of the GNU General Public License version
|
jtulach@1334
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jtulach@1334
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jtulach@1334
|
20 |
*
|
jtulach@1334
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jtulach@1334
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jtulach@1334
|
23 |
* questions.
|
jtulach@1334
|
24 |
*/
|
jtulach@1334
|
25 |
|
jtulach@1334
|
26 |
/*
|
jtulach@1334
|
27 |
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
|
jtulach@1334
|
28 |
* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
|
jtulach@1334
|
29 |
*
|
jtulach@1334
|
30 |
* The original version of this source code and documentation
|
jtulach@1334
|
31 |
* is copyrighted and owned by Taligent, Inc., a wholly-owned
|
jtulach@1334
|
32 |
* subsidiary of IBM. These materials are provided under terms
|
jtulach@1334
|
33 |
* of a License Agreement between Taligent and Sun. This technology
|
jtulach@1334
|
34 |
* is protected by multiple US and International patents.
|
jtulach@1334
|
35 |
*
|
jtulach@1334
|
36 |
* This notice and attribution to Taligent may not be removed.
|
jtulach@1334
|
37 |
* Taligent is a registered trademark of Taligent, Inc.
|
jtulach@1334
|
38 |
*/
|
jtulach@1334
|
39 |
|
jtulach@1334
|
40 |
package java.util;
|
jtulach@1334
|
41 |
|
jtulach@1334
|
42 |
import java.io.InputStream;
|
jtulach@1334
|
43 |
import java.io.Reader;
|
jtulach@1334
|
44 |
import java.io.IOException;
|
jtulach@1334
|
45 |
|
jtulach@1334
|
46 |
/**
|
jtulach@1334
|
47 |
* <code>PropertyResourceBundle</code> is a concrete subclass of
|
jtulach@1334
|
48 |
* <code>ResourceBundle</code> that manages resources for a locale
|
jtulach@1334
|
49 |
* using a set of static strings from a property file. See
|
jtulach@1334
|
50 |
* {@link ResourceBundle ResourceBundle} for more information about resource
|
jtulach@1334
|
51 |
* bundles.
|
jtulach@1334
|
52 |
*
|
jtulach@1334
|
53 |
* <p>
|
jtulach@1334
|
54 |
* Unlike other types of resource bundle, you don't subclass
|
jtulach@1334
|
55 |
* <code>PropertyResourceBundle</code>. Instead, you supply properties
|
jtulach@1334
|
56 |
* files containing the resource data. <code>ResourceBundle.getBundle</code>
|
jtulach@1334
|
57 |
* will automatically look for the appropriate properties file and create a
|
jtulach@1334
|
58 |
* <code>PropertyResourceBundle</code> that refers to it. See
|
jtulach@1334
|
59 |
* {@link ResourceBundle#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) ResourceBundle.getBundle}
|
jtulach@1334
|
60 |
* for a complete description of the search and instantiation strategy.
|
jtulach@1334
|
61 |
*
|
jtulach@1334
|
62 |
* <p>
|
jtulach@1334
|
63 |
* The following <a name="sample">example</a> shows a member of a resource
|
jtulach@1334
|
64 |
* bundle family with the base name "MyResources".
|
jtulach@1334
|
65 |
* The text defines the bundle "MyResources_de",
|
jtulach@1334
|
66 |
* the German member of the bundle family.
|
jtulach@1334
|
67 |
* This member is based on <code>PropertyResourceBundle</code>, and the text
|
jtulach@1334
|
68 |
* therefore is the content of the file "MyResources_de.properties"
|
jtulach@1334
|
69 |
* (a related <a href="ListResourceBundle.html#sample">example</a> shows
|
jtulach@1334
|
70 |
* how you can add bundles to this family that are implemented as subclasses
|
jtulach@1334
|
71 |
* of <code>ListResourceBundle</code>).
|
jtulach@1334
|
72 |
* The keys in this example are of the form "s1" etc. The actual
|
jtulach@1334
|
73 |
* keys are entirely up to your choice, so long as they are the same as
|
jtulach@1334
|
74 |
* the keys you use in your program to retrieve the objects from the bundle.
|
jtulach@1334
|
75 |
* Keys are case-sensitive.
|
jtulach@1334
|
76 |
* <blockquote>
|
jtulach@1334
|
77 |
* <pre>
|
jtulach@1334
|
78 |
* # MessageFormat pattern
|
jtulach@1334
|
79 |
* s1=Die Platte \"{1}\" enthält {0}.
|
jtulach@1334
|
80 |
*
|
jtulach@1334
|
81 |
* # location of {0} in pattern
|
jtulach@1334
|
82 |
* s2=1
|
jtulach@1334
|
83 |
*
|
jtulach@1334
|
84 |
* # sample disk name
|
jtulach@1334
|
85 |
* s3=Meine Platte
|
jtulach@1334
|
86 |
*
|
jtulach@1334
|
87 |
* # first ChoiceFormat choice
|
jtulach@1334
|
88 |
* s4=keine Dateien
|
jtulach@1334
|
89 |
*
|
jtulach@1334
|
90 |
* # second ChoiceFormat choice
|
jtulach@1334
|
91 |
* s5=eine Datei
|
jtulach@1334
|
92 |
*
|
jtulach@1334
|
93 |
* # third ChoiceFormat choice
|
jtulach@1334
|
94 |
* s6={0,number} Dateien
|
jtulach@1334
|
95 |
*
|
jtulach@1334
|
96 |
* # sample date
|
jtulach@1334
|
97 |
* s7=3. März 1996
|
jtulach@1334
|
98 |
* </pre>
|
jtulach@1334
|
99 |
* </blockquote>
|
jtulach@1334
|
100 |
*
|
jtulach@1334
|
101 |
* <p>
|
jtulach@1334
|
102 |
* <strong>Note:</strong> PropertyResourceBundle can be constructed either
|
jtulach@1334
|
103 |
* from an InputStream or a Reader, which represents a property file.
|
jtulach@1334
|
104 |
* Constructing a PropertyResourceBundle instance from an InputStream requires
|
jtulach@1334
|
105 |
* that the input stream be encoded in ISO-8859-1. In that case, characters
|
jtulach@1334
|
106 |
* that cannot be represented in ISO-8859-1 encoding must be represented by Unicode Escapes
|
jtulach@1334
|
107 |
* as defined in section 3.3 of
|
jtulach@1334
|
108 |
* <cite>The Java™ Language Specification</cite>
|
jtulach@1334
|
109 |
* whereas the other constructor which takes a Reader does not have that limitation.
|
jtulach@1334
|
110 |
*
|
jtulach@1334
|
111 |
* @see ResourceBundle
|
jtulach@1334
|
112 |
* @see ListResourceBundle
|
jtulach@1334
|
113 |
* @see Properties
|
jtulach@1334
|
114 |
* @since JDK1.1
|
jtulach@1334
|
115 |
*/
|
jtulach@1334
|
116 |
public class PropertyResourceBundle extends ResourceBundle {
|
jtulach@1334
|
117 |
/**
|
jtulach@1334
|
118 |
* Creates a property resource bundle from an {@link java.io.InputStream
|
jtulach@1334
|
119 |
* InputStream}. The property file read with this constructor
|
jtulach@1334
|
120 |
* must be encoded in ISO-8859-1.
|
jtulach@1334
|
121 |
*
|
jtulach@1334
|
122 |
* @param stream an InputStream that represents a property file
|
jtulach@1334
|
123 |
* to read from.
|
jtulach@1334
|
124 |
* @throws IOException if an I/O error occurs
|
jtulach@1334
|
125 |
* @throws NullPointerException if <code>stream</code> is null
|
jtulach@1334
|
126 |
*/
|
jtulach@1334
|
127 |
public PropertyResourceBundle (InputStream stream) throws IOException {
|
jtulach@1334
|
128 |
Properties properties = new Properties();
|
jtulach@1334
|
129 |
properties.load(stream);
|
jtulach@1334
|
130 |
lookup = new HashMap(properties);
|
jtulach@1334
|
131 |
}
|
jtulach@1334
|
132 |
|
jtulach@1334
|
133 |
/**
|
jtulach@1334
|
134 |
* Creates a property resource bundle from a {@link java.io.Reader
|
jtulach@1334
|
135 |
* Reader}. Unlike the constructor
|
jtulach@1334
|
136 |
* {@link #PropertyResourceBundle(java.io.InputStream) PropertyResourceBundle(InputStream)},
|
jtulach@1334
|
137 |
* there is no limitation as to the encoding of the input property file.
|
jtulach@1334
|
138 |
*
|
jtulach@1334
|
139 |
* @param reader a Reader that represents a property file to
|
jtulach@1334
|
140 |
* read from.
|
jtulach@1334
|
141 |
* @throws IOException if an I/O error occurs
|
jtulach@1334
|
142 |
* @throws NullPointerException if <code>reader</code> is null
|
jtulach@1334
|
143 |
* @since 1.6
|
jtulach@1334
|
144 |
*/
|
jtulach@1334
|
145 |
public PropertyResourceBundle (Reader reader) throws IOException {
|
jtulach@1334
|
146 |
Properties properties = new Properties();
|
jtulach@1334
|
147 |
properties.load(reader);
|
jtulach@1334
|
148 |
lookup = new HashMap(properties);
|
jtulach@1334
|
149 |
}
|
jtulach@1334
|
150 |
|
jtulach@1334
|
151 |
// Implements java.util.ResourceBundle.handleGetObject; inherits javadoc specification.
|
jtulach@1334
|
152 |
public Object handleGetObject(String key) {
|
jtulach@1334
|
153 |
if (key == null) {
|
jtulach@1334
|
154 |
throw new NullPointerException();
|
jtulach@1334
|
155 |
}
|
jtulach@1334
|
156 |
return lookup.get(key);
|
jtulach@1334
|
157 |
}
|
jtulach@1334
|
158 |
|
jtulach@1334
|
159 |
/**
|
jtulach@1334
|
160 |
* Returns an <code>Enumeration</code> of the keys contained in
|
jtulach@1334
|
161 |
* this <code>ResourceBundle</code> and its parent bundles.
|
jtulach@1334
|
162 |
*
|
jtulach@1334
|
163 |
* @return an <code>Enumeration</code> of the keys contained in
|
jtulach@1334
|
164 |
* this <code>ResourceBundle</code> and its parent bundles.
|
jtulach@1334
|
165 |
* @see #keySet()
|
jtulach@1334
|
166 |
*/
|
jtulach@1334
|
167 |
public Enumeration<String> getKeys() {
|
jtulach@1334
|
168 |
ResourceBundle parent = this.parent;
|
jtulach@1334
|
169 |
return new ResourceBundleEnumeration(lookup.keySet(),
|
jtulach@1334
|
170 |
(parent != null) ? parent.getKeys() : null);
|
jtulach@1334
|
171 |
}
|
jtulach@1334
|
172 |
|
jtulach@1334
|
173 |
/**
|
jtulach@1334
|
174 |
* Returns a <code>Set</code> of the keys contained
|
jtulach@1334
|
175 |
* <em>only</em> in this <code>ResourceBundle</code>.
|
jtulach@1334
|
176 |
*
|
jtulach@1334
|
177 |
* @return a <code>Set</code> of the keys contained only in this
|
jtulach@1334
|
178 |
* <code>ResourceBundle</code>
|
jtulach@1334
|
179 |
* @since 1.6
|
jtulach@1334
|
180 |
* @see #keySet()
|
jtulach@1334
|
181 |
*/
|
jtulach@1334
|
182 |
protected Set<String> handleKeySet() {
|
jtulach@1334
|
183 |
return lookup.keySet();
|
jtulach@1334
|
184 |
}
|
jtulach@1334
|
185 |
|
jtulach@1334
|
186 |
// ==================privates====================
|
jtulach@1334
|
187 |
|
jtulach@1334
|
188 |
private Map<String,Object> lookup;
|
jaroslav@1337
|
189 |
|
jaroslav@1337
|
190 |
|
jaroslav@1337
|
191 |
/**
|
jaroslav@1337
|
192 |
* Implements an Enumeration that combines elements from a Set and
|
jaroslav@1337
|
193 |
* an Enumeration. Used by ListResourceBundle and PropertyResourceBundle.
|
jaroslav@1337
|
194 |
*/
|
jaroslav@1337
|
195 |
static class ResourceBundleEnumeration implements Enumeration<String> {
|
jaroslav@1337
|
196 |
|
jaroslav@1337
|
197 |
Set<String> set;
|
jaroslav@1337
|
198 |
Iterator<String> iterator;
|
jaroslav@1337
|
199 |
Enumeration<String> enumeration; // may remain null
|
jaroslav@1337
|
200 |
|
jaroslav@1337
|
201 |
/**
|
jaroslav@1337
|
202 |
* Constructs a resource bundle enumeration.
|
jaroslav@1337
|
203 |
* @param set an set providing some elements of the enumeration
|
jaroslav@1337
|
204 |
* @param enumeration an enumeration providing more elements of the enumeration.
|
jaroslav@1337
|
205 |
* enumeration may be null.
|
jaroslav@1337
|
206 |
*/
|
jaroslav@1337
|
207 |
public ResourceBundleEnumeration(Set<String> set, Enumeration<String> enumeration) {
|
jaroslav@1337
|
208 |
this.set = set;
|
jaroslav@1337
|
209 |
this.iterator = set.iterator();
|
jaroslav@1337
|
210 |
this.enumeration = enumeration;
|
jaroslav@1337
|
211 |
}
|
jaroslav@1337
|
212 |
|
jaroslav@1337
|
213 |
String next = null;
|
jaroslav@1337
|
214 |
|
jaroslav@1337
|
215 |
public boolean hasMoreElements() {
|
jaroslav@1337
|
216 |
if (next == null) {
|
jaroslav@1337
|
217 |
if (iterator.hasNext()) {
|
jaroslav@1337
|
218 |
next = iterator.next();
|
jaroslav@1337
|
219 |
} else if (enumeration != null) {
|
jaroslav@1337
|
220 |
while (next == null && enumeration.hasMoreElements()) {
|
jaroslav@1337
|
221 |
next = enumeration.nextElement();
|
jaroslav@1337
|
222 |
if (set.contains(next)) {
|
jaroslav@1337
|
223 |
next = null;
|
jaroslav@1337
|
224 |
}
|
jaroslav@1337
|
225 |
}
|
jaroslav@1337
|
226 |
}
|
jaroslav@1337
|
227 |
}
|
jaroslav@1337
|
228 |
return next != null;
|
jaroslav@1337
|
229 |
}
|
jaroslav@1337
|
230 |
|
jaroslav@1337
|
231 |
public String nextElement() {
|
jaroslav@1337
|
232 |
if (hasMoreElements()) {
|
jaroslav@1337
|
233 |
String result = next;
|
jaroslav@1337
|
234 |
next = null;
|
jaroslav@1337
|
235 |
return result;
|
jaroslav@1337
|
236 |
} else {
|
jaroslav@1337
|
237 |
throw new NoSuchElementException();
|
jaroslav@1337
|
238 |
}
|
jaroslav@1337
|
239 |
}
|
jaroslav@1337
|
240 |
}
|
jaroslav@1337
|
241 |
|
jtulach@1334
|
242 |
}
|