|
WebObjects 5.2.3 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.io.OutputStream java.io.ObjectOutputStream com.webobjects.foundation.xml.NSXMLOutputStream
An NSXMLOutputStream writes primitive data types and graphs of Java objects to an OutputStream as XML data. The objects can be read (reconstituted) using an NSXMLInputStream. Persistent storage of objects can be accomplished by using a file for the stream. If the stream is a network socket stream, the objects can be reconsituted on another host or in another process.
Only objects that support the java.io.Serializable interface can be written to streams. The class structure of each serializable object is encoded including the class name and the super classes, the values of the object's fields and arrays, and the closure of any other objects referenced from the initial objects.
The method writeObject is used to write an object to the stream. Any object, including Strings and arrays, is written with writeObject. Multiple objects or primitives can be written to the stream. The objects must be read back from the corresponding NSXMLInputstream with the same types and in the same order as they were written.
Primitive data types can also be written to the stream using the appropriate methods from DataOutput. Strings can also be written using the writeUTF method.
The default serialization mechanism for an object writes the class structure of the object and the values of all non-transient and non-static fields. References to other objects (except in transient or static fields) cause those objects to be written also. Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original was written. Reader is encouraged to consult the serialization specification to understand how and when each reference number is assigned if there is a need to.
For example to write an object that can be read by the corresponding example in
NSXMLInputStream:
FileOutputStream ostream = new FileOutputStream("t.tmp"); NSXMLOutputStream p = new NSXMLOutputStream(ostream); if (p instanceof NSXMLOutputStream) { NSXMLOutputFormat format = new NSXMLOutputFormat(); p.setOutputFormat(format); p.writeRootComment("Writing comment", true); } p.writeInt(12345); p.writeObject("Today"); p.writeObject(new Date()); p.flush(); p.close();The default XML output is:
<?xml version="1.0" encoding="UTF-8"?> <!--Writing comment--> <content xmlns="http://www.apple.com/webobjects/XMLSerialization" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.apple.com/webobjects/XMLSerialization http://www.apple.com/webobjects/5.2/schemas/woxml.xsd"> <int>12345</int> <string id="0" xml:space="preserve">Today</string> <object id="2"> <class flag="3" id="1" name="java.util.Date" suid="7523967970034938905"/> <long ignoreEDB="1">1026763105869</long> </object> </content>
Classes that require special handling during the serialization and
deserialization process must implement special methods with these exact
signatures:
private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException; private void writeObject(java.io.ObjectOutputStream stream) throws IOException
The writeObject method is responsible for writing the state of the object for its particular class so that the corresponding readObject method can restore it. The method does not need to concern itself with the state belonging to the object's superclasses or subclasses. State is saved by writing the individual fields to the NSXMLOutputStream using the writeObject method or by using the methods for primitive data types supported by DataOutput.
Serialization does not write out the fields of any object that does not implement the java.io.Serializable interface. Subclasses of Objects that are not serializable can be serializable. In this case the non-serializable class must have a no-arg constructor to allow its fields to be initialized. In this case it is the responsibility of the subclass to save and restore the state of the non-serializable class. It is frequently the case that the fields of that class are accessible (public, package, or protected) or that there are get and set methods that can be used to restore the state.
Serialization of an object can be prevented by implementing writeObject and readObject methods that throw the NotSerializableException. The exception will be caught by the ObjectOutputStream and abort the serialization process.
Implementing the Externalizable interface allows the object to assume complete control over the contents and format of the object's serialized form. The methods of the Externalizable interface, writeExternal and readExternal, are called to save and restore the objects state. When implemented by a class they can write and read their own state using all of the methods of ObjectOutput and ObjectInput. It is the responsibility of the objects to handle any versioning that occurs.
We provided a simple interface NSXMLObjectOutput that would add a key as an attribute to the XML element representing the primitive data or the object. This key can then be used in XSLT to transform the tag name of the XML element. For example:
<float key="InterestRate">6.25</float> ---> <InterestRate>6.25</InterestRate>
DataOutput
,
ObjectInputStream
,
Serializable
,
Externalizable
Nested Class Summary |
Nested classes inherited from class java.io.ObjectOutputStream |
ObjectOutputStream.PutField |
Field Summary |
Fields inherited from interface com.webobjects.foundation.xml.NSXMLObjectStreamConstants |
ARRAY_TAG, BOOLEAN_TAG, BYTE_TAG, CHAR_TAG, CLASS_ATTR, CLASS_TAG, CONTENT_TAG, DOUBLE_TAG, EXCEPTION_TAG, FIELD_ATTR, FIELD_TAG, FLAG_ATTR, FLOAT_TAG, ID_ATTR, IDREF_ATTR, IGNORE_ATTR, INT_TAG, INTERFACE_TAG, KEY_ATTR, LENGTH_ATTR, LONG_TAG, NAME_ATTR, NAMESPACE, OBJECT_TAG, PROXY_TAG, SHORT_TAG, STRING_TAG, SUID_ATTR, SUPER_TAG, TYPE_ATTR |
Constructor Summary | |
NSXMLOutputStream(OutputStream out)
Creates an ObjectOutputStream that writes to the specified OutputStream. |
|
NSXMLOutputStream(OutputStream out,
File xslt)
Creates an ObjectOutputStream that writes to the specified OutputStream. |
|
NSXMLOutputStream(OutputStream out,
InputSource xslt)
Creates an ObjectOutputStream that writes to the specified OutputStream. |
|
NSXMLOutputStream(OutputStream out,
Transformer transformer)
Creates an ObjectOutputStream that writes to the specified OutputStream. |
Method Summary | |
void |
close()
Closes the stream. |
void |
defaultWriteObject()
Write the non-static and non-transient fields of the current class to this stream. |
boolean |
disableReferenceForString()
Disables the use of references for repeated Strings in XML output. |
int |
disableReferenceForString(int length)
See disableReferenceForString() for details first. |
boolean |
enableReferenceForString()
Enables the use of references for repeated Strings in XML output. |
void |
flush()
Flushes the stream. |
NSXMLOutputFormat |
outputFormat()
Gets the current NSXMLOutputFormat object. |
ObjectOutputStream.PutField |
putFields()
Retrieve the object used to buffer persistent fields to be written to the stream. |
void |
reset()
Reset will disregard the state of any objects already written to the stream. |
void |
setOutputFormat(NSXMLOutputFormat format)
Sets the current NSXMLOutputFormat object that will be used to change the format of the XML output. |
void |
setTransformer(Transformer transformer)
Sets the XSLT transformer that will be used to transform the XML output before the it is written out. |
boolean |
setUseBase64ForBinaryData(boolean on)
Sets the output mode for binary data (essentially array of bytes) to be either using the Base64 encoding or a simple series of numbers (-128 to 127) delimited by a space. |
void |
setXSLTSource(File xslt)
Sets the XSLT stylesheet that will be used to transform the XML output before the it is written out. |
void |
setXSLTSource(InputSource xslt)
Sets the XSLT stylesheet that will be used to transform the XML output before the it is written out. |
Transformer |
transformer()
Gets the XSLT transformer that will be used to transform the XML output before the it is written out. |
boolean |
useBase64ForBinaryData()
Gets the previously set output mode for binary data. |
boolean |
useReferenceForString()
Gets the previously set output behavior for a java.lang.String object. |
void |
write(byte[] buf)
Writes an array of bytes. |
void |
write(byte[] buf,
int off,
int len)
Writes a sub array of bytes. |
void |
write(byte[] buf,
int off,
int len,
String key)
Writes a sub array of bytes. |
void |
write(byte[] buf,
String key)
Writes an array of bytes. |
void |
write(int val)
Writes a byte. |
void |
write(int val,
String key)
Writes a byte. |
void |
writeBoolean(boolean val)
Writes a boolean. |
void |
writeBoolean(boolean val,
String key)
Writes a boolean. |
void |
writeByte(int val)
Writes an 8 bit byte. |
void |
writeByte(int val,
String key)
Writes an 8 bit byte. |
void |
writeBytes(String str)
Writes a String as a sequence of bytes. |
void |
writeBytes(String str,
String key)
Writes a String as a sequence of bytes. |
void |
writeChar(int val)
Writes a 16 bit char. |
void |
writeChar(int val,
String key)
Writes a 16 bit char. |
void |
writeChars(String str)
Writes a String as a sequence of chars. |
void |
writeChars(String str,
String key)
Writes a String as a sequence of chars. |
void |
writeComment(String comment)
Writes a comment to the XML output. |
void |
writeDouble(double val)
Writes a 64 bit double. |
void |
writeDouble(double val,
String key)
Writes a 64 bit double. |
void |
writeFields()
Write the buffered fields to the stream. |
void |
writeFloat(float val)
Writes a 32 bit float. |
void |
writeFloat(float val,
String key)
Writes a 32 bit float. |
void |
writeInt(int val)
Writes a 32 bit int. |
void |
writeInt(int val,
String key)
Writes a 32 bit int. |
void |
writeLong(long val)
Writes a 64 bit long. |
void |
writeLong(long val,
String key)
Writes a 64 bit long. |
void |
writeObject(Object obj,
String key)
Writes the specified object as XML data. |
protected void |
writeObjectOverride(Object obj)
Method used by subclasses to override the default writeObject method. |
void |
writeRootComment(String comment,
boolean before)
Writes a comment to the XML output. |
void |
writeShort(int val)
Writes a 16 bit short. |
void |
writeShort(int val,
String key)
Writes a 16 bit short. |
protected void |
writeStreamHeader()
The writeStreamHeader method is provided so subclasses can append or prepend their own header to the stream. |
void |
writeUTF(String str)
Primitive data write of this String in UTF format. |
void |
writeUTF(String str,
String key)
Primitive data write of this String in UTF format. |
Methods inherited from class java.io.ObjectOutputStream |
annotateClass, annotateProxyClass, drain, enableReplaceObject, replaceObject, useProtocolVersion, writeClassDescriptor, writeObject, writeUnshared |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public NSXMLOutputStream(OutputStream out, File xslt) throws IOException
If a security manager is installed, this constructor will check for the "enableSubclassImplementation" SerializablePermission.
out
- output stream to write toxslt
- XSLT stylesheet that will be used to transform the XML output before
the it is written to out
; the File reference must be non null
IOException
- if an I/O error occurs while writing stream header
SecurityException
- if the "enableSubclassImplementation" SerializablePermission
is not allowedNSXMLOutputStream(OutputStream)
,
NSXMLOutputStream(OutputStream, InputSource)
,
NSXMLOutputStream(OutputStream, Transformer)
,
putFields()
,
NSXMLInputStream.NSXMLInputStream(InputStream)
public NSXMLOutputStream(OutputStream out, InputSource xslt) throws IOException
If a security manager is installed, this constructor will check for the "enableSubclassImplementation" SerializablePermission.
out
- output stream to write toxslt
- XSLT stylesheet that will be used to transform the XML output before
the it is written to out
IOException
- if an I/O error occurs while writing stream header
SecurityException
- if the "enableSubclassImplementation" SerializablePermission
is not allowedNSXMLOutputStream(OutputStream, File)
,
NSXMLOutputStream(OutputStream)
,
NSXMLOutputStream(OutputStream, Transformer)
,
putFields()
,
NSXMLInputStream.NSXMLInputStream(InputStream)
,
InputSource
public NSXMLOutputStream(OutputStream out, Transformer transformer) throws IOException
If a security manager is installed, this constructor will check for the "enableSubclassImplementation" SerializablePermission.
out
- output stream to write totransformer
- XSLT transformer that will be used to transform the XML
output before the it is written to out
IOException
- if an I/O error occurs while writing stream header
SecurityException
- if the "enableSubclassImplementation" SerializablePermission
is not allowedNSXMLOutputStream(OutputStream, File)
,
NSXMLOutputStream(OutputStream, Transformer)
,
NSXMLOutputStream(OutputStream)
,
putFields()
,
NSXMLInputStream.NSXMLInputStream(InputStream)
,
Transformer
public NSXMLOutputStream(OutputStream out) throws IOException
If a security manager is installed, this constructor will check for the "enableSubclassImplementation" SerializablePermission.
out
- output stream to write to
IOException
- if an I/O error occurs while writing stream header
SecurityException
- if the "enableSubclassImplementation" SerializablePermission
is not allowedNSXMLOutputStream(OutputStream, File)
,
NSXMLOutputStream(OutputStream, InputSource)
,
NSXMLOutputStream(OutputStream, Transformer)
,
putFields()
,
NSXMLInputStream.NSXMLInputStream(InputStream)
Method Detail |
public void close() throws IOException
close
in interface ObjectOutput
IOException
- If an I/O error has occurred.public void defaultWriteObject() throws IOException
IOException
- if I/O errors occur while writing to the underlying
OutputStream
public boolean disableReferenceForString()
NSXMLObjectOutput
id
attribute by default:
<string id="39" xml:space="preserve">Well Done!</string>And any repeating output of the same String will just use the
idRef
attribute feature:
<string idRef="39" />This saves space in the output and more importantly maintain the integrity of the objects during deserialization: the two Strings are the same object.
However, sometimes you are just concerned about the data itself and want to see the content of the repeated string without following its reference. You can either use the writeUTF method or use this method to globally treat all Strings as different objects. Thus, if you are not using references for String, subsequent output of the same string looks like this:
<string id="40" xml:space="preserve">Well Done!</string>
disableReferenceForString
in interface NSXMLObjectOutput
true
if the previous behavior is to use references for Strings;
false otherwiseNSXMLObjectOutput.enableReferenceForString()
,
NSXMLObjectOutput.disableReferenceForString(int)
public int disableReferenceForString(int length)
NSXMLObjectOutput
The parameter length
allows you to further fine tune the disabled effect.
It tells NSXMLObjectOutput to revert back to default behavior when length of the string
exceeds the length
so that long strings will not waste space in the output.
For example, when the length is 100, only strings of length 100 or less are written out
as they out, without the use of references.
disableReferenceForString
in interface NSXMLObjectOutput
length
- the length by which NSXMLObjectOutput reverts to default behavior; -1 means
there is no length consideration
NSXMLObjectOutput.enableReferenceForString()
,
NSXMLObjectOutput.disableReferenceForString()
public boolean enableReferenceForString()
NSXMLObjectOutput
id
attribute by default:
<string id="39" xml:space="preserve">Well Done!</string>And any repeating output of the same String will just use the
idRef
attribute feature:
<string idRef="39" />This saves space in the output and more importantly maintain the integrity of the objects during deserialization: the two Strings are the same object.
This is the default behavior.
enableReferenceForString
in interface NSXMLObjectOutput
true
if the previous behavior is to use references for Strings;
false otherwiseNSXMLObjectOutput.disableReferenceForString()
,
NSXMLObjectOutput.disableReferenceForString(int)
public void flush() throws IOException
flush
in interface ObjectOutput
IOException
- If an I/O error has occurred.public NSXMLOutputFormat outputFormat()
NSXMLOutputFormat
public ObjectOutputStream.PutField putFields() throws IOException
IOException
- if I/O errors occurpublic void reset() throws IOException
IOException
- if reset() is invoked while serializing an object.public void setOutputFormat(NSXMLOutputFormat format)
NSXMLOutputFormat
public void setTransformer(Transformer transformer)
transformer
- the XSLT transformerTransformer
public final boolean setUseBase64ForBinaryData(boolean on)
NSXMLObjectOutput
setUseBase64ForBinaryData
in interface NSXMLObjectOutput
on
- output mode is Base64 if it is true
true
NSXMLObjectOutput.useBase64ForBinaryData()
public void setXSLTSource(File xslt) throws IOException
xslt
- the File reference to the stylesheet; must be non null
IOException
public void setXSLTSource(InputSource xslt) throws IOException
xslt
- the InputSource to the stylesheet
IOException
InputSource
public Transformer transformer()
Transformer
,
Transformer.setOutputProperty(java.lang.String, java.lang.String)
public final boolean useBase64ForBinaryData()
NSXMLObjectOutput
true
.
useBase64ForBinaryData
in interface NSXMLObjectOutput
NSXMLObjectOutput.setUseBase64ForBinaryData(boolean)
public boolean useReferenceForString()
NSXMLObjectOutput
true
.
useReferenceForString
in interface NSXMLObjectOutput
NSXMLObjectOutput.enableReferenceForString()
,
NSXMLObjectOutput.disableReferenceForString()
,
NSXMLObjectOutput.disableReferenceForString(int)
public void write(int val) throws IOException
write
in interface ObjectOutput
val
- the byte to be written to the stream
IOException
- If an I/O error has occurred.public void write(byte[] buf) throws IOException
write
in interface ObjectOutput
buf
- the data to be written
IOException
- If an I/O error has occurred.public void write(byte[] buf, int off, int len) throws IOException
write
in interface ObjectOutput
buf
- the data to be writtenoff
- the start offset in the datalen
- the number of bytes that are written
IOException
- If an I/O error has occurred.public void write(int val, String key)
write
in interface NSXMLObjectOutput
val
- the byte to be written to the streamkey
- this will appear as the data of the attribute "key"public void write(byte[] buf, String key) throws IOException
NSXMLObjectOutput
write
in interface NSXMLObjectOutput
buf
- the data to be writtenkey
- this will appear as the data of the attribute "key"
IOException
- if I/O errors occur while writing to the underlying
streampublic void write(byte[] buf, int off, int len, String key) throws IOException
NSXMLObjectOutput
write
in interface NSXMLObjectOutput
buf
- the data to be writtenoff
- the start offset in the datalen
- the number of bytes that are writtenkey
- this will appear as the data of the attribute "key"
IOException
- if I/O errors occur while writing to the underlying
streampublic void writeBoolean(boolean val) throws IOException
writeBoolean
in interface DataOutput
val
- the boolean to be written
IOException
- if I/O errors occur while writing to the underlying
streampublic void writeBoolean(boolean val, String key)
writeBoolean
in interface NSXMLObjectOutput
val
- the boolean to be writtenkey
- this will appear as the data of the attribute "key"public void writeByte(int val) throws IOException
writeByte
in interface DataOutput
val
- the byte value to be written
IOException
- if I/O errors occur while writing to the underlying
streampublic void writeByte(int val, String key)
writeByte
in interface NSXMLObjectOutput
val
- the byte value to be writtenkey
- this will appear as the data of the attribute "key"public void writeBytes(String str) throws IOException
str
is cast into a byte (and hence information could be lost), the encoding specified
by the user (using NSXMLOutputFormat) is used to convert each character to a
certain number of bytes.
writeBytes
in interface DataOutput
str
- the String of bytes to be written
IOException
- if I/O errors occur while writing to the underlying
streampublic void writeBytes(String str, String key) throws IOException
NSXMLObjectOutput
str
is cast into a byte (and hence information could be lost), the encoding specified
by the user (using NSXMLOutputFormat) is used to convert each character to a
certain number of bytes.
writeBytes
in interface NSXMLObjectOutput
str
- the String of bytes to be writtenkey
- this will appear as the data of the attribute "key"
IOException
- Any exception thrown by the underlying
OutputStream.public void writeChar(int val) throws IOException
writeChar
in interface DataOutput
val
- the char value to be written
IOException
- if I/O errors occur while writing to the underlying
streampublic void writeChar(int val, String key)
writeChar
in interface NSXMLObjectOutput
val
- the char value to be writtenkey
- this will appear as the data of the attribute "key"public void writeChars(String str) throws IOException
writeChars
in interface DataOutput
str
- the String of chars to be written
IOException
- if I/O errors occur while writing to the underlying
streampublic void writeChars(String str, String key) throws IOException
writeChars
in interface NSXMLObjectOutput
str
- the String of chars to be writtenkey
- this will appear as the data of the attribute "key"
IOException
- if I/O errors occur while writing to the underlying
streampublic void writeComment(String comment)
writeComment
in interface NSXMLObjectOutput
comment
- the comment to be written outwriteRootComment(String comment, boolean before)
public void writeDouble(double val) throws IOException
writeDouble
in interface DataOutput
val
- the double value to be written
IOException
- if I/O errors occur while writing to the underlying
streampublic void writeDouble(double val, String key)
writeDouble
in interface NSXMLObjectOutput
val
- the double value to be writtenkey
- this will appear as the data of the attribute "key"public void writeFields() throws IOException
IOException
- if I/O errors occur while writing to the underlying
stream
NotActiveException
- Called when a classes writeObject method was
not called to write the state of the object.public void writeFloat(float val) throws IOException
writeFloat
in interface DataOutput
val
- the float value to be written
IOException
- if I/O errors occur while writing to the underlying
streampublic void writeFloat(float val, String key)
writeFloat
in interface NSXMLObjectOutput
val
- the float value to be writtenkey
- this will appear as the data of the attribute "key"public void writeInt(int val) throws IOException
writeInt
in interface DataOutput
val
- the integer value to be written
IOException
- if I/O errors occur while writing to the underlying
streampublic void writeInt(int val, String key)
writeInt
in interface NSXMLObjectOutput
val
- the integer value to be writtenkey
- this will appear as the data of the attribute "key"public void writeLong(long val) throws IOException
writeLong
in interface DataOutput
val
- the long value to be written
IOException
- if I/O errors occur while writing to the underlying
streampublic void writeLong(long val, String key)
writeLong
in interface NSXMLObjectOutput
val
- the long value to be writtenkey
- this will appear as the data of the attribute "key"public void writeObject(Object obj, String key) throws IOException
Exceptions are thrown for problems with the OutputStream and for classes that should not be serialized. All exceptions are fatal to the OutputStream, which is left in an indeterminate state, and it is up to the caller to ignore or recover the stream state.
writeObject
in interface NSXMLObjectOutput
obj
- the object to be written outkey
- this will appear as the data of the attribute "key"
InvalidClassException
- Something is wrong with a class used by
serialization.
NotSerializableException
- Some object to be serialized does not
implement the java.io.Serializable interface.
IOException
- Any exception thrown by the underlying
OutputStream.protected void writeObjectOverride(Object obj) throws IOException
obj
- object to be written to the underlying stream
IOException
- if there are I/O errors while writing to the
underlying streamNSXMLOutputStream(OutputStream)
,
ObjectOutputStream.writeObject(Object)
public void writeRootComment(String comment, boolean before)
writeRootComment
in interface NSXMLObjectOutput
comment
- the comment to be written outbefore
- if true, the comment is right before the root element; otherwise, it
is right after the root elementwriteComment(String comment)
public void writeShort(int val) throws IOException
writeShort
in interface DataOutput
val
- the short value to be written
IOException
- if I/O errors occur while writing to the underlying
streampublic void writeShort(int val, String key)
writeShort
in interface NSXMLObjectOutput
val
- the short value to be writtenkey
- this will appear as the data of the attribute "key"protected void writeStreamHeader() throws IOException
IOException
- if I/O errors occur while writing to the underlying
streampublic void writeUTF(String str) throws IOException
writeUTF
in interface DataOutput
str
- the String in UTF format
IOException
- if I/O errors occur while writing to the underlying
streampublic void writeUTF(String str, String key)
writeUTF
in interface NSXMLObjectOutput
str
- the String in UTF formatkey
- this will appear as the data of the attribute "key"
|
Last updated Thu Oct 21 15:04:16 PDT 2004. | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |