org.jhotdraw.xml
Class NanoXMLDOMOutput

java.lang.Object
  extended by org.jhotdraw.xml.NanoXMLDOMOutput
All Implemented Interfaces:
Disposable, DOMOutput

public class NanoXMLDOMOutput
extends java.lang.Object
implements DOMOutput, Disposable

DOMOutput using Nano XML.

Design pattern:
Name: Adapter.
Role: Adapter.
Partners: XMLElement as Adaptee.

Version:
$Id: NanoXMLDOMOutput.java 527 2009-06-07 14:28:19Z rawcoder $
Author:
Werner Randelshofer

Field Summary
private  XMLElement current
          The current node used for output.
private  java.lang.String doctype
          The doctype of the XML document.
private  XMLElement document
          The document used for output.
private  DOMFactory factory
          The factory used to create objects.
private  java.util.HashMap<java.lang.Object,java.lang.String> objectids
          This map is used to marshall references to objects to the XML DOM.
private  java.util.HashMap<java.lang.String,java.lang.Object> prototypes
          This map is used to cache prototype objects.
private  java.util.Stack<XMLElement> stack
          The stack.
 
Constructor Summary
NanoXMLDOMOutput(DOMFactory factory)
          Creates a new instance.
 
Method Summary
 void addAttribute(java.lang.String name, boolean value)
          Adds an attribute to current element of the DOM Document.
 void addAttribute(java.lang.String name, boolean value, boolean defaultValue)
          Adds an attribute to current element of the DOM Document if it is different from the default value.
 void addAttribute(java.lang.String name, double value)
          Adds an attribute to current element of the DOM Document.
 void addAttribute(java.lang.String name, double value, double defaultValue)
          Adds an attribute to current element of the DOM Document if it is different from the default value.
 void addAttribute(java.lang.String name, float value)
          Adds an attribute to current element of the DOM Document.
 void addAttribute(java.lang.String name, float value, float defaultValue)
          Adds an attribute to current element of the DOM Document if it is different from the default value.
 void addAttribute(java.lang.String name, int value)
          Adds an attribute to current element of the DOM Document.
 void addAttribute(java.lang.String name, int value, int defaultValue)
          Adds an attribute to current element of the DOM Document if it is different from the default value.
 void addAttribute(java.lang.String name, java.lang.String value)
          Adds an attribute to current element of the DOM Document.
 void addAttribute(java.lang.String name, java.lang.String value, java.lang.String defaultValue)
          Adds an attribute to current element of the DOM Document if it is different from the default value.
 void addComment(java.lang.String comment)
          Adds a comment to the current element of the DOM Document.
 void addText(java.lang.String text)
          Adds a text to current element of the DOM Document.
 void closeElement()
          Closes the current element of the DOM Document.
 void dispose()
          Disposes of all resources held by this object so that they can be garbage collected.
 java.lang.Object getPrototype()
          Returns a prototype for the object currently being written.
 void openElement(java.lang.String tagName)
          Puts a new element into the DOM Document.
 void print(java.io.PrintWriter out)
          Prints the contents of the DOMOutput into the specified print writer.
 void save(java.io.OutputStream out)
          Writes the contents of the DOMOutput into the specified output stream.
 void save(java.io.Writer out)
          Writes the contents of the DOMOutput into the specified writer.
 void setDoctype(java.lang.String doctype)
          Sets the doctype for the XML document.
 void writeObject(java.lang.Object o)
          Writes an object.
private  XMLElement writeStorable(DOMStorable o)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

doctype

private java.lang.String doctype
The doctype of the XML document.


objectids

private java.util.HashMap<java.lang.Object,java.lang.String> objectids
This map is used to marshall references to objects to the XML DOM. A key in this map is a Java Object, a value in this map is String representing a marshalled reference to that object.


prototypes

private java.util.HashMap<java.lang.String,java.lang.Object> prototypes
This map is used to cache prototype objects.


document

private XMLElement document
The document used for output.


current

private XMLElement current
The current node used for output.


factory

private DOMFactory factory
The factory used to create objects.


stack

private java.util.Stack<XMLElement> stack
The stack.

Constructor Detail

NanoXMLDOMOutput

public NanoXMLDOMOutput(DOMFactory factory)
Creates a new instance.

Method Detail

save

public void save(java.io.OutputStream out)
          throws java.io.IOException
