Class ScriptRunner<T>

java.lang.Object
com.norconex.importer.handler.ScriptRunner<T>
Type Parameters:
T - The evaluation response type.

public class ScriptRunner<T> extends Object

Runs scripts written in supported programming languages. Any objects can be bound to instances of this class to reference them or modify them.

About Return Values

In addition to being able to modify bound objects, it is not uncommon to expect a script to return a value. Unfortunately, script engine implementations have different level of support for returning values. To get around this, all script engines provided by the Importer will always check first for the presence of a variable called returnValue (case-sensitive). When set, it will be considered to be your script output, regardless whether you have a return statement or not. If you do not set a returnValue variable, behavior is specific to each engine.

Provided Scripting Languages

At a minimum the Importer supports the following languages and versions out-of-the-box (as of this writing, see Importer release notes for updates). Replace "xyz" to any value/object/variable being returned.

Supported scripting engines
Engine Name Language Version Returning a Value E.g. Documentation Notes
JavaScript ECMAScript 2022 (ES13)
returnValue = xyz;
If returnValue is not set, the last (unscoped) variable assigned is automatically returned.
Language Specification GraalVM implementation.
lua Lua 5.2
returnValue = xyz;
If returnValue is not set (unscoped), an normal Lua return statement can be used instead.
Reference manual Luaj project
python Python 2.7
returnValue = xyz
Language reference Jython implementation
velocity Apache Velocity 2.3
#set(#returnValue = $xyz)
VTL Reference

Escaping

When using scripting as part of an XML configuration parsed by the crawler (typical command-line usage), you may need to escape certain characters. The main reason being before your script get interpreted, the configuration is first parsed by Apache Velocity. As an example, the dollar sign is used to prefix Velocity variables. So if your script is also velocity, you will need to prefix the dollar sign with a backslash (e.g., \${myvar}).

Adding Additional Scripting Languages

You can add support for your favorite scripting language, as long as there is there is a Java "Script Engine" for it, implementing the JSR 223 API specification.

Several third-party script engines already exist to support additional languages such as Groovy, JRuby, Scala, etc. Refer to appropriate third-party documentation about these languages to find out how to use them.

Note: While using a scripting language can be very convenient, it can make your setup harder to maintain by requiring programming knowledge not everybody has. It should only be considered by experimented teams or adventurous users. Use at your own risk.

See Also: