jdk/src/share/classes/java/util/Properties.java
author naoto
Thu, 27 Jun 2013 14:40:21 -0700
changeset 18568 59fcf0320687
parent 14909 eded175efec2
child 19864 41c6dfb2022e
permissions -rw-r--r--
6609431: (rb) ResourceBundle.getString() returns incorrect value Reviewed-by: okutsu, sherman
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18568
59fcf0320687 6609431: (rb) ResourceBundle.getString() returns incorrect value
naoto
parents: 14909
diff changeset
     2
 * Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3705
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3705
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3705
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3705
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3705
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.PrintWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.Reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.Writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.OutputStreamWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.BufferedWriter;
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
    37
import java.security.AccessController;
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
    38
import java.security.PrivilegedAction;
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
    39
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
    40
import sun.util.spi.XmlPropertiesProvider;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
    43
 * The {@code Properties} class represents a persistent set of
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
    44
 * properties. The {@code Properties} can be saved to a stream
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * or loaded from a stream. Each key and its corresponding value in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the property list is a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * A property list can contain another property list as its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * "defaults"; this second property list is searched if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * the property key is not found in the original property list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
    52
 * Because {@code Properties} inherits from {@code Hashtable}, the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
    53
 * {@code put} and {@code putAll} methods can be applied to a
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
    54
 * {@code Properties} object.  Their use is strongly discouraged as they
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * allow the caller to insert entries whose keys or values are not
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
    56
 * {@code Strings}.  The {@code setProperty} method should be used
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
    57
 * instead.  If the {@code store} or {@code save} method is called
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
    58
 * on a "compromised" {@code Properties} object that contains a
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
    59
 * non-{@code String} key or value, the call will fail. Similarly,
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
    60
 * the call to the {@code propertyNames} or {@code list} method
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
    61
 * will fail if it is called on a "compromised" {@code Properties}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
    62
 * object that contains a non-{@code String} key.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * The {@link #load(java.io.Reader) load(Reader)} <tt>/</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * {@link #store(java.io.Writer, java.lang.String) store(Writer, String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * methods load and store properties from and to a character based stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * in a simple line-oriented format specified below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * The {@link #load(java.io.InputStream) load(InputStream)} <tt>/</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * {@link #store(java.io.OutputStream, java.lang.String) store(OutputStream, String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * methods work the same way as the load(Reader)/store(Writer, String) pair, except
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * the input/output stream is encoded in ISO 8859-1 character encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Characters that cannot be directly represented in this encoding can be written using
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7816
diff changeset
    75
 * Unicode escapes as defined in section 3.3 of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7816
diff changeset
    76
 * <cite>The Java&trade; Language Specification</cite>;
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7816
diff changeset
    77
 * only a single 'u' character is allowed in an escape
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * sequence. The native2ascii tool can be used to convert property files to and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * from other character encodings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p> The {@link #loadFromXML(InputStream)} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * #storeToXML(OutputStream, String, String)} methods load and store properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * in a simple XML format.  By default the UTF-8 character encoding is used,
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    84
 * however a specific encoding may be specified if required. Implementations
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    85
 * are required to support UTF-8 and UTF-16 and may support other encodings.
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
    86
 * An XML properties document has the following DOCTYPE declaration:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * &lt;!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * Note that the system URI (http://java.sun.com/dtd/properties.dtd) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <i>not</i> accessed when exporting or importing properties; it merely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * serves as a string to uniquely identify the DTD, which is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *    &lt;!-- DTD for properties --&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *    &lt;!ELEMENT properties ( comment?, entry* ) &gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *    &lt;!ATTLIST properties version CDATA #FIXED "1.0"&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *    &lt;!ELEMENT comment (#PCDATA) &gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *    &lt;!ELEMENT entry (#PCDATA) &gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *    &lt;!ATTLIST entry key CDATA #REQUIRED&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
3705
6d494ca143b8 6875861: javadoc build warning on java.util.Properites from unconventional @see ordering
darcy
parents: 2
diff changeset
   110
 * <p>This class is thread-safe: multiple threads can share a single
6d494ca143b8 6875861: javadoc build warning on java.util.Properites from unconventional @see ordering
darcy
parents: 2
diff changeset
   111
 * <tt>Properties</tt> object without the need for external synchronization.
6d494ca143b8 6875861: javadoc build warning on java.util.Properites from unconventional @see ordering
darcy
parents: 2
diff changeset
   112
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * @see <a href="../../../technotes/tools/solaris/native2ascii.html">native2ascii tool for Solaris</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * @see <a href="../../../technotes/tools/windows/native2ascii.html">native2ascii tool for Windows</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * @author  Michael McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * @author  Xueming Shen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
class Properties extends Hashtable<Object,Object> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * use serialVersionUID from JDK 1.1.X for interoperability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     private static final long serialVersionUID = 4112578634029874840L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * A property list that contains default values for any keys not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * found in this property list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    protected Properties defaults;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Creates an empty property list with no default values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public Properties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        this(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Creates an empty property list with the specified defaults.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param   defaults   the defaults.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public Properties(Properties defaults) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        this.defaults = defaults;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   153
     * Calls the <tt>Hashtable</tt> method {@code put}. Provided for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * parallelism with the <tt>getProperty</tt> method. Enforces use of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * strings for property keys and values. The value returned is the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   156
     * result of the <tt>Hashtable</tt> call to {@code put}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param key the key to be placed into this property list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param value the value corresponding to <tt>key</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @return     the previous value of the specified key in this property
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   161
     *             list, or {@code null} if it did not have one.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @see #getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @since    1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public synchronized Object setProperty(String key, String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        return put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Reads a property list (key and element pairs) from the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * character stream in a simple line-oriented format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Properties are processed in terms of lines. There are two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * kinds of line, <i>natural lines</i> and <i>logical lines</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * A natural line is defined as a line of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * characters that is terminated either by a set of line terminator
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   178
     * characters ({@code \n} or {@code \r} or {@code \r\n})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * or by the end of the stream. A natural line may be either a blank line,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * a comment line, or hold all or some of a key-element pair. A logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * line holds all the data of a key-element pair, which may be spread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * out across several adjacent natural lines by escaping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * the line terminator sequence with a backslash character
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   184
     * {@code \}.  Note that a comment line cannot be extended
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * in this manner; every natural line that is a comment must have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * its own comment indicator, as described below. Lines are read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * input until the end of the stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * A natural line that contains only white space characters is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * considered blank and is ignored.  A comment line has an ASCII
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   192
     * {@code '#'} or {@code '!'} as its first non-white
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * space character; comment lines are also ignored and do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * encode key-element information.  In addition to line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * terminators, this format considers the characters space
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   196
     * ({@code ' '}, {@code '\u005Cu0020'}), tab
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   197
     * ({@code '\t'}, {@code '\u005Cu0009'}), and form feed
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   198
     * ({@code '\f'}, {@code '\u005Cu000C'}) to be white
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * If a logical line is spread across several natural lines, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * backslash escaping the line terminator sequence, the line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * terminator sequence, and any white space at the start of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * following line have no affect on the key or element values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * The remainder of the discussion of key and element parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * (when loading) will assume all the characters constituting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * the key and element appear on a single natural line after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * line continuation characters have been removed.  Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * it is <i>not</i> sufficient to only examine the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * preceding a line terminator sequence to decide if the line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * terminator is escaped; there must be an odd number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * contiguous backslashes for the line terminator to be escaped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Since the input is processed from left to right, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * non-zero even number of 2<i>n</i> contiguous backslashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * before a line terminator (or elsewhere) encodes <i>n</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * backslashes after escape processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * The key contains all of the characters in the line starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * with the first non-white space character and up to, but not
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   222
     * including, the first unescaped {@code '='},
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   223
     * {@code ':'}, or white space character other than a line
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * terminator. All of these key termination characters may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * included in the key by escaping them with a preceding backslash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * character; for example,<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   228
     * {@code \:\=}<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   230
     * would be the two-character key {@code ":="}.  Line
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   231
     * terminator characters can be included using {@code \r} and
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   232
     * {@code \n} escape sequences.  Any white space after the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * key is skipped; if the first non-white space character after
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   234
     * the key is {@code '='} or {@code ':'}, then it is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * ignored and any white space characters after it are also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * skipped.  All remaining characters on the line become part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * the associated element string; if there are no remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * characters, the element is the empty string
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   239
     * {@code ""}.  Once the raw character sequences
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * constituting the key and element are identified, escape
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * processing is performed as described above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * As an example, each of the following three lines specifies the key
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   245
     * {@code "Truth"} and the associated element value
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   246
     * {@code "Beauty"}:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Truth = Beauty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *  Truth:Beauty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Truth                    :Beauty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * As another example, the following three lines specify a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * property:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * fruits                           apple, banana, pear, \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *                                  cantaloupe, watermelon, \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *                                  kiwi, mango
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * </pre>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   261
     * The key is {@code "fruits"} and the associated element is:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * <pre>"apple, banana, pear, cantaloupe, watermelon, kiwi, mango"</pre>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   264
     * Note that a space appears before each {@code \} so that a space
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   265
     * will appear after each comma in the final result; the {@code \},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * line terminator, and leading white space on the continuation line are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * merely discarded and are <i>not</i> replaced by one or more other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * As a third example, the line:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <pre>cheeses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * </pre>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   274
     * specifies that the key is {@code "cheeses"} and the associated
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   275
     * element is the empty string {@code ""}.<p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <a name="unicodeescapes"></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Characters in keys and elements can be represented in escape
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * sequences similar to those used for character and string literals
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7816
diff changeset
   281
     * (see sections 3.3 and 3.10.6 of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7816
diff changeset
   282
     * <cite>The Java&trade; Language Specification</cite>).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * The differences from the character escape sequences and Unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * escapes used for characters and strings are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * <li> Octal escapes are not recognized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   290
     * <li> The character sequence {@code \b} does <i>not</i>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * represent a backspace character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * <li> The method does not treat a backslash character,
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   294
     * {@code \}, before a non-valid escape character as an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * error; the backslash is silently dropped.  For example, in a
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   296
     * Java string the sequence {@code "\z"} would cause a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * compile time error.  In contrast, this method silently drops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * the backslash.  Therefore, this method treats the two character
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   299
     * sequence {@code "\b"} as equivalent to the single
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   300
     * character {@code 'b'}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <li> Escapes are not necessary for single and double quotes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * however, by the rule above, single and double quote characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * preceded by a backslash still yield single and double quote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * characters, respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <li> Only a single 'u' character is allowed in a Uniocde escape
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * The specified stream remains open after this method returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param   reader   the input character stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @throws  IOException  if an error occurred when reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *          input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @throws  IllegalArgumentException if a malformed Unicode escape
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *          appears in the input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public synchronized void load(Reader reader) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        load0(new LineReader(reader));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Reads a property list (key and element pairs) from the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * byte stream. The input stream is in a simple line-oriented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * format as specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * {@link #load(java.io.Reader) load(Reader)} and is assumed to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * the ISO 8859-1 character encoding; that is each byte is one Latin1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * character. Characters not in Latin1, and certain special characters,
9266
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7816
diff changeset
   332
     * are represented in keys and elements using Unicode escapes as defined in
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7816
diff changeset
   333
     * section 3.3 of
121fb370f179 7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents: 7816
diff changeset
   334
     * <cite>The Java&trade; Language Specification</cite>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * The specified stream remains open after this method returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @param      inStream   the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @exception  IOException  if an error occurred when reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *             input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @throws     IllegalArgumentException if the input stream contains a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *             malformed Unicode escape sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public synchronized void load(InputStream inStream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        load0(new LineReader(inStream));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private void load0 (LineReader lr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        char[] convtBuf = new char[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        int limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        int keyLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        int valueStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        boolean hasSep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        boolean precedingBackslash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        while ((limit = lr.readLine()) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            c = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            keyLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            valueStart = limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            hasSep = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            //System.out.println("line=<" + new String(lineBuf, 0, limit) + ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            precedingBackslash = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            while (keyLen < limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                c = lr.lineBuf[keyLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                //need check if escaped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                if ((c == '=' ||  c == ':') && !precedingBackslash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    valueStart = keyLen + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    hasSep = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                } else if ((c == ' ' || c == '\t' ||  c == '\f') && !precedingBackslash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    valueStart = keyLen + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                if (c == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    precedingBackslash = !precedingBackslash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    precedingBackslash = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                keyLen++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            while (valueStart < limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                c = lr.lineBuf[valueStart];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                if (c != ' ' && c != '\t' &&  c != '\f') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    if (!hasSep && (c == '=' ||  c == ':')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        hasSep = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                valueStart++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            String key = loadConvert(lr.lineBuf, 0, keyLen, convtBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            String value = loadConvert(lr.lineBuf, valueStart, limit - valueStart, convtBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /* Read in a "logical line" from an InputStream/Reader, skip all comment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * and blank lines and filter out those leading whitespace characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * (\u0020, \u0009 and \u000c) from the beginning of a "natural line".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Method returns the char length of the "logical line" and stores
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * the line in "lineBuf".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    class LineReader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        public LineReader(InputStream inStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            this.inStream = inStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            inByteBuf = new byte[8192];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        public LineReader(Reader reader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            this.reader = reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            inCharBuf = new char[8192];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        byte[] inByteBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        char[] inCharBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        char[] lineBuf = new char[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        int inLimit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        int inOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        InputStream inStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        Reader reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        int readLine() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            char c = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            boolean skipWhiteSpace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            boolean isCommentLine = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            boolean isNewLine = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            boolean appendedLineBegin = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            boolean precedingBackslash = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            boolean skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                if (inOff >= inLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    inLimit = (inStream==null)?reader.read(inCharBuf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                                              :inStream.read(inByteBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                    inOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    if (inLimit <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        if (len == 0 || isCommentLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        }
18568
59fcf0320687 6609431: (rb) ResourceBundle.getString() returns incorrect value
naoto
parents: 14909
diff changeset
   446
                        if (precedingBackslash) {
59fcf0320687 6609431: (rb) ResourceBundle.getString() returns incorrect value
naoto
parents: 14909
diff changeset
   447
                            len--;
59fcf0320687 6609431: (rb) ResourceBundle.getString() returns incorrect value
naoto
parents: 14909
diff changeset
   448
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                if (inStream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    //The line below is equivalent to calling a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    //ISO8859-1 decoder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    c = (char) (0xff & inByteBuf[inOff++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    c = inCharBuf[inOff++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                if (skipLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    if (c == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                if (skipWhiteSpace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    if (c == ' ' || c == '\t' || c == '\f') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    if (!appendedLineBegin && (c == '\r' || c == '\n')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    skipWhiteSpace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    appendedLineBegin = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                if (isNewLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    isNewLine = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    if (c == '#' || c == '!') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                        isCommentLine = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                if (c != '\n' && c != '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    lineBuf[len++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    if (len == lineBuf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                        int newLength = lineBuf.length * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                        if (newLength < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                            newLength = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                        char[] buf = new char[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        System.arraycopy(lineBuf, 0, buf, 0, lineBuf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                        lineBuf = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    //flip the preceding backslash flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    if (c == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                        precedingBackslash = !precedingBackslash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                        precedingBackslash = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    // reached EOL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    if (isCommentLine || len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                        isCommentLine = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                        isNewLine = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                        skipWhiteSpace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                        len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                    if (inOff >= inLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                        inLimit = (inStream==null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                                  ?reader.read(inCharBuf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                  :inStream.read(inByteBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                        inOff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                        if (inLimit <= 0) {
18568
59fcf0320687 6609431: (rb) ResourceBundle.getString() returns incorrect value
naoto
parents: 14909
diff changeset
   516
                            if (precedingBackslash) {
59fcf0320687 6609431: (rb) ResourceBundle.getString() returns incorrect value
naoto
parents: 14909
diff changeset
   517
                                len--;
59fcf0320687 6609431: (rb) ResourceBundle.getString() returns incorrect value
naoto
parents: 14909
diff changeset
   518
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    if (precedingBackslash) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                        len -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        //skip the leading whitespace characters in following line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                        skipWhiteSpace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                        appendedLineBegin = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                        precedingBackslash = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                        if (c == '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                            skipLF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                        return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * Converts encoded &#92;uxxxx to unicode chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * and changes special saved chars to their original forms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    private String loadConvert (char[] in, int off, int len, char[] convtBuf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        if (convtBuf.length < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            int newLen = len * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            if (newLen < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                newLen = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            convtBuf = new char[newLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        char aChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        char[] out = convtBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        int outLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        int end = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        while (off < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            aChar = in[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            if (aChar == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                aChar = in[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                if(aChar == 'u') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    // Read the xxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    int value=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    for (int i=0; i<4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                        aChar = in[off++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        switch (aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                          case '0': case '1': case '2': case '3': case '4':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                          case '5': case '6': case '7': case '8': case '9':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                             value = (value << 4) + aChar - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                          case 'a': case 'b': case 'c':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                          case 'd': case 'e': case 'f':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                             value = (value << 4) + 10 + aChar - 'a';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                          case 'A': case 'B': case 'C':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                          case 'D': case 'E': case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                             value = (value << 4) + 10 + aChar - 'A';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                             break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                          default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                              throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                                           "Malformed \\uxxxx encoding.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                    out[outLen++] = (char)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    if (aChar == 't') aChar = '\t';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    else if (aChar == 'r') aChar = '\r';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    else if (aChar == 'n') aChar = '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    else if (aChar == 'f') aChar = '\f';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    out[outLen++] = aChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                out[outLen++] = aChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        return new String (out, 0, outLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * Converts unicodes to encoded &#92;uxxxx and escapes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * special characters with a preceding slash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    private String saveConvert(String theString,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                               boolean escapeSpace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                               boolean escapeUnicode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        int len = theString.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        int bufLen = len * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        if (bufLen < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            bufLen = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        StringBuffer outBuffer = new StringBuffer(bufLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        for(int x=0; x<len; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            char aChar = theString.charAt(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            // Handle common case first, selecting largest block that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            // avoids the specials below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            if ((aChar > 61) && (aChar < 127)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                if (aChar == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    outBuffer.append('\\'); outBuffer.append('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                outBuffer.append(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            switch(aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                case ' ':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                    if (x == 0 || escapeSpace)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                        outBuffer.append('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    outBuffer.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                case '\t':outBuffer.append('\\'); outBuffer.append('t');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                case '\n':outBuffer.append('\\'); outBuffer.append('n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                case '\r':outBuffer.append('\\'); outBuffer.append('r');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                case '\f':outBuffer.append('\\'); outBuffer.append('f');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                          break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                case '=': // Fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                case ':': // Fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                case '#': // Fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                case '!':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    outBuffer.append('\\'); outBuffer.append(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    if (((aChar < 0x0020) || (aChar > 0x007e)) & escapeUnicode ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                        outBuffer.append('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                        outBuffer.append('u');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                        outBuffer.append(toHex((aChar >> 12) & 0xF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                        outBuffer.append(toHex((aChar >>  8) & 0xF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                        outBuffer.append(toHex((aChar >>  4) & 0xF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                        outBuffer.append(toHex( aChar        & 0xF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                        outBuffer.append(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        return outBuffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    private static void writeComments(BufferedWriter bw, String comments)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        bw.write("#");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        int len = comments.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        int current = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        int last = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        char[] uu = new char[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        uu[0] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        uu[1] = 'u';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        while (current < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            char c = comments.charAt(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            if (c > '\u00ff' || c == '\n' || c == '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                if (last != current)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                    bw.write(comments.substring(last, current));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                if (c > '\u00ff') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    uu[2] = toHex((c >> 12) & 0xf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    uu[3] = toHex((c >>  8) & 0xf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                    uu[4] = toHex((c >>  4) & 0xf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                    uu[5] = toHex( c        & 0xf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                    bw.write(new String(uu));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                    bw.newLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    if (c == '\r' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                        current != len - 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                        comments.charAt(current + 1) == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                        current++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    if (current == len - 1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                        (comments.charAt(current + 1) != '#' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                        comments.charAt(current + 1) != '!'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                        bw.write("#");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                last = current + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            current++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        if (last != current)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            bw.write(comments.substring(last, current));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        bw.newLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   702
     * Calls the {@code store(OutputStream out, String comments)} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * and suppresses IOExceptions that were thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * @deprecated This method does not throw an IOException if an I/O error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * occurs while saving the property list.  The preferred way to save a
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   707
     * properties list is via the {@code store(OutputStream out,
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   708
     * String comments)} method or the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   709
     * {@code storeToXML(OutputStream os, String comment)} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @param   out      an output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * @param   comments   a description of the property list.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   713
     * @exception  ClassCastException  if this {@code Properties} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     *             contains any keys or values that are not
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   715
     *             {@code Strings}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    @Deprecated
6882
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 6521
diff changeset
   718
    public void save(OutputStream out, String comments)  {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            store(out, comments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * Writes this property list (key and element pairs) in this
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   727
     * {@code Properties} table to the output character stream in a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * format suitable for using the {@link #load(java.io.Reader) load(Reader)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   731
     * Properties from the defaults table of this {@code Properties}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * table (if any) are <i>not</i> written out by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   734
     * If the comments argument is not null, then an ASCII {@code #}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * character, the comments string, and a line separator are first written
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   736
     * to the output stream. Thus, the {@code comments} can serve as an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * identifying comment. Any one of a line feed ('\n'), a carriage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * return ('\r'), or a carriage return followed immediately by a line feed
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   739
     * in comments is replaced by a line separator generated by the {@code Writer}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   740
     * and if the next character in comments is not character {@code #} or
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   741
     * character {@code !} then an ASCII {@code #} is written out
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * after that line separator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * Next, a comment line is always written, consisting of an ASCII
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   745
     * {@code #} character, the current date and time (as if produced
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   746
     * by the {@code toString} method of {@code Date} for the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   747
     * current time), and a line separator as generated by the {@code Writer}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   749
     * Then every entry in this {@code Properties} table is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * written out, one per line. For each entry the key string is
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   751
     * written, then an ASCII {@code =}, then the associated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * element string. For the key, all space characters are
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   753
     * written with a preceding {@code \} character.  For the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * element, leading space characters, but not embedded or trailing
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   755
     * space characters, are written with a preceding {@code \}
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   756
     * character. The key and element characters {@code #},
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   757
     * {@code !}, {@code =}, and {@code :} are written
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * with a preceding backslash to ensure that they are properly loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * After the entries have been written, the output stream is flushed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * The output stream remains open after this method returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @param   writer      an output character stream writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * @param   comments   a description of the property list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @exception  IOException if writing this property list to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *             output stream throws an <tt>IOException</tt>.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   768
     * @exception  ClassCastException  if this {@code Properties} object
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   769
     *             contains any keys or values that are not {@code Strings}.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   770
     * @exception  NullPointerException  if {@code writer} is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    public void store(Writer writer, String comments)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        store0((writer instanceof BufferedWriter)?(BufferedWriter)writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                                                 : new BufferedWriter(writer),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
               comments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
               false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * Writes this property list (key and element pairs) in this
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   784
     * {@code Properties} table to the output stream in a format suitable
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   785
     * for loading into a {@code Properties} table using the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * {@link #load(InputStream) load(InputStream)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   788
     * Properties from the defaults table of this {@code Properties}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * table (if any) are <i>not</i> written out by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * This method outputs the comments, properties keys and values in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * the same format as specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * {@link #store(java.io.Writer, java.lang.String) store(Writer)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * with the following differences:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * <li>The stream is written using the ISO 8859-1 character encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * <li>Characters not in Latin-1 in the comments are written as
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   799
     * {@code \u005Cu}<i>xxxx</i> for their appropriate unicode
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * hexadecimal value <i>xxxx</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   802
     * <li>Characters less than {@code \u005Cu0020} and characters greater
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   803
     * than {@code \u005Cu007E} in property keys or values are written
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   804
     * as {@code \u005Cu}<i>xxxx</i> for the appropriate hexadecimal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * value <i>xxxx</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * After the entries have been written, the output stream is flushed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * The output stream remains open after this method returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * @param   out      an output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @param   comments   a description of the property list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @exception  IOException if writing this property list to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *             output stream throws an <tt>IOException</tt>.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   815
     * @exception  ClassCastException  if this {@code Properties} object
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   816
     *             contains any keys or values that are not {@code Strings}.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   817
     * @exception  NullPointerException  if {@code out} is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    public void store(OutputStream out, String comments)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        store0(new BufferedWriter(new OutputStreamWriter(out, "8859_1")),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
               comments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
               true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    private void store0(BufferedWriter bw, String comments, boolean escUnicode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        if (comments != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            writeComments(bw, comments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        bw.write("#" + new Date().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        bw.newLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        synchronized (this) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11676
diff changeset
   837
            for (Enumeration<?> e = keys(); e.hasMoreElements();) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                String key = (String)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                String val = (String)get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                key = saveConvert(key, true, escUnicode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                /* No need to escape embedded and trailing spaces for value, hence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                 * pass false to flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                val = saveConvert(val, false, escUnicode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                bw.write(key + "=" + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                bw.newLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        bw.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * Loads all of the properties represented by the XML document on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * specified input stream into this properties table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * <p>The XML document must have the following DOCTYPE declaration:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * &lt;!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * Furthermore, the document must satisfy the properties DTD described
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   863
     * <p> An implementation is required to read XML documents that use the
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   864
     * "{@code UTF-8}" or "{@code UTF-16}" encoding. An implementation may
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   865
     * support additional encodings.
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   866
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * <p>The specified stream is closed after this method returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * @param in the input stream from which to read the XML document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @throws IOException if reading from the specified input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *         results in an <tt>IOException</tt>.
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   872
     * @throws java.io.UnsupportedEncodingException if the document's encoding
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   873
     *         declaration can be read and it specifies an encoding that is not
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   874
     *         supported
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @throws InvalidPropertiesFormatException Data on input stream does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     *         constitute a valid XML document with the mandated document type.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   877
     * @throws NullPointerException if {@code in} is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * @see    #storeToXML(OutputStream, String, String)
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   879
     * @see    <a href="http://www.w3.org/TR/REC-xml/#charencoding">Character
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   880
     *         Encoding in Entities</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    public synchronized void loadFromXML(InputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        throws IOException, InvalidPropertiesFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    {
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   886
        XmlSupport.load(this, Objects.requireNonNull(in));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * Emits an XML document representing all of the properties contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * in this table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * <p> An invocation of this method of the form <tt>props.storeToXML(os,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * comment)</tt> behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * <tt>props.storeToXML(os, comment, "UTF-8");</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * @param os the output stream on which to emit the XML document.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   899
     * @param comment a description of the property list, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     *        if no comment is desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * @throws IOException if writing to the specified output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *         results in an <tt>IOException</tt>.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   903
     * @throws NullPointerException if {@code os} is null.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   904
     * @throws ClassCastException  if this {@code Properties} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     *         contains any keys or values that are not
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   906
     *         {@code Strings}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * @see    #loadFromXML(InputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     */
6882
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 6521
diff changeset
   910
    public void storeToXML(OutputStream os, String comment)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        storeToXML(os, comment, "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * Emits an XML document representing all of the properties contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * in this table, using the specified encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * <p>The XML document will have the following DOCTYPE declaration:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * &lt;!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     *
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   925
     * <p>If the specified comment is {@code null} then no comment
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * will be stored in the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     *
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   928
     * <p> An implementation is required to support writing of XML documents
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   929
     * that use the "{@code UTF-8}" or "{@code UTF-16}" encoding. An
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   930
     * implementation may support additional encodings.
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   931
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * <p>The specified stream remains open after this method returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     *
6521
3b995fa42e90 6977887: (doc) Java 6 API missing info about encoding parameter in storeToXML method
mchung
parents: 5506
diff changeset
   934
     * @param os        the output stream on which to emit the XML document.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   935
     * @param comment   a description of the property list, or {@code null}
6521
3b995fa42e90 6977887: (doc) Java 6 API missing info about encoding parameter in storeToXML method
mchung
parents: 5506
diff changeset
   936
     *                  if no comment is desired.
3b995fa42e90 6977887: (doc) Java 6 API missing info about encoding parameter in storeToXML method
mchung
parents: 5506
diff changeset
   937
     * @param  encoding the name of a supported
3b995fa42e90 6977887: (doc) Java 6 API missing info about encoding parameter in storeToXML method
mchung
parents: 5506
diff changeset
   938
     *                  <a href="../lang/package-summary.html#charenc">
3b995fa42e90 6977887: (doc) Java 6 API missing info about encoding parameter in storeToXML method
mchung
parents: 5506
diff changeset
   939
     *                  character encoding</a>
3b995fa42e90 6977887: (doc) Java 6 API missing info about encoding parameter in storeToXML method
mchung
parents: 5506
diff changeset
   940
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * @throws IOException if writing to the specified output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     *         results in an <tt>IOException</tt>.
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   943
     * @throws java.io.UnsupportedEncodingException if the encoding is not
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   944
     *         supported by the implementation.
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   945
     * @throws NullPointerException if {@code os} is {@code null},
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   946
     *         or if {@code encoding} is {@code null}.
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   947
     * @throws ClassCastException  if this {@code Properties} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *         contains any keys or values that are not
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   949
     *         {@code Strings}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * @see    #loadFromXML(InputStream)
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   951
     * @see    <a href="http://www.w3.org/TR/REC-xml/#charencoding">Character
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   952
     *         Encoding in Entities</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     */
6882
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 6521
diff changeset
   955
    public void storeToXML(OutputStream os, String comment, String encoding)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    {
14187
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   958
        XmlSupport.save(this, Objects.requireNonNull(os), comment,
9e193f8bab66 8000685: (props) Properties.storeToXML/loadFromXML should only require UTF-8 and UTF-16 to be supported
alanb
parents: 14029
diff changeset
   959
                        Objects.requireNonNull(encoding));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * Searches for the property with the specified key in this property list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * If the key is not found in this property list, the default property list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * and its defaults, recursively, are then checked. The method returns
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 10587
diff changeset
   966
     * {@code null} if the property is not found.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * @param   key   the property key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * @return  the value in this property list with the specified key value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * @see     #setProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * @see     #defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    public String getProperty(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        Object oval = super.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        String sval = (oval instanceof String) ? (String)oval : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * Searches for the property with the specified key in this property list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * If the key is not found in this property list, the default property list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * and its defaults, recursively, are then checked. The method returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * default value argument if the property is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * @param   key            the hashtable key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * @param   defaultValue   a default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * @return  the value in this property list with the specified key value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * @see     #setProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * @see     #defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    public String getProperty(String key, String defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        String val = getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        return (val == null) ? defaultValue : val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * Returns an enumeration of all the keys in this property list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * including distinct keys in the default property list if a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * of the same name has not already been found from the main
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * properties list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * @return  an enumeration of all the keys in this property list, including
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     *          the keys in the default property list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * @throws  ClassCastException if any key in this property list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     *          is not a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * @see     java.util.Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * @see     java.util.Properties#defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * @see     #stringPropertyNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    public Enumeration<?> propertyNames() {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11676
diff changeset
  1012
        Hashtable<String,Object> h = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        enumerate(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        return h.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * Returns a set of keys in this property list where
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * the key and its corresponding value are strings,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * including distinct keys in the default property list if a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * of the same name has not already been found from the main
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * properties list.  Properties whose key or value is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * of type <tt>String</tt> are omitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * The returned set is not backed by the <tt>Properties</tt> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * Changes to this <tt>Properties</tt> are not reflected in the set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * or vice versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @return  a set of keys in this property list where
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     *          the key and its corresponding value are strings,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     *          including the keys in the default property list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * @see     java.util.Properties#defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    public Set<String> stringPropertyNames() {
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 6882
diff changeset
  1036
        Hashtable<String, String> h = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        enumerateStringProperties(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        return h.keySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * Prints this property list out to the specified output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * This method is useful for debugging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @param   out   an output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * @throws  ClassCastException if any key in this property list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     *          is not a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    public void list(PrintStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        out.println("-- listing properties --");
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11676
diff changeset
  1051
        Hashtable<String,Object> h = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        enumerate(h);
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11676
diff changeset
  1053
        for (Enumeration<String> e = h.keys() ; e.hasMoreElements() ;) {
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11676
diff changeset
  1054
            String key = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            String val = (String)h.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            if (val.length() > 40) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                val = val.substring(0, 37) + "...";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            out.println(key + "=" + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * Prints this property list out to the specified output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * This method is useful for debugging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * @param   out   an output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * @throws  ClassCastException if any key in this property list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     *          is not a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * Rather than use an anonymous inner class to share common code, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * method is duplicated in order to ensure that a non-1.1 compiler can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * compile this file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    public void list(PrintWriter out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        out.println("-- listing properties --");
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11676
diff changeset
  1079
        Hashtable<String,Object> h = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        enumerate(h);
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11676
diff changeset
  1081
        for (Enumeration<String> e = h.keys() ; e.hasMoreElements() ;) {
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11676
diff changeset
  1082
            String key = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            String val = (String)h.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            if (val.length() > 40) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                val = val.substring(0, 37) + "...";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            out.println(key + "=" + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * Enumerates all key/value pairs in the specified hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * @param h the hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * @throws ClassCastException if any of the property keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     *         is not of String type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11676
diff changeset
  1097
    private synchronized void enumerate(Hashtable<String,Object> h) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        if (defaults != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            defaults.enumerate(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        }
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11676
diff changeset
  1101
        for (Enumeration<?> e = keys() ; e.hasMoreElements() ;) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            String key = (String)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            h.put(key, get(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * Enumerates all key/value pairs in the specified hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * and omits the property if the key or value is not a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * @param h the hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    private synchronized void enumerateStringProperties(Hashtable<String, String> h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        if (defaults != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            defaults.enumerateStringProperties(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        }
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 11676
diff changeset
  1116
        for (Enumeration<?> e = keys() ; e.hasMoreElements() ;) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            Object k = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            Object v = get(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            if (k instanceof String && v instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                h.put((String) k, (String) v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * Convert a nibble to a hex character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * @param   nibble  the nibble to convert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    private static char toHex(int nibble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        return hexDigit[(nibble & 0xF)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    /** A table of hex digits */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    private static final char[] hexDigit = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    };
10587
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1137
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1138
    /**
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1139
     * Supporting class for loading/storing properties in XML format.
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1140
     *
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1141
     * <p> The {@code load} and {@code store} methods defined here delegate to a
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1142
     * system-wide {@code XmlPropertiesProvider}. On first invocation of either
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1143
     * method then the system-wide provider is located as follows: </p>
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1144
     *
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1145
     * <ol>
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1146
     *   <li> If the system property {@code sun.util.spi.XmlPropertiesProvider}
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1147
     *   is defined then it is taken to be the full-qualified name of a concrete
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1148
     *   provider class. The class is loaded with the system class loader as the
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1149
     *   initiating loader. If it cannot be loaded or instantiated using a zero
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1150
     *   argument constructor then an unspecified error is thrown. </li>
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1151
     *
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1152
     *   <li> If the system property is not defined then the service-provider
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1153
     *   loading facility defined by the {@link ServiceLoader} class is used to
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1154
     *   locate a provider with the system class loader as the initiating
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1155
     *   loader and {@code sun.util.spi.XmlPropertiesProvider} as the service
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1156
     *   type. If this process fails then an unspecified error is thrown. If
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1157
     *   there is more than one service provider installed then it is
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1158
     *   not specified as to which provider will be used. </li>
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1159
     *
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1160
     *   <li> If the provider is not found by the above means then a system
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1161
     *   default provider will be instantiated and used. </li>
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1162
     * </ol>
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1163
     */
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1164
    private static class XmlSupport {
10587
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1165
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1166
        private static XmlPropertiesProvider loadProviderFromProperty(ClassLoader cl) {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1167
            String cn = System.getProperty("sun.util.spi.XmlPropertiesProvider");
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1168
            if (cn == null)
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1169
                return null;
10587
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1170
            try {
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1171
                Class<?> c = Class.forName(cn, true, cl);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1172
                return (XmlPropertiesProvider)c.newInstance();
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1173
            } catch (ClassNotFoundException |
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1174
                     IllegalAccessException |
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1175
                     InstantiationException x) {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1176
                throw new ServiceConfigurationError(null, x);
10587
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1177
            }
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1178
        }
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1179
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1180
        private static XmlPropertiesProvider loadProviderAsService(ClassLoader cl) {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1181
            Iterator<XmlPropertiesProvider> iterator =
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1182
                 ServiceLoader.load(XmlPropertiesProvider.class, cl).iterator();
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1183
            return iterator.hasNext() ? iterator.next() : null;
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1184
        }
10587
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1185
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1186
        private static XmlPropertiesProvider loadProvider() {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1187
            return AccessController.doPrivileged(
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1188
                new PrivilegedAction<XmlPropertiesProvider>() {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1189
                    public XmlPropertiesProvider run() {
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1190
                        ClassLoader cl = ClassLoader.getSystemClassLoader();
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1191
                        XmlPropertiesProvider provider = loadProviderFromProperty(cl);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1192
                        if (provider != null)
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1193
                            return provider;
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1194
                        provider = loadProviderAsService(cl);
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1195
                        if (provider != null)
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1196
                            return provider;
14909
eded175efec2 8005248: (props) Integrate small footprint parser into Properties
alanb
parents: 14187
diff changeset
  1197
                        return new jdk.internal.util.xml.BasicXmlPropertiesProvider();
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1198
                }});
10587
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1199
        }
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1200
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1201
        private static final XmlPropertiesProvider PROVIDER = loadProvider();
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1202
10587
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1203
        static void load(Properties props, InputStream in)
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1204
            throws IOException, InvalidPropertiesFormatException
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1205
        {
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1206
            PROVIDER.load(props, in);
10587
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1207
        }
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1208
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1209
        static void save(Properties props, OutputStream os, String comment,
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1210
                         String encoding)
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1211
            throws IOException
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1212
        {
14029
c684694164c2 8000354: (props) Properties.storeToXML/loadFromXML need to allow for alternative implementations
alanb
parents: 12448
diff changeset
  1213
            PROVIDER.store(props, os, comment, encoding);
10587
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1214
        }
654c00a794b6 6915797: Remove sun.tools.jar.JarImageSource that is not used
mchung
parents: 9266
diff changeset
  1215
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
}