cmsc420.xml
Class XmlUtility

java.lang.Object
  extended by cmsc420.xml.XmlUtility

public class XmlUtility
extends java.lang.Object

Wrapper class for XML API validation and parsing methods. Shortcut methods for reading, writing, parsing, validating, and transforming XML files.

Version:
2.0, 14 Mar 2007
Author:
Justin Manweiler (William and Mary), Ben Zoller (added schema validation functionality)

Method Summary
static javax.xml.parsers.DocumentBuilder getDocumentBuilder()
          Gets a DocumentBuilder from the DocumentBuilderFactory
static javax.xml.transform.Transformer getTransformer()
          Gets a Transformer from the TransformerFactory
static boolean isValidXml(java.io.File file)
          Checks if the file is valid XML (note: does not check it against a referenced schema)
static org.w3c.dom.Document parse(java.io.File file)
          Parses a given XML file and returns the DOM Document tree.
static org.w3c.dom.Document parse(java.io.InputStream inputStream)
          Parses a given XML input stream and returns the DOM Document tree.
static void print(org.w3c.dom.Document document)
          Prints a DOM Document tree to System.out
static java.io.Reader read(org.w3c.dom.Document document)
          Reads a DOM Document tree to a piped reader.
static java.io.InputStream stream(org.w3c.dom.Document document)
          Streams a DOM Document tree to a piped input stream.
static void transform(org.w3c.dom.Document xmlDom, java.io.File xsltFile, java.io.File htmlFile)
          Transforms a given XML Document with an XSLT file to an HTML file.
static void transform(java.io.File xmlFile, java.io.File xsltFile, java.io.File htmlFile)
          Transforms a given XML file with an XSLT file to an HTML file.
static void transform(javax.xml.transform.Source xmlSource, java.io.File xsltFile, java.io.File htmlFile)
          Transforms a given XML source with an XSLT file to an HTML file.
static org.w3c.dom.Document validate(java.io.File xmlFile, javax.xml.transform.Source schemaSource)
          Validates an XML file against an XML schema.
static org.w3c.dom.Document validateNoNamespace(java.io.File xmlFile)
          Validates an input file against an internal schema.
static org.w3c.dom.Document validateNoNamespace(java.io.InputStream xmlStream)
          Validates an input stream against an internal schema.
static void write(org.w3c.dom.Document document, java.io.File outFile)
          Writes a DOM Document tree to an XML file.
static void write(org.w3c.dom.Document document, java.io.OutputStream outputStream)
          Writes a DOM Document tree to a given XML output stream.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getDocumentBuilder

public static javax.xml.parsers.DocumentBuilder getDocumentBuilder()
                                                            throws javax.xml.parsers.ParserConfigurationException
Gets a DocumentBuilder from the DocumentBuilderFactory

Returns:
DocumentBuilder capable of generating a new XML Document.
Throws:
javax.xml.parsers.ParserConfigurationException - a serious configuration error

getTransformer

public static javax.xml.transform.Transformer getTransformer()
                                                      throws javax.xml.transform.TransformerConfigurationException
Gets a Transformer from the TransformerFactory

Returns:
transformer used to transform a source tree to an output stream/file
Throws:
javax.xml.transform.TransformerConfigurationException - a serious configuration error

isValidXml

public static boolean isValidXml(java.io.File file)
Checks if the file is valid XML (note: does not check it against a referenced schema)

Parameters:
file - checked if is a valid XML file
Returns:
true if the file is valid XML, false otherwise

parse

public static org.w3c.dom.Document parse(java.io.File file)
                                  throws javax.xml.parsers.ParserConfigurationException,
                                         org.xml.sax.SAXException,
                                         java.io.IOException
Parses a given XML file and returns the DOM Document tree.

Parameters:
file - XML file to be parsed
Returns:
Document tree representing XML file
Throws:
javax.xml.parsers.ParserConfigurationException - a serious configuration error
org.xml.sax.SAXException - encapsulates a SAX parsing error
java.io.IOException - problem opening/reading the file

parse

public static org.w3c.dom.Document parse(java.io.InputStream inputStream)
                                  throws javax.xml.parsers.ParserConfigurationException,
                                         org.xml.sax.SAXException,
                                         java.io.IOException
Parses a given XML input stream and returns the DOM Document tree.

Parameters:
inputStream - XML stream to be parsed
Returns:
Document tree representing XML input stream
Throws:
javax.xml.parsers.ParserConfigurationException - a serious configuration error
org.xml.sax.SAXException - encapsulates a SAX parsing error
java.io.IOException - problem reading the input stream

read

public static java.io.Reader read(org.w3c.dom.Document document)
                           throws java.io.IOException,
                                  javax.xml.transform.TransformerException
Reads a DOM Document tree to a piped reader.

Parameters:
document - DOM Document tree
Returns:
piped reader which reads XML document
Throws:
java.io.IOException - problem reading the document
javax.xml.transform.TransformerException - a problem during the transformation process

stream

public static java.io.InputStream stream(org.w3c.dom.Document document)
                                  throws java.io.IOException,
                                         javax.xml.transform.TransformerException
Streams a DOM Document tree to a piped input stream.

