uk.ac.bbk.dcs.automed.xml
Class NodeUtility

java.lang.Object
  |
  +--uk.ac.bbk.dcs.automed.xml.NodeUtility

public class NodeUtility
extends java.lang.Object

Implements useful operation on nodes.

Author:
Lucas Zamboulis

Constructor Summary
NodeUtility()
           
 
Method Summary
static org.w3c.dom.Document addGenericRoot(org.w3c.dom.Document doc, java.lang.String newRootName)
          Method that adds a generic root named newRootName on top of the old root and returns the new document.
static void banner()
          Prints a line of 90 stars.
static void banner(int num)
          Prints a line of num stars.
static org.w3c.dom.Document duplicateDOM(org.w3c.dom.Document doc)
          Method that creates and returns a new DOM representation of the input one.
static org.w3c.dom.Document duplicateDOM(java.lang.String URI, boolean validating)
           
static org.apache.xpath.domapi.XPathResultImpl evaluateXPath(org.w3c.dom.Document doc, java.lang.String xpathExpression, int resultType)
           
static org.w3c.dom.Document fileCreate(java.lang.String rootName, java.lang.String filePath, java.lang.String fileName)
          Creates a new file named "fileName" in path "filePath", with only the root, named "rootName", and returns its DOM representation.
static int findPath(org.w3c.dom.Node supposedChild, org.w3c.dom.Node supposedParent, java.util.LinkedList goingUpPath, java.util.LinkedList goingDownPath)
           
static int getChildCount(org.w3c.dom.Node parentNode)
          Counts the children of the given node.
static int getDepth(org.w3c.dom.Document document)
           
static org.w3c.dom.Node getElementByUID(org.w3c.dom.Document doc, java.lang.String elementUID)
          Returns the node in the input doc that has an AutoMedID with the value of elementUID
static org.w3c.dom.Node getElementByUIDNoSchema(org.w3c.dom.Document doc, java.lang.String elementUID)
          This method will return the first occurrence of an element containing an AutoMedID whose suffix after "schemaName:" matches the input elementUID.
static int getElementChildCount(org.w3c.dom.Node parentNode)
          Counts the element children of the given node.
static java.util.LinkedList getElementLevel(org.w3c.dom.Node n, int level)
          Returns all the element nodes in a given level.
static int getElementNodeOrder(org.w3c.dom.Node n)
          Returns the order of element node n in its parent's children NodeList - takes into account only elements.
static java.util.LinkedList getElementsByUID(org.w3c.dom.Document doc, java.lang.String id)
          Given a schema UID (schemaName:elementName:counter), this method runs through the given instance, and returns all elements with an instance UID starting with the given schema UID.
static java.util.LinkedList getLevel(org.w3c.dom.Node n, int level)
          Returns all the nodes in a given level.
static int getNodeDepth(org.w3c.dom.Node n)
          Returns the depth of node n.
static int getNodeOrder(org.w3c.dom.Node n)
           
static java.lang.String getPath(org.w3c.dom.Node n)
           
static int getPCDATAChildCount(org.w3c.dom.Node parentNode)
          Counts the element children of the given node.
static int getPCDATANodeOrder(org.w3c.dom.Node n)
           
static java.util.LinkedList getTextNodes(org.w3c.dom.Node n)
           
static java.lang.String getUnderlyingText(org.w3c.dom.Node n, java.lang.String separator)
          Returns a concatenated string of all text nodes under this node - not only its children's text nodes.
static boolean hasTextNodes(org.w3c.dom.Node n)
           
static boolean isElementNode(org.w3c.dom.Node n)
          Returns true if node n is an element node, false otherwise
static boolean isTextNode(org.w3c.dom.Node n)
          Returns true if node n is a text node, false otherwise
static void main(java.lang.String[] args)
           
static org.w3c.dom.Document parseFile(java.lang.String inputFile, boolean validating)
          Parses the input file and returns its DOM representation
static void prettyPrint(org.w3c.dom.Node n)
          Outputs the subtree with root n to System.out with identation, without any respect to the file's original identation
static void prettyPrint(org.w3c.dom.Node n, java.lang.String title)
          Outputs the subtree with root n to System.out with identation, without any respect to the file's original identation.
static void printDocument(org.w3c.dom.Document doc)
          Outputs Document doc in System.out exactly as it is in the DOM.
static void printDocument(org.w3c.dom.Document doc, java.io.File f)
          Outputs Document doc in File f exactly as it is in the DOM.
static org.w3c.dom.Document stripUIDs(org.w3c.dom.Document inputDoc)
          This method takes as input an XML document in DOM format, duplicates it, then strips the uids from the duplicated DOM.
