gr.uoa.di.madgik.commons.utils
Class XMLUtils

java.lang.Object
  extended by gr.uoa.di.madgik.commons.utils.XMLUtils

public class XMLUtils
extends java.lang.Object

Utility class to parse and traverse an XML DOM tree

Author:
gpapanikos

Constructor Summary
XMLUtils()
           
 
Method Summary
static java.lang.Boolean AttributeExists(org.w3c.dom.Element node, java.lang.String attributeName)
          Checks if the provided attribute exists in the supplied node
static org.w3c.dom.Document Deserialize(java.io.File XML)
           
static org.w3c.dom.Document Deserialize(java.lang.String XML)
          Parses the provided XML string
static java.lang.String DoReplaceSpecialCharachters(java.lang.String XML)
          Replaces special characters with xml valid escape sequences.
static java.lang.String GetAttribute(org.w3c.dom.Element node, java.lang.String attributeName)
          Retrieves an attribute's value
static java.lang.String[] GetAttributes(org.w3c.dom.Element node, boolean CollapseNamespase)
           
static java.lang.String GetChildCDataText(org.w3c.dom.Node Parent)
           
static java.util.List<org.w3c.dom.Element> GetChildElementsWithName(org.w3c.dom.Node Parent, java.lang.String name)
          Retrieves all child elements of the provided node that have the provided name
static java.util.List<org.w3c.dom.Element> GetChildElementsWithNameAndNamespace(org.w3c.dom.Node Parent, java.lang.String name, java.lang.String ns)
           
static org.w3c.dom.Element GetChildElementWithName(org.w3c.dom.Node Parent, java.lang.String name)
          Retrieves a single child element of the provided node that has the provided name.
static org.w3c.dom.Element GetChildElementWithNameAndNamespace(org.w3c.dom.Node Parent, java.lang.String name, java.lang.String ns)
           
static java.lang.String GetChildText(org.w3c.dom.Node Parent)
          Retrieves the text payload of the first available Text node that is a member of the provided node children
static java.lang.String GetChildTextOrFirstNonEmptyChildElementAsText(org.w3c.dom.Node Parent)
           
static java.lang.Object Query(org.w3c.dom.Document doc, java.lang.String query, javax.xml.namespace.QName type)
           
static java.lang.String Serialize(org.w3c.dom.Node node)
           
static java.lang.String Serialize(org.w3c.dom.Node node, boolean omitDeclaration)
           
static void Serialize(java.lang.String FileName, java.lang.String XML)
           
static java.lang.String SerializeChild(org.w3c.dom.Element parent)
           
static java.lang.String Transform(java.lang.String Source, java.lang.String xslt)
           
static java.lang.String UndoReplaceSpecialCharachters(java.lang.String XML)
          Replaces valid xml escape sequences to their original form
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUtils

public XMLUtils()
Method Detail

Serialize

public static void Serialize(java.lang.String FileName,
                             java.lang.String XML)
                      throws java.lang.Exception
Throws:
java.lang.Exception

Serialize

public static java.lang.String Serialize(org.w3c.dom.Node node,
                                         boolean omitDeclaration)
                                  throws java.lang.Exception
Throws:
java.lang.Exception

Serialize

public static java.lang.String Serialize(org.w3c.dom.Node node)
                                  throws java.lang.Exception
Throws:
java.lang.Exception

Deserialize

public static org.w3c.dom.Document Deserialize(java.lang.String XML)
                                        throws java.lang.Exception
Parses the provided XML string

Parameters:
XML - the xml to parse
Returns:
the Document constructed
Throws:
java.lang.Exception - the deserialization could not be performed

Deserialize

public static org.w3c.dom.Document Deserialize(java.io.File XML)
                                        throws java.lang.Exception
Throws:
java.lang.Exception

AttributeExists

public static java.lang.Boolean AttributeExists(org.w3c.dom.Element node,
                                                java.lang.String attributeName)
                                         throws java.lang.Exception
Checks if the provided attribute exists in the supplied node

