Jitsi: the OpenSource Java VoIP and Instant Messaging client.

net.java.sip.communicator.util.xml
Class XMLUtils

java.lang.Object
  extended by net.java.sip.communicator.util.xml.XMLUtils
Direct Known Subclasses:
XMLConfUtils

public class XMLUtils
extends Object

Common XML Tasks

Author:
Emil Ivov, Damian Minkov

Constructor Summary
XMLUtils()
           
 
Method Summary
static Document createDocument()
          Creates W3C Document.
static Document createDocument(String xml)
          Creates W3C Document from the xml.
static String createXml(Document document)
          Creates XML from W3C Document from the xml.
static Element findChild(Element parent, String tagName)
          Returns the child element with the specified tagName for the specified parent element.
static List<Element> findChildren(Element parent, String tagName)
          Returns the children elements with the specified tagName for the specified parent element.
static String getAttribute(Node node, String name)
          Extracts from node the attribute with the specified name.
static String getCData(Element element)
          Extract the CDATA content of the specified element.
static CDATASection getCDataNode(Element element)
          Returns element's CDATA child node (if it has one).
static Node getChildByType(Element element, short nodeType)
          Returns first of the element's child nodes that is of type nodeType.
static String getNamespaceUri(Node node)
          Gets the node namespace.
static String getText(Element parentNode)
          Extracts the String content of a TXT element.
static Text getTextNode(Element element)
          Returns element's TEXT child node (if it has one).
static boolean hasChildElements(Element element)
          Indicates whether element has any child element.
static void indentedWriteXML(Document doc, OutputStream out)
          A simple implementation of XML writing that also allows for indentation.
static boolean isStandartXmlNamespace(String namespace)
          Indicates whether namespace is one of the standart xml namespace.
static Element locateElement(Element root, String tagName, String keyAttributeName, String keyAttributeValue)
          Looks through all child elements of the specified root (recursively) and returns the first element that corresponds to all parameters.
static List<Element> locateElements(Element root, String tagName, String keyAttributeName, String keyAttributeValue)
          Looks through all child elements of the specified root (recursively) and returns the elements that corresponds to all parameters.
static void printChildElements(Element root, PrintStream out, boolean recurse, String prefix)
          Whenever you'd need to print a configuration node and/or its children.
static void setCData(Element element, String data)
          Sets data to be the CDATA content of element
static void setText(Element parentNode, String data)
          Sets data to be the TEXT content of element
static void writeXML(Document document, File out)
          Writes the specified document to the given file adding indentatation.
static void writeXML(Document document, StreamResult streamResult, String doctypeSystem, String doctypePublic)
          Writes the specified document to the given file adding indentatation.
static void writeXML(Document document, Writer writer)
          Writes the specified document to the given file adding indentatation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUtils

public XMLUtils()
Method Detail

getAttribute

public static String getAttribute(Node node,
                                  String name)
Extracts from node the attribute with the specified name.

Parameters:
node - the node whose attribute we'd like to extract.
name - the name of the attribute to extract.
Returns:
a String containing the trimmed value of the attribute or null if no such attribute exists

getText

public static String getText(Element parentNode)
Extracts the String content of a TXT element.

Parameters:
parentNode - the node containing the data that we'd like to get.
Returns:
the string contained by the node or null if none existed.

setText

public static void setText(Element parentNode,
                           String data)
Sets data to be the TEXT content of element

Parameters:
parentNode - the parent element.
data - the data to set.

setCData

public static void setCData(Element element,
                            String data)
Sets data to be the CDATA content of element

Parameters:
element - the parent element.
data - the data to set.

getCData

public static String getCData(Element element)
Extract the CDATA content of the specified element.

Parameters:
element - the element whose data we need
Returns:
a String containing the CDATA value of element.

getCDataNode

public static CDATASection getCDataNode(Element element)
Returns element's CDATA child node (if it has one).

Parameters:
element - the element whose CDATA we need to get.
Returns:
a CDATASection object containing the specified element's CDATA content

getTextNode

public static Text getTextNode(Element element)
Returns element's TEXT child node (if it has one).

Parameters:
element - the element whose TEXT we need to get.
Returns:
a Text object containing the specified element's text content.

getChildByType

public static Node getChildByType(Element element,
                                  short nodeType)
Returns first of the element's child nodes that is of type nodeType.

Parameters:
element - the element whose child we need.
nodeType - the type of the child we need.
Returns:
a child of the specified nodeType or null if none was found.

writeXML

public static void writeXML(Document document,
                            File out)
                     throws IOException
Writes the specified document to the given file adding indentatation. The default encoding is UTF-8.

