0N/A/*
2362N/A * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage java.beans;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport java.lang.reflect.*;
0N/Aimport java.nio.charset.Charset;
0N/Aimport java.nio.charset.CharsetEncoder;
0N/Aimport java.nio.charset.IllegalCharsetNameException;
0N/Aimport java.nio.charset.UnsupportedCharsetException;
0N/A
0N/A/**
0N/A * The <code>XMLEncoder</code> class is a complementary alternative to
0N/A * the <code>ObjectOutputStream</code> and can used to generate
0N/A * a textual representation of a <em>JavaBean</em> in the same
0N/A * way that the <code>ObjectOutputStream</code> can
0N/A * be used to create binary representation of <code>Serializable</code>
0N/A * objects. For example, the following fragment can be used to create
0N/A * a textual representation the supplied <em>JavaBean</em>
0N/A * and all its properties:
0N/A * <pre>
0N/A * XMLEncoder e = new XMLEncoder(
0N/A * new BufferedOutputStream(
0N/A * new FileOutputStream("Test.xml")));
0N/A * e.writeObject(new JButton("Hello, world"));
0N/A * e.close();
0N/A * </pre>
0N/A * Despite the similarity of their APIs, the <code>XMLEncoder</code>
0N/A * class is exclusively designed for the purpose of archiving graphs
0N/A * of <em>JavaBean</em>s as textual representations of their public
0N/A * properties. Like Java source files, documents written this way
0N/A * have a natural immunity to changes in the implementations of the classes
0N/A * involved. The <code>ObjectOutputStream</code> continues to be recommended
0N/A * for interprocess communication and general purpose serialization.
0N/A * <p>
0N/A * The <code>XMLEncoder</code> class provides a default denotation for
0N/A * <em>JavaBean</em>s in which they are represented as XML documents
0N/A * complying with version 1.0 of the XML specification and the
0N/A * UTF-8 character encoding of the Unicode/ISO 10646 character set.
0N/A * The XML documents produced by the <code>XMLEncoder</code> class are:
0N/A * <ul>
0N/A * <li>
0N/A * <em>Portable and version resilient</em>: they have no dependencies
0N/A * on the private implementation of any class and so, like Java source
0N/A * files, they may be exchanged between environments which may have
0N/A * different versions of some of the classes and between VMs from
0N/A * different vendors.
0N/A * <li>
0N/A * <em>Structurally compact</em>: The <code>XMLEncoder</code> class
0N/A * uses a <em>redundancy elimination</em> algorithm internally so that the
0N/A * default values of a Bean's properties are not written to the stream.
0N/A * <li>
0N/A * <em>Fault tolerant</em>: Non-structural errors in the file,
0N/A * caused either by damage to the file or by API changes
0N/A * made to classes in an archive remain localized
0N/A * so that a reader can report the error and continue to load the parts
0N/A * of the document which were not affected by the error.
0N/A * </ul>
0N/A * <p>
0N/A * Below is an example of an XML archive containing
0N/A * some user interface components from the <em>swing</em> toolkit:
0N/A * <pre>
0N/A * &lt;?xml version="1.0" encoding="UTF-8"?&gt;
0N/A * &lt;java version="1.0" class="java.beans.XMLDecoder"&gt;
0N/A * &lt;object class="javax.swing.JFrame"&gt;
0N/A * &lt;void property="name"&gt;
0N/A * &lt;string&gt;frame1&lt;/string&gt;
0N/A * &lt;/void&gt;
0N/A * &lt;void property="bounds"&gt;
0N/A * &lt;object class="java.awt.Rectangle"&gt;
0N/A * &lt;int&gt;0&lt;/int&gt;
0N/A * &lt;int&gt;0&lt;/int&gt;
0N/A * &lt;int&gt;200&lt;/int&gt;
0N/A * &lt;int&gt;200&lt;/int&gt;
0N/A * &lt;/object&gt;
0N/A * &lt;/void&gt;
0N/A * &lt;void property="contentPane"&gt;
0N/A * &lt;void method="add"&gt;
0N/A * &lt;object class="javax.swing.JButton"&gt;
0N/A * &lt;void property="label"&gt;
0N/A * &lt;string&gt;Hello&lt;/string&gt;
0N/A * &lt;/void&gt;
0N/A * &lt;/object&gt;
0N/A * &lt;/void&gt;
0N/A * &lt;/void&gt;
0N/A * &lt;void property="visible"&gt;
0N/A * &lt;boolean&gt;true&lt;/boolean&gt;
0N/A * &lt;/void&gt;
0N/A * &lt;/object&gt;
0N/A * &lt;/java&gt;
0N/A * </pre>
0N/A * The XML syntax uses the following conventions:
0N/A * <ul>
0N/A * <li>
0N/A * Each element represents a method call.
0N/A * <li>
0N/A * The "object" tag denotes an <em>expression</em> whose value is
0N/A * to be used as the argument to the enclosing element.
0N/A * <li>
0N/A * The "void" tag denotes a <em>statement</em> which will
0N/A * be executed, but whose result will not be used as an
0N/A * argument to the enclosing method.
0N/A * <li>
0N/A * Elements which contain elements use those elements as arguments,
0N/A * unless they have the tag: "void".
0N/A * <li>
0N/A * The name of the method is denoted by the "method" attribute.
0N/A * <li>
0N/A * XML's standard "id" and "idref" attributes are used to make
0N/A * references to previous expressions - so as to deal with
0N/A * circularities in the object graph.
0N/A * <li>
0N/A * The "class" attribute is used to specify the target of a static
0N/A * method or constructor explicitly; its value being the fully
0N/A * qualified name of the class.
0N/A * <li>
0N/A * Elements with the "void" tag are executed using
0N/A * the outer context as the target if no target is defined
0N/A * by a "class" attribute.
0N/A * <li>
0N/A * Java's String class is treated specially and is
0N/A * written &lt;string&gt;Hello, world&lt;/string&gt; where
0N/A * the characters of the string are converted to bytes
0N/A * using the UTF-8 character encoding.
0N/A * </ul>
0N/A * <p>
0N/A * Although all object graphs may be written using just these three
0N/A * tags, the following definitions are included so that common
0N/A * data structures can be expressed more concisely:
0N/A * <p>
0N/A * <ul>
0N/A * <li>
0N/A * The default method name is "new".
0N/A * <li>
0N/A * A reference to a java class is written in the form
0N/A * &lt;class&gt;javax.swing.JButton&lt;/class&gt;.
0N/A * <li>
0N/A * Instances of the wrapper classes for Java's primitive types are written
0N/A * using the name of the primitive type as the tag. For example, an
0N/A * instance of the <code>Integer</code> class could be written:
0N/A * &lt;int&gt;123&lt;/int&gt;. Note that the <code>XMLEncoder</code> class
0N/A * uses Java's reflection package in which the conversion between
0N/A * Java's primitive types and their associated "wrapper classes"
0N/A * is handled internally. The API for the <code>XMLEncoder</code> class
0N/A * itself deals only with <code>Object</code>s.
0N/A * <li>
0N/A * In an element representing a nullary method whose name
0N/A * starts with "get", the "method" attribute is replaced
0N/A * with a "property" attribute whose value is given by removing
0N/A * the "get" prefix and decapitalizing the result.
0N/A * <li>
0N/A * In an element representing a monadic method whose name
0N/A * starts with "set", the "method" attribute is replaced
0N/A * with a "property" attribute whose value is given by removing
0N/A * the "set" prefix and decapitalizing the result.
0N/A * <li>
0N/A * In an element representing a method named "get" taking one
0N/A * integer argument, the "method" attribute is replaced
0N/A * with an "index" attribute whose value the value of the
0N/A * first argument.
0N/A * <li>
0N/A * In an element representing a method named "set" taking two arguments,
0N/A * the first of which is an integer, the "method" attribute is replaced
0N/A * with an "index" attribute whose value the value of the
0N/A * first argument.
0N/A * <li>
0N/A * A reference to an array is written using the "array"
0N/A * tag. The "class" and "length" attributes specify the
0N/A * sub-type of the array and its length respectively.
0N/A * </ul>
0N/A *
0N/A *<p>
0N/A * For more information you might also want to check out
0N/A * <a
0N/A href="http://java.sun.com/products/jfc/tsc/articles/persistence4">Using XMLEncoder</a>,
0N/A * an article in <em>The Swing Connection.</em>
0N/A * @see XMLDecoder
0N/A * @see java.io.ObjectOutputStream
0N/A *
0N/A * @since 1.4
0N/A *
0N/A * @author Philip Milne
0N/A */
2574N/Apublic class XMLEncoder extends Encoder implements AutoCloseable {
0N/A
0N/A private final CharsetEncoder encoder;
0N/A private final String charset;
0N/A private final boolean declaration;
0N/A
0N/A private OutputStreamWriter out;
0N/A private Object owner;
0N/A private int indentation = 0;
0N/A private boolean internal = false;
2181N/A private Map<Object, ValueData> valueToExpression;
2181N/A private Map<Object, List<Statement>> targetToStatementList;
0N/A private boolean preambleWritten = false;
0N/A private NameGenerator nameGenerator;
0N/A
0N/A private class ValueData {
0N/A public int refs = 0;
0N/A public boolean marked = false; // Marked -> refs > 0 unless ref was a target.
0N/A public String name = null;
0N/A public Expression exp = null;
0N/A }
0N/A
0N/A /**
0N/A * Creates a new XML encoder to write out <em>JavaBeans</em>
0N/A * to the stream <code>out</code> using an XML encoding.
0N/A *
0N/A * @param out the stream to which the XML representation of
0N/A * the objects will be written
0N/A *
0N/A * @throws IllegalArgumentException
0N/A * if <code>out</code> is <code>null</code>
0N/A *
0N/A * @see XMLDecoder#XMLDecoder(InputStream)
0N/A */
0N/A public XMLEncoder(OutputStream out) {
0N/A this(out, "UTF-8", true, 0);
0N/A }
0N/A
0N/A /**
0N/A * Creates a new XML encoder to write out <em>JavaBeans</em>
0N/A * to the stream <code>out</code> using the given <code>charset</code>
0N/A * starting from the given <code>indentation</code>.
0N/A *
0N/A * @param out the stream to which the XML representation of
0N/A * the objects will be written
0N/A * @param charset the name of the requested charset;
0N/A * may be either a canonical name or an alias
0N/A * @param declaration whether the XML declaration should be generated;
0N/A * set this to <code>false</code>
0N/A * when embedding the contents in another XML document
0N/A * @param indentation the number of space characters to indent the entire XML document by
0N/A *
0N/A * @throws IllegalArgumentException
0N/A * if <code>out</code> or <code>charset</code> is <code>null</code>,
0N/A * or if <code>indentation</code> is less than 0
0N/A *
0N/A * @throws IllegalCharsetNameException
0N/A * if <code>charset</code> name is illegal
0N/A *
0N/A * @throws UnsupportedCharsetException
0N/A * if no support for the named charset is available
0N/A * in this instance of the Java virtual machine
0N/A *
0N/A * @throws UnsupportedOperationException
0N/A * if loaded charset does not support encoding
0N/A *
0N/A * @see Charset#forName(String)
0N/A *
0N/A * @since 1.7
0N/A */
0N/A public XMLEncoder(OutputStream out, String charset, boolean declaration, int indentation) {
0N/A if (out == null) {
0N/A throw new IllegalArgumentException("the output stream cannot be null");
0N/A }
0N/A if (indentation < 0) {
0N/A throw new IllegalArgumentException("the indentation must be >= 0");
0N/A }
0N/A Charset cs = Charset.forName(charset);
0N/A this.encoder = cs.newEncoder();
0N/A this.charset = charset;
0N/A this.declaration = declaration;
0N/A this.indentation = indentation;
0N/A this.out = new OutputStreamWriter(out, cs.newEncoder());
2181N/A valueToExpression = new IdentityHashMap<Object, ValueData>();
2181N/A targetToStatementList = new IdentityHashMap<Object, List<Statement>>();
0N/A nameGenerator = new NameGenerator();
0N/A }
0N/A
0N/A /**
0N/A * Sets the owner of this encoder to <code>owner</code>.
0N/A *
0N/A * @param owner The owner of this encoder.
0N/A *
0N/A * @see #getOwner
0N/A */
0N/A public void setOwner(Object owner) {
0N/A this.owner = owner;
0N/A writeExpression(new Expression(this, "getOwner", new Object[0]));
0N/A }
0N/A
0N/A /**
0N/A * Gets the owner of this encoder.
0N/A *
0N/A * @return The owner of this encoder.
0N/A *
0N/A * @see #setOwner
0N/A */
0N/A public Object getOwner() {
0N/A return owner;
0N/A }
0N/A
0N/A /**
0N/A * Write an XML representation of the specified object to the output.
0N/A *
0N/A * @param o The object to be written to the stream.
0N/A *
0N/A * @see XMLDecoder#readObject
0N/A */
0N/A public void writeObject(Object o) {
0N/A if (internal) {
0N/A super.writeObject(o);
0N/A }
0N/A else {
0N/A writeStatement(new Statement(this, "writeObject", new Object[]{o}));
0N/A }
0N/A }
0N/A
2181N/A private List<Statement> statementList(Object target) {
2181N/A List<Statement> list = targetToStatementList.get(target);
2181N/A if (list == null) {
2181N/A list = new ArrayList<Statement>();
2181N/A targetToStatementList.put(target, list);
0N/A }
0N/A return list;
0N/A }
0N/A
0N/A
0N/A private void mark(Object o, boolean isArgument) {
0N/A if (o == null || o == this) {
0N/A return;
0N/A }
0N/A ValueData d = getValueData(o);
0N/A Expression exp = d.exp;
0N/A // Do not mark liternal strings. Other strings, which might,
0N/A // for example, come from resource bundles should still be marked.
0N/A if (o.getClass() == String.class && exp == null) {
0N/A return;
0N/A }
0N/A
0N/A // Bump the reference counts of all arguments
0N/A if (isArgument) {
0N/A d.refs++;
0N/A }
0N/A if (d.marked) {
0N/A return;
0N/A }
0N/A d.marked = true;
0N/A Object target = exp.getTarget();
2181N/A mark(exp);
0N/A if (!(target instanceof Class)) {
0N/A statementList(target).add(exp);
0N/A // Pending: Why does the reference count need to
0N/A // be incremented here?
0N/A d.refs++;
0N/A }
0N/A }
0N/A
0N/A private void mark(Statement stm) {
0N/A Object[] args = stm.getArguments();
0N/A for (int i = 0; i < args.length; i++) {
0N/A Object arg = args[i];
0N/A mark(arg, true);
0N/A }
0N/A mark(stm.getTarget(), false);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Records the Statement so that the Encoder will
0N/A * produce the actual output when the stream is flushed.
0N/A * <P>
0N/A * This method should only be invoked within the context
0N/A * of initializing a persistence delegate.
0N/A *
0N/A * @param oldStm The statement that will be written
0N/A * to the stream.
0N/A * @see java.beans.PersistenceDelegate#initialize
0N/A */
0N/A public void writeStatement(Statement oldStm) {
0N/A // System.out.println("XMLEncoder::writeStatement: " + oldStm);
0N/A boolean internal = this.internal;
0N/A this.internal = true;
0N/A try {
0N/A super.writeStatement(oldStm);
0N/A /*
0N/A Note we must do the mark first as we may
0N/A require the results of previous values in
0N/A this context for this statement.
0N/A Test case is:
0N/A os.setOwner(this);
0N/A os.writeObject(this);
0N/A */
0N/A mark(oldStm);
2374N/A Object target = oldStm.getTarget();
2374N/A if (target instanceof Field) {
2374N/A String method = oldStm.getMethodName();
2374N/A Object[] args = oldStm.getArguments();
2374N/A if ((method == null) || (args == null)) {
2374N/A }
2374N/A else if (method.equals("get") && (args.length == 1)) {
2374N/A target = args[0];
2374N/A }
2374N/A else if (method.equals("set") && (args.length == 2)) {
2374N/A target = args[0];
2374N/A }
2374N/A }
2374N/A statementList(target).add(oldStm);
0N/A }
0N/A catch (Exception e) {
0N/A getExceptionListener().exceptionThrown(new Exception("XMLEncoder: discarding statement " + oldStm, e));
0N/A }
0N/A this.internal = internal;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Records the Expression so that the Encoder will
0N/A * produce the actual output when the stream is flushed.
0N/A * <P>
0N/A * This method should only be invoked within the context of
0N/A * initializing a persistence delegate or setting up an encoder to
0N/A * read from a resource bundle.
0N/A * <P>
0N/A * For more information about using resource bundles with the
0N/A * XMLEncoder, see
0N/A * http://java.sun.com/products/jfc/tsc/articles/persistence4/#i18n
0N/A *
0N/A * @param oldExp The expression that will be written
0N/A * to the stream.
0N/A * @see java.beans.PersistenceDelegate#initialize
0N/A */
0N/A public void writeExpression(Expression oldExp) {
0N/A boolean internal = this.internal;
0N/A this.internal = true;
0N/A Object oldValue = getValue(oldExp);
0N/A if (get(oldValue) == null || (oldValue instanceof String && !internal)) {
0N/A getValueData(oldValue).exp = oldExp;
0N/A super.writeExpression(oldExp);
0N/A }
0N/A this.internal = internal;
0N/A }
0N/A
0N/A /**
0N/A * This method writes out the preamble associated with the
0N/A * XML encoding if it has not been written already and
0N/A * then writes out all of the values that been
0N/A * written to the stream since the last time <code>flush</code>
0N/A * was called. After flushing, all internal references to the
0N/A * values that were written to this stream are cleared.
0N/A */
0N/A public void flush() {
0N/A if (!preambleWritten) { // Don't do this in constructor - it throws ... pending.
0N/A if (this.declaration) {
0N/A writeln("<?xml version=" + quote("1.0") +
0N/A " encoding=" + quote(this.charset) + "?>");
0N/A }
0N/A writeln("<java version=" + quote(System.getProperty("java.version")) +
0N/A " class=" + quote(XMLDecoder.class.getName()) + ">");
0N/A preambleWritten = true;
0N/A }
0N/A indentation++;
2181N/A List<Statement> statements = statementList(this);
2181N/A while (!statements.isEmpty()) {
2181N/A Statement s = statements.remove(0);
0N/A if ("writeObject".equals(s.getMethodName())) {
0N/A outputValue(s.getArguments()[0], this, true);
0N/A }
0N/A else {
0N/A outputStatement(s, this, false);
0N/A }
0N/A }
0N/A indentation--;
0N/A
6125N/A Statement statement = getMissedStatement();
6125N/A while (statement != null) {
6125N/A outputStatement(statement, this, false);
6125N/A statement = getMissedStatement();
6125N/A }
6125N/A
0N/A try {
0N/A out.flush();
0N/A }
0N/A catch (IOException e) {
0N/A getExceptionListener().exceptionThrown(e);
0N/A }
0N/A clear();
0N/A }
0N/A
0N/A void clear() {
0N/A super.clear();
0N/A nameGenerator.clear();
0N/A valueToExpression.clear();
0N/A targetToStatementList.clear();
0N/A }
0N/A
6125N/A Statement getMissedStatement() {
6125N/A for (List<Statement> statements : this.targetToStatementList.values()) {
6125N/A for (int i = 0; i < statements.size(); i++) {
6125N/A if (Statement.class == statements.get(i).getClass()) {
6125N/A return statements.remove(i);
6125N/A }
6125N/A }
6125N/A }
6125N/A return null;
6125N/A }
6125N/A
0N/A
0N/A /**
0N/A * This method calls <code>flush</code>, writes the closing
0N/A * postamble and then closes the output stream associated
0N/A * with this stream.
0N/A */
0N/A public void close() {
0N/A flush();
0N/A writeln("</java>");
0N/A try {
0N/A out.close();
0N/A }
0N/A catch (IOException e) {
0N/A getExceptionListener().exceptionThrown(e);
0N/A }
0N/A }
0N/A
0N/A private String quote(String s) {
0N/A return "\"" + s + "\"";
0N/A }
0N/A
0N/A private ValueData getValueData(Object o) {
2181N/A ValueData d = valueToExpression.get(o);
0N/A if (d == null) {
0N/A d = new ValueData();
0N/A valueToExpression.put(o, d);
0N/A }
0N/A return d;
0N/A }
0N/A
0N/A /**
0N/A * Returns <code>true</code> if the argument,
0N/A * a Unicode code point, is valid in XML documents.
0N/A * Unicode characters fit into the low sixteen bits of a Unicode code point,
0N/A * and pairs of Unicode <em>surrogate characters</em> can be combined
0N/A * to encode Unicode code point in documents containing only Unicode.
0N/A * (The <code>char</code> datatype in the Java Programming Language
0N/A * represents Unicode characters, including unpaired surrogates.)
0N/A * <par>
0N/A * [2] Char ::= #x0009 | #x000A | #x000D
0N/A * | [#x0020-#xD7FF]
0N/A * | [#xE000-#xFFFD]
0N/A * | [#x10000-#x10ffff]
0N/A * </par>
0N/A *
0N/A * @param code the 32-bit Unicode code point being tested
0N/A * @return <code>true</code> if the Unicode code point is valid,
0N/A * <code>false</code> otherwise
0N/A */
0N/A private static boolean isValidCharCode(int code) {
0N/A return (0x0020 <= code && code <= 0xD7FF)
0N/A || (0x000A == code)
0N/A || (0x0009 == code)
0N/A || (0x000D == code)
0N/A || (0xE000 <= code && code <= 0xFFFD)
0N/A || (0x10000 <= code && code <= 0x10ffff);
0N/A }
0N/A
0N/A private void writeln(String exp) {
0N/A try {
0N/A StringBuilder sb = new StringBuilder();
0N/A for(int i = 0; i < indentation; i++) {
0N/A sb.append(' ');
0N/A }
0N/A sb.append(exp);
0N/A sb.append('\n');
0N/A this.out.write(sb.toString());
0N/A }
0N/A catch (IOException e) {
0N/A getExceptionListener().exceptionThrown(e);
0N/A }
0N/A }
0N/A
0N/A private void outputValue(Object value, Object outer, boolean isArgument) {
0N/A if (value == null) {
0N/A writeln("<null/>");
0N/A return;
0N/A }
0N/A
0N/A if (value instanceof Class) {
0N/A writeln("<class>" + ((Class)value).getName() + "</class>");
0N/A return;
0N/A }
0N/A
0N/A ValueData d = getValueData(value);
0N/A if (d.exp != null) {
0N/A Object target = d.exp.getTarget();
0N/A String methodName = d.exp.getMethodName();
0N/A
0N/A if (target == null || methodName == null) {
0N/A throw new NullPointerException((target == null ? "target" :
0N/A "methodName") + " should not be null");
0N/A }
0N/A
6125N/A if (isArgument && target instanceof Field && methodName.equals("get")) {
0N/A Field f = (Field)target;
0N/A writeln("<object class=" + quote(f.getDeclaringClass().getName()) +
0N/A " field=" + quote(f.getName()) + "/>");
0N/A return;
0N/A }
0N/A
6051N/A Class primitiveType = primitiveTypeFor(value.getClass());
0N/A if (primitiveType != null && target == value.getClass() &&
0N/A methodName.equals("new")) {
0N/A String primitiveTypeName = primitiveType.getName();
0N/A // Make sure that character types are quoted correctly.
0N/A if (primitiveType == Character.TYPE) {
0N/A char code = ((Character) value).charValue();
0N/A if (!isValidCharCode(code)) {
0N/A writeln(createString(code));
0N/A return;
0N/A }
0N/A value = quoteCharCode(code);
0N/A if (value == null) {
0N/A value = Character.valueOf(code);
0N/A }
0N/A }
0N/A writeln("<" + primitiveTypeName + ">" + value + "</" +
0N/A primitiveTypeName + ">");
0N/A return;
0N/A }
0N/A
0N/A } else if (value instanceof String) {
0N/A writeln(createString((String) value));
0N/A return;
0N/A }
0N/A
0N/A if (d.name != null) {
5308N/A if (isArgument) {
5308N/A writeln("<object idref=" + quote(d.name) + "/>");
5308N/A }
5308N/A else {
5308N/A outputXML("void", " idref=" + quote(d.name), value);
5308N/A }
0N/A }
2181N/A else if (d.exp != null) {
2181N/A outputStatement(d.exp, outer, isArgument);
2181N/A }
0N/A }
0N/A
0N/A private static String quoteCharCode(int code) {
0N/A switch(code) {
0N/A case '&': return "&amp;";
0N/A case '<': return "&lt;";
0N/A case '>': return "&gt;";
0N/A case '"': return "&quot;";
0N/A case '\'': return "&apos;";
0N/A case '\r': return "&#13;";
0N/A default: return null;
0N/A }
0N/A }
0N/A
0N/A private static String createString(int code) {
0N/A return "<char code=\"#" + Integer.toString(code, 16) + "\"/>";
0N/A }
0N/A
0N/A private String createString(String string) {
0N/A StringBuilder sb = new StringBuilder();
0N/A sb.append("<string>");
0N/A int index = 0;
0N/A while (index < string.length()) {
0N/A int point = string.codePointAt(index);
0N/A int count = Character.charCount(point);
0N/A
0N/A if (isValidCharCode(point) && this.encoder.canEncode(string.substring(index, index + count))) {
0N/A String value = quoteCharCode(point);
0N/A if (value != null) {
0N/A sb.append(value);
0N/A } else {
0N/A sb.appendCodePoint(point);
0N/A }
0N/A index += count;
0N/A } else {
0N/A sb.append(createString(string.charAt(index)));
0N/A index++;
0N/A }
0N/A }
0N/A sb.append("</string>");
0N/A return sb.toString();
0N/A }
0N/A
0N/A private void outputStatement(Statement exp, Object outer, boolean isArgument) {
0N/A Object target = exp.getTarget();
0N/A String methodName = exp.getMethodName();
0N/A
0N/A if (target == null || methodName == null) {
0N/A throw new NullPointerException((target == null ? "target" :
0N/A "methodName") + " should not be null");
0N/A }
0N/A
0N/A Object[] args = exp.getArguments();
0N/A boolean expression = exp.getClass() == Expression.class;
0N/A Object value = (expression) ? getValue((Expression)exp) : null;
0N/A
0N/A String tag = (expression && isArgument) ? "object" : "void";
0N/A String attributes = "";
0N/A ValueData d = getValueData(value);
0N/A
0N/A // Special cases for targets.
0N/A if (target == outer) {
0N/A }
0N/A else if (target == Array.class && methodName.equals("newInstance")) {
0N/A tag = "array";
0N/A attributes = attributes + " class=" + quote(((Class)args[0]).getName());
0N/A attributes = attributes + " length=" + quote(args[1].toString());
0N/A args = new Object[]{};
0N/A }
0N/A else if (target.getClass() == Class.class) {
0N/A attributes = attributes + " class=" + quote(((Class)target).getName());
0N/A }
0N/A else {
0N/A d.refs = 2;
5308N/A if (d.name == null) {
5308N/A getValueData(target).refs++;
5308N/A List<Statement> statements = statementList(target);
5308N/A if (!statements.contains(exp)) {
5308N/A statements.add(exp);
5308N/A }
5308N/A outputValue(target, outer, false);
2181N/A }
2374N/A if (expression) {
2374N/A outputValue(value, outer, isArgument);
2374N/A }
0N/A return;
0N/A }
2181N/A if (expression && (d.refs > 1)) {
2181N/A String instanceName = nameGenerator.instanceName(value);
2181N/A d.name = instanceName;
2181N/A attributes = attributes + " id=" + quote(instanceName);
2181N/A }
0N/A
0N/A // Special cases for methods.
0N/A if ((!expression && methodName.equals("set") && args.length == 2 &&
0N/A args[0] instanceof Integer) ||
0N/A (expression && methodName.equals("get") && args.length == 1 &&
0N/A args[0] instanceof Integer)) {
0N/A attributes = attributes + " index=" + quote(args[0].toString());
0N/A args = (args.length == 1) ? new Object[]{} : new Object[]{args[1]};
0N/A }
0N/A else if ((!expression && methodName.startsWith("set") && args.length == 1) ||
0N/A (expression && methodName.startsWith("get") && args.length == 0)) {
2374N/A if (3 < methodName.length()) {
2374N/A attributes = attributes + " property=" +
2374N/A quote(Introspector.decapitalize(methodName.substring(3)));
2374N/A }
0N/A }
0N/A else if (!methodName.equals("new") && !methodName.equals("newInstance")) {
0N/A attributes = attributes + " method=" + quote(methodName);
0N/A }
2181N/A outputXML(tag, attributes, value, args);
2181N/A }
0N/A
2181N/A private void outputXML(String tag, String attributes, Object value, Object... args) {
2181N/A List<Statement> statements = statementList(value);
0N/A // Use XML's short form when there is no body.
0N/A if (args.length == 0 && statements.size() == 0) {
0N/A writeln("<" + tag + attributes + "/>");
0N/A return;
0N/A }
0N/A
0N/A writeln("<" + tag + attributes + ">");
0N/A indentation++;
0N/A
0N/A for(int i = 0; i < args.length; i++) {
0N/A outputValue(args[i], null, true);
0N/A }
0N/A
2181N/A while (!statements.isEmpty()) {
2181N/A Statement s = statements.remove(0);
0N/A outputStatement(s, value, false);
0N/A }
0N/A
0N/A indentation--;
0N/A writeln("</" + tag + ">");
0N/A }
6051N/A
6051N/A @SuppressWarnings("rawtypes")
6051N/A static Class primitiveTypeFor(Class wrapper) {
6051N/A if (wrapper == Boolean.class) return Boolean.TYPE;
6051N/A if (wrapper == Byte.class) return Byte.TYPE;
6051N/A if (wrapper == Character.class) return Character.TYPE;
6051N/A if (wrapper == Short.class) return Short.TYPE;
6051N/A if (wrapper == Integer.class) return Integer.TYPE;
6051N/A if (wrapper == Long.class) return Long.TYPE;
6051N/A if (wrapper == Float.class) return Float.TYPE;
6051N/A if (wrapper == Double.class) return Double.TYPE;
6051N/A if (wrapper == Void.class) return Void.TYPE;
6051N/A return null;
6051N/A }
0N/A}