de.knipp.rritoolkit
Class XmlBuilder

java.lang.Object
  extended by de.knipp.rritoolkit.XmlBuilder

public class XmlBuilder
extends java.lang.Object

helper class to simplify building DOM trees

Version:
$Revision: 14936 $
Author:
Klaus Malorny

Field Summary
private  org.w3c.dom.Node baseNode
          the base node
private  org.w3c.dom.Node curNode
          the current node, either the document node, document fragment node or an element
private  java.lang.String defaultPrefix
          the default namespace prefix
private  java.lang.String defaultURI
          the default namespace URI
 
Constructor Summary
XmlBuilder(org.w3c.dom.Document doc)
          constructor.
XmlBuilder(org.w3c.dom.DocumentFragment frag)
          constructor.
XmlBuilder(org.w3c.dom.Node node)
          constructor.
 
Method Summary
 XmlBuilder attf(javax.xml.namespace.QName qName, boolean value)
          add an attribute to the current node, if it is an element node.
 XmlBuilder attf(java.lang.String name, boolean value)
          add an attribute to the current node, if it is an element node.
 XmlBuilder attf(java.lang.String uri, java.lang.String qName, boolean value)
          add an attribute to the current node, if it is an element node.
 XmlBuilder attf(java.lang.String uri, java.lang.String prefix, java.lang.String localName, boolean value)
          add an attribute to the current node, if it is an element node.
 XmlBuilder attr(javax.xml.namespace.QName qName, java.lang.Object value)
          add an attribute to the current node, if it is an element node.
 XmlBuilder attr(java.lang.String name, java.lang.Object value)
          add an attribute to the current node, if it is an element node.
 XmlBuilder attr(java.lang.String uri, java.lang.String qName, java.lang.Object value)
          add an attribute to the current node, if it is an element node.
 XmlBuilder attr(java.lang.String uri, java.lang.String prefix, java.lang.String localName, java.lang.Object value)
          add an attribute to the current node, if it is an element node.
 XmlBuilder clearNS()
          clear the default namespace used for subsequent calls of the "simple" element creation methods.
 XmlBuilder comment(java.lang.String text)
          add a comment node to the current node
 XmlBuilder ee()
          finish an element.
 XmlBuilder elt(javax.xml.namespace.QName qName, java.lang.Object value)
          convenience method that adds an element with the given value as text.
 XmlBuilder elt(java.lang.String name, java.lang.Object value)
          convenience method that adds an element with the given value as text.
 XmlBuilder elt(java.lang.String uri, java.lang.String qName, java.lang.Object value)
          convenience method that adds an element with the given value as text.
 XmlBuilder elt(java.lang.String uri, java.lang.String prefix, java.lang.String localName, java.lang.Object value)
          convenience method that adds an element with the given value as text.
 XmlBuilder es(javax.xml.namespace.QName qName)
          create an element.
 XmlBuilder es(java.lang.String localName)
          create an element.
 XmlBuilder es(java.lang.String uri, java.lang.String qName)
          create an element.
 XmlBuilder es(java.lang.String uri, java.lang.String prefix, java.lang.String localName)
          create an element.
 org.w3c.dom.Node getBase()
          return the base node
 org.w3c.dom.Node getCurrent()
          return the current node to which attributes, elements and texts are added to.
 XmlBuilder inc(XmlBuilder xb)
          include the subtree created by the specified other instance.
 XmlBuilder ns(java.lang.String uri)
          set the default namespace used for subsequent calls of the "simple" element creation methods.
 XmlBuilder ns(java.lang.String uri, java.lang.String prefix)
          set the default namespace used for subsequent calls of the "simple" element creation methods.
 XmlBuilder t(java.lang.Object text)
          add a text node to the current node.
 XmlBuilder ttf(boolean value)
          add a text node to the current node.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

baseNode

private org.w3c.dom.Node baseNode
the base node


curNode

private org.w3c.dom.Node curNode
the current node, either the document node, document fragment node or an element


defaultURI

private java.lang.String defaultURI
the default namespace URI


defaultPrefix

private java.lang.String defaultPrefix
the default namespace prefix

Constructor Detail

XmlBuilder

public XmlBuilder(org.w3c.dom.Document doc)
           throws org.w3c.dom.DOMException
constructor. Creates a document fragment for the given document, which can be retrieved via getBaseNode.

Parameters:
doc - the document
Throws:
org.w3c.dom.DOMException - if the creation of the fragment failed

