smooks / smooks-scripting-cartridge

Smooks Scripting Cartridge

Home Page:https://www.smooks.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Smooks Scripting Cartridge

Maven Central Sonatype Nexus (Snapshots) Build Status

Support for Groovy scripting is made available through the https://www.smooks.org/xsd/smooks/groovy-2.0.xsd configuration namespace.

Example configuration:

smooks-config.xml
<?xml version="1.0"?>
<smooks-resource-list xmlns="https://www.smooks.org/xsd/smooks-2.0.xsd"
                      xmlns:g="https://www.smooks.org/xsd/smooks/groovy-2.0.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>

Usage Tips

  1. Imports can be added via the imports element. A number of classes are automatically imported:

    • org.smooks.support.DomUtils

    • org.smooks.api.bean.context.BeanContext

    • org.smooks.api.bean.repository.BeanRepository

    • org.w3c.dom.*

    • groovy.xml.dom.DOMCategory, groovy.xml.dom.DOMUtil, groovy.xml.DOMBuilder

  2. 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. 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. If the script contains special XML characters, it can be wrapped in an XML Comment or CDATA section.

Groovy Scriptlet Variables and Methods

A number of variables and methods are made directly available to groovy scriptlet code i.e. you can reference them directly in your scriptlet.

Variables

  • element: The DOM/SAX Element (depending on which filter type is in use) i.e. org.w3c.dom.Element or org.api.smooks.delivery.sax.SAXElement.

  • executionContext: The Smooks ExecutionContext instance associated with the Smooks filtering operation.

  • nodeModels: A map containing all the DOM NodeModels currently available (i.e., Map<String, Element>).

Methods

  • Get a bean from Object BeanContext#getBean(String beandId).

Adding Beans to the BeanContext

As shown above, all scriptlet code can access the beans by calling BeanContext#getBean(String) from inside the scriptlet code. One oversight when implementing this feature was that we didn’t provide an addBean method for adding beans to the bean context. However, it is still possible to do so in a slightly more long-winded method via the executionContext e.g.:

executionContext.getBeanContext().addBean("myBean", myBeanInstance);

Maven Coordinates

pom.xml
<dependency>
    <groupId>org.smooks.cartridges</groupId>
    <artifactId>smooks-scripting-cartridge</artifactId>
    <version>2.0.0-RC3</version>
</dependency>

License

Smooks Scripting Cartridge is open source and licensed under the terms of the Apache License Version 2.0, or the GNU Lesser General Public License version 3.0 or later. You may use Smooks Scripting Cartridge according to either of these licenses as is most appropriate for your project.

SPDX-License-Identifier: Apache-2.0 OR LGPL-3.0-or-later

About

Smooks Scripting Cartridge

https://www.smooks.org

License:Other


Languages

Language:Java 69.2%Language:FreeMarker 13.0%Language:Groovy 9.4%Language:HTML 8.4%