Package org.simpleframework.xml.stream
Class NodeWriter
- java.lang.Object
-
- org.simpleframework.xml.stream.NodeWriter
-
class NodeWriter extends java.lang.Object
TheNodeWriter
object is used to create a writer that will write well formed indented XML for a given output node. This is used in the serialization process to convert an object into an XML document.This keeps a stack of all the active output nodes so that if an output node has been committed it cannot write any further data to the XML document. This allows all output nodes to be independent of each other as the node write organizes the write access.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Set
active
Contains the set of as yet uncommitted elements blocks.private OutputStack
stack
Represents the stack of output nodes that are not yet ended.private boolean
verbose
This determines if we expand the namespace prefixes.private Formatter
writer
Formatter used to indent the XML elements and escape text.
-
Constructor Summary
Constructors Modifier Constructor Description NodeWriter(java.io.Writer result)
Constructor for theNodeWriter
object.NodeWriter(java.io.Writer result, Format format)
Constructor for theNodeWriter
object.private
NodeWriter(java.io.Writer result, Format format, boolean verbose)
Constructor for theNodeWriter
object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
commit(OutputNode parent)
This method is used to commit all nodes on the stack up to and including the specified node.boolean
isCommitted(OutputNode node)
This is used to determine if the specified node has been committed.boolean
isRoot(OutputNode node)
This method is used to determine if the node is the root node for the XML document.void
remove(OutputNode node)
This method is used to remove the output node from the output buffer if that node has not yet been committed.private void
writeAttributes(OutputNode node)
This is used to write the attributes of the specified node to the output.private void
writeComment(OutputNode node)
This is used to write a comment to the document.OutputNode
writeElement(OutputNode parent, java.lang.String name)
This is used to create a new element under the specified node.private void
writeEnd(OutputNode node)
This is used to write a new end element to the resulting XML document.private void
writeName(OutputNode node)
This is used to write a new start element to the resulting XML document.private void
writeNamespaces(OutputNode node)
This is used to write the namespaces of the specified node to the output.OutputNode
writeRoot()
This is used to acquire the root output node for the document.private void
writeStart(OutputNode node)
This is used to write the XML element to the underlying buffer.private OutputNode
writeStart(OutputNode parent, java.lang.String name)
This is used to begin writing on a new XML element.private void
writeValue(OutputNode node)
This is used to write an element value to the resulting XML document.
-
-
-
Field Detail
-
stack
private final OutputStack stack
Represents the stack of output nodes that are not yet ended.
-
writer
private final Formatter writer
Formatter used to indent the XML elements and escape text.
-
active
private final java.util.Set active
Contains the set of as yet uncommitted elements blocks.
-
verbose
private final boolean verbose
This determines if we expand the namespace prefixes.
-
-
Constructor Detail
-
NodeWriter
public NodeWriter(java.io.Writer result)
Constructor for theNodeWriter
object. This will create the object that is used to control an output elements access to the generated XML document. This keeps a stack of active and uncommitted elements.- Parameters:
result
- this is the output for the resulting document
-
NodeWriter
public NodeWriter(java.io.Writer result, Format format)
Constructor for theNodeWriter
object. This will create the object that is used to control an output elements access to the generated XML document. This keeps a stack of active and uncommitted elements.- Parameters:
result
- this is the output for the resulting documentformat
- this is used to format the generated document
-
NodeWriter
private NodeWriter(java.io.Writer result, Format format, boolean verbose)
Constructor for theNodeWriter
object. This will create the object that is used to control an output elements access to the generated XML document. This keeps a stack of active and uncommitted elements.- Parameters:
result
- this is the output for the resulting documentformat
- this is used to format the generated documentverbose
- this determines if we expand the namespaces
-
-
Method Detail
-
writeRoot
public OutputNode writeRoot() throws java.lang.Exception
This is used to acquire the root output node for the document. This will create an empty node that can be used to generate the root document element as a child to the document.Depending on whether or not an encoding has been specified this method will write a prolog to the generated XML document. Each prolog written uses an XML version of "1.0".
- Returns:
- this returns an output element for the document
- Throws:
java.lang.Exception
-
isRoot
public boolean isRoot(OutputNode node)
This method is used to determine if the node is the root node for the XML document. The root node is the first node in the document and has no sibling nodes. This is false if the node has a parent node or a sibling node.- Parameters:
node
- this is the node that is check as the root- Returns:
- true if the node is the root node for the document
-
isCommitted
public boolean isCommitted(OutputNode node)
This is used to determine if the specified node has been committed. If this returns tre then the node is committed and cannot be used to add further child elements.- Parameters:
node
- this is the node to check for commit status- Returns:
- this returns true if the node has been committed
-
commit
public void commit(OutputNode parent) throws java.lang.Exception
This method is used to commit all nodes on the stack up to and including the specified node. This will effectively create end tags for any nodes that are currently open up to the specified element. Once committed the output node can no longer be used to create child elements, nor can any of its child elements.- Parameters:
parent
- this is the node that is to be committed- Throws:
java.lang.Exception
-
remove
public void remove(OutputNode node) throws java.lang.Exception
This method is used to remove the output node from the output buffer if that node has not yet been committed. This allows a node that has been created to be deleted, ensuring that it will not affect the resulting XML document structure.- Parameters:
node
- this is the output node that is to be removed- Throws:
java.lang.Exception
-
writeElement
public OutputNode writeElement(OutputNode parent, java.lang.String name) throws java.lang.Exception
This is used to create a new element under the specified node. This will effectively commit all nodes that are open until this node is encountered. Once the specified node is encountered on the stack a new element is created with the specified name.- Parameters:
parent
- this is the node that is to be committedname
- this is the name of the start element to create- Returns:
- this will return a child node for the given parent
- Throws:
java.lang.Exception
-
writeStart
private OutputNode writeStart(OutputNode parent, java.lang.String name) throws java.lang.Exception
This is used to begin writing on a new XML element. This is typically done by writing any comments required. This will create an output node of the specified name before writing the comment, if any exists. Once the comment has been written the node is pushed on to the head of the output node stack.- Parameters:
parent
- this is the parent node to the next output nodename
- this is the name of the node that is to be created- Returns:
- this returns an output node used for writing content
- Throws:
java.lang.Exception
-
writeStart
private void writeStart(OutputNode node) throws java.lang.Exception
This is used to write the XML element to the underlying buffer. The element is written in the order of element prefix and name followed by the attributes an finally the namespaces for the element. Once this is finished the element is committed to- Parameters:
node
- this is the node that is to be fully written- Throws:
java.lang.Exception
-
writeComment
private void writeComment(OutputNode node) throws java.lang.Exception
This is used to write a comment to the document. Comments appear just before the element name, this allows an logical association between the comment and the node to be made.- Parameters:
node
- this is the node that is to have its name written- Throws:
java.lang.Exception
-
writeName
private void writeName(OutputNode node) throws java.lang.Exception
This is used to write a new start element to the resulting XML document. This will create an output node of the specified name before writing the start tag. Once the tag is written the node is pushed on to the head of the output node stack.- Parameters:
node
- this is the node that is to have its name written- Throws:
java.lang.Exception
-
writeValue
private void writeValue(OutputNode node) throws java.lang.Exception
This is used to write an element value to the resulting XML document. This will search the nodes parents for the write mode, if the mode is CDATA then that is what is used to write the data, otherwise the value is written as plain text.One side effect of this method is that it clears the value of the output node once it has been written to the XML. This is needed, it can however cause confusion within the API.
- Parameters:
node
- this is the node to write the value of- Throws:
java.lang.Exception
-
writeEnd
private void writeEnd(OutputNode node) throws java.lang.Exception
This is used to write a new end element to the resulting XML document. This will acquire the name and value of the given node, if the node has a value that is written. Finally a new end tag is written to the document and the output is flushed.- Parameters:
node
- this is the node that is to have an end tag- Throws:
java.lang.Exception
-
writeAttributes
private void writeAttributes(OutputNode node) throws java.lang.Exception
This is used to write the attributes of the specified node to the output. This will iterate over each node entered on to the node. Once written the node is considered inactive.- Parameters:
node
- this is the node to have is attributes written- Throws:
java.lang.Exception
-
writeNamespaces
private void writeNamespaces(OutputNode node) throws java.lang.Exception
This is used to write the namespaces of the specified node to the output. This will iterate over each namespace entered on to the node. Once written the node is considered qualified.- Parameters:
node
- this is the node to have is attributes written- Throws:
java.lang.Exception
-
-