junit.extensions.xml
Class XMLUtil

java.lang.Object
  extended byjunit.extensions.xml.XMLUtil
All Implemented Interfaces:
XMLConstants

public final class XMLUtil
extends java.lang.Object
implements XMLConstants

Interface layer to w3c dom.

Author:
Kevin Wilson

Field Summary
 
Fields inherited from interface junit.extensions.xml.XMLConstants
ACTION, ACTUALOBJ, ACTUALREFID, ADD, ASSERTENABLED, ASSERTEQUALS, ASSERTNOTEQUALS, ASSERTNOTNULL, ASSERTNOTSAME, ASSERTNULL, ASSERTSAME, CALL, CASE, CHOOSE, CLASSNAME, COLUMN, CONFIRM, DEBUG, DEFAULT, DELIMITER, DIALOG, DUMP, ECHO, ENABLED, ENCODING, EVALUATE, EXPECTEDOBJ, EXPECTEDREFID, FAIL, FILE, FOCUS, FOREACH, ID, INDEX, JFCFILELOC, LESSTHAN, LISTITEM, LOG, MARK, MESSAGE, METHOD, MODE, NAME, OTHERWISE, PATHREFID, PROCEDURE, PROPERTY, RECURSIVE, REFID, RELATIVE, REMOVE, ROW, SAVE, STDERR, STDOUT, STOPWATCH, SUITE, SWITCH, TABLECELL, TAGHANDLERS, TAGNAME, TEST, TYPE, USERE, VALUE, WHEN, WHILE
 
Constructor Summary
private XMLUtil()
          Private constructor: so that this class is not instantiated.
 
Method Summary
static java.lang.String getAttribute(org.w3c.dom.Element element, java.lang.String name)
          A utility method that will get the attribute value, and if not found, will return null.
static boolean getBooleanAttributeValue(org.w3c.dom.Element element, java.lang.String name)
          A utility method that will get the attribute value, and if not found, will return false.
static java.lang.String getName(org.w3c.dom.Node node)
          The name of the Node.
static java.lang.String getPath(org.w3c.dom.Node node)
          A utility method to build the path of the Node (with '.' are the separators).
static java.lang.String getURLFromClassPath(java.lang.String file)
          Resolve the path to the file.
static boolean hasAttribute(org.w3c.dom.Element element, java.lang.String name)
          Checks the element for an attribute with the given name.
static org.w3c.dom.Document parse(java.io.InputStream stream)
          This method is used to parse the xml file with the specified name.
static java.io.InputStream readFileFromClassContext(java.lang.Class clz, java.lang.String fileName)
          A utility method to find a file from the context of the specified class and read its contents.
static java.io.InputStream readFileFromClasspath(java.lang.String fileName)
          A utility method to find a file from the classpath and read its contents.
static void writeFile(java.lang.String encoding, java.io.OutputStream strm, org.w3c.dom.Document doc)
          Write the XML Document with the given encoding to a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUtil

private XMLUtil()
Private constructor: so that this class is not instantiated.

Method Detail

getAttribute

public static java.lang.String getAttribute(org.w3c.dom.Element element,
                                            java.lang.String name)
A utility method that will get the attribute value, and if not found, will return null.

Parameters:
element - The element to be searched
name - The name of the attribute to be searched for
Returns:
String The string value of the attribute, null if the value is not set or is an empty string

getBooleanAttributeValue

public static boolean getBooleanAttributeValue(org.w3c.dom.Element element,
                                               java.lang.String name)
A utility method that will get the attribute value, and if not found, will return false.

Parameters:
element - The element to be searched
name - The name of the attribute to be searched for
Returns:
boolean The boolean value of the attribute, false if the value is not set or is an empty string

getName

public static java.lang.String getName(org.w3c.dom.Node node)
The name of the Node. First, checks the attribute value, and if that is null, uses the getNodeName() method on the Node interface.

Parameters:
node - The Node whose name is to be found.
Returns:
String The name of the node (priority is given to the attribute called 'name')

getPath

public static java.lang.String getPath(org.w3c.dom.Node node)
A utility method to build the path of the Node (with '.' are the separators).

Parameters:
node - The Node whose path is to be found.
Returns:
String The String representation of the path.

getURLFromClassPath

public static java.lang.String getURLFromClassPath(java.lang.String file)
Resolve the path to the file.

Parameters:
file - File path to resolve.
Returns:
String absolute path or URL.

hasAttribute

public static boolean hasAttribute(org.w3c.dom.Element element,
                                   java.lang.String name)
Checks the element for an attribute with the given name.

Parameters:
element - element to be checked.
name - Name of the attribute.
Returns:
true if the attribute exists.

parse

public static org.w3c.dom.Document parse(java.io.InputStream stream)
This method is used to parse the xml file with the specified name.

Parameters:
stream - The input stream of the xml file to be parsed.
Returns:
Document The root Document object.

readFileFromClassContext

public static java.io.InputStream readFileFromClassContext(java.lang.Class clz,
                                                           java.lang.String fileName)
A utility method to find a file from the context of the specified class and read its contents. The difference between this method and the readFileFromClasspath(String) method is that this one uses the classpath, and then appends the package information of the specified class and then looks for the specified file.

Parameters:
clz - Class whos resource loader should be used.
fileName - The name of the file to be read.
Returns:
InputStream The file contents as an InputStream.

readFileFromClasspath

public static java.io.InputStream readFileFromClasspath(java.lang.String fileName)
A utility method to find a file from the classpath and read its contents.

Parameters:
fileName - The name of the file to be read.
Returns:
InputStream The file contents as an InputStream.

writeFile

public static void writeFile(java.lang.String encoding,
                             java.io.OutputStream strm,
                             org.w3c.dom.Document doc)
                      throws java.io.UnsupportedEncodingException
Write the XML Document with the given encoding to a file.

Parameters:
encoding - Encoding to use when writing the file.
strm - Stream to write the document.
doc - XML Document to be written.
Throws:
java.io.UnsupportedEncodingException - thrown if the implementation of the encoding cannot be found.


Licensed under: GNU Lesser General Public License, Ver 2.1, February 1999