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