Package org.smooks

Class Smooks

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    @ThreadSafe
    public class Smooks
    extends Object
    implements Closeable
    Smooks executor class.

    Additional configurations can be carried out on the Smooks instance through the SmooksUtil class.

    The basic usage scenario for this class might be as follows:

    1. Develop (or reuse) an implementation of ElementVisitor to perform some transformation/analysis operation on a message. There are a number of prebuilt and reuseable implemntations available as "Smooks Cartridges".
    2. Write a resource configuration to target the ElementVisitor implementation at the target fragment of the message being processed.
    3. Apply the logic as follows:
       Smooks smooks = new Smooks("smooks-config.xml");
       ExecutionContext execContext;
      
       execContext = smooks.createExecutionContext();
       smooks.filter(new StreamSource(...), new StreamSink(...), execContext);
       
    Remember, you can implement and apply multiple ElementVisitor within the context of a single filtering operation. You can also target DOMElementVisitors/ElementVisitor based on target profiles, and so use a single configuration to process multiple messages by sharing profiles across your message set.

    See Smooks Tutorials.

    Author:
    tom.fennelly@gmail.com
    • Constructor Summary

      Constructors 
      Constructor Description
      Smooks()
      Public Default Constructor.
      Smooks​(InputStream inputStream)
      Public constructor.
      Smooks​(String resourceURI)
      Public constructor.
      Smooks​(org.smooks.api.ApplicationContext applicationContext)
      Public Default Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void addResourceConfig​(org.smooks.api.resource.config.ResourceConfig resourceConfig)
      Add a resource configuration to this Smooks instance.
      void addResourceConfigs​(InputStream resourceConfigStream)
      Add a set of resource configurations to this Smooks instance.
      void addResourceConfigs​(String resourceURI)
      Add a set of resource configurations to this Smooks instance.
      void addResourceConfigs​(String baseURI, InputStream inputStream)
      Add a set of resource configurations to this Smooks instance.
      org.smooks.api.resource.config.ResourceConfig addVisitor​(org.smooks.api.resource.visitor.Visitor visitor)
      Add a visitor instance to this Smooks instance.
      org.smooks.api.resource.config.ResourceConfig addVisitor​(org.smooks.api.resource.visitor.Visitor visitor, String targetSelector)
      Add a visitor instance to this Smooks instance.
      void addVisitors​(org.smooks.api.delivery.VisitorAppender visitorAppender)
      Adds a Visitor to this Smooks via a VisitorAppender.
      void close()
      Close this Smooks instance and all associated resources.
      org.smooks.api.ExecutionContext createExecutionContext()
      Create a ExecutionContext instance for use on this Smooks instance.
      org.smooks.api.ExecutionContext createExecutionContext​(String targetProfile)
      Create a ExecutionContext instance for use on this Smooks instance.
      void filterSource​(org.smooks.api.ExecutionContext executionContext, org.smooks.api.io.Source source, org.smooks.api.io.Sink... sinks)
      Filter the content in the supplied Source instance, outputing data to the supplied Sink instances.
      void filterSource​(org.smooks.api.io.Source source)
      Filter the content in the supplied Source instance.
      void filterSource​(org.smooks.api.io.Source source, org.smooks.api.io.Sink... sinks)
      Filter the content in the supplied Source instance, outputing data to the supplied Sink instances.
      org.smooks.api.ApplicationContext getApplicationContext()
      Get the Smooks ApplicationContext associated with this Smooks instance.
      Smooks setExports​(Exports exports)
      Set the Exports for this Smooks instance.
      void setFilterSettings​(FilterSettings filterSettings)
      Deprecated.
      set filter settings from instead.
      void setNamespaces​(Properties namespaces)
      Set the namespace prefix-to-uri mappings to be used on this Smooks instance.
      void setReaderConfig​(org.smooks.api.resource.config.ReaderConfigurator readerConfigurator)
      Set the configuration for the reader to be used on this Smooks instance.
      protected void setSinks​(org.smooks.api.ExecutionContext executionContext, org.smooks.api.io.Sink... sinks)  
      protected void setSource​(org.smooks.api.ExecutionContext executionContext, org.smooks.api.io.Source source)  
    • Method Detail

      • setFilterSettings

        @Deprecated
        public void setFilterSettings​(FilterSettings filterSettings)
        Deprecated.
        set filter settings from instead.
        Set the filter settings for this Smooks instance.
        Parameters:
        filterSettings - The filter settings to be used.
      • setExports

        public Smooks setExports​(Exports exports)
        Set the Exports for this Smooks instance.
        Parameters:
        exports - The exports that will be created by this Smooks instance.
      • setReaderConfig

        public void setReaderConfig​(org.smooks.api.resource.config.ReaderConfigurator readerConfigurator)
        Set the configuration for the reader to be used on this Smooks instance.
        Parameters:
        readerConfigurator - ReaderConfigurator instance.
      • setNamespaces

        public void setNamespaces​(Properties namespaces)
        Set the namespace prefix-to-uri mappings to be used on this Smooks instance.
        Parameters:
        namespaces - The namespace prefix-to-uri mappings.
      • addVisitor

        public org.smooks.api.resource.config.ResourceConfig addVisitor​(org.smooks.api.resource.visitor.Visitor visitor)
        Add a visitor instance to this Smooks instance.

        This Visitor will be targeted at the root (#document) fragment.

        Parameters:
        visitor - The visitor implementation.
      • addVisitor

        public org.smooks.api.resource.config.ResourceConfig addVisitor​(org.smooks.api.resource.visitor.Visitor visitor,
                                                                        String targetSelector)
        Add a visitor instance to this Smooks instance.
        Parameters:
        visitor - The visitor implementation.
        targetSelector - The message fragment target selector.
      • addVisitors

        public void addVisitors​(org.smooks.api.delivery.VisitorAppender visitorAppender)
        Adds a Visitor to this Smooks via a VisitorAppender.
        Parameters:
        visitorAppender - the VisitorAppender
      • addResourceConfig

        public void addResourceConfig​(org.smooks.api.resource.config.ResourceConfig resourceConfig)
        Add a resource configuration to this Smooks instance.

        These configurations do not overwrite previously added configurations. They are added to the list of configurations on this Smooks instance.

        Parameters:
        resourceConfig - The resource configuration to be added.
      • addResourceConfigs

        public void addResourceConfigs​(String resourceURI)
                                throws IOException,
                                       SAXException
        Add a set of resource configurations to this Smooks instance.

        Uses the URIResourceLocator class to load the resource.

        These configurations do not overwrite previously added configurations. They are added to the list of configurations on this Smooks instance.

        Parameters:
        resourceURI - The URI string for the resource configuration list. See URIResourceLocator.
        Throws:
        IOException - Error reading resource stream.
        SAXException - Error parsing the resource stream.
      • addResourceConfigs

        public void addResourceConfigs​(String baseURI,
                                       InputStream inputStream)
                                throws SAXException,
                                       IOException
        Add a set of resource configurations to this Smooks instance.

        These configurations do not overwrite previously added configurations. They are added to the list of configurations on this Smooks instance.

        The base URI is required for resolving resource imports. Just specify the location of the resource file.

        Parameters:
        baseURI - The base URI string for the resource configuration list. See URIResourceLocator.
        inputStream - The resource configuration stream.
        Throws:
        IOException - Error reading resource stream.
        SAXException - Error parsing the resource stream.
      • createExecutionContext

        public org.smooks.api.ExecutionContext createExecutionContext()
        Create a ExecutionContext instance for use on this Smooks instance.

        The created context is profile agnostic and should be used where profile based targeting is not in use.

        The context returned from this method is used in subsequent calls to filterSource(ExecutionContext, org.smooks.api.io.Source, org.smooks.api.io.Sink...) It allows access to the execution context instance before and after calls on this method. This means the caller has an opportunity to set and get data bound to the execution context (before and after the calls), providing the caller with a mechanism for interacting with the content filtering phases.

        Returns:
        Execution context instance.
      • createExecutionContext

        public org.smooks.api.ExecutionContext createExecutionContext​(String targetProfile)
                                                               throws org.smooks.api.profile.UnknownProfileMemberException
        Create a ExecutionContext instance for use on this Smooks instance.

        The created context is profile aware and should be used where profile based targeting is in use. In this case, the transfromation/analysis resources must be configured with profile targeting information.

        The context returned from this method is used in subsequent calls to filterSource(ExecutionContext, org.smooks.api.io.Source, org.smooks.api.io.Sink...). It allows access to the execution context instance before and after calls on this method. This means the caller has an opportunity to set and get data bound to the execution context (before and after the calls), providing the caller with a mechanism for interacting with the content filtering phases.

        Parameters:
        targetProfile - The target profile (base profile) on behalf of whom the filtering/serialisation filter is to be executed.
        Returns:
        Execution context instance.
        Throws:
        org.smooks.api.profile.UnknownProfileMemberException - Unknown target profile.
      • filterSource

        public void filterSource​(org.smooks.api.io.Source source)
                          throws org.smooks.api.SmooksException
        Filter the content in the supplied Source instance.

        Not producing a Sink.

        Parameters:
        source - The content Source.
        Throws:
        org.smooks.api.SmooksException - Failed to filter.
      • filterSource

        public void filterSource​(org.smooks.api.io.Source source,
                                 org.smooks.api.io.Sink... sinks)
                          throws org.smooks.api.SmooksException
        Filter the content in the supplied Source instance, outputing data to the supplied Sink instances.
        Parameters:
        source - The filter Source.
        sinks - The filter Sinks.
        Throws:
        org.smooks.api.SmooksException - Failed to filter.
      • filterSource

        public void filterSource​(org.smooks.api.ExecutionContext executionContext,
                                 org.smooks.api.io.Source source,
                                 org.smooks.api.io.Sink... sinks)
                          throws org.smooks.api.SmooksException
        Filter the content in the supplied Source instance, outputing data to the supplied Sink instances.
        Parameters:
        executionContext - The ExecutionContext for this filter operation. See createExecutionContext(String).
        source - The filter Source.
        sinks - The filter Sinks.
        Throws:
        org.smooks.api.SmooksException - Failed to filter.
      • setSource

        protected void setSource​(org.smooks.api.ExecutionContext executionContext,
                                 org.smooks.api.io.Source source)
      • setSinks

        protected void setSinks​(org.smooks.api.ExecutionContext executionContext,
                                org.smooks.api.io.Sink... sinks)
      • getApplicationContext

        public org.smooks.api.ApplicationContext getApplicationContext()
        Get the Smooks ApplicationContext associated with this Smooks instance.
        Returns:
        The Smooks ApplicationContext.
      • close

        public void close()
        Close this Smooks instance and all associated resources.

        Should result in the uninitialization of all allocated ContentHandler instances.

        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable