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