uk.ac.bbk.dcs.automed.xml.utilities
Class XMLUtilities

java.lang.Object
  extended by uk.ac.bbk.dcs.automed.xml.utilities.XMLUtilities

public class XMLUtilities
extends java.lang.Object

Implements useful operation on nodes.

Author:
Lucas Zamboulis

Field Summary
private static java.lang.String XML_ENCODING
           
private static java.lang.String XML_VERSION
           
 
Constructor Summary
XMLUtilities()
           
 
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 collapseWhiteSpace(org.w3c.dom.Node n)
           
static java.lang.String collapseWhitespace(java.lang.String s)
           
static org.w3c.dom.Document createDOMDocument(org.w3c.dom.Element root, 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 org.w3c.dom.Document duplicateDOM(org.w3c.dom.Document doc)
          Method that creates and returns a new DOM representation of the input one - no validation on either doc.
static org.w3c.dom.Document duplicateDOM(java.lang.String URI, boolean validating)
           
static int findPath(org.w3c.dom.Node supposedChild, org.w3c.dom.Node supposedParent, java.util.LinkedList goingUpPath, java.util.LinkedList goingDownPath)
          IMPORTANT: Both goingUpPath and goingDownPath lists contain the common ancestor.
static float getAverageFanOut(org.w3c.dom.Document document)
           
static int getChildCount(org.w3c.dom.Node parentNode)
          Counts the children of the given node.
private static javax.xml.parsers.DocumentBuilderFactory getDBFactory()
           
static int getDepth(org.w3c.dom.Document document)
           
static int getElementChildCount(org.w3c.dom.Node parentNode)
          Counts the element children of the given node.
static java.util.List getElementChildren(org.w3c.dom.Element e)
           
static java.util.List getElementChildren(org.w3c.dom.Element e, java.lang.String tagName)
           
static java.util.List getElementLevel(org.w3c.dom.Node n, int level)
          Returns all the element nodes in a given level.
static int getElementNodeOrder(org.w3c.dom.Element n)
          Returns the order of element node n in its parent's children NodeList - takes into account only elements.
static java.lang.String getElementPathExpression(org.w3c.dom.Element e)
           
static org.w3c.dom.Document getEmptyDocument(java.lang.String rootName)
           
static org.w3c.dom.Document getEmptyDOM(java.lang.String rootName)
           
static java.util.List 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 int getTextChildCount(org.w3c.dom.Node parentNode)
          Counts the number of text chidren of the input node.
static java.util.List getTextChildren(org.w3c.dom.Element e)
           
static int getTextNodeOrder(org.w3c.dom.Node n)
           
static java.util.List getTextNodes(org.w3c.dom.Element e)
           
static int getTransformationPathwayLength(java.lang.String sourceSchemaName, java.lang.String targetSchemaName)
          Prints out the transformation pathway created by the schema transformation algorithm.
static boolean hasTextNodes(org.w3c.dom.Element n)
           
private static void helpGetElementLevel(java.util.List levelList, org.w3c.dom.Node currentNode, int level)
          Method complementary to getLevel
private static void helpGetLevel(java.util.List levelList, org.w3c.dom.Node currentNode, int level)
          Method complementary to getLevel
static boolean isCDataNode(org.w3c.dom.Node n)
          Returns true if node n is a CData node, false otherwise
static boolean isElementNode(org.w3c.dom.Node n)
          Returns true if node n is an element node, false otherwise
static boolean isPCDataNode(org.w3c.dom.Node n)
          Returns true if node n is a PCData node, false otherwise
static boolean isTextNode(org.w3c.dom.Node n)
          Returns true if node n is a text node, false otherwise
static org.w3c.dom.Document parseFile(java.lang.String inputFile, boolean validating)
          Parses the input file and returns its DOM representation
static org.w3c.dom.Document parseFileNS(java.lang.String inputFile, boolean validating)
          Parses the input file and returns its DOM representation
static org.w3c.dom.Document parseString(java.lang.String inputString, boolean validating)
           
static org.w3c.dom.Document parseStringNS(java.lang.String inputString, boolean validating)
           
static void writeDOM(org.w3c.dom.Document doc, java.io.File f)
           
static void writeDOM(org.w3c.dom.Document doc, java.lang.String url)
          Serializes the input DOM representation into file "url".
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".
static void writeDOM(org.w3c.dom.Node doc, java.io.File f)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

XML_VERSION

private static final java.lang.String XML_VERSION
See Also:
Constant Field Values

XML_ENCODING

private static final java.lang.String XML_ENCODING
See Also:
Constant Field Values
Constructor Detail

XMLUtilities

public XMLUtilities()
Method Detail

getElementChildren

public static java.util.List getElementChildren(org.w3c.dom.Element e)

getTextChildren

public static java.util.List getTextChildren(org.w3c.dom.Element e)

getElementChildren

public static java.util.List getElementChildren(org.w3c.dom.Element e,
                                                java.lang.String tagName)

getElementPathExpression

public static java.lang.String getElementPathExpression(org.w3c.dom.Element e)
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)
IMPORTANT: Both goingUpPath and goingDownPath lists contain the common ancestor. It is up to the function using the result of this method to remove the duplicate common ancestor coming from one of these two lists.

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, if indeed supposedChild is in the same branch with supposed parent and is deeper than supposedParent. In this case, it is returned full. As the name suggests, this list represents going up the schema.
goingDownPath - a LinkedList object containing the nodes from common ancestor to supposedParent or from supposedChild to supposedParent, if they are in the same branch and supposedParent is deeper than supposedChild. supposedParent is always the last node in the list. As the name suggests, this list represents going up the schema.
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.List 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 List with the nodes comprising the level (ordered)

