org.deegree.xml
Class XMLTools

java.lang.Object
  extended byorg.deegree.xml.XMLTools

public class XMLTools
extends java.lang.Object

XML Tools based on JAXP 1.1 for parsing documents and retrieving node values/node attributes.

Version:
$Revision: 1.28 $
Author:
Markus Schneider, Andreas Poth

Constructor Summary
XMLTools()
           
 
Method Summary
static void appendNode(org.w3c.dom.Node node, java.lang.String indent, java.lang.StringBuffer sb)
          Appends a node and it's children to the given StringBuffer.
static org.w3c.dom.Node copyNode(org.w3c.dom.Node source, org.w3c.dom.Node dest)
          copies one node to another node (of a different dom document).
static org.w3c.dom.Document create()
          creates a new and empty dom document
static java.lang.String getAttrValue(org.w3c.dom.Node node, java.lang.String attrName)
          Returns the attribute value of the given node.
static java.lang.String getAttrValue(org.w3c.dom.Node node, java.lang.String namespace, java.lang.String attrName)
          Returns the attribute value of the given node.
static java.lang.String getAttrValue(java.lang.String name, org.w3c.dom.Node node)
          Returns the value of the specified node attribute or null if it is missing.
static org.w3c.dom.Element getChildByName(java.lang.String name, java.lang.String namespace, org.w3c.dom.Node node)
          Returns the specified child element of the given elemen.
static ElementList getChildElements(org.w3c.dom.Node node)
          Returns all child ELEMENTs.
static ElementList getChildElementsByName(java.lang.String name, java.lang.String namespace, org.w3c.dom.Node node)
          Returns the specified child elements of the given element.
static double getDoubleValue(java.lang.String name, java.lang.String namespace, org.w3c.dom.Node node, double defaultValue)
          Returns the numerical value of the text contained in the specified child element of the given element as a double (if it denotes a double).
static org.w3c.dom.Element getFirstElement(org.w3c.dom.Node node)
          returns the first child element of the submitted node
static org.w3c.dom.Element getNamedChild(org.w3c.dom.Node node, java.lang.String name)
          returns the first child element of the submitted node
static org.w3c.dom.Element getNamedChild(org.w3c.dom.Node node, java.lang.String namespace, java.lang.String name)
          Returns the first child element of the submitted node that matches the given namespace and name.
static java.lang.String getRequiredAttrValue(java.lang.String name, org.w3c.dom.Node node)
          Returns the value of the specified node attribute.
static java.lang.String getRequiredAttrValue(java.lang.String name, java.lang.String namespace, org.w3c.dom.Node node)
          Returns the value of the specified node attribute. // * FIXME: Due to apparent bugs in getNamedItemNS (name, namespace), // * when used to find attribute nodes, the current implementation // * uses a workaround.
static org.w3c.dom.Element getRequiredChildByName(java.lang.String name, java.lang.String namespace, org.w3c.dom.Node node)
          Returns the specified child element of the given elemen.
static double getRequiredDoubleValue(java.lang.String name, java.lang.String namespace, org.w3c.dom.Node node)
          Returns the numerical value of the text contained in the specified child element of the given element as a double (if it denotes a double).
static java.lang.String getRequiredStringValue(java.lang.String name, java.lang.String namespace, org.w3c.dom.Node node)
          Returns the text contained in the specified child element of the given element.
static java.lang.String getStringValue(org.w3c.dom.Node node)
          Returns the text contained in the specified element.
static java.lang.String getStringValue(java.lang.String name, java.lang.String namespace, org.w3c.dom.Node node, java.lang.String defaultValue)
          Returns the text contained in the specified child element of the given element.
static java.lang.String getValue(org.w3c.dom.Node node)
          Returns the concatenated Strings of all children that are TEXT_NODEs.
static org.w3c.dom.Node insertNodeInto(org.w3c.dom.Node source, org.w3c.dom.Node dest)
          inserts a node into a dom element (of a different dom document)
static org.w3c.dom.Document mergeSchemas(org.w3c.dom.Document[] schemas)
          the method merges two or more XML-schema definitions into one 'meta'-schema. for this it is nessecary that all into schemas uses the same prefix for namespace: http://www.w3.org/2001/XMLSchema.
static org.w3c.dom.Document parse(java.io.Reader reader)
          Parses a XML document and returns a DOM object.
static org.w3c.dom.Document parse(java.lang.String fileName)
          Parses a XML document and returns a DOM object.
