Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/beans/XMLEncoder.java
38829 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package java.beans;2526import java.io.*;27import java.util.*;28import java.lang.reflect.*;29import java.nio.charset.Charset;30import java.nio.charset.CharsetEncoder;31import java.nio.charset.IllegalCharsetNameException;32import java.nio.charset.UnsupportedCharsetException;3334/**35* The <code>XMLEncoder</code> class is a complementary alternative to36* the <code>ObjectOutputStream</code> and can used to generate37* a textual representation of a <em>JavaBean</em> in the same38* way that the <code>ObjectOutputStream</code> can39* be used to create binary representation of <code>Serializable</code>40* objects. For example, the following fragment can be used to create41* a textual representation the supplied <em>JavaBean</em>42* and all its properties:43* <pre>44* XMLEncoder e = new XMLEncoder(45* new BufferedOutputStream(46* new FileOutputStream("Test.xml")));47* e.writeObject(new JButton("Hello, world"));48* e.close();49* </pre>50* Despite the similarity of their APIs, the <code>XMLEncoder</code>51* class is exclusively designed for the purpose of archiving graphs52* of <em>JavaBean</em>s as textual representations of their public53* properties. Like Java source files, documents written this way54* have a natural immunity to changes in the implementations of the classes55* involved. The <code>ObjectOutputStream</code> continues to be recommended56* for interprocess communication and general purpose serialization.57* <p>58* The <code>XMLEncoder</code> class provides a default denotation for59* <em>JavaBean</em>s in which they are represented as XML documents60* complying with version 1.0 of the XML specification and the61* UTF-8 character encoding of the Unicode/ISO 10646 character set.62* The XML documents produced by the <code>XMLEncoder</code> class are:63* <ul>64* <li>65* <em>Portable and version resilient</em>: they have no dependencies66* on the private implementation of any class and so, like Java source67* files, they may be exchanged between environments which may have68* different versions of some of the classes and between VMs from69* different vendors.70* <li>71* <em>Structurally compact</em>: The <code>XMLEncoder</code> class72* uses a <em>redundancy elimination</em> algorithm internally so that the73* default values of a Bean's properties are not written to the stream.74* <li>75* <em>Fault tolerant</em>: Non-structural errors in the file,76* caused either by damage to the file or by API changes77* made to classes in an archive remain localized78* so that a reader can report the error and continue to load the parts79* of the document which were not affected by the error.80* </ul>81* <p>82* Below is an example of an XML archive containing83* some user interface components from the <em>swing</em> toolkit:84* <pre>85* <?xml version="1.0" encoding="UTF-8"?>86* <java version="1.0" class="java.beans.XMLDecoder">87* <object class="javax.swing.JFrame">88* <void property="name">89* <string>frame1</string>90* </void>91* <void property="bounds">92* <object class="java.awt.Rectangle">93* <int>0</int>94* <int>0</int>95* <int>200</int>96* <int>200</int>97* </object>98* </void>99* <void property="contentPane">100* <void method="add">101* <object class="javax.swing.JButton">102* <void property="label">103* <string>Hello</string>104* </void>105* </object>106* </void>107* </void>108* <void property="visible">109* <boolean>true</boolean>110* </void>111* </object>112* </java>113* </pre>114* The XML syntax uses the following conventions:115* <ul>116* <li>117* Each element represents a method call.118* <li>119* The "object" tag denotes an <em>expression</em> whose value is120* to be used as the argument to the enclosing element.121* <li>122* The "void" tag denotes a <em>statement</em> which will123* be executed, but whose result will not be used as an124* argument to the enclosing method.125* <li>126* Elements which contain elements use those elements as arguments,127* unless they have the tag: "void".128* <li>129* The name of the method is denoted by the "method" attribute.130* <li>131* XML's standard "id" and "idref" attributes are used to make132* references to previous expressions - so as to deal with133* circularities in the object graph.134* <li>135* The "class" attribute is used to specify the target of a static136* method or constructor explicitly; its value being the fully137* qualified name of the class.138* <li>139* Elements with the "void" tag are executed using140* the outer context as the target if no target is defined141* by a "class" attribute.142* <li>143* Java's String class is treated specially and is144* written <string>Hello, world</string> where145* the characters of the string are converted to bytes146* using the UTF-8 character encoding.147* </ul>148* <p>149* Although all object graphs may be written using just these three150* tags, the following definitions are included so that common151* data structures can be expressed more concisely:152* <p>153* <ul>154* <li>155* The default method name is "new".156* <li>157* A reference to a java class is written in the form158* <class>javax.swing.JButton</class>.159* <li>160* Instances of the wrapper classes for Java's primitive types are written161* using the name of the primitive type as the tag. For example, an162* instance of the <code>Integer</code> class could be written:163* <int>123</int>. Note that the <code>XMLEncoder</code> class164* uses Java's reflection package in which the conversion between165* Java's primitive types and their associated "wrapper classes"166* is handled internally. The API for the <code>XMLEncoder</code> class167* itself deals only with <code>Object</code>s.168* <li>169* In an element representing a nullary method whose name170* starts with "get", the "method" attribute is replaced171* with a "property" attribute whose value is given by removing172* the "get" prefix and decapitalizing the result.173* <li>174* In an element representing a monadic method whose name175* starts with "set", the "method" attribute is replaced176* with a "property" attribute whose value is given by removing177* the "set" prefix and decapitalizing the result.178* <li>179* In an element representing a method named "get" taking one180* integer argument, the "method" attribute is replaced181* with an "index" attribute whose value the value of the182* first argument.183* <li>184* In an element representing a method named "set" taking two arguments,185* the first of which is an integer, the "method" attribute is replaced186* with an "index" attribute whose value the value of the187* first argument.188* <li>189* A reference to an array is written using the "array"190* tag. The "class" and "length" attributes specify the191* sub-type of the array and its length respectively.192* </ul>193*194*<p>195* For more information you might also want to check out196* <a197href="http://java.sun.com/products/jfc/tsc/articles/persistence4">Using XMLEncoder</a>,198* an article in <em>The Swing Connection.</em>199* @see XMLDecoder200* @see java.io.ObjectOutputStream201*202* @since 1.4203*204* @author Philip Milne205*/206public class XMLEncoder extends Encoder implements AutoCloseable {207208private final CharsetEncoder encoder;209private final String charset;210private final boolean declaration;211212private OutputStreamWriter out;213private Object owner;214private int indentation = 0;215private boolean internal = false;216private Map<Object, ValueData> valueToExpression;217private Map<Object, List<Statement>> targetToStatementList;218private boolean preambleWritten = false;219private NameGenerator nameGenerator;220221private class ValueData {222public int refs = 0;223public boolean marked = false; // Marked -> refs > 0 unless ref was a target.224public String name = null;225public Expression exp = null;226}227228/**229* Creates a new XML encoder to write out <em>JavaBeans</em>230* to the stream <code>out</code> using an XML encoding.231*232* @param out the stream to which the XML representation of233* the objects will be written234*235* @throws IllegalArgumentException236* if <code>out</code> is <code>null</code>237*238* @see XMLDecoder#XMLDecoder(InputStream)239*/240public XMLEncoder(OutputStream out) {241this(out, "UTF-8", true, 0);242}243244/**245* Creates a new XML encoder to write out <em>JavaBeans</em>246* to the stream <code>out</code> using the given <code>charset</code>247* starting from the given <code>indentation</code>.248*249* @param out the stream to which the XML representation of250* the objects will be written251* @param charset the name of the requested charset;252* may be either a canonical name or an alias253* @param declaration whether the XML declaration should be generated;254* set this to <code>false</code>255* when embedding the contents in another XML document256* @param indentation the number of space characters to indent the entire XML document by257*258* @throws IllegalArgumentException259* if <code>out</code> or <code>charset</code> is <code>null</code>,260* or if <code>indentation</code> is less than 0261*262* @throws IllegalCharsetNameException263* if <code>charset</code> name is illegal264*265* @throws UnsupportedCharsetException266* if no support for the named charset is available267* in this instance of the Java virtual machine268*269* @throws UnsupportedOperationException270* if loaded charset does not support encoding271*272* @see Charset#forName(String)273*274* @since 1.7275*/276public XMLEncoder(OutputStream out, String charset, boolean declaration, int indentation) {277if (out == null) {278throw new IllegalArgumentException("the output stream cannot be null");279}280if (indentation < 0) {281throw new IllegalArgumentException("the indentation must be >= 0");282}283Charset cs = Charset.forName(charset);284this.encoder = cs.newEncoder();285this.charset = charset;286this.declaration = declaration;287this.indentation = indentation;288this.out = new OutputStreamWriter(out, cs.newEncoder());289valueToExpression = new IdentityHashMap<>();290targetToStatementList = new IdentityHashMap<>();291nameGenerator = new NameGenerator();292}293294/**295* Sets the owner of this encoder to <code>owner</code>.296*297* @param owner The owner of this encoder.298*299* @see #getOwner300*/301public void setOwner(Object owner) {302this.owner = owner;303writeExpression(new Expression(this, "getOwner", new Object[0]));304}305306/**307* Gets the owner of this encoder.308*309* @return The owner of this encoder.310*311* @see #setOwner312*/313public Object getOwner() {314return owner;315}316317/**318* Write an XML representation of the specified object to the output.319*320* @param o The object to be written to the stream.321*322* @see XMLDecoder#readObject323*/324public void writeObject(Object o) {325if (internal) {326super.writeObject(o);327}328else {329writeStatement(new Statement(this, "writeObject", new Object[]{o}));330}331}332333private List<Statement> statementList(Object target) {334List<Statement> list = targetToStatementList.get(target);335if (list == null) {336list = new ArrayList<>();337targetToStatementList.put(target, list);338}339return list;340}341342343private void mark(Object o, boolean isArgument) {344if (o == null || o == this) {345return;346}347ValueData d = getValueData(o);348Expression exp = d.exp;349// Do not mark liternal strings. Other strings, which might,350// for example, come from resource bundles should still be marked.351if (o.getClass() == String.class && exp == null) {352return;353}354355// Bump the reference counts of all arguments356if (isArgument) {357d.refs++;358}359if (d.marked) {360return;361}362d.marked = true;363Object target = exp.getTarget();364mark(exp);365if (!(target instanceof Class)) {366statementList(target).add(exp);367// Pending: Why does the reference count need to368// be incremented here?369d.refs++;370}371}372373private void mark(Statement stm) {374Object[] args = stm.getArguments();375for (int i = 0; i < args.length; i++) {376Object arg = args[i];377mark(arg, true);378}379mark(stm.getTarget(), stm instanceof Expression);380}381382383/**384* Records the Statement so that the Encoder will385* produce the actual output when the stream is flushed.386* <P>387* This method should only be invoked within the context388* of initializing a persistence delegate.389*390* @param oldStm The statement that will be written391* to the stream.392* @see java.beans.PersistenceDelegate#initialize393*/394public void writeStatement(Statement oldStm) {395// System.out.println("XMLEncoder::writeStatement: " + oldStm);396boolean internal = this.internal;397this.internal = true;398try {399super.writeStatement(oldStm);400/*401Note we must do the mark first as we may402require the results of previous values in403this context for this statement.404Test case is:405os.setOwner(this);406os.writeObject(this);407*/408mark(oldStm);409Object target = oldStm.getTarget();410if (target instanceof Field) {411String method = oldStm.getMethodName();412Object[] args = oldStm.getArguments();413if ((method == null) || (args == null)) {414}415else if (method.equals("get") && (args.length == 1)) {416target = args[0];417}418else if (method.equals("set") && (args.length == 2)) {419target = args[0];420}421}422statementList(target).add(oldStm);423}424catch (Exception e) {425getExceptionListener().exceptionThrown(new Exception("XMLEncoder: discarding statement " + oldStm, e));426}427this.internal = internal;428}429430431/**432* Records the Expression so that the Encoder will433* produce the actual output when the stream is flushed.434* <P>435* This method should only be invoked within the context of436* initializing a persistence delegate or setting up an encoder to437* read from a resource bundle.438* <P>439* For more information about using resource bundles with the440* XMLEncoder, see441* http://java.sun.com/products/jfc/tsc/articles/persistence4/#i18n442*443* @param oldExp The expression that will be written444* to the stream.445* @see java.beans.PersistenceDelegate#initialize446*/447public void writeExpression(Expression oldExp) {448boolean internal = this.internal;449this.internal = true;450Object oldValue = getValue(oldExp);451if (get(oldValue) == null || (oldValue instanceof String && !internal)) {452getValueData(oldValue).exp = oldExp;453super.writeExpression(oldExp);454}455this.internal = internal;456}457458/**459* This method writes out the preamble associated with the460* XML encoding if it has not been written already and461* then writes out all of the values that been462* written to the stream since the last time <code>flush</code>463* was called. After flushing, all internal references to the464* values that were written to this stream are cleared.465*/466public void flush() {467if (!preambleWritten) { // Don't do this in constructor - it throws ... pending.468if (this.declaration) {469writeln("<?xml version=" + quote("1.0") +470" encoding=" + quote(this.charset) + "?>");471}472writeln("<java version=" + quote(System.getProperty("java.version")) +473" class=" + quote(XMLDecoder.class.getName()) + ">");474preambleWritten = true;475}476indentation++;477List<Statement> statements = statementList(this);478while (!statements.isEmpty()) {479Statement s = statements.remove(0);480if ("writeObject".equals(s.getMethodName())) {481outputValue(s.getArguments()[0], this, true);482}483else {484outputStatement(s, this, false);485}486}487indentation--;488489Statement statement = getMissedStatement();490while (statement != null) {491outputStatement(statement, this, false);492statement = getMissedStatement();493}494495try {496out.flush();497}498catch (IOException e) {499getExceptionListener().exceptionThrown(e);500}501clear();502}503504void clear() {505super.clear();506nameGenerator.clear();507valueToExpression.clear();508targetToStatementList.clear();509}510511Statement getMissedStatement() {512for (List<Statement> statements : this.targetToStatementList.values()) {513for (int i = 0; i < statements.size(); i++) {514if (Statement.class == statements.get(i).getClass()) {515return statements.remove(i);516}517}518}519return null;520}521522523/**524* This method calls <code>flush</code>, writes the closing525* postamble and then closes the output stream associated526* with this stream.527*/528public void close() {529flush();530writeln("</java>");531try {532out.close();533}534catch (IOException e) {535getExceptionListener().exceptionThrown(e);536}537}538539private String quote(String s) {540return "\"" + s + "\"";541}542543private ValueData getValueData(Object o) {544ValueData d = valueToExpression.get(o);545if (d == null) {546d = new ValueData();547valueToExpression.put(o, d);548}549return d;550}551552/**553* Returns <code>true</code> if the argument,554* a Unicode code point, is valid in XML documents.555* Unicode characters fit into the low sixteen bits of a Unicode code point,556* and pairs of Unicode <em>surrogate characters</em> can be combined557* to encode Unicode code point in documents containing only Unicode.558* (The <code>char</code> datatype in the Java Programming Language559* represents Unicode characters, including unpaired surrogates.)560* <par>561* [2] Char ::= #x0009 | #x000A | #x000D562* | [#x0020-#xD7FF]563* | [#xE000-#xFFFD]564* | [#x10000-#x10ffff]565* </par>566*567* @param code the 32-bit Unicode code point being tested568* @return <code>true</code> if the Unicode code point is valid,569* <code>false</code> otherwise570*/571private static boolean isValidCharCode(int code) {572return (0x0020 <= code && code <= 0xD7FF)573|| (0x000A == code)574|| (0x0009 == code)575|| (0x000D == code)576|| (0xE000 <= code && code <= 0xFFFD)577|| (0x10000 <= code && code <= 0x10ffff);578}579580private void writeln(String exp) {581try {582StringBuilder sb = new StringBuilder();583for(int i = 0; i < indentation; i++) {584sb.append(' ');585}586sb.append(exp);587sb.append('\n');588this.out.write(sb.toString());589}590catch (IOException e) {591getExceptionListener().exceptionThrown(e);592}593}594595private void outputValue(Object value, Object outer, boolean isArgument) {596if (value == null) {597writeln("<null/>");598return;599}600601if (value instanceof Class) {602writeln("<class>" + ((Class)value).getName() + "</class>");603return;604}605606ValueData d = getValueData(value);607if (d.exp != null) {608Object target = d.exp.getTarget();609String methodName = d.exp.getMethodName();610611if (target == null || methodName == null) {612throw new NullPointerException((target == null ? "target" :613"methodName") + " should not be null");614}615616if (isArgument && target instanceof Field && methodName.equals("get")) {617Field f = (Field)target;618writeln("<object class=" + quote(f.getDeclaringClass().getName()) +619" field=" + quote(f.getName()) + "/>");620return;621}622623Class<?> primitiveType = primitiveTypeFor(value.getClass());624if (primitiveType != null && target == value.getClass() &&625methodName.equals("new")) {626String primitiveTypeName = primitiveType.getName();627// Make sure that character types are quoted correctly.628if (primitiveType == Character.TYPE) {629char code = ((Character) value).charValue();630if (!isValidCharCode(code)) {631writeln(createString(code));632return;633}634value = quoteCharCode(code);635if (value == null) {636value = Character.valueOf(code);637}638}639writeln("<" + primitiveTypeName + ">" + value + "</" +640primitiveTypeName + ">");641return;642}643644} else if (value instanceof String) {645writeln(createString((String) value));646return;647}648649if (d.name != null) {650if (isArgument) {651writeln("<object idref=" + quote(d.name) + "/>");652}653else {654outputXML("void", " idref=" + quote(d.name), value);655}656}657else if (d.exp != null) {658outputStatement(d.exp, outer, isArgument);659}660}661662private static String quoteCharCode(int code) {663switch(code) {664case '&': return "&";665case '<': return "<";666case '>': return ">";667case '"': return """;668case '\'': return "'";669case '\r': return " ";670default: return null;671}672}673674private static String createString(int code) {675return "<char code=\"#" + Integer.toString(code, 16) + "\"/>";676}677678private String createString(String string) {679StringBuilder sb = new StringBuilder();680sb.append("<string>");681int index = 0;682while (index < string.length()) {683int point = string.codePointAt(index);684int count = Character.charCount(point);685686if (isValidCharCode(point) && this.encoder.canEncode(string.substring(index, index + count))) {687String value = quoteCharCode(point);688if (value != null) {689sb.append(value);690} else {691sb.appendCodePoint(point);692}693index += count;694} else {695sb.append(createString(string.charAt(index)));696index++;697}698}699sb.append("</string>");700return sb.toString();701}702703private void outputStatement(Statement exp, Object outer, boolean isArgument) {704Object target = exp.getTarget();705String methodName = exp.getMethodName();706707if (target == null || methodName == null) {708throw new NullPointerException((target == null ? "target" :709"methodName") + " should not be null");710}711712Object[] args = exp.getArguments();713boolean expression = exp.getClass() == Expression.class;714Object value = (expression) ? getValue((Expression)exp) : null;715716String tag = (expression && isArgument) ? "object" : "void";717String attributes = "";718ValueData d = getValueData(value);719720// Special cases for targets.721if (target == outer) {722}723else if (target == Array.class && methodName.equals("newInstance")) {724tag = "array";725attributes = attributes + " class=" + quote(((Class)args[0]).getName());726attributes = attributes + " length=" + quote(args[1].toString());727args = new Object[]{};728}729else if (target.getClass() == Class.class) {730attributes = attributes + " class=" + quote(((Class)target).getName());731}732else {733d.refs = 2;734if (d.name == null) {735getValueData(target).refs++;736List<Statement> statements = statementList(target);737if (!statements.contains(exp)) {738statements.add(exp);739}740outputValue(target, outer, false);741}742if (expression) {743outputValue(value, outer, isArgument);744}745return;746}747if (expression && (d.refs > 1)) {748String instanceName = nameGenerator.instanceName(value);749d.name = instanceName;750attributes = attributes + " id=" + quote(instanceName);751}752753// Special cases for methods.754if ((!expression && methodName.equals("set") && args.length == 2 &&755args[0] instanceof Integer) ||756(expression && methodName.equals("get") && args.length == 1 &&757args[0] instanceof Integer)) {758attributes = attributes + " index=" + quote(args[0].toString());759args = (args.length == 1) ? new Object[]{} : new Object[]{args[1]};760}761else if ((!expression && methodName.startsWith("set") && args.length == 1) ||762(expression && methodName.startsWith("get") && args.length == 0)) {763if (3 < methodName.length()) {764attributes = attributes + " property=" +765quote(Introspector.decapitalize(methodName.substring(3)));766}767}768else if (!methodName.equals("new") && !methodName.equals("newInstance")) {769attributes = attributes + " method=" + quote(methodName);770}771outputXML(tag, attributes, value, args);772}773774private void outputXML(String tag, String attributes, Object value, Object... args) {775List<Statement> statements = statementList(value);776// Use XML's short form when there is no body.777if (args.length == 0 && statements.size() == 0) {778writeln("<" + tag + attributes + "/>");779return;780}781782writeln("<" + tag + attributes + ">");783indentation++;784785for(int i = 0; i < args.length; i++) {786outputValue(args[i], null, true);787}788789while (!statements.isEmpty()) {790Statement s = statements.remove(0);791outputStatement(s, value, false);792}793794indentation--;795writeln("</" + tag + ">");796}797798@SuppressWarnings("rawtypes")799static Class primitiveTypeFor(Class wrapper) {800if (wrapper == Boolean.class) return Boolean.TYPE;801if (wrapper == Byte.class) return Byte.TYPE;802if (wrapper == Character.class) return Character.TYPE;803if (wrapper == Short.class) return Short.TYPE;804if (wrapper == Integer.class) return Integer.TYPE;805if (wrapper == Long.class) return Long.TYPE;806if (wrapper == Float.class) return Float.TYPE;807if (wrapper == Double.class) return Double.TYPE;808if (wrapper == Void.class) return Void.TYPE;809return null;810}811}812813814