001: /*
002: * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: /*
027: * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
028: * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
029: *
030: * The original version of this source code and documentation
031: * is copyrighted and owned by Taligent, Inc., a wholly-owned
032: * subsidiary of IBM. These materials are provided under terms
033: * of a License Agreement between Taligent and Sun. This technology
034: * is protected by multiple US and International patents.
035: *
036: * This notice and attribution to Taligent may not be removed.
037: * Taligent is a registered trademark of Taligent, Inc.
038: */
039:
040: package java.util;
041:
042: import java.io.InputStream;
043: import java.io.Reader;
044: import java.io.IOException;
045: import sun.util.ResourceBundleEnumeration;
046:
047: /**
048: * <code>PropertyResourceBundle</code> is a concrete subclass of
049: * <code>ResourceBundle</code> that manages resources for a locale
050: * using a set of static strings from a property file. See
051: * {@link ResourceBundle ResourceBundle} for more information about resource
052: * bundles.
053: *
054: * <p>
055: * Unlike other types of resource bundle, you don't subclass
056: * <code>PropertyResourceBundle</code>. Instead, you supply properties
057: * files containing the resource data. <code>ResourceBundle.getBundle</code>
058: * will automatically look for the appropriate properties file and create a
059: * <code>PropertyResourceBundle</code> that refers to it. See
060: * {@link ResourceBundle#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) ResourceBundle.getBundle}
061: * for a complete description of the search and instantiation strategy.
062: *
063: * <p>
064: * The following <a name="sample">example</a> shows a member of a resource
065: * bundle family with the base name "MyResources".
066: * The text defines the bundle "MyResources_de",
067: * the German member of the bundle family.
068: * This member is based on <code>PropertyResourceBundle</code>, and the text
069: * therefore is the content of the file "MyResources_de.properties"
070: * (a related <a href="ListResourceBundle.html#sample">example</a> shows
071: * how you can add bundles to this family that are implemented as subclasses
072: * of <code>ListResourceBundle</code>).
073: * The keys in this example are of the form "s1" etc. The actual
074: * keys are entirely up to your choice, so long as they are the same as
075: * the keys you use in your program to retrieve the objects from the bundle.
076: * Keys are case-sensitive.
077: * <blockquote>
078: * <pre>
079: * # MessageFormat pattern
080: * s1=Die Platte \"{1}\" enthält {0}.
081: *
082: * # location of {0} in pattern
083: * s2=1
084: *
085: * # sample disk name
086: * s3=Meine Platte
087: *
088: * # first ChoiceFormat choice
089: * s4=keine Dateien
090: *
091: * # second ChoiceFormat choice
092: * s5=eine Datei
093: *
094: * # third ChoiceFormat choice
095: * s6={0,number} Dateien
096: *
097: * # sample date
098: * s7=3. März 1996
099: * </pre>
100: * </blockquote>
101: *
102: * <p>
103: * <strong>Note:</strong> PropertyResourceBundle can be constructed either
104: * from an InputStream or a Reader, which represents a property file.
105: * Constructing a PropertyResourceBundle instance from an InputStream requires
106: * that the input stream be encoded in ISO-8859-1. In that case, characters
107: * that cannot be represented in ISO-8859-1 encoding must be represented by
108: * <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.3">Unicode Escapes</a>,
109: * whereas the other constructor which takes a Reader does not have that limitation.
110: *
111: * @see ResourceBundle
112: * @see ListResourceBundle
113: * @see Properties
114: * @since JDK1.1
115: */
116: public class PropertyResourceBundle extends ResourceBundle {
117: /**
118: * Creates a property resource bundle from an {@link java.io.InputStream
119: * InputStream}. The property file read with this constructor
120: * must be encoded in ISO-8859-1.
121: *
122: * @param stream an InputStream that represents a property file
123: * to read from.
124: * @throws IOException if an I/O error occurs
125: * @throws NullPointerException if <code>stream</code> is null
126: */
127: public PropertyResourceBundle(InputStream stream)
128: throws IOException {
129: Properties properties = new Properties();
130: properties.load(stream);
131: lookup = new HashMap(properties);
132: }
133:
134: /**
135: * Creates a property resource bundle from a {@link java.io.Reader
136: * Reader}. Unlike the constructor
137: * {@link #PropertyResourceBundle(java.io.InputStream) PropertyResourceBundle(InputStream)},
138: * there is no limitation as to the encoding of the input property file.
139: *
140: * @param reader a Reader that represents a property file to
141: * read from.
142: * @throws IOException if an I/O error occurs
143: * @throws NullPointerException if <code>reader</code> is null
144: * @since 1.6
145: */
146: public PropertyResourceBundle(Reader reader) throws IOException {
147: Properties properties = new Properties();
148: properties.load(reader);
149: lookup = new HashMap(properties);
150: }
151:
152: // Implements java.util.ResourceBundle.handleGetObject; inherits javadoc specification.
153: public Object handleGetObject(String key) {
154: if (key == null) {
155: throw new NullPointerException();
156: }
157: return lookup.get(key);
158: }
159:
160: /**
161: * Returns an <code>Enumeration</code> of the keys contained in
162: * this <code>ResourceBundle</code> and its parent bundles.
163: *
164: * @return an <code>Enumeration</code> of the keys contained in
165: * this <code>ResourceBundle</code> and its parent bundles.
166: * @see #keySet()
167: */
168: public Enumeration<String> getKeys() {
169: ResourceBundle parent = this .parent;
170: return new ResourceBundleEnumeration(lookup.keySet(),
171: (parent != null) ? parent.getKeys() : null);
172: }
173:
174: /**
175: * Returns a <code>Set</code> of the keys contained
176: * <em>only</em> in this <code>ResourceBundle</code>.
177: *
178: * @return a <code>Set</code> of the keys contained only in this
179: * <code>ResourceBundle</code>
180: * @since 1.6
181: * @see #keySet()
182: */
183: protected Set<String> handleKeySet() {
184: return lookup.keySet();
185: }
186:
187: // ==================privates====================
188:
189: private Map<String, Object> lookup;
190: }
|