Methods Summary |
---|
void | clear()
super.clear();
nameGenerator.clear();
valueToExpression.clear();
targetToStatementList.clear();
|
public void | close()This method calls flush , writes the closing
postamble and then closes the output stream associated
with this stream.
flush();
writeln("</java>");
try {
out.close();
}
catch (IOException e) {
getExceptionListener().exceptionThrown(e);
}
|
private static java.lang.String | createString(int code)
return "<char code=\"#" + Integer.toString(code, 16) + "\"/>";
|
private java.lang.String | createString(java.lang.String string)
CharsetEncoder encoder = Charset.forName(encoding).newEncoder();
StringBuilder sb = new StringBuilder();
sb.append("<string>");
int index = 0;
while (index < string.length()) {
int point = string.codePointAt(index);
int count = Character.charCount(point);
if (isValidCharCode(point) && encoder.canEncode(string.substring(index, index + count))) {
String value = quoteCharCode(point);
if (value != null) {
sb.append(value);
} else {
sb.appendCodePoint(point);
}
index += count;
} else {
sb.append(createString(string.charAt(index)));
index++;
}
/*
String value = isValidCharCode(point) && encoder.canEncode(string.substring(index, index + count))
? quoteCharCode(point)
: createString(point);
if (value != null) {
sb.append(value);
} else {
sb.appendCodePoint(point);
}
index += count;
*/
}
sb.append("</string>");
return sb.toString();
|
public void | flush()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.
if (!preambleWritten) { // Don't do this in constructor - it throws ... pending.
writeln("<?xml version=" + quote("1.0") +
" encoding=" + quote(encoding) + "?>");
writeln("<java version=" + quote(System.getProperty("java.version")) +
" class=" + quote(XMLDecoder.class.getName()) + ">");
preambleWritten = true;
}
indentation++;
Vector roots = statementList(this);
for(int i = 0; i < roots.size(); i++) {
Statement s = (Statement)roots.get(i);
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();
|
public java.lang.Object | getOwner()Gets the owner of this encoder.
return owner;
|
private java.beans.XMLEncoder$ValueData | getValueData(java.lang.Object o)
ValueData d = (ValueData)valueToExpression.get(o);
if (d == null) {
d = new ValueData();
valueToExpression.put(o, d);
}
return d;
|
private static boolean | isValidCharCode(int code)Returns true if the argument,
a Unicode code point, is valid in XML documents.
Unicode characters fit into the low sixteen bits of a Unicode code point,
and pairs of Unicode surrogate characters can be combined
to encode Unicode code point in documents containing only Unicode.
(The char datatype in the Java Programming Language
represents Unicode characters, including unpaired surrogates.)
[2] Char ::= #x0009 | #x000A | #x000D
| [#x0020-#xD7FF]
| [#xE000-#xFFFD]
| [#x10000-#x10ffff]
return (0x0020 <= code && code <= 0xD7FF)
|| (0x000A == code)
|| (0x0009 == code)
|| (0x000D == code)
|| (0xE000 <= code && code <= 0xFFFD)
|| (0x10000 <= code && code <= 0x10ffff);
|
private void | mark(java.lang.Object o, boolean isArgument)
if (o == null || o == this) {
return;
}
ValueData d = getValueData(o);
Expression exp = d.exp;
// Do not mark liternal strings. Other strings, which might,
// for example, come from resource bundles should still be marked.
if (o.getClass() == String.class && exp == null) {
return;
}
// Bump the reference counts of all arguments
if (isArgument) {
d.refs++;
}
if (d.marked) {
return;
}
d.marked = true;
Object target = exp.getTarget();
if (!(target instanceof Class)) {
statementList(target).add(exp);
// Pending: Why does the reference count need to
// be incremented here?
d.refs++;
}
mark(exp);
|
private void | mark(java.beans.Statement stm)
Object[] args = stm.getArguments();
for (int i = 0; i < args.length; i++) {
Object arg = args[i];
mark(arg, true);
}
mark(stm.getTarget(), false);
|
private void | outputStatement(java.beans.Statement exp, java.lang.Object outer, boolean isArgument)
Object target = exp.getTarget();
String methodName = exp.getMethodName();
if (target == null || methodName == null) {
throw new NullPointerException((target == null ? "target" :
"methodName") + " should not be null");
}
Object[] args = exp.getArguments();
boolean expression = exp.getClass() == Expression.class;
Object value = (expression) ? getValue((Expression)exp) : null;
String tag = (expression && isArgument) ? "object" : "void";
String attributes = "";
ValueData d = getValueData(value);
if (expression) {
if (d.refs > 1) {
String instanceName = nameGenerator.instanceName(value);
d.name = instanceName;
attributes = attributes + " id=" + quote(instanceName);
}
}
// Special cases for targets.
if (target == outer) {
}
else if (target == Array.class && methodName.equals("newInstance")) {
tag = "array";
attributes = attributes + " class=" + quote(((Class)args[0]).getName());
attributes = attributes + " length=" + quote(args[1].toString());
args = new Object[]{};
}
else if (target.getClass() == Class.class) {
attributes = attributes + " class=" + quote(((Class)target).getName());
}
else {
d.refs = 2;
outputValue(target, outer, false);
outputValue(value, outer, false);
return;
}
// Special cases for methods.
if ((!expression && methodName.equals("set") && args.length == 2 &&
args[0] instanceof Integer) ||
(expression && methodName.equals("get") && args.length == 1 &&
args[0] instanceof Integer)) {
attributes = attributes + " index=" + quote(args[0].toString());
args = (args.length == 1) ? new Object[]{} : new Object[]{args[1]};
}
else if ((!expression && methodName.startsWith("set") && args.length == 1) ||
(expression && methodName.startsWith("get") && args.length == 0)) {
attributes = attributes + " property=" +
quote(Introspector.decapitalize(methodName.substring(3)));
}
else if (!methodName.equals("new") && !methodName.equals("newInstance")) {
attributes = attributes + " method=" + quote(methodName);
}
Vector statements = statementList(value);
// Use XML's short form when there is no body.
if (args.length == 0 && statements.size() == 0) {
writeln("<" + tag + attributes + "/>");
return;
}
writeln("<" + tag + attributes + ">");
indentation++;
for(int i = 0; i < args.length; i++) {
outputValue(args[i], null, true);
}
for(int i = 0; i < statements.size(); i++) {
Statement s = (Statement)statements.get(i);
outputStatement(s, value, false);
}
indentation--;
writeln("</" + tag + ">");
|
private void | outputValue(java.lang.Object value, java.lang.Object outer, boolean isArgument)
if (value == null) {
writeln("<null/>");
return;
}
if (value instanceof Class) {
writeln("<class>" + ((Class)value).getName() + "</class>");
return;
}
ValueData d = getValueData(value);
if (d.exp != null) {
Object target = d.exp.getTarget();
String methodName = d.exp.getMethodName();
if (target == null || methodName == null) {
throw new NullPointerException((target == null ? "target" :
"methodName") + " should not be null");
}
if (target instanceof Field && methodName.equals("get")) {
Field f = (Field)target;
writeln("<object class=" + quote(f.getDeclaringClass().getName()) +
" field=" + quote(f.getName()) + "/>");
return;
}
Class primitiveType = ReflectionUtils.primitiveTypeFor(value.getClass());
if (primitiveType != null && target == value.getClass() &&
methodName.equals("new")) {
String primitiveTypeName = primitiveType.getName();
// Make sure that character types are quoted correctly.
if (primitiveType == Character.TYPE) {
char code = ((Character) value).charValue();
if (!isValidCharCode(code)) {
writeln(createString(code));
return;
}
value = quoteCharCode(code);
if (value == null) {
value = Character.valueOf(code);
}
}
writeln("<" + primitiveTypeName + ">" + value + "</" +
primitiveTypeName + ">");
return;
}
} else if (value instanceof String) {
writeln(createString((String) value));
return;
}
if (d.name != null) {
writeln("<object idref=" + quote(d.name) + "/>");
return;
}
outputStatement(d.exp, outer, isArgument);
|
private java.lang.String | quote(java.lang.String s)
return "\"" + s + "\"";
|
private static java.lang.String | quoteCharCode(int code)
switch(code) {
case '&": return "&";
case '<": return "<";
case '>": return ">";
case '"": return """;
case '\'": return "'";
case '\r": return "
";
default: return null;
}
|
public void | setOwner(java.lang.Object owner)Sets the owner of this encoder to owner .
this.owner = owner;
writeExpression(new Expression(this, "getOwner", new Object[0]));
|
private java.util.Vector | statementList(java.lang.Object target)
Vector list = (Vector)targetToStatementList.get(target);
if (list != null) {
return list;
}
list = new Vector();
targetToStatementList.put(target, list);
return list;
|
public void | writeExpression(java.beans.Expression oldExp)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
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;
|
public void | writeObject(java.lang.Object o)Write an XML representation of the specified object to the output.
if (internal) {
super.writeObject(o);
}
else {
writeStatement(new Statement(this, "writeObject", new Object[]{o}));
}
|
public void | writeStatement(java.beans.Statement oldStm)Records the Statement 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.
// 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);
statementList(oldStm.getTarget()).add(oldStm);
}
catch (Exception e) {
getExceptionListener().exceptionThrown(new Exception("XMLEncoder: discarding statement " + oldStm, e));
}
this.internal = internal;
|
private void | writeln(java.lang.String exp)
try {
for(int i = 0; i < indentation; i++) {
out.write(' ");
}
out.write(exp.getBytes(encoding));
out.write(" \n".getBytes(encoding));
}
catch (IOException e) {
getExceptionListener().exceptionThrown(e);
}
|