org.sourcejammer.xml
Class XMLUtil

java.lang.Object
  |
  +--org.sourcejammer.xml.XMLUtil

public class XMLUtil
extends java.lang.Object

Title: $FileName: XMLUtil.java$

Version:
$VerNum: 6$ $KeyWordsOff: $

Utility class for common functions that are performed with XML.
Author:
$AuthorName: Rob MacGrogan$

Constructor Summary
XMLUtil()
          Default constructor.
 
Method Summary
static org.w3c.dom.Element addNewChildElement(java.lang.String name, org.w3c.dom.Element parent)
           
static org.w3c.dom.Element addNewChildElement(java.lang.String name, java.lang.String value, org.w3c.dom.Element parent)
          Creates a new Element node with the specified name and value, and appends it to the parent Element.
static void addOrReplaceElementText(org.w3c.dom.Element elm, java.lang.String newValue)
          Replace current text node with new text if exists.
static org.w3c.dom.Element addRootElement(java.lang.String name, org.w3c.dom.Document parent)
          Add a root element to a document.
static org.w3c.dom.Element getChildElement(java.lang.String sElementName, org.w3c.dom.Element elmParent)
          Return the child element of elm with the name sElementName.
static org.w3c.dom.Element getChildElement(java.lang.String sElementName, org.w3c.dom.Element elmParent, boolean bIgnoreCase)
          Return the child element of elm with the name sElementName.
static java.lang.String getStringFromNode(org.w3c.dom.Node node, java.lang.String concatString)
          Convenience method for returning result of getTextFromNode(0) as String.
static java.lang.StringBuffer getTextFromNode(org.w3c.dom.Node node, java.lang.String concatString)
          Concatenates the text nodes together for the node passed in into a single string.
static java.lang.String getValue(java.lang.String nodeName, org.w3c.dom.Element parentElement)
          Call this to get a specific value out of the configuration document.
static java.lang.String[] getValues(java.lang.String nodeName, org.w3c.dom.Element parentElement)
          Call this to get a set of values out of the configuration document.
static org.w3c.dom.Document getXMLDoc(java.io.File xmlFile)
          Reads in a file and passes it to an XML parser from which it will get and return a document object.
static org.w3c.dom.Document getXMLDoc(org.xml.sax.InputSource source)
          Reads in an input source and passes it to an XML parser from which it will get and return a document object.
static org.w3c.dom.Document getXMLDoc(java.lang.String xmlDocAsString)
          Function that takes a String and returns a Document object.
static void main(java.lang.String[] args)
           
static org.w3c.dom.Document newDocument()
          Return a new, empty XML document.
static org.w3c.dom.Document oldGetXMLDoc(org.xml.sax.InputSource source)
           
static java.lang.String replaceInvalidCharWithSpace(java.lang.String input)
          Turns invalid characters into spaces.
static void writeXMLDocToFileSys(org.w3c.dom.Document doc, java.io.File file)
           
static void writeXMLDocToFileSys(org.w3c.dom.Document doc, java.io.File file, boolean indent)
          Writes the xml doc to the specified file.
static java.lang.StringBuffer xmlDocToStringBuffer(org.w3c.dom.Document doc)
          Function take an XML document, serializes it and returns a string buffer.
static java.lang.StringBuffer xmlDocToStringBuffer(org.w3c.dom.Document doc, boolean indent)
          Function take an XML document, serializes it (with an indented format for viewing) and returns a string buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUtil

public XMLUtil()
Default constructor.

Method Detail

xmlDocToStringBuffer

public static java.lang.StringBuffer xmlDocToStringBuffer(org.w3c.dom.Document doc)
                                                   throws java.io.IOException,
                                                          javax.xml.transform.TransformerException
Function take an XML document, serializes it and returns a string buffer. No indention will be done.

Parameters:
doc - the XML document to be serialized into a string buffer.
Returns:
The XML document as a string buffer.
Throws:
java.io.IOException - thrown when there is an exception writing the XML document to the string buffer.
javax.xml.transform.TransformerException

xmlDocToStringBuffer

public static java.lang.StringBuffer xmlDocToStringBuffer(org.w3c.dom.Document doc,
                                                          boolean indent)
                                                   throws java.io.IOException,
                                                          javax.xml.transform.TransformerException
Function take an XML document, serializes it (with an indented format for viewing) and returns a string buffer.

Parameters:
doc - the XML document to be serialized into a string buffer.
Returns:
The XML document as a string buffer.
Throws:
java.io.IOException - thrown when there is an exception writing the XML document to the string buffer.
javax.xml.transform.TransformerException

getXMLDoc

public static org.w3c.dom.Document getXMLDoc(java.lang.String xmlDocAsString)
                                      throws java.io.IOException,
                                             org.xml.sax.SAXException,
                                             javax.xml.parsers.ParserConfigurationException
Function that takes a String and returns a Document object.

Parameters:
xmlDocAsString - the XML document as a string.
Returns:
An XML Document object built from the string passed in.
Throws:
java.io.IOException - thrown when there is an exception reading the XML document from the string.
org.xml.sax.SAXException - thrown when there is an error while parsing the XML document.
javax.xml.parsers.ParserConfigurationException - thrown when there is an error while parsing the XML document.

getXMLDoc

public static org.w3c.dom.Document getXMLDoc(java.io.File xmlFile)
                                      throws java.io.IOException,
                                             org.xml.sax.SAXException,
                                             javax.xml.parsers.ParserConfigurationException
Reads in a file and passes it to an XML parser from which it will get and return a document object.

Parameters:
xmlFile - a file object that represents the file to be read in.
Returns:
An XML document object built from the file passed in.
Throws:
java.io.IOException - thrown when there is an exception reading the XML document from the file.
org.xml.sax.SAXException - thrown when there is an error while parsing the XML document.
javax.xml.parsers.ParserConfigurationException - thrown when there is an error while parsing the XML document.

newDocument

public static org.w3c.dom.Document newDocument()
Return a new, empty XML document.


getXMLDoc

public static org.w3c.dom.Document getXMLDoc(org.xml.sax.InputSource source)
                                      throws java.io.IOException,
                                             javax.xml.parsers.ParserConfigurationException,
                                             org.xml.sax.SAXException
Reads in an input source and passes it to an XML parser from which it will get and return a document object.

Parameters:
source - the input source to be parsed into an XML document.
Returns:
An XML document object built from the file passed in.
Throws:
java.io.IOException - thrown when there is an exception reading the XML document from the input source.
org.xml.sax.SAXException - thrown when there is an error while parsing the XML document.
javax.xml.parsers.ParserConfigurationException - thrown when there is an error while parsing the XML document.

oldGetXMLDoc

public static org.w3c.dom.Document oldGetXMLDoc(org.xml.sax.InputSource source)
                                         throws java.io.IOException,
                                                javax.xml.parsers.ParserConfigurationException,
                                                org.xml.sax.SAXException
java.io.IOException
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException

getStringFromNode

public static java.lang.String getStringFromNode(org.w3c.dom.Node node,
                                                 java.lang.String concatString)
Convenience method for returning result of getTextFromNode(0) as String.


getTextFromNode

public static java.lang.StringBuffer getTextFromNode(org.w3c.dom.Node node,
                                                     java.lang.String concatString)
Concatenates the text nodes together for the node passed in into a single string.

Parameters:
node - the node to extract the text nodes from.
concatString - the string that will be concatenated between each text node.
Returns:
A string buffer representing all of the text nodes concatenated together with concatString. Returns null if the node is null. Returns an empty string of it does not have any text nodes.

getValue

public static java.lang.String getValue(java.lang.String nodeName,
                                        org.w3c.dom.Element parentElement)
Call this to get a specific value out of the configuration document.