getTextNodes

public static java.util.List getTextNodes(org.w3c.dom.Element e)

helpGetLevel

private static void helpGetLevel(java.util.List levelList,
                                 org.w3c.dom.Node currentNode,
                                 int level)
Method complementary to getLevel


getElementLevel

public static java.util.List 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 List with the nodes comprising the level (ordered)

helpGetElementLevel

private static void helpGetElementLevel(java.util.List levelList,
                                        org.w3c.dom.Node currentNode,
                                        int level)
Method complementary to getLevel


getAverageFanOut

public static float getAverageFanOut(org.w3c.dom.Document document)

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.


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.


getTextChildCount

public static int getTextChildCount(org.w3c.dom.Node parentNode)
Counts the number of text chidren of the input 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 text nodes (PCData and CData)

getTextNodeOrder

public static int getTextNodeOrder(org.w3c.dom.Node n)
                            throws XMLException
Parameters:
n - the text node (PCData or CData)
Returns:
the order of the text node in the list of children of its parent
Throws:
XMLException

getElementNodeOrder

public static int getElementNodeOrder(org.w3c.dom.Element 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


isPCDataNode

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


isCDataNode

public static boolean isCDataNode(org.w3c.dom.Node n)
Returns true if node n is a CData 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.Element n)

getEmptyDOM

public static org.w3c.dom.Document getEmptyDOM(java.lang.String rootName)

getEmptyDocument

public static org.w3c.dom.Document getEmptyDocument(java.lang.String rootName)

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

parseString

public static org.w3c.dom.Document parseString(java.lang.String inputString,
                                               boolean validating)

parseFileNS

public static org.w3c.dom.Document parseFileNS(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

parseStringNS

public static org.w3c.dom.Document parseStringNS(java.lang.String inputString,
                                                 boolean validating)

writeDOM

public static void writeDOM(org.w3c.dom.Document doc,
                            java.io.File f)

writeDOM

public static void writeDOM(org.w3c.dom.Node doc,
                            java.io.File f)

writeDOM

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


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".


createDOMDocument

public static org.w3c.dom.Document createDOMDocument(org.w3c.dom.Element root,
                                                     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 - no validation on either doc.


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 to turn validation on for the input document
Returns:
a non-validated copy of the input XML file

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.


getDBFactory

private static javax.xml.parsers.DocumentBuilderFactory getDBFactory()

getTransformationPathwayLength

public static int getTransformationPathwayLength(java.lang.String sourceSchemaName,
                                                 java.lang.String targetSchemaName)
Prints out the transformation pathway created by the schema transformation algorithm.

Parameters:
sourceSchemaName - the name of the source schema
targetSchemaName - the name of the target schema

collapseWhiteSpace

public static void collapseWhiteSpace(org.w3c.dom.Node n)
                               throws XMLException
Parameters:
d - removes all whitespace (as in XML Schema's collapse whitespace) in the input document
Throws:
XMLException

collapseWhitespace

public static java.lang.String collapseWhitespace(java.lang.String s)