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