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