static void title(java.lang.String title)
          Prints a line of stars, then the input title centered inside a line of stars, then another line of stars.
static void writeDOM(org.w3c.dom.Document doc, java.lang.String url)
          Serializes the input DOM representation into file "fileName" in path "filePath".
static void writeDOM(org.w3c.dom.Document doc, java.lang.String filePath, java.lang.String fileName)
          Serializes the input DOM representation into file "fileName" in path "filePath".
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NodeUtility

public NodeUtility()
Method Detail

printDocument

public static void printDocument(org.w3c.dom.Document doc,
                                 java.io.File f)
Outputs Document doc in File f exactly as it is in the DOM. This means that if there is no identation, it will not add any.


printDocument

public static void printDocument(org.w3c.dom.Document doc)
Outputs Document doc in System.out exactly as it is in the DOM. This means that if there is no identation, it will not add any.


banner

public static void banner(int num)
Prints a line of num stars.


title

public static void title(java.lang.String title)
Prints a line of stars, then the input title centered inside a line of stars, then another line of stars.


banner

public static void banner()
Prints a line of 90 stars.


prettyPrint

public static void prettyPrint(org.w3c.dom.Node n,
                               java.lang.String title)
Outputs the subtree with root n to System.out with identation, without any respect to the file's original identation.

Parameters:
n - the root of the subtree - will accept a Document variable

prettyPrint

public static void prettyPrint(org.w3c.dom.Node n)
Outputs the subtree with root n to System.out with identation, without any respect to the file's original identation

Parameters:
n - the root of the subtree - will accept a Document variable

getPath

public static java.lang.String getPath(org.w3c.dom.Node n)
Parameters:
n - the input node
Returns:
the path from the root to the input node - separator is '/'

findPath

public static int findPath(org.w3c.dom.Node supposedChild,
                           org.w3c.dom.Node supposedParent,
                           java.util.LinkedList goingUpPath,
                           java.util.LinkedList goingDownPath)
Parameters:
supposedChild - the node from where the path begins
supposedParent - the node the path ends at
goingUpPath - a LinkedList object containing the nodes from supposedChild to supposedParent. Should be given empty, is returned full. Contains the whole path only if the path contains only upwards edges.
goingDownPath - a LinkedList object containing the nodes from common ancestor to supposedParent. Should be given empty, is returned full if the path includes a downwards sub-path.
Returns:
an integer in [1,5]:
- 1 means the two nodes are in the same branch and toNode is higher than fromNode
- 2 means the two nodes are in the same branch and toNode is lower than fromNode
- 3 means the two nodes are in different branches and toNode is higher than fromNode
- 4 means the two nodes are in different branches and toNode is lower than fromNode
- 5 means the two nodes are in different branches and toNode is in the same height as fromNode

getLevel

public static java.util.LinkedList getLevel(org.w3c.dom.Node n,
                                            int level)
Returns all the nodes in a given level. The root has a level of 0.

Parameters:
n - the root of the subtree - will accept a Document variable
level - the needed level - remember that the root has a level of 0
Returns:
a LinkedList with the nodes comprising the level (ordered)

getTextNodes

public static java.util.LinkedList getTextNodes(org.w3c.dom.Node n)

getElementLevel

public static java.util.LinkedList getElementLevel(org.w3c.dom.Node n,
                                                   int level)
Returns all the element nodes in a given level. The root has a level of 0.

Parameters:
n - the root of the subtree - will accept a Document variable
level - the needed level - remember that the root has a level of 0
Returns:
a LinkedList with the nodes comprising the level (ordered)

getDepth

public static int getDepth(org.w3c.dom.Document document)
Parameters:
document - the input document
Returns:
the depth of the tree (2 if there exists a root and 2 more levels, 0 if there exists only the root)

getNodeDepth

public static int getNodeDepth(org.w3c.dom.Node n)
Returns the depth of node n. If n is the root, its depth is 0, etc.


getElementsByUID

public static java.util.LinkedList getElementsByUID(org.w3c.dom.Document doc,
                                                    java.lang.String id)
Given a schema UID (schemaName:elementName:counter), this method runs through the given instance, and returns all elements with an instance UID starting with the given schema UID.

Parameters:
doc - the instance document
id - the schema AutoMedID
Returns:
a LinkedList containing matched elements

getElementByUID

public static org.w3c.dom.Node getElementByUID(org.w3c.dom.Document doc,
                                               java.lang.String elementUID)
Returns the node in the input doc that has an AutoMedID with the value of elementUID


getElementByUIDNoSchema

