Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

This section describes a number of common use cases in which Smooks can be used.

Table of Contents
maxLevel2

Templating

Include Page
Templating with Smooks
Templating with Smooks

Groovy Scripting

Support for Groovy based scripting is made available through the http://www.milyn.org/xsd/smooks/groovy-1.1.xsd configuration namespace. This adds support for DOM or SAX based Groovy scripting.

Example configuration:

No Format
<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:g="http://www.milyn.org/xsd/smooks/groovy-1.1.xsd">

    <g:groovy executeOnElement="xxx">
        <g:script>
            <!--
            //Rename the target fragment element from "xxx" to "yyy"...
            DomUtils.renameElement(element, "yyy", true, true);
            -->
        </g:script>
    </g:groovy>

</smooks-resource-list>
Info
titleUsage Tips
  1. Imports: Imports can be added via the "imports" element. A number of classes are automatically imported:
    • org.milyn.xml.DomUtils
    • org.milyn.javabean.repository.BeanRepository
    • org.w3c.dom.*
    • groovy.xml.dom.DOMCategory, groovy.xml.dom.DOMUtil, groovy.xml.DOMBuilder
  2. Visited Element: The visited element is available to the script through the variable "element". It is also available under a variable name equal to the element name, but only if the element name contains alpha-numeric characters only.
  3. Execute Before/After: By default, the script is executed on the visitAfter event. You can direct it to be executed on the visitBefore by setting the "executeBefore" attribute to "true".
  4. Comment/CDATA Script Wrapping: If the script contains special XML characters, it can be wrapped in an XML Comment or CDATA section. See example below.

Mixed DOM and SAX with Groovy

Because Groovy has a number of very useful DOM processing features, we added support for the mixed DOM and SAX processing models.

What this means is that you can use Groovies DOM utilities to process the targeted message fragment. The "element" received by the Groovy script will be a DOM Element. This makes Groovy scripting via the SAX filter a lot easier, while at the same time maintaining the ability to process huge messages in a streamed fashion.

Info
titleMixed SAX and DOM Gotchas
  1. Only available in default mode i.e. when executeBefore equals "false". If executeBefore is configured "true", this facility is not available and the Groovy script will only have access to the element as a SAXElement.
  2. The DOM fragment must be explicitly written to the result using "writeFragment". See example below.
  3. There is an obvious performance overhead incurred using this facility (DOM construction). That said, it can still be used to process huge messages because of how the DomModelCreator works for SAX. The tradeoff is usability Vs performance.

Mixed DOM and SAX Example

Take an XML message such as:

No Format
<shopping>
    <category type="groceries">
        <item>Chocolate</item>
        <item>Coffee</item>
    </category>
    <category type="supplies">
        <item>Paper</item>
        <item quantity="4">Pens</item>
    </category>
    <category type="present">
        <item when="Aug 10">Kathryn's Birthday</item>
    </category>
</shopping>

Using Groovy, we want to modify the "supplies" category in the shopping list, adding 2 to the quantity, where the item is "Pens". To do this, we write a simple little Groovy script and target it at the <category> elements in the message. The script simple iterates over the <item> elements in the category and increments the quantity by 2, where the category type is "supplies" and the item is "Pens":

No Format
<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:g="http://www.milyn.org/xsd/smooks/groovy-1.1.xsd">

    <params>
        <param name="stream.filter.type">SAX</param>
    </params>
    
    <g:groovy executeOnElement="category">
        <g:script>
            <!--
            use(DOMCategory) {

                // Modify "supplies": we need an extra 2 pens...
                if (category.'@type' == 'supplies') {
                    category.item.each { item ->
                        if (item.text() == 'Pens') {
                            item['@quantity'] = item.'@quantity'.toInteger() + 2;
                        }
                    }
                }
            }

            // When using the SAX filter, we need to explicitly write the fragment
            // to the result stream...
            writeFragment(category);
            -->
        </g:script>
    </g:groovy>

</smooks-resource-list>

Processing Non-XML Data (CSV, EDI, JSON, Java etc)

Include Page
Stream Readers
Stream Readers

Java Binding

Include Page
Binding Message Data to Java Object Models
Binding Message Data to Java Object Models

Java to Java Transformations

Include Page
Java to Java Transformations
Java to Java Transformations

Processing Huge Messages (GBs)

Include Page
Processing Huge Messages with Smooks
Processing Huge Messages with Smooks

Message Splitting & Routing

Please refer to the Splitting & Routing section in the previous section.

Persistence (Database Reading and Writing)

Include Page
SQLExecutor
SQLExecutor

Message Enrichment

Use the SQLExecutor to query a database. The queried data will be bound to the bean context (ExecutionContext). Use the bound query data to enrich your messages e.g. where you are splitting and routing.

TODO!!