1 /*
   2  * Copyright (c) 1996, 2016, 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.InputStreamReader;
  44 import java.io.Reader;
  45 import java.io.IOException;
  46 import java.nio.charset.MalformedInputException;
  47 import java.nio.charset.StandardCharsets;
  48 import java.nio.charset.UnmappableCharacterException;
  49 import sun.security.action.GetPropertyAction;
  50 import sun.util.PropertyResourceBundleCharset;
  51 import sun.util.ResourceBundleEnumeration;
  52 
  53 /**
  54  * <code>PropertyResourceBundle</code> is a concrete subclass of
  55  * <code>ResourceBundle</code> that manages resources for a locale
  56  * using a set of static strings from a property file. See
  57  * {@link ResourceBundle ResourceBundle} for more information about resource
  58  * bundles.
  59  *
  60  * <p>
  61  * Unlike other types of resource bundle, you don't subclass
  62  * <code>PropertyResourceBundle</code>.  Instead, you supply properties
  63  * files containing the resource data.  <code>ResourceBundle.getBundle</code>
  64  * will automatically look for the appropriate properties file and create a
  65  * <code>PropertyResourceBundle</code> that refers to it. See
  66  * {@link ResourceBundle#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) ResourceBundle.getBundle}
  67  * for a complete description of the search and instantiation strategy.
  68  *
  69  * <p>
  70  * The following <a name="sample">example</a> shows a member of a resource
  71  * bundle family with the base name "MyResources".
  72  * The text defines the bundle "MyResources_de",
  73  * the German member of the bundle family.
  74  * This member is based on <code>PropertyResourceBundle</code>, and the text
  75  * therefore is the content of the file "MyResources_de.properties"
  76  * (a related <a href="ListResourceBundle.html#sample">example</a> shows
  77  * how you can add bundles to this family that are implemented as subclasses
  78  * of <code>ListResourceBundle</code>).
  79  * The keys in this example are of the form "s1" etc. The actual
  80  * keys are entirely up to your choice, so long as they are the same as
  81  * the keys you use in your program to retrieve the objects from the bundle.
  82  * Keys are case-sensitive.
  83  * <blockquote>
  84  * <pre>
  85  * # MessageFormat pattern
  86  * s1=Die Platte \"{1}\" enth&auml;lt {0}.
  87  *
  88  * # location of {0} in pattern
  89  * s2=1
  90  *
  91  * # sample disk name
  92  * s3=Meine Platte
  93  *
  94  * # first ChoiceFormat choice
  95  * s4=keine Dateien
  96  *
  97  * # second ChoiceFormat choice
  98  * s5=eine Datei
  99  *
 100  * # third ChoiceFormat choice
 101  * s6={0,number} Dateien
 102  *
 103  * # sample date
 104  * s7=3. M&auml;rz 1996
 105  * </pre>
 106  * </blockquote>
 107  *
 108  * <p>
 109  * The implementation of a {@code PropertyResourceBundle} subclass must be
 110  * thread-safe if it's simultaneously used by multiple threads. The default
 111  * implementations of the non-abstract methods in this class are thread-safe.
 112  *
 113  * <p>
 114  * <strong>Note:</strong> PropertyResourceBundle can be constructed either
 115  * from an InputStream or a Reader, which represents a property file.
 116  * Constructing a PropertyResourceBundle instance from an InputStream requires
 117  * that the input stream be encoded in UTF-8. By default, if a
 118  * {@link java.nio.charset.MalformedInputException} or an
 119  * {@link java.nio.charset.UnmappableCharacterException} occurs on reading the
 120  * input stream, then the PropertyResourceBundle instance resets to the state
 121  * before the exception, re-reads the input stream in {@code ISO-8859-1}, and
 122  * continues reading. If the system property
 123  * {@code java.util.PropertyResourceBundle.encoding} is set to either
 124  * "ISO-8859-1" or "UTF-8", the input stream is solely read in that encoding,
 125  * and throws the exception if it encounters an invalid sequence.
 126  * If "ISO-8859-1" is specified, characters that cannot be represented in
 127  * ISO-8859-1 encoding must be represented by Unicode Escapes as defined in section
 128  * 3.3 of <cite>The Java&trade; Language Specification</cite>
 129  * whereas the other constructor which takes a Reader does not have that limitation.
 130  * Other encoding values are ignored for this system property.
 131  *
 132  * @see ResourceBundle
 133  * @see ListResourceBundle
 134  * @see Properties
 135  * @since 1.1
 136  */
 137 public class PropertyResourceBundle extends ResourceBundle {
 138 
 139     // Check whether the strict encoding is specified.
 140     // The possible encoding is either "ISO-8859-1" or "UTF-8".
 141     private static final String encoding = GetPropertyAction
 142         .privilegedGetProperty("java.util.PropertyResourceBundle.encoding", "")
 143         .toUpperCase(Locale.ROOT);
 144 
 145     /**
 146      * Creates a property resource bundle from an {@link java.io.InputStream
 147     * InputStream}. This constructor reads the property file in UTF-8 by default.
 148     * If a {@link java.nio.charset.MalformedInputException} or an
 149     * {@link java.nio.charset.UnmappableCharacterException} occurs on reading the
 150     * input stream, then the PropertyResourceBundle instance resets to the state
 151     * before the exception, re-reads the input stream in {@code ISO-8859-1} and
 152     * continues reading. If the system property
 153     * {@code java.util.PropertyResourceBundle.encoding} is set to either
 154     * "ISO-8859-1" or "UTF-8", the input stream is solely read in that encoding,
 155     * and throws the exception if it encounters an invalid sequence. Other
 156     * encoding values are ignored for this system property.
 157      *
 158      * @param stream an InputStream that represents a property file
 159      *        to read from.
 160      * @throws IOException if an I/O error occurs
 161      * @throws NullPointerException if <code>stream</code> is null
 162      * @throws IllegalArgumentException if {@code stream} contains a
 163      *     malformed Unicode escape sequence.
 164      * @throws MalformedInputException if the system property
 165      *     {@code java.util.PropertyResourceBundle.encoding} is set to "UTF-8"
 166      *     and {@code stream} contains an invalid UTF-8 byte sequence.
 167      * @throws UnmappableCharacterException if the system property
 168      *     {@code java.util.PropertyResourceBundle.encoding} is set to "UTF-8"
 169      *     and {@code stream} contains an unmappable UTF-8 byte sequence.
 170      */
 171     @SuppressWarnings({"unchecked", "rawtypes"})
 172     public PropertyResourceBundle (InputStream stream) throws IOException {
 173         this(new InputStreamReader(stream,
 174             "ISO-8859-1".equals(encoding) ?
 175                 StandardCharsets.ISO_8859_1.newDecoder() :
 176                 new PropertyResourceBundleCharset("UTF-8".equals(encoding)).newDecoder()));
 177     }
 178 
 179     /**
 180      * Creates a property resource bundle from a {@link java.io.Reader
 181      * Reader}.  Unlike the constructor
 182      * {@link #PropertyResourceBundle(java.io.InputStream) PropertyResourceBundle(InputStream)},
 183      * there is no limitation as to the encoding of the input property file.
 184      *
 185      * @param reader a Reader that represents a property file to
 186      *        read from.
 187      * @throws IOException if an I/O error occurs
 188      * @throws NullPointerException if <code>reader</code> is null
 189      * @throws IllegalArgumentException if a malformed Unicode escape sequence appears
 190      *     from {@code reader}.
 191      * @since 1.6
 192      */
 193     @SuppressWarnings({"unchecked", "rawtypes"})
 194     public PropertyResourceBundle (Reader reader) throws IOException {
 195         Properties properties = new Properties();
 196         properties.load(reader);
 197         lookup = new HashMap(properties);
 198     }
 199 
 200     // Implements java.util.ResourceBundle.handleGetObject; inherits javadoc specification.
 201     public Object handleGetObject(String key) {
 202         if (key == null) {
 203             throw new NullPointerException();
 204         }
 205         return lookup.get(key);
 206     }
 207 
 208     /**
 209      * Returns an <code>Enumeration</code> of the keys contained in
 210      * this <code>ResourceBundle</code> and its parent bundles.
 211      *
 212      * @return an <code>Enumeration</code> of the keys contained in
 213      *         this <code>ResourceBundle</code> and its parent bundles.
 214      * @see #keySet()
 215      */
 216     public Enumeration<String> getKeys() {
 217         ResourceBundle parent = this.parent;
 218         return new ResourceBundleEnumeration(lookup.keySet(),
 219                 (parent != null) ? parent.getKeys() : null);
 220     }
 221 
 222     /**
 223      * Returns a <code>Set</code> of the keys contained
 224      * <em>only</em> in this <code>ResourceBundle</code>.
 225      *
 226      * @return a <code>Set</code> of the keys contained only in this
 227      *         <code>ResourceBundle</code>
 228      * @since 1.6
 229      * @see #keySet()
 230      */
 231     protected Set<String> handleKeySet() {
 232         return lookup.keySet();
 233     }
 234 
 235     // ==================privates====================
 236 
 237     private final Map<String,Object> lookup;
 238 }