static org.w3c.dom.Node removeNamedChildNodes(org.w3c.dom.Node node, java.lang.String nodeName)
          removes all direct child nodes of the submitted node with the also submitted name
static java.lang.String toLocalName(java.lang.String nodeName)
          extracts the local name from a node name
static java.lang.StringBuffer validateCDATA(java.lang.String cdata)
          Checks if a given CDATA-value has to be escaped if it is used as a text value in an XML element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLTools

public XMLTools()
Method Detail

validateCDATA

public static java.lang.StringBuffer validateCDATA(java.lang.String cdata)
Checks if a given CDATA-value has to be escaped if it is used as a text value in an XML element. If the submitted string contains a character that have to be escaped or if the string is made of more than 1500 characters it is encapsulated into a CDATA-section. Returns a version that is safe to be used.

The method is just proofed for a UTF-8 character encoding.

Parameters:
cdata - value to be used
Returns:
the very same value (but escaped if necessary)

getRequiredChildByName

public static org.w3c.dom.Element getRequiredChildByName(java.lang.String name,
                                                         java.lang.String namespace,
                                                         org.w3c.dom.Node node)
                                                  throws XMLParsingException
Returns the specified child element of the given elemen. If there are more than one with the same name, the first one is returned.

Parameters:
name - name of the child element
namespace - namespace of the child element
node - current element
Returns:
the element or null, if it is missing
Throws:
XMLParsingException - specified child element is missing and required is true

getChildByName

public static org.w3c.dom.Element getChildByName(java.lang.String name,
                                                 java.lang.String namespace,
                                                 org.w3c.dom.Node node)
Returns the specified child element of the given elemen. If there are more than one with the same name, the first one is returned.

Parameters:
name - name of the child element
namespace - namespace of the child element
node - current element
Returns:
the element or null, if it is missing

getChildElementsByName

public static ElementList getChildElementsByName(java.lang.String name,
                                                 java.lang.String namespace,
                                                 org.w3c.dom.Node node)
Returns the specified child elements of the given element.

Parameters:
name - name of the child elements
namespace - namespace of the child elements
node - current element
Returns:
the list of matching child elements

getStringValue

public static java.lang.String getStringValue(org.w3c.dom.Node node)
Returns the text contained in the specified element. The returned value is trimmed by calling the trim() method of java.lang.String

Parameters:
node - current element
Returns:
the textual contents of the element or null, if it is missing

getStringValue

public static java.lang.String getStringValue(java.lang.String name,
                                              java.lang.String namespace,
                                              org.w3c.dom.Node node,
                                              java.lang.String defaultValue)
Returns the text contained in the specified child element of the given element.

Parameters:
name - name of the child element
namespace - namespace of the child element
node - current element
defaultValue - default value if element is missing
Returns:
the textual contents of the element or the given default value, if missing

getRequiredStringValue

public static java.lang.String getRequiredStringValue(java.lang.String name,
                                                      java.lang.String namespace,
                                                      org.w3c.dom.Node node)
                                               throws XMLParsingException
Returns the text contained in the specified child element of the given element.

Parameters:
name - name of the child element
namespace - namespace of the child element
node - current element
Returns:
the textual contents of the element or null, if it is missing
Throws:
XMLParsingException

getDoubleValue

public static double getDoubleValue(java.lang.String name,
                                    java.lang.String namespace,
                                    org.w3c.dom.Node node,
                                    double defaultValue)
Returns the numerical value of the text contained in the specified child element of the given element as a double (if it denotes a double).

Parameters:
name - name of the child element
namespace - namespace of the child element
node - current element
defaultValue - value to be used if the specified element is missing or it's value is not numerical
Returns:
the textual contents of the element as a double-value

getRequiredDoubleValue

public static double getRequiredDoubleValue(java.lang.String name,
                                            java.lang.String namespace,
                                            org.w3c.dom.Node node)
                                     throws XMLParsingException
Returns the numerical value of the text contained in the specified child element of the given element as a double (if it denotes a double).

Parameters:
name - name of the child element
namespace - namespace of the child element
node - current element
Returns:
the textual contents of the element as a double-value
Throws:
XMLParsingException - specified child element is missing or the contained text does not denote a double value

getAttrValue

public static java.lang.String getAttrValue(java.lang.String name,
                                            org.w3c.dom.Node node)
                                     throws XMLParsingException
Returns the value of the specified node attribute or null if it is missing.

