@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.
org.milyn.xml.DomUtilsBeanContextorg.w3c.dom.*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:
SAXElement.DomModelCreator works for SAX.
<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>
PARAM_RESTYPE| Constructor and Description |
|---|
GroovyContentHandlerFactory() |
| Modifier and Type | Method and Description |
|---|---|
ContentHandler |
create(SmooksResourceConfiguration configuration)
Create the content handler instance.
|
void |
initialize() |
@Initialize public void initialize() throws IOException
IOExceptionpublic ContentHandler create(SmooksResourceConfiguration configuration) throws SmooksConfigurationException
ContentHandlerFactorycreate in interface ContentHandlerFactoryconfiguration - The SmooksResourceConfiguration for the ContentHandler
to be created.SmooksConfigurationException - Successfully created ContentHandler, but an error occured during configuration.Copyright © 2020. All rights reserved.