Writes the contents of the DOMOutput into the specified output stream.

Throws:
java.io.IOException

save

public void save(java.io.Writer out)
          throws java.io.IOException
Writes the contents of the DOMOutput into the specified writer.

Throws:
java.io.IOException

print

public void print(java.io.PrintWriter out)
Prints the contents of the DOMOutput into the specified print writer.


openElement

public void openElement(java.lang.String tagName)
Puts a new element into the DOM Document. The new element is added as a child to the current element in the DOM document. Then it becomes the current element. The element must be closed using closeElement.

Specified by:
openElement in interface DOMOutput

closeElement

public void closeElement()
Closes the current element of the DOM Document. The parent of the current element becomes the current element.

Specified by:
closeElement in interface DOMOutput
Throws:
java.lang.IllegalArgumentException - if the provided tagName does not match the tag name of the element.

addComment

public void addComment(java.lang.String comment)
Adds a comment to the current element of the DOM Document.

Specified by:
addComment in interface DOMOutput

addText

public void addText(java.lang.String text)
Adds a text to current element of the DOM Document. Note: Multiple consecutives texts will be merged.

Specified by:
addText in interface DOMOutput

addAttribute

public void addAttribute(java.lang.String name,
                         java.lang.String value)
Adds an attribute to current element of the DOM Document.

Specified by:
addAttribute in interface DOMOutput

addAttribute

public void addAttribute(java.lang.String name,
                         int value)
Adds an attribute to current element of the DOM Document.

Specified by:
addAttribute in interface DOMOutput

addAttribute

public void addAttribute(java.lang.String name,
                         boolean value)
Adds an attribute to current element of the DOM Document.

Specified by:
addAttribute in interface DOMOutput

addAttribute

public void addAttribute(java.lang.String name,
                         float value)
Adds an attribute to current element of the DOM Document.

Specified by:
addAttribute in interface DOMOutput

addAttribute

public void addAttribute(java.lang.String name,
                         double value)
Adds an attribute to current element of the DOM Document.

Specified by:
addAttribute in interface DOMOutput

writeObject

public void writeObject(java.lang.Object o)
                 throws java.io.IOException
Description copied from interface: DOMOutput
Writes an object.

Specified by:
writeObject in interface DOMOutput
Throws:
java.io.IOException

writeStorable

private XMLElement writeStorable(DOMStorable o)
                          throws java.io.IOException
Throws:
java.io.IOException

addAttribute

public void addAttribute(java.lang.String name,
                         float value,
                         float defaultValue)
Description copied from interface: DOMOutput
Adds an attribute to current element of the DOM Document if it is different from the default value.

Specified by:
addAttribute in interface DOMOutput

addAttribute

public void addAttribute(java.lang.String name,
                         int value,
                         int defaultValue)
Description copied from interface: DOMOutput
Adds an attribute to current element of the DOM Document if it is different from the default value.

Specified by:
addAttribute in interface DOMOutput

addAttribute

public void addAttribute(java.lang.String name,
                         double value,
                         double defaultValue)
Description copied from interface: DOMOutput
Adds an attribute to current element of the DOM Document if it is different from the default value.

Specified by:
addAttribute in interface DOMOutput

addAttribute

public void addAttribute(java.lang.String name,
                         boolean value,
                         boolean defaultValue)
Description copied from interface: DOMOutput
Adds an attribute to current element of the DOM Document if it is different from the default value.

Specified by:
addAttribute in interface DOMOutput

addAttribute

public void addAttribute(java.lang.String name,
                         java.lang.String value,
                         java.lang.String defaultValue)
Description copied from interface: DOMOutput
Adds an attribute to current element of the DOM Document if it is different from the default value.

Specified by:
addAttribute in interface DOMOutput

getPrototype

public java.lang.Object getPrototype()
Description copied from interface: DOMOutput
Returns a prototype for the object currently being written. This can be used, to reduce the amount of data written to DOMOutput. For example, by not writing object attributes, which have the same values as the prototype.

Specified by:
getPrototype in interface DOMOutput

setDoctype

public void setDoctype(java.lang.String doctype)
Description copied from interface: DOMOutput
Sets the doctype for the XML document.

Specified by:
setDoctype in interface DOMOutput

dispose

public void dispose()
Description copied from interface: Disposable
Disposes of all resources held by this object so that they can be garbage collected.

Specified by:
dispose in interface Disposable