org.milyn.smooks.scripting.groovy
Class GroovyContentHandlerFactory

java.lang.Object
  extended by org.milyn.smooks.scripting.groovy.GroovyContentHandlerFactory
All Implemented Interfaces:
ContentHandler, ContentHandlerFactory

@Resource(type="groovy")
public class GroovyContentHandlerFactory
extends Object
implements ContentHandlerFactory

Visitor Factory class for the Groovy scripting language.

Implement DOM or SAX visitors using the Groovy scripting language.

Usage Tips

Mixing SAX and DOM Models

When using the SAX filter, Groovy scripts can take advantage of the DomModelCreator. This is only the case when the script is applied on the visitAfter event of the targeted element (i.e. executeBefore="false", which is the default). If executeBefore is set to "true", the DomModelCreator will not be utilized.

What this means is that you can use 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.

Notes:

  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 writen 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.

Example Configuration

Take an XML message such as:
 <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":
 <?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">

     <!--
     Use the SAX filter.  Note how we can still process the fragment as a DOM, and write it out
     to the result stream after processing.
     -->
     <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
                         }
                     }
                 }
             }

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

 </smooks-resource-list>
 

Author:
tom.fennelly@gmail.com

Field Summary
 
Fields inherited from interface org.milyn.delivery.ContentHandlerFactory
PARAM_RESTYPE
 
Constructor Summary
GroovyContentHandlerFactory()
           
 
Method Summary
 ContentHandler create(SmooksResourceConfiguration configuration)
           
 void initialize()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GroovyContentHandlerFactory

public GroovyContentHandlerFactory()
Method Detail

initialize

@Initialize
public void initialize()
                throws IOException
Throws:
IOException

create

public ContentHandler create(SmooksResourceConfiguration configuration)
                      throws SmooksConfigurationException,
                             InstantiationException
Specified by:
create in interface ContentHandlerFactory
Throws:
SmooksConfigurationException
InstantiationException


Copyright © 2014. All Rights Reserved.