Class XmlServiceSupport

java.lang.Object
net.solarnetwork.service.support.BasicIdentifiable
All Implemented Interfaces:
net.solarnetwork.service.Identifiable

public class XmlServiceSupport extends HttpClientSupport
A helper class to support services that use XML.
Since:
2.0
Version:
1.0
Author:
matt
  • Field Details

  • Constructor Details

    • XmlServiceSupport

      public XmlServiceSupport()
      Default constructor.
  • Method Details

    • init

      public void init()
      Initialize this class after properties are set.
    • 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
    • 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
    • getSimpleSource

      public Source getSimpleSource(Object o, String elementName)
      Turn an object into a simple XML Document.

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

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

      public Document getSimpleDocument(Object o, String elementName)
      Turn an object into a simple XML Document.

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

       <powerDatum
         id="123"
         pvVolts="123.123"
         ... />
       
      Parameters:
      o - the object to turn into XML
      elementName - the name of the XML element
      Returns:
      the element, as an XML DOM Document
    • 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 XML 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 XML 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(Object obj, Node xml, Map<String,XPathExpression> xpathMap)
      Populate JavaBean properties via XPath extraction.

      This method will call registerCustomEditors(BeanWrapper) so custom editors can be registered if desired.

      Parameters:
      obj - the object to set properties on, or a BeanWrapper
      xml - the XML
      xpathMap - the mapping of JavaBean property names to XPaths
    • registerCustomEditors

      public void registerCustomEditors(org.springframework.beans.BeanWrapper bean)
      Extending classes can override this method to register custom bean editors.

      This method does nothing itself, and is designed to have custom implementation in extending classes.

      Parameters:
      bean - the bean in question
    • getInputSourceFromURLConnection

      public InputSource getInputSourceFromURLConnection(URLConnection conn) throws IOException
      Get a SAX InputSource from a URLConnection's InputStream.

      This method handles gzip and deflate decoding automatically, if the contentType is reported as such.

      Parameters:
      conn - the URLConnection
      Returns:
      the InputSource
      Throws:
      IOException - if any IO error occurs
    • webFormPost

      public InputSource webFormPost(org.springframework.beans.BeanWrapper bean, String url, Map<String,?> attributes)
      Send a bean as a web form POST and return an XML InputSource from the response content.
      Parameters:
      bean - the bean
      url - the URL to POST to
      attributes - extra POST attributes and bean override values
      Returns:
      an InputSource to the response content XML
    • webFormGet

      public InputSource webFormGet(org.springframework.beans.BeanWrapper bean, String url, Map<String,?> attributes)
      Send a bean as a web form GET and return an XML InputSource from the response content.
      Parameters:
      bean - the bean to extract GET parameters from, or null for no parameters
      url - the URL to GET to
      attributes - extra GET attributes and bean override values
      Returns:
      an InputSource to the response content XML
    • webFormPostForBean

      public void webFormPostForBean(org.springframework.beans.BeanWrapper bean, Object obj, String url, Map<String,?> attributes, Map<String,XPathExpression> xpathMap)
      Send a bean as a web form POST and parse the XML response as bean properties.
      Parameters:
      bean - the bean to POST
      obj - the result bean to populate from the HTTP response XML
      url - the URL to POST to
      attributes - extra POST attributes and bean override values
      xpathMap - the mapping of JavaBean property names to XPaths
    • webFormGetForBean

      public void webFormGetForBean(org.springframework.beans.BeanWrapper bean, Object obj, String url, Map<String,?> attributes, Map<String,XPathExpression> xpathMap)
      Send a bean as a web GET and parse the XML response as bean properties.

      This method calls webFormGet(BeanWrapper, String, Map) followed by extractBeanDataFromXml(Object, Node, Map).

      Parameters:
      bean - the bean whose properties to send as GET parameters, or null for no parameters
      obj - the result bean to populate from the HTTP response XML
      url - the URL to GET
      attributes - extra GET attributes and bean override values
      xpathMap - the mapping of JavaBean property names to XPaths
      See Also:
    • extractTrackingId

      public Long extractTrackingId(InputSource xml, XPathExpression xp, String xpath)
      Extract a tracking ID from an XML string.
      Parameters:
      xml - the XML to extract from
      xp - the XPath to use that returns a number
      xpath - the XPath as a string (for debugging)
      Returns:
      the tracking ID, or null if not found
    • webFormPostForTrackingId

      public Long webFormPostForTrackingId(org.springframework.beans.BeanWrapper bean, String url, XPathExpression trackingIdXPath, String xpath, Map<String,?> attributes)
      Send a bean as a web form POST and parse the XML response for a bean.
      Parameters:
      bean - the bean
      url - the URL to POST to
      trackingIdXPath - the XPath for extracting the tracking ID
      xpath - the trackingIdXPath as a String (for debugging)
      attributes - extra POST attributes and bean override values
      Returns:
      the extracted tracking ID, or null if none found
    • postEvent

      public final void postEvent(org.osgi.service.event.Event event)
      Post an Event.

      This method only works if a EventAdmin has been configured via setEventAdmin(OptionalService). Otherwise the event is silently ignored.

      Parameters:
      event - the event to post
      Since:
      1.5
    • getNsContext

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

      public void setNsContext(NamespaceContext nsContext)
      Set an optional NamespaceContext to use for proper XML namespace handling in some contexts, such as XPath.
      Parameters:
      nsContext - the context to use
    • getDocBuilderFactory

      public DocumentBuilderFactory getDocBuilderFactory()
      Get the DOM factory.

      If an explicit one has not been configured via setDocBuilderFactory(DocumentBuilderFactory) then a default one will be instantiated and cached when this method is called.

      Returns:
      the DOM factory
    • setDocBuilderFactory

      public void setDocBuilderFactory(DocumentBuilderFactory docBuilderFactory)
      Set a JAXP DocumentBuilderFactory to use.
      Parameters:
      docBuilderFactory - the DOM factory to use
    • getXpathFactory

      public XPathFactory getXpathFactory()
      Get the XPath factory.

      If not explicit factory has been configured via setXpathFactory(XPathFactory) then a default one will be instantiated and cached when this method is called.

      Returns:
      the factory
    • setXpathFactory

      public void setXpathFactory(XPathFactory xpathFactory)
      Set a JAXP XPathFactory for handling XPath operations with.
      Parameters:
      xpathFactory - the factory to use
    • getTransformerFactory

      public TransformerFactory getTransformerFactory()
      Get the XSLT factory to use.

      If an expliciy one has not been configured via setTransformerFactory(TransformerFactory) then a default one will be created and cached when this method is called.

      Returns:
      the XSLT factory
    • setTransformerFactory

      public void setTransformerFactory(TransformerFactory transformerFactory)
      Set a JAXP TransformerFactory for handling XSLT transformations with.
      Parameters:
      transformerFactory - the factory
    • getEventAdmin

      public net.solarnetwork.service.OptionalService<org.osgi.service.event.EventAdmin> getEventAdmin()
      Get the EventAdmin service.
      Returns:
      the EventAdmin service
    • setEventAdmin

      public void setEventAdmin(net.solarnetwork.service.OptionalService<org.osgi.service.event.EventAdmin> eventAdmin)
      Set an EventAdmin service to use.
      Parameters:
      eventAdmin - the EventAdmin to use