public static org.w3c.dom.Node getElementByUIDNoSchema(org.w3c.dom.Document doc,
                                                       java.lang.String elementUID)
                                                throws NotFoundException
This method will return the first occurrence of an element containing an AutoMedID whose suffix after "schemaName:" matches the input elementUID. Created for cases where we know that elementUID is unique within the given document, but are not sure about the document's schemaName. Not to be used in documents containing AutoMedIDs with multiple "schemaName:" prefixes.

Parameters:
doc - the instance document from which to get the element needed
elementUID - the UID without the "schemaName:" prefix
Returns:
the DOM node with the matching elementUID
NotFoundException

getChildCount

public static int getChildCount(org.w3c.dom.Node parentNode)
Counts the children of the given node.


getElementChildCount

public static int getElementChildCount(org.w3c.dom.Node parentNode)
Counts the element children of the given node.


getPCDATAChildCount

public static int getPCDATAChildCount(org.w3c.dom.Node parentNode)
Counts the element children of the given node.


getNodeOrder

public static int getNodeOrder(org.w3c.dom.Node n)
Parameters:
n - the input node
Returns:
the order of node n in its parent's children NodeList - taking into account both elements and PCDATA

getPCDATANodeOrder

public static int getPCDATANodeOrder(org.w3c.dom.Node n)

getElementNodeOrder

public static int getElementNodeOrder(org.w3c.dom.Node n)
Returns the order of element node n in its parent's children NodeList - takes into account only elements.


isTextNode

public static boolean isTextNode(org.w3c.dom.Node n)
Returns true if node n is a text node, false otherwise


isElementNode

public static boolean isElementNode(org.w3c.dom.Node n)
Returns true if node n is an element node, false otherwise


hasTextNodes

public static boolean hasTextNodes(org.w3c.dom.Node n)

parseFile

public static org.w3c.dom.Document parseFile(java.lang.String inputFile,
                                             boolean validating)
Parses the input file and returns its DOM representation

Parameters:
inputFile - the location of the input file
validating - whether the parser should consult the accompanying (?) schema
Returns:
the DOM representation of the input file

writeDOM

public static void writeDOM(org.w3c.dom.Document doc,
                            java.lang.String url)
Serializes the input DOM representation into file "fileName" in path "filePath".


writeDOM

public static void writeDOM(org.w3c.dom.Document doc,
                            java.lang.String filePath,
                            java.lang.String fileName)
Serializes the input DOM representation into file "fileName" in path "filePath".


fileCreate

public static org.w3c.dom.Document fileCreate(java.lang.String rootName,
                                              java.lang.String filePath,
                                              java.lang.String fileName)
Creates a new file named "fileName" in path "filePath", with only the root, named "rootName", and returns its DOM representation. Deletes the temp file.


duplicateDOM

public static org.w3c.dom.Document duplicateDOM(org.w3c.dom.Document doc)
Method that creates and returns a new DOM representation of the input one.


duplicateDOM

public static org.w3c.dom.Document duplicateDOM(java.lang.String URI,
                                                boolean validating)
Parameters:
URI - the location of the XML file
validating - set to true in order to validate file against referenced DTD/XML Schema
Returns:
returns a new DOM representation of the input XML file

stripUIDs

public static org.w3c.dom.Document stripUIDs(org.w3c.dom.Document inputDoc)
This method takes as input an XML document in DOM format, duplicates it, then strips the uids from the duplicated DOM.

Parameters:
inputDoc - the input document
Returns:
the inputDoc without uids

evaluateXPath

public static org.apache.xpath.domapi.XPathResultImpl evaluateXPath(org.w3c.dom.Document doc,
                                                                    java.lang.String xpathExpression,
                                                                    int resultType)
Parameters:
doc - the document against which to evaluate the XPath expression
xpathExpression - the XPath expression to evaluate
resultType - 0 for ORDERED_NODE_SNAPSHOT_TYPE, 1 for ORDERED_NODE_ITERATOR_TYPE
Returns:
the result of the evaluation of the XPath expression against document doc, in an XPathResultImpl form

getUnderlyingText

public static java.lang.String getUnderlyingText(org.w3c.dom.Node n,
                                                 java.lang.String separator)
Returns a concatenated string of all text nodes under this node - not only its children's text nodes.

Parameters:
n - the root of the subtree - will accept a Document variable
separator - the string separating the strings of the underlying text nodes

addGenericRoot

public static org.w3c.dom.Document addGenericRoot(org.w3c.dom.Document doc,
                                                  java.lang.String newRootName)
Method that adds a generic root named newRootName on top of the old root and returns the new document.


main

public static void main(java.lang.String[] args)