Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
Milyn - Copyright (C) 2006 - 2010
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (version 2.1) as published by the Free Software
Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details:
http://www.gnu.org/licenses/lgpl.txt
*/
package org.milyn.smooks.scripting.groovy;
import groovy.lang.GroovyClassLoader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.groovy.control.CompilationFailedException;
import org.milyn.cdr.SmooksConfigurationException;
import org.milyn.cdr.SmooksResourceConfiguration;
import org.milyn.cdr.annotation.Configurator;
import org.milyn.delivery.ContentHandler;
import org.milyn.delivery.ContentHandlerFactory;
import org.milyn.delivery.DomModelCreator;
import org.milyn.delivery.Visitor;
import org.milyn.delivery.sax.SAXElement;
import org.milyn.delivery.annotation.Initialize;
import org.milyn.delivery.annotation.Resource;
import org.milyn.io.StreamUtils;
import org.milyn.javabean.context.BeanContext;
import org.milyn.util.FreeMarkerTemplate;
import org.milyn.xml.DomUtils;
import org.w3c.dom.Element;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
/**
* {@link Visitor} Factory class for the Groovy scripting language.
*
* Implement DOM or SAX visitors using the Groovy scripting language.
*
*
Usage Tips
*
*
Imports: Imports can be added via the "imports" element. A number of classes are automatically imported:
*
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.
*
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".
*
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.
*
*
*
Mixing SAX and DOM Models
* When using the SAX filter, Groovy scripts can take advantage of the {@link 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 {@link 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 {@link 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:
*
*
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 {@link SAXElement}.
*
The DOM fragment must be explicitly writen to the result using "writeFragment". See example below.
*
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 {@link DomModelCreator} works for SAX.
*
* 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>
*