XmlBuilder

public XmlBuilder(org.w3c.dom.Node node)
constructor. The given node is used as the starting point.

Parameters:
node - the node

XmlBuilder

public XmlBuilder(org.w3c.dom.DocumentFragment frag)
constructor. The given fragment is used as the starting point.

Parameters:
frag - the fragment
Method Detail

getCurrent

public org.w3c.dom.Node getCurrent()
return the current node to which attributes, elements and texts are added to.

Returns:
the node

getBase

public org.w3c.dom.Node getBase()
return the base node

Returns:
the node

ns

public XmlBuilder ns(java.lang.String uri,
                     java.lang.String prefix)
set the default namespace used for subsequent calls of the "simple" element creation methods. It is not used for attributes, though. This "default" namespace may not be mistaken for the XML default namespace.

Parameters:
uri - the URI; if null, no namespace is assigned to the subsequent elements
prefix - the prefix, may be null
Returns:
this object, allowing call chaining

ns

public XmlBuilder ns(java.lang.String uri)
set the default namespace used for subsequent calls of the "simple" element creation methods. It is not used for attributes, though. This "default" namespace may not be mistaken for the XML default namespace.

Parameters:
uri - the URI; if null, no namespace is assigned to the subsequent elements
Returns:
this object, allowing call chaining

clearNS

public XmlBuilder clearNS()
clear the default namespace used for subsequent calls of the "simple" element creation methods. This "default" namespace may not be mistaken for the XML default namespace.

Returns:
this object, allowing call chaining

es

public XmlBuilder es(java.lang.String localName)
              throws org.w3c.dom.DOMException
create an element. If a default namespace has been defined, it is used for the creation of the element, otherwise the element will neither have a namespace nor a prefix. The element is appended to the current node and becomes the new current node. The mnemonic "es" stands for "element start".

Parameters:
localName - the local part of the element name
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

es

public XmlBuilder es(java.lang.String uri,
                     java.lang.String qName)
              throws org.w3c.dom.DOMException
create an element. The element is appended to the current node and becomes the new current node. The default name space is left unchanged. The mnemonic "es" stands for "element start".

Parameters:
uri - the name space URI
qName - the qualified name, i.e. a name with or without a prefix
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

es

public XmlBuilder es(java.lang.String uri,
                     java.lang.String prefix,
                     java.lang.String localName)
              throws org.w3c.dom.DOMException
create an element. The element is appended to the current node and becomes the new current node. The default name space is left unchanged. The mnemonic "es" stands for "element start".

Parameters:
uri - the name space URI
prefix - the prefix, may be null or empty
localName - the local element name
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

es

public XmlBuilder es(javax.xml.namespace.QName qName)
              throws org.w3c.dom.DOMException
create an element. The element is appended to the current node and becomes the new current node. The default name space is left unchanged. The mnemonic "es" stands for "element start".

Parameters:
qName - the qualified name (as an object)
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

ee

public XmlBuilder ee()
finish an element. The parent of the current element becomes the new current node. The mnemonic "ee" stands for "element end".

Returns:
this object, allowing call chaining

attr

public XmlBuilder attr(java.lang.String name,
                       java.lang.Object value)
                throws org.w3c.dom.DOMException
add an attribute to the current node, if it is an element node. The string representation of the given value is added as the attribute value. If the value is null the attribute is not added at all. The mnemonic "attr" stands for "attribute".

Parameters:
name - the name of the attribute
value - the value
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

attf

public XmlBuilder attf(java.lang.String name,
                       boolean value)
                throws org.w3c.dom.DOMException
add an attribute to the current node, if it is an element node. The value of the attribute is either "true" or "false", depending on the parameter "value". The mnemonic "attf" stands for "attribute as true/false".

Parameters:
name - the name of the attribute
value - the value
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

attr

public XmlBuilder attr(java.lang.String uri,
                       java.lang.String qName,
                       java.lang.Object value)
                throws org.w3c.dom.DOMException
add an attribute to the current node, if it is an element node. The string representation of the given value is added as the attribute value. If the value is null the attribute is not added at all. The mnemonic "attr" stands for "attribute".

Parameters:
uri - the name space URI
qName - the qualified name, i.e. a name with or
value - the value
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

attf

public XmlBuilder attf(java.lang.String uri,
                       java.lang.String qName,
                       boolean value)
                throws org.w3c.dom.DOMException
