uk.ac.starlink.util
Class DOMUtils

java.lang.Object
  extended by uk.ac.starlink.util.DOMUtils

public class DOMUtils
extends Object

Provides convenience methods for handling DOMs.


Method Summary
static Element getChildElementByName(Node parent, String name)
          Returns the first child element of a node which has a given name.
static Element getFirstElementSibling(Node node)
          Returns the first subsequent sibling of a given node which is an Element.
static String getTextContent(Element el)
          Returns a string representing the plain text content of an element.
static String mapNodeType(short nodeType)
          Maps a node type, as returned by to a name.
static Document newDocument()
          Returns a new Document instance.
static Node relativizeDOM(Node n, URI baseURI, String attname)
          Traverses the given DOM, relativising all the URIs in the uri attributes of each Element.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getChildElementByName

public static Element getChildElementByName(Node parent,
                                            String name)
Returns the first child element of a node which has a given name.

Parameters:
parent - the node whose children are to be searched
name - the name of the element being searched for
Returns:
the first child of parent which is an Element and has the tagname name, or null if none match

getTextContent

public static String getTextContent(Element el)
Returns a string representing the plain text content of an element. Any comments, attributes, elements or other non-text children are ignored, and all CDATA and Text nodes are merged to give a single string.

Parameters:
el - the element whose text content is wanted
Returns:
the pure text content. If there is none, an empty string is returned.

getFirstElementSibling

public static Element getFirstElementSibling(Node node)
Returns the first subsequent sibling of a given node which is an Element. This is useful for naviating a DOM as a tree of elements when the presence of text or attribute children is a distraction.

Parameters:
node - the node whose siblings (including itself) you are interested in. May be null
Returns:
the first sibling of node which is an Element. If node itself is an element, that is returned. If node has no subsequent siblings which are elements, or if it is null, then null is returned.

relativizeDOM

public static Node relativizeDOM(Node n,
                                 URI baseURI,
                                 String attname)
Traverses the given DOM, relativising all the URIs in the uri attributes of each Element.

The (uri-attribute) nodes in the input DOM are modified by this method; if this is a problem, use Node.cloneNode(boolean) first.

Parameters:
n - a node containing the DOM whose URIs are to be relativized. If this is null, the method immediately returns null
baseURI - the URI relative to which the DOM is to be relativised. If this is null, then the input node is immediately returned unchanged.
attname - the attribute name to be used. If null, this defaults to uri
Returns:
the input node
See Also:
URI.relativize(java.net.URI)

mapNodeType

public static String mapNodeType(short nodeType)
Maps a node type, as returned by to a name. The node types returned by Node.getNodeType() are numeric and are therefore inconveniently opaque.

Parameters:
nodeType - a numeric Node type, one of the node type constants defined in Node
Returns:
a string name for the type

newDocument

public static Document newDocument()
Returns a new Document instance. This method just does all the tedious business of mucking about with factories for you.

Returns:
an empty Document

Copyright © 2004 CLRC: Central Laboratory of the Research Councils. All rights reserved.