net.ex_337.gradient.util
Class XMLUtils

java.lang.Object
  extended bynet.ex_337.gradient.util.XMLUtils

public class XMLUtils
extends Object

Static XML utility methods.


Field Summary
static String DIRECTIVE
          The element name for a Gradient of an document modification directive.
static String DIRECTIVE_SET
          The element name for an XMPP extension enclosing a set of document modification directives.
static String DOC_REQUEST
          The element name for an Gradient document request extension.
static String DOC_RESPONSE
          The element name for the response to an a Gradient document request.
static Document FACTORY_DOCUMENT
          A generic factory document for use in creating Elements etc.
static String GRADIENT_NAMESPACE
          This is the namespace used for tagging Gradient document modification directives, Message/Presence target path/document request extensions, and thread attributes on outgoing IQs.
static DOM2XmlPullBuilder PULL2DOM
          A Pull-API to DOM converter.
static String SVG_NAMESPACE
          The namespace used by the W3C SVG 1.0 specification.
static String TARGET_PATH
          The element name for a Gradient XMPP extension specifying a target path for a given Message/Presence stanza.
 
Constructor Summary
XMLUtils()
           
 
Method Summary
static void appendTo(Document document, NamespaceContext context, String xPathExpression, List nodeList)
          Appends the nodes in nodeList to each Node in the document matching the given XPath expression using the given namespace context.
static Map getAttributesWithNS(Element element, String namespaceURI)
          Returns a Map of all attributes of the given element with the given namespace.
static Map getAttributesWithPrefix(Element element, String prefix)
          This is an ugly hack, there must be a nicer way to do it.
static String getChildCharacterData(Element parentEl)
          Returns all child character data of the given element, including CDATA sections but NOT entity references.
static List getChildElements(Element element)
          Returns a List of all child Elements of the given Element.
static Element getElementFromStream(InputStream input)
          Uses the default DOM2PullParser to retrieve a DOM Element from an InputStream.
static Element getElementFromString(String string)
          Uses the default DOM2PullParser to retrieve a DOM Element from a String The parent document will be FACTORY_DOCUMENT.
static List getMatchingNodes(Document document, NamespaceContext context, String xPathExpression)
          Compiles the given XPath expression with the given namepace context, and returns a list of all matching nodes in the given document.
static void insertBefore(Document document, NamespaceContext context, String xPathExpression, List nodeList)
          Inserts the nodes in nodeList before each Node in the document matching the given XPath expression using the given namespace context.
static Document loadDocument(InputStream input)
          For some reason this is really, really slow.
static Element loadElement(InputStream input)
          Returns the document element.
static void main(String[] args)
          Tests whatever method I last added to this class.
static Document newDocument()
          Creates a new Document.
static void printElements(List l1)
          Debug method.
static void remove(Document document, NamespaceContext context, String xPathExpression)
          Removes each node matching the given XPath expression in the given document.
static List removeElementsWithNamespace(List elements, String namespace)
          Removes all elements with the given namespace from the provided list.
static List removeElementsWithTagName(List elements, String tagName)
          Removes all elements with the given tag name from the given list
static void replace(Document document, NamespaceContext context, String xPathExpression, Node replacementNode)
          Replaces each node in the document matching the given XPath expression with the given node.
static String serialiseElement(Element element)
          Returns the String serialisation of an Element.
static String serialiseElementNicely(Element element)
          Returns the String serialisation of an Element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FACTORY_DOCUMENT

public static final Document FACTORY_DOCUMENT
A generic factory document for use in creating Elements etc. that will never be attached to a document.


SVG_NAMESPACE

public static final String SVG_NAMESPACE
The namespace used by the W3C SVG 1.0 specification. Value is http://www.w3.org/2000/svg

See Also:
Constant Field Values

DOC_REQUEST

public static final String DOC_REQUEST
The element name for an Gradient document request extension. Currently "request".

See Also:
Constant Field Values

GRADIENT_NAMESPACE

public static final String GRADIENT_NAMESPACE
This is the namespace used for tagging Gradient document modification directives, Message/Presence target path/document request extensions, and thread attributes on outgoing IQs. Currently http://ex-337.net/xmlns/xmpp/gradient.

See Also:
Constant Field Values

DIRECTIVE_SET

public static final String DIRECTIVE_SET
The element name for an XMPP extension enclosing a set of document modification directives. Currently "directive-set".

See Also:
Constant Field Values

DIRECTIVE

public static final String DIRECTIVE
The element name for a Gradient of an document modification directive. Currently "directive".

See Also:
Constant Field Values

TARGET_PATH

public static final String TARGET_PATH
The element name for a Gradient XMPP extension specifying a target path for a given Message/Presence stanza. Currently "target-path".

See Also:
Constant Field Values

DOC_RESPONSE

public static final String DOC_RESPONSE
The element name for the response to an a Gradient document request. Currently "response".

See Also:
Constant Field Values

PULL2DOM

public static DOM2XmlPullBuilder PULL2DOM
A Pull-API to DOM converter. Since it's stateless, and we use one all over the place, it saves is trouble by putting one here.

Constructor Detail

XMLUtils

public XMLUtils()
Method Detail

serialiseElementNicely

public static String serialiseElementNicely(Element element)
Returns the String serialisation of an Element. The output format omits indentation, comments, and DTD/XML declarations.

Parameters:
element - the element to serialise
Returns:
the string representation of the element, and all it's child nodes.

serialiseElement

public static String serialiseElement(Element element)
Returns the String serialisation of an Element. The output format omits comments, and DTD/XML declarations, but indented

Parameters:
element - the element to serialise
Returns:
the string representation of the element, and all it's child nodes.

newDocument

public static Document newDocument()
Creates a new Document. Ridiculously slow the first time, only used in test classes here.