Parameters:
document - DOM Document tree
Returns:
piped input stream which reads XML document
Throws:
java.io.IOException - problem streaming the document
javax.xml.transform.TransformerException - a problem during the transformation process

print

public static void print(org.w3c.dom.Document document)
                  throws javax.xml.transform.TransformerException
Prints a DOM Document tree to System.out

Parameters:
document - DOM Document tree which represents an XML file
Throws:
javax.xml.transform.TransformerException - a problem during the transformation process

write

public static void write(org.w3c.dom.Document document,
                         java.io.File outFile)
                  throws javax.xml.transform.TransformerException,
                         java.io.FileNotFoundException
Writes a DOM Document tree to an XML file.

Parameters:
document - DOM Document tree
outFile - XML output file
Throws:
javax.xml.transform.TransformerException - a problem during the transformation process
java.io.FileNotFoundException - output file is inaccessible

write

public static void write(org.w3c.dom.Document document,
                         java.io.OutputStream outputStream)
                  throws javax.xml.transform.TransformerException
Writes a DOM Document tree to a given XML output stream.

Parameters:
document - DOM Document tree
outputStream - XML output stream
Throws:
javax.xml.transform.TransformerException - a problem during the transformation process

transform

public static void transform(java.io.File xmlFile,
                             java.io.File xsltFile,
                             java.io.File htmlFile)
                      throws javax.xml.transform.TransformerException,
                             java.io.FileNotFoundException
Transforms a given XML file with an XSLT file to an HTML file.

Parameters:
xmlFile - XML input file to be transformed
xsltFile - XSLT file which determines how the XML is transformed to HTML
htmlFile - HTML output file
Throws:
javax.xml.transform.TransformerException - a problem during the transformation process
java.io.FileNotFoundException - XML output file is inaccessible

transform

public static void transform(org.w3c.dom.Document xmlDom,
                             java.io.File xsltFile,
                             java.io.File htmlFile)
                      throws javax.xml.transform.TransformerException,
                             java.io.FileNotFoundException
Transforms a given XML Document with an XSLT file to an HTML file.

Parameters:
xmlDom - XML input Document to be transformed
xsltFile - XSLT file which determines how the XML is transformed to HTML
htmlFile - HTML output file
Throws:
javax.xml.transform.TransformerException - a problem during the transformation process
java.io.FileNotFoundException - XML output file is inaccessible

transform

public static void transform(javax.xml.transform.Source xmlSource,
                             java.io.File xsltFile,
                             java.io.File htmlFile)
                      throws javax.xml.transform.TransformerException,
                             java.io.FileNotFoundException
Transforms a given XML source with an XSLT file to an HTML file.

Parameters:
xmlSource - XML input source to be transformed
xsltFile - XSLT file which determines how the XML is transformed to HTML
htmlFile - HTML output file
Throws:
javax.xml.transform.TransformerException - a problem during the transformation process
java.io.FileNotFoundException - XML output file is inaccessible

validate

public static org.w3c.dom.Document validate(java.io.File xmlFile,
                                            javax.xml.transform.Source schemaSource)
                                     throws org.xml.sax.SAXException,
                                            java.io.IOException,
                                            javax.xml.parsers.ParserConfigurationException
Validates an XML file against an XML schema. Throws a SAXException if the XML is invalid. Else returns the XML file as a org.w3c.dom.Document

Parameters:
xmlFile - XML file to be validated
schemaSource - XML Schema source - can be created this way: Source schemaSource = new StreamSource(new File(schemaFileName));
Returns:
XML file as a org.w3c.dom.Document if successful
Throws:
org.xml.sax.SAXException - encapsulates a problem validating the document
java.io.IOException - problem opening/reading the file
javax.xml.parsers.ParserConfigurationException - a serious configuration error

validateNoNamespace

public static org.w3c.dom.Document validateNoNamespace(java.io.File xmlFile)
                                                throws org.xml.sax.SAXException,
                                                       java.io.IOException,
                                                       javax.xml.parsers.ParserConfigurationException
Validates an input file against an internal schema. First parses the stream into a DOM Document. Then it gets the schema file (works for local or external (online) schemas) and validates the Document against the schema.

Parameters:
xmlFile - XML file containing schema reference
Returns:
representation of XML document tree
Throws:
org.xml.sax.SAXException - encapsulates problem validating the XML document
java.io.IOException - problem reading the XML file
javax.xml.parsers.ParserConfigurationException - a serious configuration error
javax.naming.OperationNotSupportedException

validateNoNamespace

public static org.w3c.dom.Document validateNoNamespace(java.io.InputStream xmlStream)
                                                throws org.xml.sax.SAXException,
                                                       java.io.IOException,
                                                       javax.xml.parsers.ParserConfigurationException
Validates an input stream against an internal schema. First parses the stream into a DOM Document. Then it gets the schema file (works for local or external (online) schemas) and validates the Document against the schema. Online schemas should be accessed using HTTP (not HTTPS).

Parameters:
xmlStream - XML input stream containing schema reference
Returns:
representation of XML document tree
Throws:
org.xml.sax.SAXException - encapsulates problem validating the XML document
java.io.IOException - problem reading the XML input stream
javax.xml.parsers.ParserConfigurationException - a serious configuration error
javax.naming.OperationNotSupportedException