net.mtu.eggplant.xml
Class XMLUtils

java.lang.Object
  extended by net.mtu.eggplant.xml.XMLUtils

public class XMLUtils
extends Object

Some utilities for working with XML.


Field Summary
static DocumentBuilder DOCUMENT_BUILDER
           
static ThreadLocal<DateFormat> XML_TIME_FORMAT
          Date format for time type.
 
Constructor Summary
XMLUtils()
           
 
Method Summary
static Boolean getBooleanAttributeValue(Element element, String attributeName)
           
static Double getDoubleAttributeValue(Element element, String attributeName)
          Get a double value from an attribute.
static String getStringAttributeValue(Element element, String attributeName)
           
static Document parse(Reader stream, Schema schema)
          Parse the document from the given stream.
static Document parseXMLDocument(InputStream xmlDocStream)
          Parse xmlDoc an XML document.
static void writeXML(Document doc, Writer writer)
          Write the document to a writer.
static void writeXML(Document doc, Writer writer, String encoding)
          Write the document to a writer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

XML_TIME_FORMAT

public static final ThreadLocal<DateFormat> XML_TIME_FORMAT
Date format for time type.


DOCUMENT_BUILDER

public static final DocumentBuilder DOCUMENT_BUILDER
Constructor Detail

XMLUtils

public XMLUtils()
Method Detail

parse

public static Document parse(Reader stream,
                             Schema schema)
                      throws IOException,
                             SAXException
Parse the document from the given stream. The document is validated with the specified schema. Does not close the stream after reading. Warnings are output to the logger for this class.

Parameters:
stream - a stream containing document
Returns:
the challengeDocument, null on an error
Throws:
IOException - if there is an error reading the stream
SAXException - if there is an error parsing the document or it doesn't match the schema
RuntimeException - if there is an error configuring the XML parser, this shouldn't happen

parseXMLDocument

public static Document parseXMLDocument(InputStream xmlDocStream)
                                 throws SAXException,
                                        IOException
Parse xmlDoc an XML document. Just does basic parsing, no validity checks. Does not close the stream after parsing. Warnings are output to the logger for this class.

Throws:
IOException - if there is an error reading the stream
SAXException - if the document is found to be invalid
RuntimeException - if there is an error configuring the XML parser, this shouldn't happen

getStringAttributeValue

public static String getStringAttributeValue(Element element,
                                             String attributeName)
See Also:
getDoubleAttributeValue(Element, String)

getBooleanAttributeValue

public static Boolean getBooleanAttributeValue(Element element,
                                               String attributeName)
See Also:
getDoubleAttributeValue(Element, String)

getDoubleAttributeValue

public static Double getDoubleAttributeValue(Element element,
                                             String attributeName)
Get a double value from an attribute.

Parameters:
element - the element to get the attribute from, may be null
attributeName - the attribute name to get
Returns:
the value, null if element is null or the attribute value is null or empty

writeXML

public static void writeXML(Document doc,
                            Writer writer)
Write the document to a writer.

Parameters:
doc - the document to write
writer - where to write the document

writeXML

public static void writeXML(Document doc,
                            Writer writer,
                            String encoding)
Write the document to a writer.

Parameters:
doc - the document to write
writer - where to write the document
encoding - if non-null use this as the encoding for the text
Throws:
RuntimeException - if a TransformerException occurs.