org.optimizationservices.oscommon.util
Class XMLUtil

java.lang.Object
  extended by org.optimizationservices.oscommon.util.XMLUtil

public class XMLUtil
extends java.lang.Object

The XMLUtil class contains methods for performing common basic XML-related operations used by various classes in the Optimization Services (OS) framework.

Since:
OS1.0
Version:
1.0, 03/14/2004
Author:
Robert Fourer, Jun Ma, Kipp Martin

Constructor Summary
XMLUtil()
           
 
Method Summary
static java.util.GregorianCalendar createNativeDateTime(java.lang.String xsDateTime)
          Create the the native date/time from the standard xs:dateTime.
static org.w3c.dom.Document createNewDocument()
          Obtain a new instance of a DOM Document object to build a DOM tree with.
static org.w3c.dom.Element createOSxLRootElement(org.w3c.dom.Document document, java.lang.String osxlName)
          Create the basic OSxL root element.
static java.lang.String createXSDateTime(java.util.GregorianCalendar dateTime)
          Create the standard xs:dateTime string from the native date/time
static org.w3c.dom.Node findChildNode(org.w3c.dom.Node parentNode, java.lang.String childNodeName)
          Find a parent node's first child node by the child node's name.
static java.lang.String generateSaxParseExceptionInXML(java.lang.String exceptionType, org.xml.sax.SAXParseException e)
          Returns the SAX Exception in the following XML format:
static java.util.Vector<org.w3c.dom.Element> getChildElementsByTagName(org.w3c.dom.Node parentNode, java.lang.String childNodeName)
          Get a parent node's child elements by the child node's tag name.
static java.lang.String getElementValue(org.w3c.dom.Element element)
          Get the value of an element in a string.
static java.lang.String getElementValueByName(org.w3c.dom.Element rootElement, java.lang.String descendantName)
          Get the text value of a descendant element by the element's name.
static void main(java.lang.String[] args)
          main for test purposes.
static org.w3c.dom.Document parseFileUsingDOM(java.lang.String fileName, boolean validate)
          Parse the xml file that contains the xml instance using DOM.
static boolean parseFileUsingSAX(java.lang.String fileName, org.xml.sax.helpers.DefaultHandler saxHandler, boolean validate)
          Parse the xml file that contains the xml instance using SAX.
static org.w3c.dom.Document parseStringUsingDOM(java.lang.String xmlString, boolean validate)
          Parse the xml string that contains the xml instance using DOM.
static boolean parseStringUsingSAX(java.lang.String xmlString, org.xml.sax.helpers.DefaultHandler saxHandler, boolean validate)
          Parse the xml string that contains the xml instance using SAX.
static int removeAllAttributes(org.w3c.dom.Element element)
          Remove all the attributes of an element.
static int removeAllChildren(org.w3c.dom.Node parentNode)
          Remove all the child nodes of a parent node.
static int removeChildrenByName(org.w3c.dom.Node parentNode, java.lang.String childName)
          Remove all the child nodes of a parent node with a given name.