add an attribute to the current node, if it is an element node. The value of the attribute is either "true" or "false", depending on the parameter "value". The mnemonic "attf" stands for "attribute as true/false".

Parameters:
uri - the name space URI
qName - the qualified name, i.e. a name with or without a prefix
value - the value
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

attr

public XmlBuilder attr(java.lang.String uri,
                       java.lang.String prefix,
                       java.lang.String localName,
                       java.lang.Object value)
                throws org.w3c.dom.DOMException
add an attribute to the current node, if it is an element node. The string representation of the given value is added as the attribute value. If the value is null the attribute is not added at all. The mnemonic "attr" stands for "attribute".

Parameters:
uri - the name space URI
prefix - the prefix, may be null or empty
localName - the local element name
value - the value
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

attf

public XmlBuilder attf(java.lang.String uri,
                       java.lang.String prefix,
                       java.lang.String localName,
                       boolean value)
                throws org.w3c.dom.DOMException
add an attribute to the current node, if it is an element node. The value of the attribute is either "true" or "false", depending on the parameter "value". The mnemonic "attf" stands for "attribute as true/false".

Parameters:
uri - the name space URI
prefix - the prefix, may be null or empty
localName - the local element name
value - the value
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

attr

public XmlBuilder attr(javax.xml.namespace.QName qName,
                       java.lang.Object value)
                throws org.w3c.dom.DOMException
add an attribute to the current node, if it is an element node. The string representation of the given value is added as the attribute value. If the value is null the attribute is not added at all. The mnemonic "attr" stands for "attribute".

Parameters:
qName - the qualified name
value - the value
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

attf

public XmlBuilder attf(javax.xml.namespace.QName qName,
                       boolean value)
                throws org.w3c.dom.DOMException
add an attribute to the current node, if it is an element node. The value of the attribute is either "true" or "false", depending on the parameter "value". The mnemonic "attf" stands for "attribute as true/false".

Parameters:
qName - the qualified name
value - the value
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

t

public XmlBuilder t(java.lang.Object text)
             throws org.w3c.dom.DOMException
add a text node to the current node. The given object is converted to a text string via toString. The mnemnonic "t" stands for "text".

Parameters:
text - the text to add. If null no node is created
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

ttf

public XmlBuilder ttf(boolean value)
               throws org.w3c.dom.DOMException
add a text node to the current node. Depending on the value, either "true" or "false" is added. The mnemnonic "ttf" stands for "text as true/false".

Parameters:
value - the value
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

comment

public XmlBuilder comment(java.lang.String text)
                   throws org.w3c.dom.DOMException
add a comment node to the current node

Parameters:
text - the text
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

elt

public XmlBuilder elt(java.lang.String name,
                      java.lang.Object value)
               throws org.w3c.dom.DOMException
convenience method that adds an element with the given value as text. If the value is null, the element is not added at all. The mnemonic "elt" stands for "element with text".

Parameters:
name - the name of the element
value - the value
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

elt

public XmlBuilder elt(java.lang.String uri,
                      java.lang.String qName,
                      java.lang.Object value)
               throws org.w3c.dom.DOMException
convenience method that adds an element with the given value as text. If the value is null, the element is not added at all. The mnemonic "elt" stands for "element with text".

Parameters:
uri - the name space URI
qName - the qualified name, i.e. a name with or without a prefix
value - the value
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

elt

public XmlBuilder elt(java.lang.String uri,
                      java.lang.String prefix,
                      java.lang.String localName,
                      java.lang.Object value)
               throws org.w3c.dom.DOMException
convenience method that adds an element with the given value as text. If the value is null, the element is not added at all. The mnemonic "elt" stands for "element with text".

Parameters:
uri - the name space URI
prefix - the prefix, may be null or empty
localName - the local element name
value - the value
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

elt

public XmlBuilder elt(javax.xml.namespace.QName qName,
                      java.lang.Object value)
               throws org.w3c.dom.DOMException
convenience method that adds an element with the given value as text. If the value is null, the element is not added at all. The mnemonic "elt" stands for "element with text".

Parameters:
qName - the qualified name
value - the value
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed

inc

public XmlBuilder inc(XmlBuilder xb)
               throws org.w3c.dom.DOMException
include the subtree created by the specified other instance. The mnemonic "inc" stands for "include".

Parameters:
xb - the other builder
Returns:
this object, allowing call chaining
Throws:
org.w3c.dom.DOMException - if the creation failed


Copyright © 2005-2011 Knipp Medien und Kommunikation GmbH