Parameters:
nodeName - the name of the XML node to retrieve.
parentElement - the parent of the node to find and retrieve its values for.
Returns:
the value of the XML text node.

getValues

public static java.lang.String[] getValues(java.lang.String nodeName,
                                           org.w3c.dom.Element parentElement)
Call this to get a set of values out of the configuration document.

Parameters:
nodeName - the name of the XML node(s) to retrieve.
parentElement - the parent of the node to find and retrieve its values for.
Returns:
the value of the XML text node(s).

replaceInvalidCharWithSpace

public static java.lang.String replaceInvalidCharWithSpace(java.lang.String input)
Turns invalid characters into spaces. Valid characters are defined by the following: [2] Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] // any Unicode character, excluding the | [#xE000-#xFFFD] | [#x10000-#x10FFFF] // surrogate blocks, FFFE, and FFFF. Implementation Note: The algorithm for determining what is a valid character and what is not was copied from org.apache.xerces.framework.XMLDocumentScanner.scanCharRef()

Parameters:
input - the string to replace invalid characters with spaces.
Returns:
a string with all invalid characters replaced by a space each.

getChildElement

public static org.w3c.dom.Element getChildElement(java.lang.String sElementName,
                                                  org.w3c.dom.Element elmParent)
                                           throws XMLNodeDoesNotExistException
Return the child element of elm with the name sElementName. Case is not ignored in comparing the element name to sElementName.

Parameters:
sElementName - the name of the element to be returned.
elmParent - the parent Element in which this method will search for a child element with sElementName as its name.
Returns:
an Element with name sElementName, which is a child of elmParent.
Throws:
XMLNodeDoesNotExistException - if the requested Element does not exist in the parent Element.

getChildElement

public static org.w3c.dom.Element getChildElement(java.lang.String sElementName,
                                                  org.w3c.dom.Element elmParent,
                                                  boolean bIgnoreCase)
                                           throws XMLNodeDoesNotExistException
Return the child element of elm with the name sElementName.

Parameters:
sElementName - the name of the element to be returned.
elmParent - the parent Element in which this method will search for a child element with sElementName as its name.
bIgnoreCase - if true, case is ignored when comparing the names of child Elements to sElementName.
Returns:
an Element with name sElementName, which is a child of elmParent.
Throws:
XMLNodeDoesNotExistException - if the requested Element does not exist in the parent Element.

addNewChildElement

public static org.w3c.dom.Element addNewChildElement(java.lang.String name,
                                                     org.w3c.dom.Element parent)

addNewChildElement

public static org.w3c.dom.Element addNewChildElement(java.lang.String name,
                                                     java.lang.String value,
                                                     org.w3c.dom.Element parent)
Creates a new Element node with the specified name and value, and appends it to the parent Element.

Parameters:
name - -- name for the new Element.
value - -- value for the new Element.
parent - -- parent Element of new Element.
Returns:
The newly created Element.

addOrReplaceElementText

public static void addOrReplaceElementText(org.w3c.dom.Element elm,
                                           java.lang.String newValue)
                                    throws XMLNodeDoesNotExistException
Replace current text node with new text if exists. Otherwise, adds text to element.

XMLNodeDoesNotExistException

addRootElement

public static org.w3c.dom.Element addRootElement(java.lang.String name,
                                                 org.w3c.dom.Document parent)
Add a root element to a document.


writeXMLDocToFileSys

public static void writeXMLDocToFileSys(org.w3c.dom.Document doc,
                                        java.io.File file)
                                 throws java.io.IOException,
                                        javax.xml.transform.TransformerException
java.io.IOException
javax.xml.transform.TransformerException

writeXMLDocToFileSys

public static void writeXMLDocToFileSys(org.w3c.dom.Document doc,
                                        java.io.File file,
                                        boolean indent)
                                 throws java.io.IOException,
                                        javax.xml.transform.TransformerException
Writes the xml doc to the specified file.

java.io.IOException
javax.xml.transform.TransformerException

main

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


Copyright 2003 Soucejammer project.