net.sf.xbus.base.xml
Class XMLHelper

java.lang.Object
  extended by net.sf.xbus.base.xml.XMLHelper

public class XMLHelper
extends java.lang.Object

XMLHelper offers some support for treating XML structures.

Author:
Stefan Fleckenstein

Constructor Summary
XMLHelper()
           
 
Method Summary
static void deleteTag(org.w3c.dom.Document doc, java.lang.String tag)
          Deletes the first occurance of the tag in the given org.w3c.dom.Document.
static java.lang.String getAttribute(org.w3c.dom.Node node, java.lang.String attr)
          Returns the value of the attribute.
static javax.xml.parsers.DocumentBuilder getDocumentBuilder(java.lang.String messageName, java.lang.String system)
          Returns a DocumentBuilder needed for parsing.
static java.lang.String getNodeText(org.w3c.dom.Node tag)
           
static java.lang.String getTagAttribute(org.w3c.dom.Document doc, java.lang.String tag, java.lang.String attr)
          getTagAttribute gets the text of the attribute named attr in the first occurence of the tag in the given org.w3c.dom.Document.
static java.lang.String getTagCDATA(org.w3c.dom.Document doc, java.lang.String tag)
          getTagCDATA gets the content of the first CDATA section in the first occurence of the tag in the given org.w3c.dom.Document.
static java.lang.String getTagText(org.w3c.dom.Document doc, java.lang.String tag)
          Gets the text of the first occurence of the tag in the given org.w3c.dom.Document.
static java.util.Hashtable getTagTextList(org.w3c.dom.Document doc, java.lang.String tag)
          Gets the name/value list composed by the child element nodes of the first occurence of the tag in the given org.w3c.dom.Document.
static org.w3c.dom.Document parseXML(java.lang.String xml, java.lang.String messageName, java.lang.String system)
          Parses the given XML string.
static java.lang.String serializeXML(org.w3c.dom.Document doc, java.lang.String systemID)
          Serializes a XML document.
static void setTagCDATA(org.w3c.dom.Document doc, java.lang.String tag, java.lang.String text)
          setTagCDATA sets the text in the first occurence of the tag in the given org.w3c.dom.Document as CDATA section.
static void setTagText(org.w3c.dom.Document doc, java.lang.String tag, java.lang.String text)
          setTagText sets the text in the first occurence of the tag in the given org.w3c.dom.Document.
static void setTagTextList(org.w3c.dom.Document doc, java.lang.String tag, java.util.Hashtable entries)
          setTagTextList stotes a list of name/value pairs in the first occurence of the tag in the given org.w3c.dom.Document.
static void setTransformerProperties()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLHelper

public XMLHelper()
Method Detail

getTagAttribute

public static java.lang.String getTagAttribute(org.w3c.dom.Document doc,
                                               java.lang.String tag,
                                               java.lang.String attr)
getTagAttribute gets the text of the attribute named attr in the first occurence of the tag in the given org.w3c.dom.Document.

Parameters:
doc - the document in which to search
tag - the tag to serch for
attr - the attribute name to search for
Returns:
the attribute value or null if the no tag node exists or if the first tag node does not contain the attribute attr.

getAttribute

public static java.lang.String getAttribute(org.w3c.dom.Node node,
                                            java.lang.String attr)
Returns the value of the attribute.

Parameters:
node - the node which shall contain the attribute
attr - the attribute name to search for
Returns:
the attribute value or null if the node does not contain the attribute attr.

getTagCDATA

public static java.lang.String getTagCDATA(org.w3c.dom.Document doc,
                                           java.lang.String tag)
getTagCDATA gets the content of the first CDATA section in the first occurence of the tag in the given org.w3c.dom.Document.

Parameters:
doc - the document in which to search
tag - the tag to serch for
Returns:
the CDATA section value or null if the no tag node exists or if the first tag node does not contain a CDATA section labelled with tag exists.

