|
J avolution v3.7 (J2SE 1.5+) | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjavolution.xml.ObjectWriter<T>
public class ObjectWriter<T>
This class takes an object and formats it to XML (SAX2 events or stream).
ObjectWriter ow = new ObjectWriter();
...
ow.write(obj, contentHandler); // SAX2 Events.
<i>or</i> ow.write(obj, writer); // Writer encoding.
<i>or</i> ow.write(obj, outputStream); // UTF-8 stream.
<i>or</i> ow.write(obj, byteBuffer); // UTF-8 NIO ByteBuffer.
Namespaces are supported and may be associated to Java packages
in order to reduce the size of the xml generated (and to increase
readability). For example, the following code creates an
ObjectWriter using the default namespace for all
org.jscience.physics.quantities.* classes and the
math prefix for the
org.jscience.mathematics.matrices.* classes.
Here is an example of the xml data produced by such a writer:
ObjectWriter ow = new ObjectWriter();
ow.setPackagePrefix("", "org.jscience.physics.quantities");
ow.setPackagePrefix("math", "org.jscience.mathematics.matrices");
<math:Matrix xmlns:j="http://javolution.org"
xmlns="java:org.jscience.physics.quantities"
xmlns:math="java:org.jscience.mathematics.matrices"
row="2" column="2">
<Mass value="2.3" unit="mg"/>
<Pressure value="0.2" unit="Pa"/>
<Force value="20.0" unit="µN"/>
<Length value="3.0" unit="ft"/>
</math:Matrix>
For more control over the xml document generated (e.g. indentation,
prolog, etc.), applications may use the
write(Object, ContentHandler) method in conjonction with
a custom WriterHandler. For example:
OutputStream out = new FileOutputStream("C:/document.xml");
Writer writer = new Utf8StreamWriter().setOuptutStream(out); // UTF-8 encoding.
WriterHandler handler = new WriterHandler().setWriter(writer);
handler.setIndent("\t"); // Indents with tabs.
handler.setProlog("<?xml version=\"1.0\" encoding=\"UTF-8\"/>");
...
ow.write(obj, handler);
| Constructor Summary | |
|---|---|
ObjectWriter()
Default constructor. |
|
| Method Summary | |
|---|---|
void |
reset()
Resets all internal data maintained by this writer including any namespace associations; objects previously written will not be referred to, they will be send again. |
void |
setClassIdentifierEnabled(boolean enabled)
Enables/disables class identifier attributes (default true |
void |
setExpandReferences(boolean value)
Controls whether or not references are expanced (default false). |
void |
setNamespace(java.lang.String prefix,
java.lang.String uri)
Sets the document namespaces for this writer. |
void |
setPackagePrefix(java.lang.String prefix,
java.lang.String packageName)
Maps a namespace to a Java package. |
void |
setReferencesEnabled(boolean enabled)
Enables/disables xml elements cross references (default false). |
void |
setRootName(java.lang.String name)
Sets the element name or the root object. |
void |
write(T obj,
java.nio.ByteBuffer byteBuffer)
Writes the specified object to the given byte buffer in XML format. |
void |
write(T obj,
ContentHandler handler)
Generates the SAX events corresponding to the serialization of the specified object. |
void |
write(T obj,
java.io.OutputStream out)
Writes the specified object to the given output stream in XML format. |
void |
write(T obj,
java.io.Writer writer)
Writes the specified object to the given writer in XML format. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public ObjectWriter()
| Method Detail |
|---|
public void setNamespace(java.lang.String prefix,
java.lang.String uri)
prefix - the namespace prefix or an empty sequence to set
the default namespace.uri - the namespace uri.
java.lang.IllegalArgumentException - if the prefix is "j" (reserved for
the "http://javolution.org" uri).
public void setPackagePrefix(java.lang.String prefix,
java.lang.String packageName)
prefix - the namespace prefix or empty sequence to set
the default namespace.packageName - of the package associated to the specified prefix.
java.lang.IllegalArgumentException - if the prefix is "j" (reserved for
the "http://javolution.org" uri).
public void write(T obj,
java.io.Writer writer)
throws java.io.IOException
XmlOutputStream
should be used instead.
obj - the object to format.writer - the writer to write to.
java.io.IOException - if there's any problem writing.
public void write(T obj,
java.io.OutputStream out)
throws java.io.IOException
XmlOutputStream
should be used instead.
obj - the object to format.out - the output stream to write to.
java.io.IOException - if there's any problem writing.
public void write(T obj,
java.nio.ByteBuffer byteBuffer)
throws java.io.IOException
obj - the object to format.byteBuffer - the byte buffer to write to.
java.io.IOException - if there's any problem writing.
public void write(T obj,
ContentHandler handler)
throws org.xml.sax.SAXException
obj - the object to format.handler - the SAX event handler.
org.xml.sax.SAXExceptionpublic void reset()
reset in interface Reusablepublic void setReferencesEnabled(boolean enabled)
false).
When enabled, identifiers attributes are added during serialization;
the name of these attributes is defined by XmlFormat.identifier(boolean).
enabled - true if an unique identifier attribute is
added to objects being serialized; false otherwise.public void setExpandReferences(boolean value)
false). References are not expanded if currently
being expanded (to avoid infinite recursion).
value - true to expand references;
false otherwise.XmlFormat.identifier(boolean)public void setClassIdentifierEnabled(boolean enabled)
true).
Disabling the class identifier should only be done if the serialized
objects does not need to be deserialized (e.g. pure xml formatting).
- Parameters:
enabled - true to allow for an additional "j:class"
attribute; false otherwise.- See Also:
XmlElement.add(Object),
XmlElement.add(Object, String)
public void setRootName(java.lang.String name)
name - the name of the root element.
|
J avolution v3.7 (J2SE 1.5+) | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||