Class XmlSupport

java.lang.Object
net.solarnetwork.support.XmlSupport

public class XmlSupport extends Object
A class to support services that use XML.

The configurable properties of this class are:

docBuilderFactory
A JAXP DocumentBuilderFactory to use. If not configured, the DocumentBuilderFactory.newInstance() method will be used to create a default one.
transformerFactory
A JAXP TransformerFactory for handling XSLT transformations with. If not configured, the TransformerFactory.newInstance() method will be used to create a default one.
xpathFactory
A JAXP XPathFactory for handling XPath operations with. If not configured the XPathFactory.newInstance() method will be used to create a default one.
nsContext
An optional NamespaceContext to use for proper XML namespace handling in some contexts, such as XPath.
Version:
1.2
Author:
matt
  • Field Details

    • log

      protected final org.slf4j.Logger log
      A class-level logger.
  • Constructor Details

    • XmlSupport

      public XmlSupport()
  • Method Details

    • getXPathExpressionMap

      public Map<String,XPathExpression> getXPathExpressionMap(Map<String,String> xpathMap)
      Compile XPathExpression mappings from String XPath expressions.
      Parameters:
      xpathMap - the XPath string expressions
      Returns:
      the XPathExperssion mapping
    • getXPathExpression

      public XPathExpression getXPathExpression(String xpath) throws XPathExpressionException
      Compile a single XPathExpression from a String XPath expression. The getNsContext() will be configured on the resulting XPathExpression, if available.
      Parameters:
      xpath - the XPath to compile
      Returns:
      the compiled XPath
      Throws:
      XPathExpressionException - if the XPath cannot be compiled
    • getTemplates

      public Templates getTemplates(org.springframework.core.io.Resource resource)
      Get an XSLT Templates object from an XSLT Resource.
      Parameters:
      resource - the XSLT Resource to load
      Returns:
      the compiled Templates
    • getDocument

      public Document getDocument(org.springframework.beans.BeanWrapper bean, String elementName)
      Turn an object into a simple XML Document, supporting custom property editors.

      The returned XML will be a document with a single element with all JavaBean properties turned into attributes. For example:

       <powerDatum
         id="123"
         pvVolts="123.123"
         ... />
       

      PropertyEditor instances can be registered with the supplied BeanWrapper for custom handling of properties, e.g. dates.

      Parameters:
      bean - the object to turn into XML
      elementName - the name of the XML element
      Returns:
      the element, as an XML DOM Document
    • getElement

      public Element getElement(org.springframework.beans.BeanWrapper bean, Document dom)
      Turn an object into a simple XML Element, supporting custom property editors.

      The returned XML will be a single element with all JavaBean properties turned into attributes and the element named after the bean object's class name. For example:

       <PowerDatum
         id="123"
         pvVolts="123.123"
         ... />
       

      PropertyEditor instances can be registered with the supplied BeanWrapper for custom handling of properties, e.g. dates.

      Parameters:
      bean - the object to turn into XML
      dom - the document
      Returns:
      the element, as an XML DOM Document
    • getElement

      public Element getElement(org.springframework.beans.BeanWrapper bean, String elementName, Document dom)
      Turn an object into a simple XML Element, supporting custom property editors.

      The returned XML will be a single element with all JavaBean properties turned into attributes. For example:

       <powerDatum
         id="123"
         pvVolts="123.123"
         ... />
       

      PropertyEditor instances can be registered with the supplied BeanWrapper for custom handling of properties, e.g. dates.

      Parameters:
      bean - the object to turn into XML
      elementName - the name of the XML element
      dom - the document
      Returns:
      the element, as an XML DOM Element
    • getSource

      public Source getSource(org.springframework.beans.BeanWrapper bean, String elementName)
      Turn an object into a simple XML Document, supporting custom property editors.

      The returned XML will be a single element with all JavaBean properties turned into attributed. For example:

       <powerDatum
         id="123"
         pvVolts="123.123"
         ... />
       
      Parameters:
      bean - the object to turn into XML
      elementName - the name of the XML element
      Returns:
      the element, as XSLT Source
      See Also:
    • getSource

      public Source getSource(Document dom)
      Turn a Document into a Source.

      This method will log the XML document at the FINEST level.

      Parameters:
      dom - the Document to turn into XSLT source
      Returns:
      the document, as XSLT Source
    • getXmlAsString

      public String getXmlAsString(Source source, boolean indent)
      Turn an XML Source into a String.
      Parameters:
      source - the XML Source
      indent - if true then indent the result
      Returns:
      the XML, as a String
    • extractBeanDataFromXml

      public void extractBeanDataFromXml(org.springframework.beans.PropertyAccessor bean, Node xml, Map<String,XPathExpression> xpathMap)
      Populate JavaBean properties via XPath extraction.
      Parameters:
      bean - the object to set properties on
      xml - the XML
      xpathMap - the mapping of JavaBean property names to XPaths
    • extractStringFromXml

      public String extractStringFromXml(Node xml, XPathExpression xpath)
      Extract a String value via an XPath expression.
      Parameters:
      xml - the XML
      xpath - the XPath expression
      Returns:
      the String
    • getNsContext

      public NamespaceContext getNsContext()
      Get the namespace context.
      Returns:
      the context
    • setNsContext

      public void setNsContext(NamespaceContext nsContext)
      Set the namespace context.
      Parameters:
      nsContext - the context to set
    • getDocBuilderFactory

      public DocumentBuilderFactory getDocBuilderFactory()
      Get a document builder factory.
      Returns:
      the factory
    • setDocBuilderFactory

      public void setDocBuilderFactory(DocumentBuilderFactory docBuilderFactory)
      Set the document builder factor.
      Parameters:
      docBuilderFactory - the factory to set
    • getXpathFactory

      public XPathFactory getXpathFactory()
      Get an XPath factory.
      Returns:
      the factory
    • setXpathFactory

      public void setXpathFactory(XPathFactory xpathFactory)
      Set the XPath factory.
      Parameters:
      xpathFactory - the factory to set
    • getTransformerFactory

      public TransformerFactory getTransformerFactory()
      Get a transformer factory.
      Returns:
      the factory
    • setTransformerFactory

      public void setTransformerFactory(TransformerFactory transformerFactory)
      Set the transformer factory.
      Parameters:
      transformerFactory - the factory to set