Executes code written in specified scripting language. Web-Harvest supports BeanShell, Groovy and Javascript. All of them are powerfull, wide-spread and popular scripting languages.

Body of script processors is executed in specified language and optionally evaluated expression specified in return attribute is returned. All variables defined during configuration execution are also available in the script processor. However, it must be noted that variables used throughtout Web-Harvest are not simple types - they all are org.webharvest.runtime.variables.Variable objects (internal Web-Harvest class) that expose convinient methods:

The way to push value back to the Web-Harvest after script finishes is command sys.defineVariable(varName, varValue, [overwrite]) which creates appropriate wrapper around specified value : list variables for java.util.List and arrays and simple variables for other objects. The best way to illustrate this is simple example bellow.

Each script engine used in the single Web-Harvest configuration, once created, preserves its variable context throughout the configuration, meaning that all variables and objects are available in further script processors that use the same language.

Syntax

<script language="script_language" return="value_to_return">
    body as script
</script>

Attributes

Name Required Default Description
language no Default scripting language if defined in config element, or beanshell if nothing is defined. Defines which scripting engine is used in the processor. Valid values are beanshell, javascript and groovy.
return no Empty value Specifies what this processor should evaluate at the end and return as processing value.

Example

<config>
    <var-def name="birthDate">
        11/4/1958
    </var-def>

    <var-def name="web_harvest_day_variable">
        <script return="namedDay.toUpperCase()"><![CDATA[
            tokenizer = new StringTokenizer(birthDate.toString(), "./-\\");

            day = Integer.parseInt(tokenizer.nextToken());
            month = Integer.parseInt(tokenizer.nextToken());
            year = Integer.parseInt(tokenizer.nextToken());

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.DAY_OF_MONTH, day);
            cal.set(Calendar.MONTH, month-1);
            cal.set(Calendar.YEAR, year);

            switch( cal.get(Calendar.DAY_OF_WEEK) ) {
                case 0 : namedDay = "Sunday"; break;
                case 1 : namedDay = "Monday"; break;
                case 2 : namedDay = "Tuesday"; break;
                case 3 : namedDay = "Wendsday"; break;
                case 4 : namedDay = "Thursday"; break;
                case 5 : namedDay = "Friday"; break;
                default: namedDay = "Saturday"; break;
            }
        ]]></script>
    </var-def>

    <template>
        The day when you were born was ${namedDay}.
    </template>

    <file action="write" path="day.txt">
        <var name="web_harvest_day_variable"/>
    </file>
</config>

This example also shows that script internal variables once defined, are available in all the following script and template processors (namedDay).