Encoder.java revision 0
/*
* Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/**
* An <code>Encoder</code> is a class which can be used to create
* files or streams that encode the state of a collection of
* JavaBeans in terms of their public APIs. The <code>Encoder</code>,
* in conjunction with its persistence delegates, is responsible for
* breaking the object graph down into a series of <code>Statements</code>s
* and <code>Expression</code>s which can be used to create it.
* A subclass typically provides a syntax for these expressions
* using some human readable form - like Java source code or XML.
*
* @since 1.4
*
* @author Philip Milne
*/
public class Encoder {
private ExceptionListener exceptionListener;
boolean executeStatements = true;
private Map attributes;
/**
* Write the specified object to the output stream.
* The serialized form will denote a series of
* expressions, the combined effect of which will create
* an equivalent object when the input stream is read.
* By default, the object is assumed to be a <em>JavaBean</em>
* with a nullary constructor, whose state is defined by
* the matching pairs of "setter" and "getter" methods
* returned by the Introspector.
*
* @param o The object to be written to the stream.
*
* @see XMLDecoder#readObject
*/
protected void writeObject(Object o) {
if (o == this) {
return;
}
info.writeObject(o, this);
}
/**
* Sets the exception handler for this stream to <code>exceptionListener</code>.
* The exception handler is notified when this stream catches recoverable
* exceptions.
*
* @param exceptionListener The exception handler for this stream;
* if <code>null</code> the default exception listener will be used.
*
* @see #getExceptionListener
*/
this.exceptionListener = exceptionListener;
}
/**
* Gets the exception handler for this stream.
*
* @return The exception handler for this stream;
* Will return the default exception listener if this has not explicitly been set.
*
* @see #setExceptionListener
*/
public ExceptionListener getExceptionListener() {
}
try {
}
catch (Exception e) {
}
}
/**
* Returns the persistence delegate for the given type.
* The persistence delegate is calculated
* by applying the following of rules in order:
* <ul>
* <li>
* If the type is an array, an internal persistence
* delegate is returned which will instantiate an
* array of the appropriate type and length, initializing
* each of its elements as if they are properties.
* <li>
* If the type is a proxy, an internal persistence
* delegate is returned which will instantiate a
* new proxy instance using the static
* "newProxyInstance" method defined in the
* Proxy class.
* <li>
* If the BeanInfo for this type has a <code>BeanDescriptor</code>
* which defined a "persistenceDelegate" property, this
* value is returned.
* <li>
* In all other cases the default persistence delegate
* is returned. The default persistence delegate assumes
* the type is a <em>JavaBean</em>, implying that it has a default constructor
* and that its state may be characterized by the matching pairs
* of "setter" and "getter" methods returned by the Introspector.
* The default constructor is the constructor with the greatest number
* of parameters that has the {@link ConstructorProperties} annotation.
* If none of the constructors have the {@code ConstructorProperties} annotation,
* then the nullary constructor (constructor with no parameters) will be used.
* For example, in the following the nullary constructor
* for {@code Foo} will be used, while the two parameter constructor
* for {@code Bar} will be used.
* <code>
* public class Foo {
* public Foo() { ... }
* public Foo(int x) { ... }
* }
* public class Bar {
* public Bar() { ... }
* @ConstructorProperties({"x"})
* public Bar(int x) { ... }
* @ConstructorProperties({"x", "y"})
* public Bar(int x, int y) { ... }
* }
* </code>
* </ul>
*
* @param type The type of the object.
* @return The persistence delegate for this type of object.
*
* @see #setPersistenceDelegate
* @see java.beans.Introspector#getBeanInfo
* @see java.beans.BeanInfo#getBeanDescriptor
*/
}
/**
* Sets the persistence delegate associated with this <code>type</code> to
* <code>persistenceDelegate</code>.
*
* @param type The class of objects that <code>persistenceDelegate</code> applies to.
* @param persistenceDelegate The persistence delegate for instances of <code>type</code>.
*
* @see #getPersistenceDelegate
* @see java.beans.Introspector#getBeanInfo
* @see java.beans.BeanInfo#getBeanDescriptor
*/
{
if (persistenceDelegate != null) {
} else {
}
}
/**
* Removes the entry for this instance, returning the old entry.
*
* @param oldInstance The entry that should be removed.
* @return The entry that was removed.
*
* @see #get
*/
}
/**
* Returns a tentative value for <code>oldInstance</code> in
* the environment created by this stream. A persistence
* delegate can use its <code>mutatesTo</code> method to
* determine whether this value may be initialized to
* form the equivalent object at the output or whether
* a new object must be instantiated afresh. If the
* stream has not yet seen this value, null is returned.
*
* @param oldInstance The instance to be looked up.
* @return The object, null if the object has not been seen before.
*/
return oldInstance;
}
}
if (o == null) {
o = get(oldInstance);
}
return o;
}
}
}
else {
}
}
/**
* Writes statement <code>oldStm</code> to the stream.
* The <code>oldStm</code> should be written entirely
* in terms of the callers environment, i.e. the
* target and all arguments should be part of the
* object graph being written. These expressions
* represent a series of "what happened" expressions
* which tell the output stream how to produce an
* object graph like the original.
* <p>
* The implementation of this method will produce
* a second expression to represent the same expression in
* an environment that will exist when the stream is read.
* This is achieved simply by calling <code>writeObject</code>
* on the target and all the arguments and building a new
* expression with the results.
*
* @param oldStm The expression to be written to the stream.
*/
// System.out.println("writeStatement: " + oldExp);
try {
} catch (Exception e) {
+ newStm, e));
}
}
}
/**
* The implementation first checks to see if an
* expression with this value has already been written.
* If not, the expression is cloned, using
* the same procedure as <code>writeStatement</code>,
* and the value of this expression is reconciled
* with the value of the cloned expression
* by calling <code>writeObject</code>.
*
* @param oldExp The expression to be written to the stream.
*/
// System.out.println("Encoder::writeExpression: " + oldExp);
return;
}
}
void clear() {
}
// Package private method for setting an attributes table for the encoder
if (attributes == null) {
attributes = new HashMap();
}
}
if (attributes == null) {
return null;
}
}
}