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