java.beans
public class: XMLEncoder [javadoc |
source]
java.lang.Object
java.beans.Encoder
java.beans.XMLEncoder
All Implemented Interfaces:
AutoCloseable
The
XMLEncoder
class is a complementary alternative to
the
ObjectOutputStream
and can used to generate
a textual representation of a
JavaBean in the same
way that the
ObjectOutputStream
can
be used to create binary representation of
Serializable
objects. For example, the following fragment can be used to create
a textual representation the supplied
JavaBean
and all its properties:
XMLEncoder e = new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream("Test.xml")));
e.writeObject(new JButton("Hello, world"));
e.close();
Despite the similarity of their APIs, the
XMLEncoder
class is exclusively designed for the purpose of archiving graphs
of
JavaBeans as textual representations of their public
properties. Like Java source files, documents written this way
have a natural immunity to changes in the implementations of the classes
involved. The
ObjectOutputStream
continues to be recommended
for interprocess communication and general purpose serialization.
The XMLEncoder
class provides a default denotation for
JavaBeans in which they are represented as XML documents
complying with version 1.0 of the XML specification and the
UTF-8 character encoding of the Unicode/ISO 10646 character set.
The XML documents produced by the XMLEncoder
class are:
-
Portable and version resilient: they have no dependencies
on the private implementation of any class and so, like Java source
files, they may be exchanged between environments which may have
different versions of some of the classes and between VMs from
different vendors.
-
Structurally compact: The
XMLEncoder
class
uses a redundancy elimination algorithm internally so that the
default values of a Bean's properties are not written to the stream.
-
Fault tolerant: Non-structural errors in the file,
caused either by damage to the file or by API changes
made to classes in an archive remain localized
so that a reader can report the error and continue to load the parts
of the document which were not affected by the error.
Below is an example of an XML archive containing
some user interface components from the swing toolkit:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.0" class="java.beans.XMLDecoder">
<object class="javax.swing.JFrame">
<void property="name">
<string>frame1</string>
</void>
<void property="bounds">
<object class="java.awt.Rectangle">
<int>0</int>
<int>0</int>
<int>200</int>
<int>200</int>
</object>
</void>
<void property="contentPane">
<void method="add">
<object class="javax.swing.JButton">
<void property="label">
<string>Hello</string>
</void>
</object>
</void>
</void>
<void property="visible">
<boolean>true</boolean>
</void>
</object>
</java>
The XML syntax uses the following conventions:
-
Each element represents a method call.
-
The "object" tag denotes an expression whose value is
to be used as the argument to the enclosing element.
-
The "void" tag denotes a statement which will
be executed, but whose result will not be used as an
argument to the enclosing method.
-
Elements which contain elements use those elements as arguments,
unless they have the tag: "void".
-
The name of the method is denoted by the "method" attribute.
-
XML's standard "id" and "idref" attributes are used to make
references to previous expressions - so as to deal with
circularities in the object graph.
-
The "class" attribute is used to specify the target of a static
method or constructor explicitly; its value being the fully
qualified name of the class.
-
Elements with the "void" tag are executed using
the outer context as the target if no target is defined
by a "class" attribute.
-
Java's String class is treated specially and is
written <string>Hello, world</string> where
the characters of the string are converted to bytes
using the UTF-8 character encoding.
Although all object graphs may be written using just these three
tags, the following definitions are included so that common
data structures can be expressed more concisely:
-
The default method name is "new".
-
A reference to a java class is written in the form
<class>javax.swing.JButton</class>.
-
Instances of the wrapper classes for Java's primitive types are written
using the name of the primitive type as the tag. For example, an
instance of the
Integer
class could be written:
<int>123</int>. Note that the XMLEncoder
class
uses Java's reflection package in which the conversion between
Java's primitive types and their associated "wrapper classes"
is handled internally. The API for the XMLEncoder
class
itself deals only with Object
s.
-
In an element representing a nullary method whose name
starts with "get", the "method" attribute is replaced
with a "property" attribute whose value is given by removing
the "get" prefix and decapitalizing the result.
-
In an element representing a monadic method whose name
starts with "set", the "method" attribute is replaced
with a "property" attribute whose value is given by removing
the "set" prefix and decapitalizing the result.
-
In an element representing a method named "get" taking one
integer argument, the "method" attribute is replaced
with an "index" attribute whose value the value of the
first argument.
-
In an element representing a method named "set" taking two arguments,
the first of which is an integer, the "method" attribute is replaced
with an "index" attribute whose value the value of the
first argument.
-
A reference to an array is written using the "array"
tag. The "class" and "length" attributes specify the
sub-type of the array and its length respectively.
For more information you might also want to check out
Using XMLEncoder,
an article in The Swing Connection.
Constructor: |
public XMLEncoder(OutputStream out) {
this(out, "UTF-8", true, 0);
}
Creates a new XML encoder to write out JavaBeans
to the stream out using an XML encoding. Parameters:
out - the stream to which the XML representation of
the objects will be written
Throws:
IllegalArgumentException -
if out is null
Also see:
- XMLDecoder#XMLDecoder(InputStream)
|
public XMLEncoder(OutputStream out,
String charset,
boolean declaration,
int indentation) {
if (out == null) {
throw new IllegalArgumentException("the output stream cannot be null");
}
if (indentation < 0) {
throw new IllegalArgumentException("the indentation must be >= 0");
}
Charset cs = Charset.forName(charset);
this.encoder = cs.newEncoder();
this.charset = charset;
this.declaration = declaration;
this.indentation = indentation;
this.out = new OutputStreamWriter(out, cs.newEncoder());
valueToExpression = new IdentityHashMap< Object, ValueData >();
targetToStatementList = new IdentityHashMap< Object, List< Statement > >();
nameGenerator = new NameGenerator();
}
Creates a new XML encoder to write out JavaBeans
to the stream out using the given charset
starting from the given indentation . Parameters:
out - the stream to which the XML representation of
the objects will be written
charset - the name of the requested charset;
may be either a canonical name or an alias
declaration - whether the XML declaration should be generated;
set this to false
when embedding the contents in another XML document
indentation - the number of space characters to indent the entire XML document by
Throws:
IllegalArgumentException -
if out or charset is null ,
or if indentation is less than 0
IllegalCharsetNameException -
if charset name is illegal
UnsupportedCharsetException -
if no support for the named charset is available
in this instance of the Java virtual machine
UnsupportedOperationException -
if loaded charset does not support encoding
Also see:
- Charset#forName(String)
- since:
1.7 -
|
Methods from java.beans.Encoder: |
---|
clear, get, getAttribute, getExceptionListener, getPersistenceDelegate, getValue, remove, setAttribute, setExceptionListener, setPersistenceDelegate, writeExpression, writeObject, writeStatement |
Methods from java.lang.Object: |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method from java.beans.XMLEncoder Detail: |
void clear() {
super.clear();
nameGenerator.clear();
valueToExpression.clear();
targetToStatementList.clear();
}
|
public void close() {
flush();
writeln("< /java >");
try {
out.close();
}
catch (IOException e) {
getExceptionListener().exceptionThrown(e);
}
}
This method calls flush , writes the closing
postamble and then closes the output stream associated
with this stream. |
public void flush() {
if (!preambleWritten) { // Don't do this in constructor - it throws ... pending.
if (this.declaration) {
writeln("< ?xml version=" + quote("1.0") +
" encoding=" + quote(this.charset) + "? >");
}
writeln("< java version=" + quote(System.getProperty("java.version")) +
" class=" + quote(XMLDecoder.class.getName()) + " >");
preambleWritten = true;
}
indentation++;
List< Statement > statements = statementList(this);
while (!statements.isEmpty()) {
Statement s = statements.remove(0);
if ("writeObject".equals(s.getMethodName())) {
outputValue(s.getArguments()[0], this, true);
}
else {
outputStatement(s, this, false);
}
}
indentation--;
try {
out.flush();
}
catch (IOException e) {
getExceptionListener().exceptionThrown(e);
}
clear();
}
This method writes out the preamble associated with the
XML encoding if it has not been written already and
then writes out all of the values that been
written to the stream since the last time flush
was called. After flushing, all internal references to the
values that were written to this stream are cleared. |
public Object getOwner() {
return owner;
}
Gets the owner of this encoder. |
public void setOwner(Object owner) {
this.owner = owner;
writeExpression(new Expression(this, "getOwner", new Object[0]));
}
Sets the owner of this encoder to owner . |
public void writeExpression(Expression oldExp) {
boolean internal = this.internal;
this.internal = true;
Object oldValue = getValue(oldExp);
if (get(oldValue) == null || (oldValue instanceof String && !internal)) {
getValueData(oldValue).exp = oldExp;
super.writeExpression(oldExp);
}
this.internal = internal;
}
Records the Expression so that the Encoder will
produce the actual output when the stream is flushed.
This method should only be invoked within the context of
initializing a persistence delegate or setting up an encoder to
read from a resource bundle.
For more information about using resource bundles with the
XMLEncoder, see
http://java.sun.com/products/jfc/tsc/articles/persistence4/#i18n |
public void writeObject(Object o) {
if (internal) {
super.writeObject(o);
}
else {
writeStatement(new Statement(this, "writeObject", new Object[]{o}));
}
}
Write an XML representation of the specified object to the output. |
public void writeStatement(Statement oldStm) {
// System.out.println("XMLEncoder::writeStatement: " + oldStm);
boolean internal = this.internal;
this.internal = true;
try {
super.writeStatement(oldStm);
/*
Note we must do the mark first as we may
require the results of previous values in
this context for this statement.
Test case is:
os.setOwner(this);
os.writeObject(this);
*/
mark(oldStm);
Object target = oldStm.getTarget();
if (target instanceof Field) {
String method = oldStm.getMethodName();
Object[] args = oldStm.getArguments();
if ((method == null) || (args == null)) {
}
else if (method.equals("get") && (args.length == 1)) {
target = args[0];
}
else if (method.equals("set") && (args.length == 2)) {
target = args[0];
}
}
statementList(target).add(oldStm);
}
catch (Exception e) {
getExceptionListener().exceptionThrown(new Exception("XMLEncoder: discarding statement " + oldStm, e));
}
this.internal = internal;
}
|