jdk/src/share/classes/java/beans/XMLEncoder.java
author mcimadamore
Thu, 01 Dec 2011 18:34:23 +0000
changeset 11120 f8576c769572
parent 5998 586c1b1dbb10
child 13653 012c6f8f0279
permissions -rw-r--r--
7116954: Misc warnings in java.beans/java.beans.context Summary: Remove generic warnings form java.beans and java.beans.context Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4969
diff changeset
     2
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4969
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4969
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4969
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4969
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4969
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.beans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.charset.CharsetEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.charset.IllegalCharsetNameException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.charset.UnsupportedCharsetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The <code>XMLEncoder</code> class is a complementary alternative to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * the <code>ObjectOutputStream</code> and can used to generate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * a textual representation of a <em>JavaBean</em> in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * way that the <code>ObjectOutputStream</code> can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * be used to create binary representation of <code>Serializable</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * objects. For example, the following fragment can be used to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * a textual representation the supplied <em>JavaBean</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * and all its properties:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *       XMLEncoder e = new XMLEncoder(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *                          new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *                              new FileOutputStream("Test.xml")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *       e.writeObject(new JButton("Hello, world"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *       e.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Despite the similarity of their APIs, the <code>XMLEncoder</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * class is exclusively designed for the purpose of archiving graphs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * of <em>JavaBean</em>s as textual representations of their public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * properties. Like Java source files, documents written this way
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * have a natural immunity to changes in the implementations of the classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * involved. The <code>ObjectOutputStream</code> continues to be recommended
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * for interprocess communication and general purpose serialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * The <code>XMLEncoder</code> class provides a default denotation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <em>JavaBean</em>s in which they are represented as XML documents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * complying with version 1.0 of the XML specification and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * UTF-8 character encoding of the Unicode/ISO 10646 character set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * The XML documents produced by the <code>XMLEncoder</code> class are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <em>Portable and version resilient</em>: they have no dependencies
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * on the private implementation of any class and so, like Java source
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * files, they may be exchanged between environments which may have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * different versions of some of the classes and between VMs from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * different vendors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <em>Structurally compact</em>: The <code>XMLEncoder</code> class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * uses a <em>redundancy elimination</em> algorithm internally so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * default values of a Bean's properties are not written to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <em>Fault tolerant</em>: Non-structural errors in the file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * caused either by damage to the file or by API changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * made to classes in an archive remain localized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * so that a reader can report the error and continue to load the parts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * of the document which were not affected by the error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * Below is an example of an XML archive containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * some user interface components from the <em>swing</em> toolkit:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * &lt;?xml version="1.0" encoding="UTF-8"?&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * &lt;java version="1.0" class="java.beans.XMLDecoder"&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * &lt;object class="javax.swing.JFrame"&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *   &lt;void property="name"&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *     &lt;string&gt;frame1&lt;/string&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *   &lt;/void&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *   &lt;void property="bounds"&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *     &lt;object class="java.awt.Rectangle"&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *       &lt;int&gt;0&lt;/int&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *       &lt;int&gt;0&lt;/int&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *       &lt;int&gt;200&lt;/int&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *       &lt;int&gt;200&lt;/int&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *     &lt;/object&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *   &lt;/void&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *   &lt;void property="contentPane"&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *     &lt;void method="add"&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *       &lt;object class="javax.swing.JButton"&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *         &lt;void property="label"&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *           &lt;string&gt;Hello&lt;/string&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *         &lt;/void&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *       &lt;/object&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *     &lt;/void&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *   &lt;/void&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *   &lt;void property="visible"&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *     &lt;boolean&gt;true&lt;/boolean&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *   &lt;/void&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * &lt;/object&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * &lt;/java&gt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * The XML syntax uses the following conventions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * Each element represents a method call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * The "object" tag denotes an <em>expression</em> whose value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * to be used as the argument to the enclosing element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * The "void" tag denotes a <em>statement</em> which will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * be executed, but whose result will not be used as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * argument to the enclosing method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * Elements which contain elements use those elements as arguments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * unless they have the tag: "void".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * The name of the method is denoted by the "method" attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * XML's standard "id" and "idref" attributes are used to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * references to previous expressions - so as to deal with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * circularities in the object graph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * The "class" attribute is used to specify the target of a static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * method or constructor explicitly; its value being the fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * qualified name of the class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * Elements with the "void" tag are executed using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * the outer context as the target if no target is defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * by a "class" attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * Java's String class is treated specially and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * written &lt;string&gt;Hello, world&lt;/string&gt; where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * the characters of the string are converted to bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * using the UTF-8 character encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * Although all object graphs may be written using just these three
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * tags, the following definitions are included so that common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * data structures can be expressed more concisely:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * The default method name is "new".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * A reference to a java class is written in the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 *  &lt;class&gt;javax.swing.JButton&lt;/class&gt;.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * Instances of the wrapper classes for Java's primitive types are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * using the name of the primitive type as the tag. For example, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * instance of the <code>Integer</code> class could be written:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * &lt;int&gt;123&lt;/int&gt;. Note that the <code>XMLEncoder</code> class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * uses Java's reflection package in which the conversion between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * Java's primitive types and their associated "wrapper classes"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * is handled internally. The API for the <code>XMLEncoder</code> class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * itself deals only with <code>Object</code>s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * In an element representing a nullary method whose name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * starts with "get", the "method" attribute is replaced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * with a "property" attribute whose value is given by removing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * the "get" prefix and decapitalizing the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * In an element representing a monadic method whose name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * starts with "set", the "method" attribute is replaced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * with a "property" attribute whose value is given by removing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * the "set" prefix and decapitalizing the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * In an element representing a method named "get" taking one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * integer argument, the "method" attribute is replaced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * with an "index" attribute whose value the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * first argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * In an element representing a method named "set" taking two arguments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * the first of which is an integer, the "method" attribute is replaced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * with an "index" attribute whose value the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * first argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * A reference to an array is written using the "array"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * tag. The "class" and "length" attributes specify the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * sub-type of the array and its length respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * </ul>
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
 * For more information you might also want to check out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 * <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 href="http://java.sun.com/products/jfc/tsc/articles/persistence4">Using XMLEncoder</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * an article in <em>The Swing Connection.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * @see XMLDecoder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * @see java.io.ObjectOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 */
5998
586c1b1dbb10 6963723: Project Coin: Retrofit more JDK classes for ARM
darcy
parents: 5597
diff changeset
   207
public class XMLEncoder extends Encoder implements AutoCloseable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private final CharsetEncoder encoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private final String charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private final boolean declaration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private OutputStreamWriter out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private Object owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    private int indentation = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private boolean internal = false;
4969
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   217
    private Map<Object, ValueData> valueToExpression;
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   218
    private Map<Object, List<Statement>> targetToStatementList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private boolean preambleWritten = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private NameGenerator nameGenerator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private class ValueData {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        public int refs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        public boolean marked = false; // Marked -> refs > 0 unless ref was a target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        public String name = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        public Expression exp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Creates a new XML encoder to write out <em>JavaBeans</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * to the stream <code>out</code> using an XML encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param out  the stream to which the XML representation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *             the objects will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *          if <code>out</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @see XMLDecoder#XMLDecoder(InputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public XMLEncoder(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        this(out, "UTF-8", true, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Creates a new XML encoder to write out <em>JavaBeans</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * to the stream <code>out</code> using the given <code>charset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * starting from the given <code>indentation</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param out          the stream to which the XML representation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *                     the objects will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @param charset      the name of the requested charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *                     may be either a canonical name or an alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param declaration  whether the XML declaration should be generated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *                     set this to <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *                     when embedding the contents in another XML document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param indentation  the number of space characters to indent the entire XML document by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *          if <code>out</code> or <code>charset</code> is <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *          or if <code>indentation</code> is less than 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @throws  IllegalCharsetNameException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *          if <code>charset</code> name is illegal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @throws  UnsupportedCharsetException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *          if no support for the named charset is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *          in this instance of the Java virtual machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @throws  UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *          if loaded charset does not support encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @see Charset#forName(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public XMLEncoder(OutputStream out, String charset, boolean declaration, int indentation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (out == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw new IllegalArgumentException("the output stream cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (indentation < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            throw new IllegalArgumentException("the indentation must be >= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        Charset cs = Charset.forName(charset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        this.encoder = cs.newEncoder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        this.charset = charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        this.declaration = declaration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        this.indentation = indentation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        this.out = new OutputStreamWriter(out, cs.newEncoder());
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5998
diff changeset
   290
        valueToExpression = new IdentityHashMap<>();
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5998
diff changeset
   291
        targetToStatementList = new IdentityHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        nameGenerator = new NameGenerator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Sets the owner of this encoder to <code>owner</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param owner The owner of this encoder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @see #getOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public void setOwner(Object owner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        this.owner = owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        writeExpression(new Expression(this, "getOwner", new Object[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Gets the owner of this encoder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @return The owner of this encoder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @see #setOwner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public Object getOwner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return owner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Write an XML representation of the specified object to the output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param o The object to be written to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @see XMLDecoder#readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public void writeObject(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (internal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            super.writeObject(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            writeStatement(new Statement(this, "writeObject", new Object[]{o}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
4969
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   334
    private List<Statement> statementList(Object target) {
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   335
        List<Statement> list = targetToStatementList.get(target);
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   336
        if (list == null) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5998
diff changeset
   337
            list = new ArrayList<>();
4969
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   338
            targetToStatementList.put(target, list);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    private void mark(Object o, boolean isArgument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (o == null || o == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        ValueData d = getValueData(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        Expression exp = d.exp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        // Do not mark liternal strings. Other strings, which might,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        // for example, come from resource bundles should still be marked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (o.getClass() == String.class && exp == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        // Bump the reference counts of all arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (isArgument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            d.refs++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (d.marked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        d.marked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        Object target = exp.getTarget();
4969
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   365
        mark(exp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (!(target instanceof Class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            statementList(target).add(exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            // Pending: Why does the reference count need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            // be incremented here?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            d.refs++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    private void mark(Statement stm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        Object[] args = stm.getArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        for (int i = 0; i < args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            Object arg = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            mark(arg, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        mark(stm.getTarget(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * Records the Statement so that the Encoder will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * produce the actual output when the stream is flushed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * This method should only be invoked within the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * of initializing a persistence delegate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @param oldStm The statement that will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *               to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @see java.beans.PersistenceDelegate#initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public void writeStatement(Statement oldStm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        // System.out.println("XMLEncoder::writeStatement: " + oldStm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        boolean internal = this.internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        this.internal = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            super.writeStatement(oldStm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
               Note we must do the mark first as we may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
               require the results of previous values in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
               this context for this statement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
               Test case is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                   os.setOwner(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                   os.writeObject(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            mark(oldStm);
5583
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   410
            Object target = oldStm.getTarget();
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   411
            if (target instanceof Field) {
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   412
                String method = oldStm.getMethodName();
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   413
                Object[] args = oldStm.getArguments();
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   414
                if ((method == null) || (args == null)) {
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   415
                }
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   416
                else if (method.equals("get") && (args.length == 1)) {
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   417
                    target = args[0];
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   418
                }
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   419
                else if (method.equals("set") && (args.length == 2)) {
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   420
                    target = args[0];
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   421
                }
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   422
            }
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   423
            statementList(target).add(oldStm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            getExceptionListener().exceptionThrown(new Exception("XMLEncoder: discarding statement " + oldStm, e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        this.internal = internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * Records the Expression so that the Encoder will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * produce the actual output when the stream is flushed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * This method should only be invoked within the context of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * initializing a persistence delegate or setting up an encoder to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * read from a resource bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * For more information about using resource bundles with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * XMLEncoder, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * http://java.sun.com/products/jfc/tsc/articles/persistence4/#i18n
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @param oldExp The expression that will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *               to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @see java.beans.PersistenceDelegate#initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    public void writeExpression(Expression oldExp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        boolean internal = this.internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        this.internal = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        Object oldValue = getValue(oldExp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (get(oldValue) == null || (oldValue instanceof String && !internal)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            getValueData(oldValue).exp = oldExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            super.writeExpression(oldExp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        this.internal = internal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * This method writes out the preamble associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * XML encoding if it has not been written already and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * then writes out all of the values that been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * written to the stream since the last time <code>flush</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * was called. After flushing, all internal references to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * values that were written to this stream are cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    public void flush() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        if (!preambleWritten) { // Don't do this in constructor - it throws ... pending.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (this.declaration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                writeln("<?xml version=" + quote("1.0") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                            " encoding=" + quote(this.charset) + "?>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            writeln("<java version=" + quote(System.getProperty("java.version")) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                           " class=" + quote(XMLDecoder.class.getName()) + ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            preambleWritten = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        indentation++;
4969
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   478
        List<Statement> statements = statementList(this);
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   479
        while (!statements.isEmpty()) {
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   480
            Statement s = statements.remove(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            if ("writeObject".equals(s.getMethodName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                outputValue(s.getArguments()[0], this, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                outputStatement(s, this, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        indentation--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            getExceptionListener().exceptionThrown(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        super.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        nameGenerator.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        valueToExpression.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        targetToStatementList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * This method calls <code>flush</code>, writes the closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * postamble and then closes the output stream associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        writeln("</java>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            getExceptionListener().exceptionThrown(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    private String quote(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return "\"" + s + "\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    private ValueData getValueData(Object o) {
4969
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   528
        ValueData d = valueToExpression.get(o);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        if (d == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            d = new ValueData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            valueToExpression.put(o, d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        return d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * Returns <code>true</code> if the argument,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * a Unicode code point, is valid in XML documents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Unicode characters fit into the low sixteen bits of a Unicode code point,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * and pairs of Unicode <em>surrogate characters</em> can be combined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * to encode Unicode code point in documents containing only Unicode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * (The <code>char</code> datatype in the Java Programming Language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * represents Unicode characters, including unpaired surrogates.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * <par>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * [2] Char ::= #x0009 | #x000A | #x000D
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *            | [#x0020-#xD7FF]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *            | [#xE000-#xFFFD]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *            | [#x10000-#x10ffff]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * </par>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @param code  the 32-bit Unicode code point being tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @return  <code>true</code> if the Unicode code point is valid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *          <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    private static boolean isValidCharCode(int code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        return (0x0020 <= code && code <= 0xD7FF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            || (0x000A == code)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            || (0x0009 == code)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            || (0x000D == code)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            || (0xE000 <= code && code <= 0xFFFD)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            || (0x10000 <= code && code <= 0x10ffff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    private void writeln(String exp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            for(int i = 0; i < indentation; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                sb.append(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            sb.append(exp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            sb.append('\n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            this.out.write(sb.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            getExceptionListener().exceptionThrown(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    private void outputValue(Object value, Object outer, boolean isArgument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            writeln("<null/>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (value instanceof Class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            writeln("<class>" + ((Class)value).getName() + "</class>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        ValueData d = getValueData(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        if (d.exp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            Object target = d.exp.getTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            String methodName = d.exp.getMethodName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            if (target == null || methodName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                throw new NullPointerException((target == null ? "target" :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                                                "methodName") + " should not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            if (target instanceof Field && methodName.equals("get")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                Field f = (Field)target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                writeln("<object class=" + quote(f.getDeclaringClass().getName()) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                        " field=" + quote(f.getName()) + "/>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 5998
diff changeset
   607
            Class<?> primitiveType = ReflectionUtils.primitiveTypeFor(value.getClass());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            if (primitiveType != null && target == value.getClass() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                methodName.equals("new")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                String primitiveTypeName = primitiveType.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                // Make sure that character types are quoted correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                if (primitiveType == Character.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                    char code = ((Character) value).charValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    if (!isValidCharCode(code)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                        writeln(createString(code));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    value = quoteCharCode(code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                        value = Character.valueOf(code);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                writeln("<" + primitiveTypeName + ">" + value + "</" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                        primitiveTypeName + ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        } else if (value instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            writeln(createString((String) value));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        if (d.name != null) {
4969
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   634
            outputXML(isArgument ? "object" : "void", " idref=" + quote(d.name), value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
4969
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   636
        else if (d.exp != null) {
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   637
            outputStatement(d.exp, outer, isArgument);
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   638
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    private static String quoteCharCode(int code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        switch(code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
          case '&':  return "&amp;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
          case '<':  return "&lt;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
          case '>':  return "&gt;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
          case '"':  return "&quot;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
          case '\'': return "&apos;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
          case '\r': return "&#13;";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
          default:   return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    private static String createString(int code) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        return "<char code=\"#" + Integer.toString(code, 16) + "\"/>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    private String createString(String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        sb.append("<string>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        while (index < string.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            int point = string.codePointAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            int count = Character.charCount(point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            if (isValidCharCode(point) && this.encoder.canEncode(string.substring(index, index + count))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                String value = quoteCharCode(point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                    sb.append(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                    sb.appendCodePoint(point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                index += count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                sb.append(createString(string.charAt(index)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        sb.append("</string>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    private void outputStatement(Statement exp, Object outer, boolean isArgument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        Object target = exp.getTarget();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        String methodName = exp.getMethodName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        if (target == null || methodName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            throw new NullPointerException((target == null ? "target" :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                                            "methodName") + " should not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        Object[] args = exp.getArguments();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        boolean expression = exp.getClass() == Expression.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        Object value = (expression) ? getValue((Expression)exp) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        String tag = (expression && isArgument) ? "object" : "void";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        String attributes = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        ValueData d = getValueData(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        // Special cases for targets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        if (target == outer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        else if (target == Array.class && methodName.equals("newInstance")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            tag = "array";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            attributes = attributes + " class=" + quote(((Class)args[0]).getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            attributes = attributes + " length=" + quote(args[1].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            args = new Object[]{};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        else if (target.getClass() == Class.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            attributes = attributes + " class=" + quote(((Class)target).getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            d.refs = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            getValueData(target).refs++;
4969
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   714
            List<Statement> statements = statementList(target);
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   715
            if (!statements.contains(exp)) {
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   716
                statements.add(exp);
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   717
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            outputValue(target, outer, false);
5583
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   719
            if (expression) {
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   720
                outputValue(value, outer, isArgument);
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   721
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
4969
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   724
        if (expression && (d.refs > 1)) {
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   725
            String instanceName = nameGenerator.instanceName(value);
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   726
            d.name = instanceName;
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   727
            attributes = attributes + " id=" + quote(instanceName);
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   728
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        // Special cases for methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        if ((!expression && methodName.equals("set") && args.length == 2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
             args[0] instanceof Integer) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
             (expression && methodName.equals("get") && args.length == 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
              args[0] instanceof Integer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            attributes = attributes + " index=" + quote(args[0].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            args = (args.length == 1) ? new Object[]{} : new Object[]{args[1]};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        else if ((!expression && methodName.startsWith("set") && args.length == 1) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                 (expression && methodName.startsWith("get") && args.length == 0)) {
5583
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   740
            if (3 < methodName.length()) {
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   741
                attributes = attributes + " property=" +
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   742
                    quote(Introspector.decapitalize(methodName.substring(3)));
6cb5bb2e8335 6479191: LTP: XMLEncoder does not update initialized property of GridBagConstraints type
malenkov
parents: 4969
diff changeset
   743
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        else if (!methodName.equals("new") && !methodName.equals("newInstance")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            attributes = attributes + " method=" + quote(methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
4969
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   748
        outputXML(tag, attributes, value, args);
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   749
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
4969
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   751
    private void outputXML(String tag, String attributes, Object value, Object... args) {
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   752
        List<Statement> statements = statementList(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        // Use XML's short form when there is no body.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        if (args.length == 0 && statements.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            writeln("<" + tag + attributes + "/>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        writeln("<" + tag + attributes + ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        indentation++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        for(int i = 0; i < args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            outputValue(args[i], null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
4969
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   766
        while (!statements.isEmpty()) {
5a6e31d5ab6d 6921644: XMLEncoder generates invalid XML
malenkov
parents: 2
diff changeset
   767
            Statement s = statements.remove(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            outputStatement(s, value, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        indentation--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        writeln("</" + tag + ">");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
}