Parameters:
out - the output File
document - the document to write
Throws:
IOException - in case a TransformerException is thrown by the underlying Transformer.

writeXML

public static void writeXML(Document document,
                            Writer writer)
                     throws IOException
Writes the specified document to the given file adding indentatation. The default encoding is UTF-8.

Parameters:
writer - the writer to use when writing the File
document - the document to write
Throws:
IOException - in case a TransformerException is thrown by the underlying Transformer.

writeXML

public static void writeXML(Document document,
                            StreamResult streamResult,
                            String doctypeSystem,
                            String doctypePublic)
                     throws IOException
Writes the specified document to the given file adding indentatation. The default encoding is UTF-8.

Parameters:
streamResult - the streamResult object where the document should be written
document - the document to write
doctypeSystem - the doctype system of the xml document that we should record in the file or null if none is specified.
doctypePublic - the public identifier to be used in the document type declaration.
Throws:
IOException - in case a TransformerException is thrown by the underlying Transformer.

indentedWriteXML

public static void indentedWriteXML(Document doc,
                                    OutputStream out)
A simple implementation of XML writing that also allows for indentation.

Parameters:
doc - the Document that we will be writing.
out - an OutputStream to write the document through.

printChildElements

public static void printChildElements(Element root,
                                      PrintStream out,
                                      boolean recurse,
                                      String prefix)
Whenever you'd need to print a configuration node and/or its children.

Parameters:
root - the root node to print.
out - the print stream that should be used to outpu
recurse - boolean
prefix - String

findChild

public static Element findChild(Element parent,
                                String tagName)
Returns the child element with the specified tagName for the specified parent element.

Parameters:
parent - The parent whose child we're looking for.
tagName - the name of the child to find
Returns:
The child with the specified name or null if no such child was found.
Throws:
NullPointerException - if parent or tagName are null

findChildren

public static List<Element> findChildren(Element parent,
                                         String tagName)
Returns the children elements with the specified tagName for the specified parent element.

Parameters:
parent - The parent whose children we're looking for.
tagName - the name of the child to find
Returns:
List of the children with the specified name
Throws:
NullPointerException - if parent or tagName are null

locateElement

public static Element locateElement(Element root,
                                    String tagName,
                                    String keyAttributeName,
                                    String keyAttributeValue)
Looks through all child elements of the specified root (recursively) and returns the first element that corresponds to all parameters.

Parameters:
root - the Element where the search should begin
tagName - the name of the node we're looking for
keyAttributeName - the name of an attribute that the node has to have
keyAttributeValue - the value that attribute must have
Returns:
the Element in the tree under root that matches the specified parameters.
Throws:
NullPointerException - if any of the arguments is null.

locateElements

public static List<Element> locateElements(Element root,
                                           String tagName,
                                           String keyAttributeName,
                                           String keyAttributeValue)
Looks through all child elements of the specified root (recursively) and returns the elements that corresponds to all parameters.

Parameters:
root - the Element where the search should begin
tagName - the name of the node we're looking for
keyAttributeName - the name of an attribute that the node has to have
keyAttributeValue - the value that attribute must have
Returns:
list of Elements in the tree under root that match the specified parameters.
Throws:
NullPointerException - if any of the arguments is null.

isStandartXmlNamespace

public static boolean isStandartXmlNamespace(String namespace)
Indicates whether namespace is one of the standart xml namespace.

Parameters:
namespace - the namespace to analyze.
Returns:
true if namespace is one of the standart xml namespace otherwise false.

getNamespaceUri

public static String getNamespaceUri(Node node)
Gets the node namespace.

Parameters:
node - the Element or Attr node to analyze.
Returns:
the node namespace or null.

hasChildElements

public static boolean hasChildElements(Element element)
Indicates whether element has any child element.

Parameters:
element - the namespace to analyze.
Returns:
true if element has any child element otherwise false.

createDocument

public static Document createDocument()
                               throws Exception
Creates W3C Document.

Returns:
the W3C Document.
Throws:
Exception - is there is some error during operation.

createDocument

public static Document createDocument(String xml)
                               throws Exception
Creates W3C Document from the xml.

Parameters:
xml - the xml that needs to be converted.
Returns:
the W3C Document.
Throws:
Exception - is there is some error during operation.

createXml

public static String createXml(Document document)
                        throws Exception
Creates XML from W3C Document from the xml.

Parameters:
document - the xml that needs to be converted.
Returns:
the XML.
Throws:
Exception - is there is some error during operation.

Jitsi: the OpenSource Java VoIP and Instant Messaging client.

Jitsi, the OpenSource Java VoIP and Instant Messaging client.
Distributable under LGPL license.