Parameters:
node - the Node that should hold the attribute
attributeName - the name of the attribute to check for
Returns:
whether or not the attribute exists
Throws:
java.lang.Exception - The operation could not be performed

GetAttribute

public static java.lang.String GetAttribute(org.w3c.dom.Element node,
                                            java.lang.String attributeName)
                                     throws java.lang.Exception
Retrieves an attribute's value

Parameters:
node - The node the attribute should exist in
attributeName - the name of the attribute
Returns:
the value of the attribute
Throws:
java.lang.Exception - The attribute does not exist

GetAttributes

public static java.lang.String[] GetAttributes(org.w3c.dom.Element node,
                                               boolean CollapseNamespase)
                                        throws java.lang.Exception
Throws:
java.lang.Exception

SerializeChild

public static java.lang.String SerializeChild(org.w3c.dom.Element parent)
                                       throws java.lang.Exception
Throws:
java.lang.Exception

GetChildElementWithName

public static org.w3c.dom.Element GetChildElementWithName(org.w3c.dom.Node Parent,
                                                          java.lang.String name)
                                                   throws java.lang.Exception
Retrieves a single child element of the provided node that has the provided name. If more than one elements have the same name, only the first one is returned

Parameters:
Parent - the node to check its children
name - the name of the child node to retrieve
Returns:
the child element or null if it doesn't exist
Throws:
java.lang.Exception - the Operation could not be performed

GetChildElementWithNameAndNamespace

public static org.w3c.dom.Element GetChildElementWithNameAndNamespace(org.w3c.dom.Node Parent,
                                                                      java.lang.String name,
                                                                      java.lang.String ns)
                                                               throws java.lang.Exception
Throws:
java.lang.Exception

GetChildElementsWithName

public static java.util.List<org.w3c.dom.Element> GetChildElementsWithName(org.w3c.dom.Node Parent,
                                                                           java.lang.String name)
                                                                    throws java.lang.Exception
Retrieves all child elements of the provided node that have the provided name

Parameters:
Parent - the node to check its children
name - the name of the child node to retrieve
Returns:
the child element or null if it doesn't exist
Throws:
java.lang.Exception - the Operation could not be performed

GetChildElementsWithNameAndNamespace

public static java.util.List<org.w3c.dom.Element> GetChildElementsWithNameAndNamespace(org.w3c.dom.Node Parent,
                                                                                       java.lang.String name,
                                                                                       java.lang.String ns)
                                                                                throws java.lang.Exception
Throws:
java.lang.Exception

GetChildTextOrFirstNonEmptyChildElementAsText

public static java.lang.String GetChildTextOrFirstNonEmptyChildElementAsText(org.w3c.dom.Node Parent)
                                                                      throws java.lang.Exception
Throws:
java.lang.Exception

GetChildCDataText

public static java.lang.String GetChildCDataText(org.w3c.dom.Node Parent)
                                          throws java.lang.Exception
Throws:
java.lang.Exception

GetChildText

public static java.lang.String GetChildText(org.w3c.dom.Node Parent)
                                     throws java.lang.Exception
Retrieves the text payload of the first available Text node that is a member of the provided node children

Parameters:
Parent - The node whose child text node's payload should be retrieved
Returns:
The text or null if no text node could be found
Throws:
java.lang.Exception - the Operation could not be performed

Query

public static java.lang.Object Query(org.w3c.dom.Document doc,
                                     java.lang.String query,
                                     javax.xml.namespace.QName type)
                              throws java.lang.Exception
Throws:
java.lang.Exception

Transform

public static java.lang.String Transform(java.lang.String Source,
                                         java.lang.String xslt)
                                  throws java.lang.Exception
Throws:
java.lang.Exception

DoReplaceSpecialCharachters

public static java.lang.String DoReplaceSpecialCharachters(java.lang.String XML)
Replaces special characters with xml valid escape sequences.

Parameters:
XML - The xml to escape
Returns:
the escaped xml

UndoReplaceSpecialCharachters

public static java.lang.String UndoReplaceSpecialCharachters(java.lang.String XML)
Replaces valid xml escape sequences to their original form

Parameters:
XML - the escaped xml
Returns:
the original xml