magoffin.matt.ma2.util
Class XmlHelper

java.lang.Object
  extended by magoffin.matt.ma2.util.XmlHelper

public class XmlHelper
extends java.lang.Object

Utility class for working with XML data.

documentBuilderFactory
The JAXP DocumentBuilderFactory to use for creating Document objects.
jaxbContext
The JAXBContext to use for MA2 JAXB domain operations.
marshallerProperties
A Map of properties to set on the JAXB Marshaller. This is useful for passing such properties as com.sun.xml.bind.namespacePrefixMapper property.
objectFactory
The JAXB ObjectFactory to use for MA2 JAXB domain objects.
starObjectFactory
The JAXB ObjectFactory to use for MA2 JAXB domain objects.
transformerFactory
The JAXP TransformerFactory to use for creating Transformer objects.
xpathFactory
The JAXP XPathFactory to use for evaluating XPath queries.

Version:
$Revision: 43 $ $Date: 2007-10-21 11:10:01 +1300 (Sun, 21 Oct 2007) $
Author:
Matt Magoffin (spamsqr@msqr.us)

Constructor Summary
XmlHelper()
           
 
Method Summary
 void debugJaxbObject(java.lang.String notice, java.lang.Object jaxbObject, org.apache.log4j.Logger aLog)
          Marshal a JAXB object as XML to the specifiec Logger.
 void debugXml(java.lang.String notice, javax.xml.transform.Source xml, org.apache.log4j.Logger aLog)
          Debug an XML Source to a Logger.
 java.lang.Object evaluateXPath(java.lang.Object object, java.lang.String xpath, javax.xml.namespace.QName returnType)
          Evaluate an XPath expression and return the result.
 org.w3c.dom.Document getDocument(org.xml.sax.InputSource xmlSource)
          Get a Document object from an XML InputSource.
 org.w3c.dom.Document getDocument(java.io.InputStream xmlInput)
          Get a Document object from an XML data stream.
 org.w3c.dom.Document getDocument(java.lang.String xml)
          Get a Document object from an XML string.
 javax.xml.parsers.DocumentBuilderFactory getDocumentBuilderFactory()
           
 javax.xml.bind.JAXBContext getJaxbContext()
           
 javax.xml.bind.Marshaller getMarshaller()
          Get a JAXB Marshaller, configured for our context.
 java.util.Map<java.lang.String,java.lang.String> getMarshallerProperties()
           
 org.w3c.dom.Document getNewDocument()
          Get a new Document instance.
 ObjectFactory getObjectFactory()
           
 net.sf.ehcache.Ehcache getSchemaCache()
           
 javax.xml.validation.Schema getSchemaFromResource(org.springframework.core.io.Resource schemaResource)
          Get a Schema, possibly from cache.
 javax.xml.transform.TransformerFactory getTransformerFactory()
           
 java.lang.String getXmlString(java.io.InputStream xmlInput)
          Get XML data as a String.
 javax.xml.xpath.XPathFactory getXpathFactory()
           
 javax.xml.namespace.NamespaceContext getXpathNamespaceContext()
           
 void init()
          Initialize this instance.
 void setDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory)
           
 void setJaxbContext(javax.xml.bind.JAXBContext jaxbContext)
           
 void setMarshallerProperties(java.util.Map<java.lang.String,java.lang.String> marshallerProperties)
           
 void setObjectFactory(ObjectFactory objectFactory)
           
 void setSchemaCache(net.sf.ehcache.Ehcache schemaCache)
           
 void setTransformerFactory(javax.xml.transform.TransformerFactory transformerFactory)
           
 void setXpathFactory(javax.xml.xpath.XPathFactory xpathFactory)
           
 void setXpathNamespaceContext(javax.xml.namespace.NamespaceContext xpathNamespaceContext)
           
 void transformXml(javax.xml.transform.Source source, javax.xml.transform.Result result)
          Perform an copy transformation (copy XML from source to result).
 void transformXml(javax.xml.transform.Source source, javax.xml.transform.Result result, java.util.Map<java.lang.String,?> xsltParameters)
          Perform an copy transformation (copy XML from source to result).
 void transformXml(javax.xml.transform.Source source, javax.xml.transform.Result result, javax.xml.transform.Templates xslt)
          Perform an XSLT transformation.
 void transformXml(javax.xml.transform.Source source, javax.xml.transform.Result result, javax.xml.transform.Templates xslt, java.util.Map<java.lang.String,?> xsltParameters)
          Perform an XSLT transformation with parameters.
 void validateXml(javax.xml.transform.Source xml, org.springframework.core.io.Resource schemaResource)
          Validate XLM against a schema.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlHelper

public XmlHelper()
Method Detail

init

public void init()
Initialize this instance.

This method should be called after setting all properties and before using any (non-property) methods.


evaluateXPath

public java.lang.Object evaluateXPath(java.lang.Object object,
                                      java.lang.String xpath,
                                      javax.xml.namespace.QName returnType)
Evaluate an XPath expression and return the result.

Parameters:
object - - The starting context.
xpath - - The XPath expression.
returnType - - The desired return type.
Returns:
Result of evaluating an XPath expression

getMarshaller

public javax.xml.bind.Marshaller getMarshaller()
                                        throws javax.xml.bind.JAXBException
Get a JAXB Marshaller, configured for our context.

Returns:
a Marshaller
Throws:
javax.xml.bind.JAXBException - if a JAXB exception occurs

debugJaxbObject

public void debugJaxbObject(java.lang.String notice,
                            java.lang.Object jaxbObject,
                            org.apache.log4j.Logger aLog)
Marshal a JAXB object as XML to the specifiec Logger.