getTagText

public static java.lang.String getTagText(org.w3c.dom.Document doc,
                                          java.lang.String tag)
Gets the text of the first occurence of the tag in the given org.w3c.dom.Document. Exactly spoken, it retrieves the value of the first text node of that occurence.

Parameters:
doc - the document in which to search
tag - the tag to serch for
Returns:
the text value or null if no tag node exists of if the first one does nor contain text.

getNodeText

public static java.lang.String getNodeText(org.w3c.dom.Node tag)

getTagTextList

public static java.util.Hashtable getTagTextList(org.w3c.dom.Document doc,
                                                 java.lang.String tag)
                                          throws XException
Gets the name/value list composed by the child element nodes of the first occurence of the tag in the given org.w3c.dom.Document. The mentioned name is the tag of the child node and the value in the name/value pair just the node value. Child nodes other than element nodes are ignored. The value is extracted from the first child node of the element node - if this one is a text node. All other child nodes are ignored.

Parameters:
doc - the document in which to search
tag - the tag to serch for
Returns:
the text value or null if no tag node exists of if the first one does nor contain text.
Throws:
XException - if one of the examined element nodes for a name/value pair has no child or another node than a text node as first child

setTagTextList

public static void setTagTextList(org.w3c.dom.Document doc,
                                  java.lang.String tag,
                                  java.util.Hashtable entries)
setTagTextList stotes a list of name/value pairs in the first occurence of the tag in the given org.w3c.dom.Document. Each pair makes up an element node with the given name as tag and the given value as node value (in a text node). If the tag doesn't exist, nothing happens. If tag exists, its previous content is replaced by the list of text nodes.

Parameters:
doc - the org.w3c.dom.Document containing the tag
tag - name of the tag that should be replaced
entries - contains the data for the list of text nodes. Both keys and elements must be of type String. The keys are treated as tag names, the elements are treated as values for the text nodes.

setTagText

public static void setTagText(org.w3c.dom.Document doc,
                              java.lang.String tag,
                              java.lang.String text)
setTagText sets the text in the first occurence of the tag in the given org.w3c.dom.Document. If the tag doesn't exist, nothing happens, otherwise the corresponding node is completely replaced.


setTagCDATA

public static void setTagCDATA(org.w3c.dom.Document doc,
                               java.lang.String tag,
                               java.lang.String text)
setTagCDATA sets the text in the first occurence of the tag in the given org.w3c.dom.Document as CDATA section. If the tag doesn't exist, nothing happens, otherwise the corresponding node is completely replaced.


deleteTag

public static void deleteTag(org.w3c.dom.Document doc,
                             java.lang.String tag)
Deletes the first occurance of the tag in the given org.w3c.dom.Document.

Does nothing if the tag doesn't exist.


parseXML

public static org.w3c.dom.Document parseXML(java.lang.String xml,
                                            java.lang.String messageName,
                                            java.lang.String system)
                                     throws XException
Parses the given XML string.

Throws:
XException

getDocumentBuilder

public static javax.xml.parsers.DocumentBuilder getDocumentBuilder(java.lang.String messageName,
                                                                   java.lang.String system)
                                                            throws XException
Returns a DocumentBuilder needed for parsing. For better performance, DocumentBuilders are cached per thread. Due to the message type the parser may be instructed Configuration keys control these options.

Returns:
a DocumentBuilder object for xml parsing
Throws:
XException - in case that any problem with instantiating the DocumentBuilder object arises

serializeXML

public static java.lang.String serializeXML(org.w3c.dom.Document doc,
                                            java.lang.String systemID)
                                     throws XException
Serializes a XML document.

Parameters:
doc - the given org.w3c.dom.Document to be serialized
systemID - reference to the DTD of the XML document
Returns:
the content of the XML document in its string representation
Throws:
XException

setTransformerProperties

public static void setTransformerProperties()
                                     throws XException
Throws:
XException