rt/emul/compact/src/main/java/java/util/PropertyResourceBundle.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 03 Oct 2013 17:36:44 +0200
changeset 1337 c794024954b5
parent 1334 588d5bf7a560
permissions -rw-r--r--
Implementation of few more JDK classes
     1 /*
     2  * Copyright (c) 1996, 2006, 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 
    46 /**
    47  * <code>PropertyResourceBundle</code> is a concrete subclass of
    48  * <code>ResourceBundle</code> that manages resources for a locale
    49  * using a set of static strings from a property file. See
    50  * {@link ResourceBundle ResourceBundle} for more information about resource
    51  * bundles.
    52  *
    53  * <p>
    54  * Unlike other types of resource bundle, you don't subclass
    55  * <code>PropertyResourceBundle</code>.  Instead, you supply properties
    56  * files containing the resource data.  <code>ResourceBundle.getBundle</code>
    57  * will automatically look for the appropriate properties file and create a
    58  * <code>PropertyResourceBundle</code> that refers to it. See
    59  * {@link ResourceBundle#getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader) ResourceBundle.getBundle}
    60  * for a complete description of the search and instantiation strategy.
    61  *
    62  * <p>
    63  * The following <a name="sample">example</a> shows a member of a resource
    64  * bundle family with the base name "MyResources".
    65  * The text defines the bundle "MyResources_de",
    66  * the German member of the bundle family.
    67  * This member is based on <code>PropertyResourceBundle</code>, and the text
    68  * therefore is the content of the file "MyResources_de.properties"
    69  * (a related <a href="ListResourceBundle.html#sample">example</a> shows
    70  * how you can add bundles to this family that are implemented as subclasses
    71  * of <code>ListResourceBundle</code>).
    72  * The keys in this example are of the form "s1" etc. The actual
    73  * keys are entirely up to your choice, so long as they are the same as
    74  * the keys you use in your program to retrieve the objects from the bundle.
    75  * Keys are case-sensitive.
    76  * <blockquote>
    77  * <pre>
    78  * # MessageFormat pattern
    79  * s1=Die Platte \"{1}\" enth&auml;lt {0}.
    80  *
    81  * # location of {0} in pattern
    82  * s2=1
    83  *
    84  * # sample disk name
    85  * s3=Meine Platte
    86  *
    87  * # first ChoiceFormat choice
    88  * s4=keine Dateien
    89  *
    90  * # second ChoiceFormat choice
    91  * s5=eine Datei
    92  *
    93  * # third ChoiceFormat choice
    94  * s6={0,number} Dateien
    95  *
    96  * # sample date
    97  * s7=3. M&auml;rz 1996
    98  * </pre>
    99  * </blockquote>
   100  *
   101  * <p>
   102  * <strong>Note:</strong> PropertyResourceBundle can be constructed either
   103  * from an InputStream or a Reader, which represents a property file.
   104  * Constructing a PropertyResourceBundle instance from an InputStream requires
   105  * that the input stream be encoded in ISO-8859-1.  In that case, characters
   106  * that cannot be represented in ISO-8859-1 encoding must be represented by Unicode Escapes
   107  * as defined in section 3.3 of
   108  * <cite>The Java&trade; Language Specification</cite>
   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) throws IOException {
   128         Properties properties = new Properties();
   129         properties.load(stream);
   130         lookup = new HashMap(properties);
   131     }
   132 
   133     /**
   134      * Creates a property resource bundle from a {@link java.io.Reader
   135      * Reader}.  Unlike the constructor
   136      * {@link #PropertyResourceBundle(java.io.InputStream) PropertyResourceBundle(InputStream)},
   137      * there is no limitation as to the encoding of the input property file.
   138      *
   139      * @param reader a Reader that represents a property file to
   140      *        read from.
   141      * @throws IOException if an I/O error occurs
   142      * @throws NullPointerException if <code>reader</code> is null
   143      * @since 1.6
   144      */
   145     public PropertyResourceBundle (Reader reader) throws IOException {
   146         Properties properties = new Properties();
   147         properties.load(reader);
   148         lookup = new HashMap(properties);
   149     }
   150 
   151     // Implements java.util.ResourceBundle.handleGetObject; inherits javadoc specification.
   152     public Object handleGetObject(String key) {
   153         if (key == null) {
   154             throw new NullPointerException();
   155         }
   156         return lookup.get(key);
   157     }
   158 
   159     /**
   160      * Returns an <code>Enumeration</code> of the keys contained in
   161      * this <code>ResourceBundle</code> and its parent bundles.
   162      *
   163      * @return an <code>Enumeration</code> of the keys contained in
   164      *         this <code>ResourceBundle</code> and its parent bundles.
   165      * @see #keySet()
   166      */
   167     public Enumeration<String> getKeys() {
   168         ResourceBundle parent = this.parent;
   169         return new ResourceBundleEnumeration(lookup.keySet(),
   170                 (parent != null) ? parent.getKeys() : null);
   171     }
   172 
   173     /**
   174      * Returns a <code>Set</code> of the keys contained
   175      * <em>only</em> in this <code>ResourceBundle</code>.
   176      *
   177      * @return a <code>Set</code> of the keys contained only in this
   178      *         <code>ResourceBundle</code>
   179      * @since 1.6
   180      * @see #keySet()
   181      */
   182     protected Set<String> handleKeySet() {
   183         return lookup.keySet();
   184     }
   185 
   186     // ==================privates====================
   187 
   188     private Map<String,Object> lookup;
   189     
   190 
   191     /**
   192      * Implements an Enumeration that combines elements from a Set and
   193      * an Enumeration. Used by ListResourceBundle and PropertyResourceBundle.
   194      */
   195     static class ResourceBundleEnumeration implements Enumeration<String> {
   196 
   197         Set<String> set;
   198         Iterator<String> iterator;
   199         Enumeration<String> enumeration; // may remain null
   200 
   201         /**
   202          * Constructs a resource bundle enumeration.
   203          * @param set an set providing some elements of the enumeration
   204          * @param enumeration an enumeration providing more elements of the enumeration.
   205          *        enumeration may be null.
   206          */
   207         public ResourceBundleEnumeration(Set<String> set, Enumeration<String> enumeration) {
   208             this.set = set;
   209             this.iterator = set.iterator();
   210             this.enumeration = enumeration;
   211         }
   212 
   213         String next = null;
   214 
   215         public boolean hasMoreElements() {
   216             if (next == null) {
   217                 if (iterator.hasNext()) {
   218                     next = iterator.next();
   219                 } else if (enumeration != null) {
   220                     while (next == null && enumeration.hasMoreElements()) {
   221                         next = enumeration.nextElement();
   222                         if (set.contains(next)) {
   223                             next = null;
   224                         }
   225                     }
   226                 }
   227             }
   228             return next != null;
   229         }
   230 
   231         public String nextElement() {
   232             if (hasMoreElements()) {
   233                 String result = next;
   234                 next = null;
   235                 return result;
   236             } else {
   237                 throw new NoSuchElementException();
   238             }
   239         }
   240     }
   241     
   242 }