Returns:
a new Document.

loadDocument

public static Document loadDocument(InputStream input)
For some reason this is really, really slow. Like, 3 seconds for a simple FACTORY_DOCUMENT. Generally used in debugging, examples and tests.

Parameters:
input - the XML input stream
Returns:
a loaded Document.

loadElement

public static Element loadElement(InputStream input)
Returns the document element. Owner document is FACTORY_DOCUMENT. Really, really slow.

Parameters:
input - the inputstream containg an XML document.
Returns:
the root element.

getChildElements

public static List getChildElements(Element element)
Returns a List of all child Elements of the given Element.

Parameters:
element - the element from which to retrieve child elements
Returns:
a List of child Elements.
Throws:
NullPointerException - if the element is null.

removeElementsWithNamespace

public static List removeElementsWithNamespace(List elements,
                                               String namespace)
Removes all elements with the given namespace from the provided list. Assumes all list objects are Elements.

Parameters:
elements - the list of elements
namespace - the namespace of the elements to remove
Returns:
the list, minus elements of the given namespace.
Throws:
ClassCastException - if any of the list members are not Elements

removeElementsWithTagName

public static List removeElementsWithTagName(List elements,
                                             String tagName)
Removes all elements with the given tag name from the given list

Parameters:
elements - the list of elements
tagName - the tag name
Returns:
thelist, minus elements with the specified name
Throws:
ClassCastException - if any of the list members are not Elements

getChildCharacterData

public static String getChildCharacterData(Element parentEl)
Returns all child character data of the given element, including CDATA sections but NOT entity references.

Parameters:
parentEl - the parent element.
Returns:
the child character data of the element, or null if the element is null.

getAttributesWithNS

public static Map getAttributesWithNS(Element element,
                                      String namespaceURI)
Returns a Map of all attributes of the given element with the given namespace. Why can we not create our own NamedNodeMaps? This would save trouble.

Parameters:
element - the elment from which to retrieve the attributes.
namespaceURI - the namespace of the attributes to retrieve.
Returns:
a Map containing the attributes names and their values.

getAttributesWithPrefix

public static Map getAttributesWithPrefix(Element element,
                                          String prefix)
This is an ugly hack, there must be a nicer way to do it. Using getPrefix doesn't work because it stops us from getting xmlns: attributes, which is what I'm using this for. Using getNamespace doesn't seem to work on xmlns attributes either. Trims the prefix on the map key.

Parameters:
element - the element from which to retrieve the attributes.
prefix - the prefix of the attributes to retrieve
Returns:
a Map containing the attributes names and their values.

printElements

public static void printElements(List l1)
Debug method. Prints to System.out each Element in the given list.

Parameters:
l1 - the list of elements to print.

getMatchingNodes

public static List getMatchingNodes(Document document,
                                    NamespaceContext context,
                                    String xPathExpression)
Compiles the given XPath expression with the given namepace context, and returns a list of all matching nodes in the given document.

Parameters:
document - the document to run the XPath expression against.
context - the namespace context to use.
xPathExpression - the XPath expression to use.
Returns:
a List of all nodes matching the XPath expression.
Throws:
FailedDirectiveException - if the XPath exrpression is malformed.

appendTo

public static void appendTo(Document document,
                            NamespaceContext context,
                            String xPathExpression,
                            List nodeList)
Appends the nodes in nodeList to each Node in the document matching the given XPath expression using the given namespace context.

Parameters:
document - the document to run the XPath expression against.
context - the namespace context of the XPath expression.
xPathExpression - the XPath expression that retrieves the list of Nodes from the Document.
nodeList - the list of nodes to append to each result of the XPath expression.

insertBefore

public static void insertBefore(Document document,
                                NamespaceContext context,
                                String xPathExpression,
                                List nodeList)
Inserts the nodes in nodeList before each Node in the document matching the given XPath expression using the given namespace context.

Parameters:
document - the document to run the XPath expression against.
context - the namespace context of the XPath expression.
xPathExpression - the XPath expression that retrieves the list of Nodes from the Document.
nodeList - the list of nodes to insert before each result of the XPath expression.

replace

public static void replace(Document document,
                           NamespaceContext context,
                           String xPathExpression,
                           Node replacementNode)
Replaces each node in the document matching the given XPath expression with the given node.

Parameters:
document - the document to run the XPath expression against.
context - the namespace context of the XPath expression.
xPathExpression - the XPath expression that retrieves the list of Nodes from the Document.
replacementNode - the node with which to replace each result of the XPath expression.

remove

public static void remove(Document document,
                          NamespaceContext context,
                          String xPathExpression)
Removes each node matching the given XPath expression in the given document.

Parameters:
document - the document to run the XPath expression against.
context - the namespace context of the XPath expression.
xPathExpression - the XPath expression that retrieves the list of Nodes to remove from the Document.

getElementFromStream

public static Element getElementFromStream(InputStream input)
Uses the default DOM2PullParser to retrieve a DOM Element from an InputStream. The parent document will be FACTORY_DOCUMENT. Aw, a brand new baby node! Congratulations!

Parameters:
input - the InputStream from which to read the element markup
Returns:
the Element produced by the DOM2PullParser.
Throws:
GeneralRuntimeException - if an IOException or XmlPullParserException is thrown during parsing.

getElementFromString

public static Element getElementFromString(String string)
Uses the default DOM2PullParser to retrieve a DOM Element from a String The parent document will be FACTORY_DOCUMENT.

Parameters:
string - the String containing the element markup
Returns:
the Element produced by the DOM2PullParser.
Throws:
GeneralRuntimeException - if an IOException or XmlPullParserException is thrown during parsing.

main

public static void main(String[] args)
Tests whatever method I last added to this class.

Parameters:
args - the arguments, if you insist.