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 package java.util; 27 28 import java.io.IOException; 29 import java.io.PrintStream; 30 import java.io.PrintWriter; 31 import java.io.InputStream; 32 import java.io.OutputStream; 33 import java.io.Reader; 34 import java.io.Writer; 35 import java.io.OutputStreamWriter; 36 import java.io.BufferedWriter; 37 import java.io.ObjectInputStream; 38 import java.io.ObjectOutputStream; 39 import java.io.StreamCorruptedException; 40 import java.util.concurrent.ConcurrentHashMap; 41 import java.util.function.BiConsumer; 42 import java.util.function.BiFunction; 43 import java.util.function.Function; 44 45 import jdk.internal.misc.SharedSecrets; 46 import jdk.internal.util.xml.PropertiesDefaultHandler; 47 48 /** 49 * The {@code Properties} class represents a persistent set of 50 * properties. The {@code Properties} can be saved to a stream 51 * or loaded from a stream. Each key and its corresponding value in 52 * the property list is a string. 53 * <p> 54 * A property list can contain another property list as its 55 * "defaults"; this second property list is searched if 56 * the property key is not found in the original property list. 57 * <p> 58 * Because {@code Properties} inherits from {@code Hashtable}, the 59 * {@code put} and {@code putAll} methods can be applied to a 980 } 981 982 /** 983 * Emits an XML document representing all of the properties contained 984 * in this table, using the specified encoding. 985 * 986 * <p>The XML document will have the following DOCTYPE declaration: 987 * <pre> 988 * <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> 989 * </pre> 990 * 991 * <p>If the specified comment is {@code null} then no comment 992 * will be stored in the document. 993 * 994 * <p> An implementation is required to support writing of XML documents 995 * that use the "{@code UTF-8}" or "{@code UTF-16}" encoding. An 996 * implementation may support additional encodings. 997 * 998 * <p>The specified stream remains open after this method returns. 999 * 1000 * @param os the output stream on which to emit the XML document. 1001 * @param comment a description of the property list, or {@code null} 1002 * if no comment is desired. 1003 * @param encoding the name of a supported 1004 * <a href="../lang/package-summary.html#charenc"> 1005 * character encoding</a> 1006 * 1007 * @throws IOException if writing to the specified output stream 1008 * results in an {@code IOException}. 1009 * @throws java.io.UnsupportedEncodingException if the encoding is not 1010 * supported by the implementation. 1011 * @throws NullPointerException if {@code os} is {@code null}, 1012 * or if {@code encoding} is {@code null}. 1013 * @throws ClassCastException if this {@code Properties} object 1014 * contains any keys or values that are not 1015 * {@code Strings}. 1016 * @see #loadFromXML(InputStream) 1017 * @see <a href="http://www.w3.org/TR/REC-xml/#charencoding">Character 1018 * Encoding in Entities</a> 1019 * @since 1.5 1020 */ 1021 public void storeToXML(OutputStream os, String comment, String encoding) 1022 throws IOException 1023 { 1024 Objects.requireNonNull(os); 1025 Objects.requireNonNull(encoding); 1026 PropertiesDefaultHandler handler = new PropertiesDefaultHandler(); 1027 handler.store(this, os, comment, encoding); 1028 } 1029 1030 /** 1031 * Searches for the property with the specified key in this property list. 1032 * If the key is not found in this property list, the default property list, 1033 * and its defaults, recursively, are then checked. The method returns 1034 * {@code null} if the property is not found. 1035 * 1036 * @param key the property key. 1037 * @return the value in this property list with the specified key value. 1038 * @see #setProperty 1039 * @see #defaults 1040 */ 1041 public String getProperty(String key) { 1042 Object oval = map.get(key); 1043 String sval = (oval instanceof String) ? (String)oval : null; 1044 return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval; 1045 } 1046 1047 /** | 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 package java.util; 27 28 import java.io.IOException; 29 import java.io.PrintStream; 30 import java.io.PrintWriter; 31 import java.io.InputStream; 32 import java.io.OutputStream; 33 import java.io.Reader; 34 import java.io.Writer; 35 import java.io.OutputStreamWriter; 36 import java.io.BufferedWriter; 37 import java.io.ObjectInputStream; 38 import java.io.ObjectOutputStream; 39 import java.io.StreamCorruptedException; 40 import java.io.UnsupportedEncodingException; 41 import java.nio.charset.Charset; 42 import java.nio.charset.IllegalCharsetNameException; 43 import java.nio.charset.UnsupportedCharsetException; 44 import java.util.concurrent.ConcurrentHashMap; 45 import java.util.function.BiConsumer; 46 import java.util.function.BiFunction; 47 import java.util.function.Function; 48 49 import jdk.internal.misc.SharedSecrets; 50 import jdk.internal.util.xml.PropertiesDefaultHandler; 51 52 /** 53 * The {@code Properties} class represents a persistent set of 54 * properties. The {@code Properties} can be saved to a stream 55 * or loaded from a stream. Each key and its corresponding value in 56 * the property list is a string. 57 * <p> 58 * A property list can contain another property list as its 59 * "defaults"; this second property list is searched if 60 * the property key is not found in the original property list. 61 * <p> 62 * Because {@code Properties} inherits from {@code Hashtable}, the 63 * {@code put} and {@code putAll} methods can be applied to a 984 } 985 986 /** 987 * Emits an XML document representing all of the properties contained 988 * in this table, using the specified encoding. 989 * 990 * <p>The XML document will have the following DOCTYPE declaration: 991 * <pre> 992 * <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> 993 * </pre> 994 * 995 * <p>If the specified comment is {@code null} then no comment 996 * will be stored in the document. 997 * 998 * <p> An implementation is required to support writing of XML documents 999 * that use the "{@code UTF-8}" or "{@code UTF-16}" encoding. An 1000 * implementation may support additional encodings. 1001 * 1002 * <p>The specified stream remains open after this method returns. 1003 * 1004 * <p>This method behaves the same as 1005 * {@linkplain #storeToXML(OutputStream os, String comment, Charset charset)} 1006 * except that it will {@linkplain java.nio.charset.Charset#forName look up the charset} 1007 * using the given encoding name. 1008 * 1009 * @param os the output stream on which to emit the XML document. 1010 * @param comment a description of the property list, or {@code null} 1011 * if no comment is desired. 1012 * @param encoding the name of a supported 1013 * <a href="../lang/package-summary.html#charenc"> 1014 * character encoding</a> 1015 * 1016 * @throws IOException if writing to the specified output stream 1017 * results in an {@code IOException}. 1018 * @throws java.io.UnsupportedEncodingException if the encoding is not 1019 * supported by the implementation. 1020 * @throws NullPointerException if {@code os} is {@code null}, 1021 * or if {@code encoding} is {@code null}. 1022 * @throws ClassCastException if this {@code Properties} object 1023 * contains any keys or values that are not {@code Strings}. 1024 * @see #loadFromXML(InputStream) 1025 * @see <a href="http://www.w3.org/TR/REC-xml/#charencoding">Character 1026 * Encoding in Entities</a> 1027 * @since 1.5 1028 */ 1029 public void storeToXML(OutputStream os, String comment, String encoding) 1030 throws IOException { 1031 Objects.requireNonNull(os); 1032 Objects.requireNonNull(encoding); 1033 1034 try { 1035 Charset charset = Charset.forName(encoding); 1036 storeToXML(os, comment, charset); 1037 } catch (IllegalCharsetNameException | UnsupportedCharsetException e) { 1038 throw new UnsupportedEncodingException(encoding); 1039 } 1040 } 1041 1042 /** 1043 * Emits an XML document representing all of the properties contained 1044 * in this table, using the specified encoding. 1045 * 1046 * <p>The XML document will have the following DOCTYPE declaration: 1047 * <pre> 1048 * <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> 1049 * </pre> 1050 * 1051 * <p>If the specified comment is {@code null} then no comment 1052 * will be stored in the document. 1053 * 1054 * <p> An implementation is required to support writing of XML documents 1055 * that use the "{@code UTF-8}" or "{@code UTF-16}" encoding. An 1056 * implementation may support additional encodings. 1057 * 1058 * <p> Unmappable characters for the specified charset will be encoded as 1059 * numeric character references. 1060 * 1061 * <p>The specified stream remains open after this method returns. 1062 * 1063 * @param os the output stream on which to emit the XML document. 1064 * @param comment a description of the property list, or {@code null} 1065 * if no comment is desired. 1066 * @param charset the charset 1067 * 1068 * @throws IOException if writing to the specified output stream 1069 * results in an {@code IOException}. 1070 * @throws NullPointerException if {@code os} or {@code charset} is {@code null}. 1071 * @throws ClassCastException if this {@code Properties} object 1072 * contains any keys or values that are not {@code Strings}. 1073 * @see #loadFromXML(InputStream) 1074 * @see <a href="http://www.w3.org/TR/REC-xml/#charencoding">Character 1075 * Encoding in Entities</a> 1076 * @since 10 1077 */ 1078 public void storeToXML(OutputStream os, String comment, Charset charset) 1079 throws IOException { 1080 Objects.requireNonNull(os, "OutputStream"); 1081 Objects.requireNonNull(charset, "Charset"); 1082 PropertiesDefaultHandler handler = new PropertiesDefaultHandler(); 1083 handler.store(this, os, comment, charset); 1084 } 1085 1086 /** 1087 * Searches for the property with the specified key in this property list. 1088 * If the key is not found in this property list, the default property list, 1089 * and its defaults, recursively, are then checked. The method returns 1090 * {@code null} if the property is not found. 1091 * 1092 * @param key the property key. 1093 * @return the value in this property list with the specified key value. 1094 * @see #setProperty 1095 * @see #defaults 1096 */ 1097 public String getProperty(String key) { 1098 Object oval = map.get(key); 1099 String sval = (oval instanceof String) ? (String)oval : null; 1100 return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval; 1101 } 1102 1103 /** |