Parameters:
name - (local) name of attribute
node - current element
Returns:
the textual contents of the attribute or null
Throws:
XMLParsingException

getRequiredAttrValue

public static java.lang.String getRequiredAttrValue(java.lang.String name,
                                                    org.w3c.dom.Node node)
                                             throws XMLParsingException
Returns the value of the specified node attribute.

Parameters:
name - (local) name of attribute
node - current element
Returns:
the textual contents of the attribute
Throws:
XMLParsingException - if specified attribute is missing

getRequiredAttrValue

public static java.lang.String getRequiredAttrValue(java.lang.String name,
                                                    java.lang.String namespace,
                                                    org.w3c.dom.Node node)
                                             throws XMLParsingException
Returns the value of the specified node attribute. // * FIXME: Due to apparent bugs in getNamedItemNS (name, namespace), // * when used to find attribute nodes, the current implementation // * uses a workaround.

Parameters:
name - (local) name of attribute
namespace - namespace of attribute
node - current element
Returns:
the textual contents of the attribute
Throws:
XMLParsingException - if specified attribute is missing

create

public static org.w3c.dom.Document create()
creates a new and empty dom document


getAttrValue

public static java.lang.String getAttrValue(org.w3c.dom.Node node,
                                            java.lang.String attrName)
Returns the attribute value of the given node.

Parameters:
node -
attrName -
Returns:

getAttrValue

public static java.lang.String getAttrValue(org.w3c.dom.Node node,
                                            java.lang.String namespace,
                                            java.lang.String attrName)
Returns the attribute value of the given node.

Parameters:
node -
attrName -
Returns:

parse

public static org.w3c.dom.Document parse(java.lang.String fileName)
                                  throws java.io.IOException,
                                         org.xml.sax.SAXException
Parses a XML document and returns a DOM object.

Parameters:
fileName - the filename of the XML file to be parsed
Returns:
a DOM object
Throws:
java.io.IOException
org.xml.sax.SAXException

parse

public static org.w3c.dom.Document parse(java.io.Reader reader)
                                  throws java.io.IOException,
                                         org.xml.sax.SAXException
Parses a XML document and returns a DOM object.

Parameters:
reader - accessing the resource to parse
Returns:
a DOM object
Throws:
java.io.IOException
org.xml.sax.SAXException

copyNode

public static org.w3c.dom.Node copyNode(org.w3c.dom.Node source,
                                        org.w3c.dom.Node dest)
copies one node to another node (of a different dom document).


insertNodeInto

public static org.w3c.dom.Node insertNodeInto(org.w3c.dom.Node source,
                                              org.w3c.dom.Node dest)
inserts a node into a dom element (of a different dom document)


getFirstElement

public static org.w3c.dom.Element getFirstElement(org.w3c.dom.Node node)
returns the first child element of the submitted node


removeNamedChildNodes

public static org.w3c.dom.Node removeNamedChildNodes(org.w3c.dom.Node node,
                                                     java.lang.String nodeName)
removes all direct child nodes of the submitted node with the also submitted name


getNamedChild

public static org.w3c.dom.Element getNamedChild(org.w3c.dom.Node node,
                                                java.lang.String name)
returns the first child element of the submitted node


getNamedChild

public static org.w3c.dom.Element getNamedChild(org.w3c.dom.Node node,
                                                java.lang.String namespace,
                                                java.lang.String name)
Returns the first child element of the submitted node that matches the given namespace and name.


mergeSchemas

public static org.w3c.dom.Document mergeSchemas(org.w3c.dom.Document[] schemas)
                                         throws java.lang.Exception
the method merges two or more XML-schema definitions into one 'meta'-schema. for this it is nessecary that all into schemas uses the same prefix for namespace: http://www.w3.org/2001/XMLSchema. the result schema won't have a target namespace because the input schemas may use different target namespace (it's very probably that they will).

Throws:
java.lang.Exception

getValue

public static java.lang.String getValue(org.w3c.dom.Node node)
Returns the concatenated Strings of all children that are TEXT_NODEs.


getChildElements

public static ElementList getChildElements(org.w3c.dom.Node node)
Returns all child ELEMENTs.


appendNode

public static void appendNode(org.w3c.dom.Node node,
                              java.lang.String indent,
                              java.lang.StringBuffer sb)
Appends a node and it's children to the given StringBuffer. Indentation is added on recursion.


toLocalName

public static java.lang.String toLocalName(java.lang.String nodeName)
extracts the local name from a node name