JADE v6.1

com.dautelle.xml
Class ObjectWriter

java.lang.Object
  extended bycom.dautelle.xml.ObjectWriter

public class ObjectWriter
extends java.lang.Object

This class takes a Representable object and formats it to a stream as XML. Objects written using this facility may be read using the Constructor class.

Namespaces are supported (including default namespace).

For example, the following code creates an ObjectWriter using a default namespace for all classes within the package com.dautelle, excepts for the com.dautelle.math classes which will be using the math prefix.

        ObjectWriter objectWriter = new ObjectWriter();
        objectWriter.setNamespace("", "com.dautelle"); // Default namespace.
        objectWriter.setNamespace("math", "com.dautelle.math");
        ...
        objectWriter.write(matrix, outputStream); // UTF-8 encoding.
        objectWriter.write(matrix, writer);       // Writer encoding.
 
    

The more discriminative namespace is used during serialization. If an object being serialized has no matching namespace (including the default namespace for which the default value "" matches all possible classes), then a local namespace is automatically created (with the prefix pkg).

Version:
4.0, February 29, 2003
Author:
Jean-Marie Dautelle

Constructor Summary
ObjectWriter()
          Creates a default writer.
 
Method Summary
 void setIndent(java.lang.String indent)
          Sets the indentation String (default two-spaces).
 void setNamespace(java.lang.String prefix, java.lang.String packageName)
          Maps a namespace to a Java package.
 void setProlog(boolean isProlog)
          Indicates if the XML prolog has to be written (default true).
 void write(Representable r, java.io.File file)
          Writes the specified Representable to the specified file in XML format.
 void write(Representable r, java.io.OutputStream out)
          Writes the specified Representable to the given output stream in XML format.
 void write(Representable r, java.io.Writer writer)
          Writes the specified Representable 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

ObjectWriter

public ObjectWriter()
Creates a default writer.

Method Detail

setNamespace

public void setNamespace(java.lang.String prefix,
                         java.lang.String packageName)
Maps a namespace to a Java package. The specified prefix is used to shorten the tag name of the object being serialized. For example: setNamespace("math", "com.dautelle.math") associates the namespace prefix math with the namespace name java:com.dautelle.math. Any class within the package com.dautelle.math now uses the math prefix (e.g. <math:Complex real='0' imaginary='1'\>). Any previous association of the specified prefix or package is removed.

Parameters:
prefix - the namespace prefix or "" to set the default namespace.
packageName - of the package associated to the specified prefix.
Throws:
java.lang.IllegalArgumentException - if the prefix "pkg" if used (reserved for local namespace declarations).

setIndent

public void setIndent(java.lang.String indent)
Sets the indentation String (default two-spaces).

Parameters:
indent - the indent String, usually some number of spaces.

setProlog

public void setProlog(boolean isProlog)
Indicates if the XML prolog has to be written (default true).

Parameters:
isProlog - true if the XML prolog has to be written; false otherwise.

write

public void write(Representable r,
                  java.io.File file)
           throws java.io.IOException
Writes the specified Representable to the specified file in XML format. The characters are written using UTF-8 encoding.

Parameters:
r - the Representable object to format.
file - the file to write to.
Throws:
java.io.IOException - if there's any problem writing.

write

public void write(Representable r,
                  java.io.OutputStream out)
           throws java.io.IOException
Writes the specified Representable to the given output stream in XML format. The characters are written using UTF-8 encoding.

Parameters:
r - the Representable object to format.
out - the output stream to write to.
Throws:
java.io.IOException - if there's any problem writing.

write

public void write(Representable r,
                  java.io.Writer writer)
           throws java.io.IOException
Writes the specified Representable to the given writer in XML format.

Note: The encoding specified in the prolog is "UTF-16" except for java.io.OutputStreamWriter instances for which the actual stream encoding is used.

Parameters:
r - the Representable object to format.
writer - the writer to write to.
Throws:
java.io.IOException - if there's any problem writing.

JADE v6.1

Copyright © 2004 Jean-Marie Dautelle.