se.lth.cs.nlp.nlputils.core
Class DOMUtils

java.lang.Object
  extended by se.lth.cs.nlp.nlputils.core.DOMUtils

public class DOMUtils
extends Object

A utility class which contains static methods to simplify some chores common when working with DOM trees.

Author:
Richard Johansson (richard@cs.lth.se)

Method Summary
static void cacheNode(Node fromNode, String id, Node toNode)
           
static Document createDocument(String docName, String dtd)
          Creates a new document.
static byte[] documentToXMLBytes(Document document, String encoding)
          Converts the Document to a byte array, taking the declared XML character encoding into account.
static Node findChildByName(Node n, String childName)
          Finds the first child node with a certain name if one exists.
static Node findNodeById(Node node, String id)
          This is needed because Document.getElementById does not seems to work well in our DOM implementation.
static Node firstChild(Node node)
          Returns the first non-text child of the node.
static Node lastChild(Node node)
          Returns the last non-text child of the node.
static Node nextSibling(Node node)
          Returns the next non-text sibling of the node.
static Node previousSibling(Node node)
          Returns the previous non-text sibling of the node.
static void printStats()
           
static Document readDocument(InputSource in)
          Reads a document.
static Document readDocument(InputStream in)
          Reads a document.
static void replaceChildByName(Node n, String childName, Node newChild, String[] predecessors)
          Replaces a child node.
static String treeToString(Document document)
           
static String treeToString(Document document, String charset)
          Returns a string containing the XML printout of a document.
static void validate(Document document)
          Validates a document against its DTD.
static void writeDocument(Document document, OutputStream out)
           
static void writeDocument(Document document, StreamResult out)
           
static Document XMLBytesToDocument(byte[] bytes)
          Converts the byte array to a Document, taking the declared XML character encoding into account.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

replaceChildByName

public static void replaceChildByName(Node n,
                                      String childName,
                                      Node newChild,
                                      String[] predecessors)
Replaces a child node. This method replaces a certain child node when the children must appear in a specific order.

Parameters:
n - The parent node.
childName - The node name of the child to replace.
newChild - The new child node.
predecessors - The names of those children which may appear before the child to replace.

findChildByName

public static Node findChildByName(Node n,
                                   String childName)
Finds the first child node with a certain name if one exists.

Parameters:
n - The parent node.
childName - The name of the child node.
Returns:
The child node.

createDocument

public static Document createDocument(String docName,
                                      String dtd)
Creates a new document.


readDocument

public static Document readDocument(InputSource in)
Reads a document.


readDocument

public static Document readDocument(InputStream in)
Reads a document.


writeDocument

public static void writeDocument(Document document,
                                 StreamResult out)

writeDocument

public static void writeDocument(Document document,
                                 OutputStream out)

treeToString

public static String treeToString(Document document,
                                  String charset)
Returns a string containing the XML printout of a document.


treeToString

public static String treeToString(Document document)

XMLBytesToDocument

public static Document XMLBytesToDocument(byte[] bytes)
Converts the byte array to a Document, taking the declared XML character encoding into account.


documentToXMLBytes

public static byte[] documentToXMLBytes(Document document,
                                        String encoding)
Converts the Document to a byte array, taking the declared XML character encoding into account.


findNodeById

public static Node findNodeById(Node node,
                                String id)
This is needed because Document.getElementById does not seems to work well in our DOM implementation. We assume that every attribute which is called "id" is an id. Will only find nodes which are reachable from the first node.


cacheNode

public static void cacheNode(Node fromNode,
                             String id,
                             Node toNode)

printStats

public static void printStats()

validate

public static void validate(Document document)
Validates a document against its DTD.


firstChild

public static Node firstChild(Node node)
Returns the first non-text child of the node.


nextSibling

public static Node nextSibling(Node node)
Returns the next non-text sibling of the node.


lastChild

public static Node lastChild(Node node)
Returns the last non-text child of the node.


previousSibling

public static Node previousSibling(Node node)
Returns the previous non-text sibling of the node.