KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > PropertyResourceBundle


1 /*
2  * @(#)PropertyResourceBundle.java 1.27 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN 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 JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 /**
28  * <code>PropertyResourceBundle</code> is a concrete subclass of
29  * <code>ResourceBundle</code> that manages resources for a locale
30  * using a set of static strings from a property file. See
31  * {@link ResourceBundle ResourceBundle} for more information about resource
32  * bundles. See {@link Properties Properties} for more information
33  * about properties files, in particular the
34  * <a HREF="Properties.html#encoding">information on character encodings</a>.
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&auml;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&auml;rz 1996
81  * </pre>
82  * </blockquote>
83  *
84  * @see ResourceBundle
85  * @see ListResourceBundle
86  * @see Properties
87  * @since JDK1.1
88  */

89 public class PropertyResourceBundle extends ResourceBundle JavaDoc {
90     /**
91      * Creates a property resource bundle.
92      * @param stream property file to read from.
93      */

94     public PropertyResourceBundle (InputStream JavaDoc stream) throws IOException JavaDoc {
95         Properties JavaDoc properties = new Properties JavaDoc();
96         properties.load(stream);
97         lookup = new HashMap JavaDoc(properties);
98     }
99
100     // Implements java.util.ResourceBundle.handleGetObject; inherits javadoc specification.
101
public Object JavaDoc handleGetObject(String JavaDoc key) {
102         if (key == null) {
103             throw new NullPointerException JavaDoc();
104         }
105         return lookup.get(key);
106     }
107
108     /**
109      * Implementation of ResourceBundle.getKeys.
110      */

111     public Enumeration JavaDoc<String JavaDoc> getKeys() {
112         ResourceBundle JavaDoc parent = this.parent;
113         return new ResourceBundleEnumeration JavaDoc(lookup.keySet(),
114                 (parent != null) ? parent.getKeys() : null);
115     }
116
117     // ==================privates====================
118

119     private Map JavaDoc lookup;
120 }
121
Popular Tags