groovygroovy
Script Engine to execute Groovy
Home > Books > NetKernel API and Services Reference > Accessor Listing > Scripting Runtimes > groovy

Rate this page:
Really useful
Satisfactory
Not helpful
Confusing
Incorrect
Unsure
Extra comments:


Module

urn:org:ten60:netkernel:ext:script

The groovy accessor is exported by the urn:org:ten60:netkernel:ext:script module. Import this module to gain access to the accessor.

Syntax

URI
active:groovy

ArgumentRulesDescription
operatorMandatory script to be executed
arg1Optional An argument to pass to the script
argNOptional An argument to pass to the script

Example Usage

DPML

<instr>
  <type>groovy</type>
  <operator>myscript.groovy</operator>
  <myargument>mydoc.xml</myargument>
  <target>var:result</target>
</instr>

NetKernel Foundation API

req=context.createSubRequest("active:groovy");
req.addArgument("operator", [resource representation, aspect, or URI] );
result=context.issueSubRequest(req);

Purpose

The groovy accessor executes Groovy language scripts.

The operator specifies the resource to be executed as a program. The script may either be a plain/text script or may be encapsulated in an XML document with the script specified as the text of the root element. When using XML it is strongly advisable to enclose your script inside a CDATA section, for example:

<script><![CDATA[ ...your script .. ]]></script>

Script Context

When executed, each script is provided an instance of the INKFConvenienceHelper class as the local variable context. The context variable has two purposes. It supports introspection of the request which initiated the script and it allows sub-requests to be issued. For more details see the NKF Reference Guide.

Once a response object is created:

response=context.createResponseFrom(...);

it will be returned via the context when the script exits.

Parameters

Parameters can be passed to a script using named arguments. Argument values are available within the script through the context using the this:param URI address space convention.

For example if the groovy accessor is requested with...

active:groovy+operator@myscript.groovy+myargument@data.xml

The myargument parameter can be obtained inside the script using the context and the argument URI "this:param:myargument".

context.source("this:param:myargument")

Classpath

A groovy script inherits the classpath of the execution context. The order for locating a class is as follows:

  1. The ext-script module
  2. The superstack back to the module where the groovy accessor was invoked
  3. The libraries and imports of the module originating the script execution request.
  4. The NetKernel core classes
  5. The Java VM classpath

A Java class can be imported into a script with a regular Java import statement:

import java.util.Random;

Example

This example shows a Groovy script performing an XSLT transformation, logging the result and returning the result as the response:

req = context.createSubRequest();
req.setURI("active:xslt");
uris = [
        "operator" : "examples/resources/act_to_html.xsl",
        "operand"  : "examples/resources/lear.xml"
       ]
uris.each { | key, value | req.addArgument(key,value) }
rep  = context.issueSubRequest(req);
resp = context.createResponseFrom(rep);
© 2003-2007, 1060 Research Limited. 1060 registered trademark, NetKernel trademark of 1060 Research Limited.