Package org.simpleframework.xml.stream
Class OutputElement
- java.lang.Object
-
- org.simpleframework.xml.stream.OutputElement
-
- All Implemented Interfaces:
Node,OutputNode
class OutputElement extends java.lang.Object implements OutputNode
TheOutputElementobject represents an XML element. Attributes can be added to this before ant child element has been acquired from it. Once a child element has been acquired the attributes will be written an can no longer be manipulated, the same applies to any text value set for the element.
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.StringcommentThis is the comment that has been set on this element.private ModemodeThis is the output mode that this element object is using.private java.lang.StringnameRepresents the name of the element for this output node.private OutputNodeparentThis is the parent XML element to this output node.private java.lang.StringreferenceThis is the namespace reference URI associated with this.private NamespaceMapscopeThis is the namespace map that contains the namespaces.private OutputNodeMaptableRepresents the attributes that have been set for the element.private java.lang.StringvalueRepresents the value that has been set for the element.private NodeWriterwriterUsed to write the start tag and attributes for the document.
-
Constructor Summary
Constructors Constructor Description OutputElement(OutputNode parent, NodeWriter writer, java.lang.String name)Constructor for theOutputElementobject.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcommit()This will commit this element and any uncommitted elements elements that are decendents of this node.OutputNodeMapgetAttributes()This returns aNodeMapwhich can be used to add nodes to the element before that element has been committed.OutputNodegetChild(java.lang.String name)This is used to create a child element within the element that this object represents.java.lang.StringgetComment()This is used to get the text comment for the element.ModegetMode()TheModeis used to indicate the output mode of this node.java.lang.StringgetName()Returns the name of the node that this represents.NamespaceMapgetNamespaces()This returns theNamespaceMapfor this node.OutputNodegetParent()This is used to acquire theNodethat is the parent of this node.java.lang.StringgetPrefix()This is used to acquire the prefix for this output node.java.lang.StringgetPrefix(boolean inherit)This is used to acquire the prefix for this output node.java.lang.StringgetReference()This is used to acquire the namespace URI reference associated with this node.java.lang.StringgetValue()Returns the value for the node that this represents.booleanisCommitted()This is used to determine whether this node has been committed.booleanisRoot()This method is used to determine if this node is the root node for the XML document.voidremove()This is used to remove any uncommitted changes.OutputNodesetAttribute(java.lang.String name, java.lang.String value)This method is used for convinience to add an attribute node to the attributeNodeMap.voidsetComment(java.lang.String comment)This is used to set a text comment to the element.voidsetData(boolean data)This is used to set the output mode of this node to either be CDATA or escaped.voidsetMode(Mode mode)This is used to set the output mode of this node to either be CDATA, escaped, or inherited.voidsetName(java.lang.String name)This is used to change the name of an output node.voidsetReference(java.lang.String reference)This is used to set the reference for the node.voidsetValue(java.lang.String value)This is used to set a text value to the element.java.lang.StringtoString()This is the string representation of the element.
-
-
-
Field Detail
-
table
private OutputNodeMap table
Represents the attributes that have been set for the element.
-
scope
private NamespaceMap scope
This is the namespace map that contains the namespaces.
-
writer
private NodeWriter writer
Used to write the start tag and attributes for the document.
-
parent
private OutputNode parent
This is the parent XML element to this output node.
-
reference
private java.lang.String reference
This is the namespace reference URI associated with this.
-
comment
private java.lang.String comment
This is the comment that has been set on this element.
-
value
private java.lang.String value
Represents the value that has been set for the element.
-
name
private java.lang.String name
Represents the name of the element for this output node.
-
mode
private Mode mode
This is the output mode that this element object is using.
-
-
Constructor Detail
-
OutputElement
public OutputElement(OutputNode parent, NodeWriter writer, java.lang.String name)
Constructor for theOutputElementobject. This is used to create an output element that can create elements for an XML document. This requires the writer that is used to generate the actual document and the name of this node.- Parameters:
parent- this is the parent node to this output nodewriter- this is the writer used to generate the filename- this is the name of the element this represents
-
-
Method Detail
-
getPrefix
public java.lang.String getPrefix()
This is used to acquire the prefix for this output node. This will search its parent nodes until the prefix that is currently in scope is found. If a prefix is not found in the parent nodes then a prefix in the current nodes namespace mappings is searched, failing that the prefix returned is null.- Specified by:
getPrefixin interfaceOutputNode- Returns:
- this returns the prefix associated with this node
-
getPrefix
public java.lang.String getPrefix(boolean inherit)
This is used to acquire the prefix for this output node. If the output node is an element then this will search its parent nodes until the prefix that is currently in scope is found. If however this node is an attribute then the hierarchy of nodes is not searched as attributes to not inherit namespaces.- Specified by:
getPrefixin interfaceOutputNode- Parameters:
inherit- if there is no explicit prefix then inherit- Returns:
- this returns the prefix associated with this node
-
getReference
public java.lang.String getReference()
This is used to acquire the namespace URI reference associated with this node. Although it is recommended that the namespace reference is a URI it does not have to be, it can be any unique identifier that can be used to distinguish the qualified names.- Specified by:
getReferencein interfaceOutputNode- Returns:
- this returns the namespace URI reference for this
-
setReference
public void setReference(java.lang.String reference)
This is used to set the reference for the node. Setting the reference implies that the node is a qualified node within the XML document. Both elements and attributes can be qualified. Depending on the prefix set on this node or, failing that, any parent node for the reference, the element will appear in the XML document with that string prefixed to the node name.- Specified by:
setReferencein interfaceOutputNode- Parameters:
reference- this is used to set the reference for the node
-
getNamespaces
public NamespaceMap getNamespaces()
This returns theNamespaceMapfor this node. Only an element can have namespaces, so if this node represents an attribute the elements namespaces will be provided when this is requested. By adding a namespace it becomes in scope for the current element all all child elements of that element.- Specified by:
getNamespacesin interfaceOutputNode- Returns:
- this returns the namespaces associated with the node
-
getParent
public OutputNode getParent()
This is used to acquire theNodethat is the parent of this node. This will return the node that is the direct parent of this node and allows for siblings to make use of nodes with their parents if required.- Specified by:
getParentin interfaceNode- Specified by:
getParentin interfaceOutputNode- Returns:
- this returns the parent node for this node
-
getName
public java.lang.String getName()
Returns the name of the node that this represents. This is an immutable property and cannot be changed. This will be written as the tag name when this has been committed.
-
getValue
public java.lang.String getValue()
Returns the value for the node that this represents. This is a modifiable property for the node and can be changed, however once committed any change will be irrelevant.
-
getComment
public java.lang.String getComment()
This is used to get the text comment for the element. This can be null if no comment has been set. If no comment is set on the node then no comment will be written to the resulting XML.- Specified by:
getCommentin interfaceOutputNode- Returns:
- this is the comment associated with this element
-
isRoot
public boolean isRoot()
This method is used to determine if this 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.- Specified by:
isRootin interfaceOutputNode- Returns:
- true if this is the root node within the document
-
getMode
public Mode getMode()
TheModeis used to indicate the output mode of this node. Three modes are possible, each determines how a value, if specified, is written to the resulting XML document. This is determined by thesetDatamethod which will set the output to be CDATA or escaped, if neither is specified the mode is inherited.- Specified by:
getModein interfaceOutputNode- Returns:
- this returns the mode of this output node object
-
setMode
public void setMode(Mode mode)
This is used to set the output mode of this node to either be CDATA, escaped, or inherited. If the mode is set to data then any value specified will be written in a CDATA block, if this is set to escaped values are escaped. If however this method is set to inherited then the mode is inherited from the parent node.- Specified by:
setModein interfaceOutputNode- Parameters:
mode- this is the output mode to set the node to
-
getAttributes
public OutputNodeMap getAttributes()
This returns aNodeMapwhich can be used to add nodes to the element before that element has been committed. Nodes can be removed or added to the map and will appear as attributes on the written element when it is committed.- Specified by:
getAttributesin interfaceOutputNode- Returns:
- returns the node map used to manipulate attributes
-
setComment
public void setComment(java.lang.String comment)
This is used to set a text comment to the element. This will be written just before the actual element is written. Only a single comment can be set for each output node written.- Specified by:
setCommentin interfaceOutputNode- Parameters:
comment- this is the comment to set on the node
-
setValue
public void setValue(java.lang.String value)
This is used to set a text value to the element. This should be added to the element if the element contains no child elements. If the value cannot be added an exception is thrown.- Specified by:
setValuein interfaceOutputNode- Parameters:
value- this is the text value to add to this element
-
setName
public void setName(java.lang.String name)
This is used to change the name of an output node. This will only affect the name of the node if the node has not yet been committed. If the node is committed then this will not be reflected in the resulting XML generated.- Specified by:
setNamein interfaceOutputNode- Parameters:
name- this is the name to change the node to
-
setData
public void setData(boolean data)
This is used to set the output mode of this node to either be CDATA or escaped. If this is set to true the any value specified will be written in a CDATA block, if this is set to false the values is escaped. If however this method is never invoked then the mode is inherited from the parent.- Specified by:
setDatain interfaceOutputNode- Parameters:
data- if true the value is written as a CDATA block
-
setAttribute
public OutputNode setAttribute(java.lang.String name, java.lang.String value)
This method is used for convinience to add an attribute node to the attributeNodeMap. The attribute added can be removed from the element by useing the node map.- Specified by:
setAttributein interfaceOutputNode- Parameters:
name- this is the name of the attribute to be addedvalue- this is the value of the node to be added- Returns:
- this will return the attribute that was just set
-
getChild
public OutputNode getChild(java.lang.String name) throws java.lang.Exception
This is used to create a child element within the element that this object represents. When a new child is created with this method then the previous child is committed to the document. The createdOutputNodeobject can be used to add attributes to the child element as well as other elements.- Specified by:
getChildin interfaceOutputNode- Parameters:
name- this is the name of the child element to create- Throws:
java.lang.Exception
-
remove
public void remove() throws java.lang.ExceptionThis is used to remove any uncommitted changes. Removal of an output node can only be done if it has no siblings and has not yet been committed. If the node is committed then this will throw an exception to indicate that it cannot be removed.- Specified by:
removein interfaceOutputNode- Throws:
java.lang.Exception- thrown if the node cannot be removed
-
commit
public void commit() throws java.lang.ExceptionThis will commit this element and any uncommitted elements elements that are decendents of this node. For instance if any child or grand child remains open under this element then those elements will be closed before this is closed.- Specified by:
commitin interfaceOutputNode- Throws:
java.lang.Exception- this is thrown if there is an I/O error
-
isCommitted
public boolean isCommitted()
This is used to determine whether this node has been committed. If the node is committed then no further child elements can be created from this node instance. A node is considered to be committed if a parent creates another child element or if thecommitmethod is invoked.- Specified by:
isCommittedin interfaceOutputNode- Returns:
- true if the node has been committed
-
toString
public java.lang.String toString()
This is the string representation of the element. It is used for debugging purposes. When evaluating the element the to string can be used to print out the element name.- Overrides:
toStringin classjava.lang.Object- Returns:
- this returns a text description of the element
-
-