static void setElementValue(org.w3c.dom.Element element, java.lang.String value)
          Set the value (i.e.
static boolean writeXMLDocumentToFile(org.w3c.dom.Document xmlDocument, java.lang.String fileName)
          Write an xml file from a DOM tree document to a file.
static boolean writeXMLDocumentToStandardOutput(org.w3c.dom.Document xmlDocument)
          Write an xml file from a DOM tree document to the standard output (e.g.
static java.lang.String writeXMLDocumentToString(org.w3c.dom.Document xmlDocument)
          Write an xml file from a DOM tree document to a string.
static boolean writeXMLElementToFile(org.w3c.dom.Element element, java.lang.String fileName)
          Write DOM tree element to a file.
static boolean writeXMLElementToStandardOutput(org.w3c.dom.Element element)
          Write DOM tree element to the standard output (e.g.
static java.lang.String writeXMLElementToString(org.w3c.dom.Element element)
          Write an xml file from a DOM tree document to a string.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUtil

public XMLUtil()
Method Detail

generateSaxParseExceptionInXML

public static java.lang.String generateSaxParseExceptionInXML(java.lang.String exceptionType,
                                                              org.xml.sax.SAXParseException e)
Returns the SAX Exception in the following XML format:

<parse_exceptionType>

<file> ... </file>

<line>...</line>

<column>...</column>

<message>...</message>

</parse_exceptionType>.

Parameters:
exceptionType - e.g. "warning", "error", "fatalError".

parseFileUsingSAX

public static boolean parseFileUsingSAX(java.lang.String fileName,
                                        org.xml.sax.helpers.DefaultHandler saxHandler,
                                        boolean validate)
Parse the xml file that contains the xml instance using SAX.

Parameters:
fileName - holds the name of the file that contains the xml instance
saxHandler - is a user implementation of the core sax handlers.
validate - holds whether the parser should be validating against the xml schema or not.
Returns:
whether the file is read successfully without any error.

parseStringUsingSAX

public static boolean parseStringUsingSAX(java.lang.String xmlString,
                                          org.xml.sax.helpers.DefaultHandler saxHandler,
                                          boolean validate)
Parse the xml string that contains the xml instance using SAX.

Parameters:
xmlString - holds the xml string that contains the xml instance
saxHandler - is a user implementation of the core sax handlers.
validate - holds whether the parser should be validating against the xml schema or not.
Returns:
whether the string is parsed successfully without any error.

parseFileUsingDOM

public static org.w3c.dom.Document parseFileUsingDOM(java.lang.String fileName,
                                                     boolean validate)
Parse the xml file that contains the xml instance using DOM.

Parameters:
fileName - holds the name of the file that contains the xml instance.
validate - holds whether the parser should be validating against the xml schema or not.
Returns:
xml file in a DOM document. If there is error, return null.

parseStringUsingDOM

public static org.w3c.dom.Document parseStringUsingDOM(java.lang.String xmlString,
                                                       boolean validate)
Parse the xml string that contains the xml instance using DOM.

Parameters:
xmlString - holds the xml string that contains the xml instance.
validate - holds whether the parser should be validating against the xml schema or not.
Returns:
xml string in a DOM document. If there is error, return null.

createNewDocument

public static org.w3c.dom.Document createNewDocument()
Obtain a new instance of a DOM Document object to build a DOM tree with. An alternative way to create a DOM Document object is to use the getDOMImplementation method to get a DOM Level 2 DOMImplementation object and then use DOM Level 2 methods on that object to create a DOM Document object.

Returns:
A new instance of a DOM Document object. Return null if an error occurs.

writeXMLDocumentToFile

public static boolean writeXMLDocumentToFile(org.w3c.dom.Document xmlDocument,
                                             java.lang.String fileName)
Write an xml file from a DOM tree document to a file.

Parameters:
xmlDocument - holds the xml document to write out to a file.
fileName - holds the xml filename to write out the file to.
Returns:
whether the file is written successfully without any error.

writeXMLDocumentToStandardOutput

public static boolean writeXMLDocumentToStandardOutput(org.w3c.dom.Document xmlDocument)
Write an xml file from a DOM tree document to the standard output (e.g. screen)..

Parameters:
xmlDocument - holds the xml document to write out to the standard output.
Returns:
whether the file is written successfully without any error.

writeXMLDocumentToString

public static java.lang.String writeXMLDocumentToString(org.w3c.dom.Document xmlDocument)
Write an xml file from a DOM tree document to a string.

Parameters:
xmlDocument - holds the xml document to write out to the String.
Returns:
a string that contains the xml. If error is encountered in writing the string, null is returned.

writeXMLElementToFile

public static boolean writeXMLElementToFile(org.w3c.dom.Element element,
                                            java.lang.String fileName)
Write DOM tree element to a file.

Parameters:
element - holds the xml element to write out to a file.
fileName - holds the xml filename to write out the file to.
Returns:
whether the file is written successfully without any error.

writeXMLElementToStandardOutput

public static boolean writeXMLElementToStandardOutput(org.w3c.dom.Element element)
Write DOM tree element to the standard output (e.g. screen)..

Parameters:
element - holds the xml element to write out to the standard output.
Returns:
whether the file is written successfully without any error.

writeXMLElementToString

public static java.lang.String writeXMLElementToString(org.w3c.dom.Element element)
Write an xml file from a DOM tree document to a string.

Parameters:
element - holds the xml element to write out to the String.
Returns:
a string that contains the xml. If error is encountered in writing the string, null is returned.

findChildNode

public static org.w3c.dom.Node findChildNode(org.w3c.dom.Node parentNode,
                                             java.lang.String childNodeName)
Find a parent node's first child node by the child node's name.

Parameters:
parentNode - holds the parent node.
childNodeName - holds the name of the child node to find.
Returns:
the first child node if found, otherwise null.

getChildElementsByTagName

public static java.util.Vector<org.w3c.dom.Element> getChildElementsByTagName(org.w3c.dom.Node parentNode,
                                                                              java.lang.String childNodeName)
Get a parent node's child elements by the child node's tag name.

Parameters:
parentNode - holds the parent node.
childNodeName - holds the name of the children node to find.
Returns:
a vector of child elements that mathces the name.

removeAllChildren

public static int removeAllChildren(org.w3c.dom.Node parentNode)
Remove all the child nodes of a parent node.

Parameters:
parentNode - holds the node for which its child nodes are to be removed.
Returns:
the number of child nodes removed.

removeAllAttributes

public static int removeAllAttributes(org.w3c.dom.Element element)
Remove all the attributes of an element.

Parameters:
element - holds the element for which its attributes are to be removed.
Returns:
the number of attributes removed.

removeChildrenByName

public static int removeChildrenByName(org.w3c.dom.Node parentNode,
                                       java.lang.String childName)
Remove all the child nodes of a parent node with a given name.

Parameters:
parentNode - holds the node for which its child nodes are to be removed.
childName - holds the name of the children to be removed.
Returns:
the number of child nodes removed.

getElementValueByName

public static java.lang.String getElementValueByName(org.w3c.dom.Element rootElement,
                                                     java.lang.String descendantName)
Get the text value of a descendant element by the element's name. If there are multiple elements with the same name, the method always return the value of the first element.

Parameters:
rootElement - holds the element for which its descendant element is to be found.
descendantName - holds the name of the descendant to be found.
Returns:
the text value of the first descendant element. If none found, a null value is returned.

getElementValue

public static java.lang.String getElementValue(org.w3c.dom.Element element)
Get the value of an element in a string.

Parameters:
element - holds the element for which its value is to be retrieved.
Returns:
the value of the element in a string.

setElementValue

public static void setElementValue(org.w3c.dom.Element element,
                                   java.lang.String value)
Set the value (i.e. it's text child node) of an element -- <element>value</element>.

Parameters:
element - holds the element to be set the value.
value - holds the value to set.

createOSxLRootElement

public static org.w3c.dom.Element createOSxLRootElement(org.w3c.dom.Document document,
                                                        java.lang.String osxlName)
Create the basic OSxL root element.

Parameters:
document - holds the W3C DOM type document to create XML elements and attributes. It is the parent of the OSxL root element, e.g. the <OSiL> element in OSiL. It is used to create all the nodes in the DOM tree.
osxlName - holds the OSxL name used to create the root element.
Returns:
the OSxL root element.

createXSDateTime

public static java.lang.String createXSDateTime(java.util.GregorianCalendar dateTime)
Create the standard xs:dateTime string from the native date/time

Parameters:
dateTime - holds the native date/time.
Returns:
the standard xs:dateTime in a string.

createNativeDateTime

public static java.util.GregorianCalendar createNativeDateTime(java.lang.String xsDateTime)
Create the the native date/time from the standard xs:dateTime.

Parameters:
xsDateTime - holds standard xs:dateTime in a string.
Returns:
the the native date/time (gregorian calendar).

main

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

Parameters:
argv - command line arguments.