Class DOMutil

java.lang.Object
  extended by DOMutil

public class DOMutil
extends java.lang.Object

Tools for dealing with XML files

Author:
Guy Lapalme, Université de Montréal, 2013

Nested Class Summary
private static class DOMutil.MyErrorHandler
          Error handling of the validation Set valid to false in the case of a fatal error or an error
 
Field Summary
private static java.lang.StringBuffer blanks
           
private static long start
           
private static boolean valid
           
 
Constructor Summary
DOMutil()
           
 
Method Summary
static java.lang.String attribute(org.w3c.dom.Element node, java.lang.String name)
          Return the value of the attribute of an element
private static java.lang.String blanks(int n)
           
static java.util.List<org.w3c.dom.Element> child(org.w3c.dom.Element node, java.lang.String name)
          Return a list of Elements that are immediate children of a node whose name matches name
static void compact(org.w3c.dom.Node node, java.lang.String indent)
          Output a "compact" form of a DOM node and its descendants taken from http://www.iro.umontreal.ca/~lapalme/ForestInsteadOfTheTrees/programs/Java/DOMCompact.java This is more informative than the standard toString() on Node(s)
static org.w3c.dom.Element firstChildElement(org.w3c.dom.Node node)
          Return the first child element (skipping non element node) of a node
static java.lang.String getText(org.w3c.dom.Element node)
          Get the text context of the first child node
static boolean isValid()
          Indicates if last parsed document was valid
static org.w3c.dom.Element nextElement(org.w3c.dom.Node node)
          Return the next sibling element of a node (skipping non element node)
static org.w3c.dom.Element parseXMLdoc(java.lang.String fileName)
          Parse an XML file without validation
static org.w3c.dom.Element parseXMLdoc(java.lang.String fileName, java.lang.String schemaFileName)
          Parse an XML document with validation and return the root element
static void setStartTime()
          Set timing start
static java.lang.String showTime()
          Show time elapsed since call to setStartTime
static void stripSpace(org.w3c.dom.Node node)
          Remove empty text nodes (ie nothing else than spaces and carriage return) and nodes that are not text or element ones
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

valid

private static boolean valid

start

private static long start

blanks

private static java.lang.StringBuffer blanks
Constructor Detail

DOMutil

public DOMutil()
Method Detail

setStartTime

public static void setStartTime()
Set timing start


showTime

public static java.lang.String showTime()
Show time elapsed since call to setStartTime

Returns:
formatted string giving time in seconds

parseXMLdoc

public static org.w3c.dom.Element parseXMLdoc(java.lang.String fileName,
                                              java.lang.String schemaFileName)
Parse an XML document with validation and return the root element

Parameters:
fileName - name of the XML file
schemaFileName - name of the schema file if null, no validation type of validation depends on the extension of the file name (.dtd, .xsd, .rnc)
Returns:
root element

parseXMLdoc

public static org.w3c.dom.Element parseXMLdoc(java.lang.String fileName)
Parse an XML file without validation

Parameters:
fileName - name of XML file
Returns:
root element

isValid

public static boolean isValid()
Indicates if last parsed document was valid

Returns:
true if validation without error or fatal error or if not validation was performed

stripSpace

public static void stripSpace(org.w3c.dom.Node node)
Remove empty text nodes (ie nothing else than spaces and carriage return) and nodes that are not text or element ones

Parameters:
node -

attribute

public static java.lang.String attribute(org.w3c.dom.Element node,
                                         java.lang.String name)
Return the value of the attribute of an element

Parameters:
node - the element
name - name of the attribute
Returns:
string value of the attribute

child

public static java.util.List<org.w3c.dom.Element> child(org.w3c.dom.Element node,
                                                        java.lang.String name)
Return a list of Elements that are immediate children of a node whose name matches name

Parameters:
node - the element
name - regexp for the name of the children to return
Returns:
list of element children

nextElement

public static org.w3c.dom.Element nextElement(org.w3c.dom.Node node)
Return the next sibling element of a node (skipping non element node)

Parameters:
node - node
Returns:
next sibling element

firstChildElement

public static org.w3c.dom.Element firstChildElement(org.w3c.dom.Node node)
Return the first child element (skipping non element node) of a node

Parameters:
node - the element
Returns:
the first child element

getText

public static java.lang.String getText(org.w3c.dom.Element node)
Get the text context of the first child node

Parameters:
node - the element
Returns:
its text content (throws IllegalArgumentException is the type of the first child is not a text node)

compact

public static void compact(org.w3c.dom.Node node,
                           java.lang.String indent)
Output a "compact" form of a DOM node and its descendants taken from http://www.iro.umontreal.ca/~lapalme/ForestInsteadOfTheTrees/programs/Java/DOMCompact.java This is more informative than the standard toString() on Node(s)

Parameters:
node - node to start compacting from
indent - current indentation

blanks

private static java.lang.String blanks(int n)