Parameters:
notice - a string to prefix the output XML with
jaxbObject - the JAXB object to debug
aLog - the Logger to log to

debugXml

public void debugXml(java.lang.String notice,
                     javax.xml.transform.Source xml,
                     org.apache.log4j.Logger aLog)
Debug an XML Source to a Logger.

Note the XML will only be serialized if the log has DEBUG level enabled.

Parameters:
notice - a string to prefix the output XML with
xml - the XML Source to debug
aLog - the Logger to log to

transformXml

public void transformXml(javax.xml.transform.Source source,
                         javax.xml.transform.Result result)
Perform an copy transformation (copy XML from source to result).

This method can be used to serialize DOM or JAXB sources to an XML String.

Parameters:
source - the XML source
result - the XML result

transformXml

public void transformXml(javax.xml.transform.Source source,
                         javax.xml.transform.Result result,
                         java.util.Map<java.lang.String,?> xsltParameters)
Perform an copy transformation (copy XML from source to result).

This method can be used to serialize DOM or JAXB sources to an XML String.

Parameters:
source - the XML source
result - the XML result
xsltParameters - optional XSLT parameters

transformXml

public void transformXml(javax.xml.transform.Source source,
                         javax.xml.transform.Result result,
                         javax.xml.transform.Templates xslt)
Perform an XSLT transformation.

Parameters:
source - the XML source
result - the XML result
xslt - the XSLT to transform with

transformXml

public void transformXml(javax.xml.transform.Source source,
                         javax.xml.transform.Result result,
                         javax.xml.transform.Templates xslt,
                         java.util.Map<java.lang.String,?> xsltParameters)
Perform an XSLT transformation with parameters.

Parameters:
source - the XML source
result - the XML result
xslt - the XSLT to transform with
xsltParameters - optional XSLT parameters to set

getNewDocument

public org.w3c.dom.Document getNewDocument()
Get a new Document instance.

Returns:
the new Document

getDocument

public org.w3c.dom.Document getDocument(java.io.InputStream xmlInput)
Get a Document object from an XML data stream.

Parameters:
xmlInput - the XML data stream
Returns:
the Document

getDocument

public org.w3c.dom.Document getDocument(org.xml.sax.InputSource xmlSource)
Get a Document object from an XML InputSource.

Parameters:
xmlSource - the XML data
Returns:
the Document

getDocument

public org.w3c.dom.Document getDocument(java.lang.String xml)
Get a Document object from an XML string.

Parameters:
xml - the XML string
Returns:
the Document

getXmlString

public java.lang.String getXmlString(java.io.InputStream xmlInput)
Get XML data as a String.

This method exists to validate the input XML is valid.

Parameters:
xmlInput - the XML data stream
Returns:
the XML as a String

validateXml

public void validateXml(javax.xml.transform.Source xml,
                        org.springframework.core.io.Resource schemaResource)
                 throws org.xml.sax.SAXException
Validate XLM against a schema.

Parameters:
xml - the XML to validate
schemaResource - the schema to validate against
Throws:
org.xml.sax.SAXException - if an XML validation error occurs

getSchemaFromResource

public javax.xml.validation.Schema getSchemaFromResource(org.springframework.core.io.Resource schemaResource)
                                                  throws java.io.IOException
Get a Schema, possibly from cache.

Parameters:
schemaResource - the resource to get the Schema object ffrom
Returns:
the schema
Throws:
java.io.IOException - if an IO error occurs

getDocumentBuilderFactory

public javax.xml.parsers.DocumentBuilderFactory getDocumentBuilderFactory()
Returns:
the documentBuilderFactory

setDocumentBuilderFactory

public void setDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory documentBuilderFactory)
Parameters:
documentBuilderFactory - the documentBuilderFactory to set

getJaxbContext

public javax.xml.bind.JAXBContext getJaxbContext()
Returns:
the jaxbContext

setJaxbContext

public void setJaxbContext(javax.xml.bind.JAXBContext jaxbContext)
Parameters:
jaxbContext - the jaxbContext to set

getMarshallerProperties

public java.util.Map<java.lang.String,java.lang.String> getMarshallerProperties()
Returns:
the marshallerProperties

setMarshallerProperties

public void setMarshallerProperties(java.util.Map<java.lang.String,java.lang.String> marshallerProperties)
Parameters:
marshallerProperties - the marshallerProperties to set

getObjectFactory

public ObjectFactory getObjectFactory()
Returns:
the objectFactory

setObjectFactory

public void setObjectFactory(ObjectFactory objectFactory)
Parameters:
objectFactory - the objectFactory to set

getTransformerFactory

public javax.xml.transform.TransformerFactory getTransformerFactory()
Returns:
the transformerFactory

setTransformerFactory

public void setTransformerFactory(javax.xml.transform.TransformerFactory transformerFactory)
Parameters:
transformerFactory - the transformerFactory to set

getXpathFactory

public javax.xml.xpath.XPathFactory getXpathFactory()
Returns:
the xpathFactory

setXpathFactory

public void setXpathFactory(javax.xml.xpath.XPathFactory xpathFactory)
Parameters:
xpathFactory - the xpathFactory to set

getXpathNamespaceContext

public javax.xml.namespace.NamespaceContext getXpathNamespaceContext()
Returns:
the xpathNamespaceContext

setXpathNamespaceContext

public void setXpathNamespaceContext(javax.xml.namespace.NamespaceContext xpathNamespaceContext)
Parameters:
xpathNamespaceContext - the xpathNamespaceContext to set

getSchemaCache

public net.sf.ehcache.Ehcache getSchemaCache()
Returns:
the schemaCache

setSchemaCache

public void setSchemaCache(net.sf.ehcache.Ehcache schemaCache)
Parameters:
schemaCache - the schemaCache to set