apache > cocoon

- Home

# ScriptGenerator in Cocoon

## ScriptGenerator

NAME | script
WHAT | The `ScriptGenerator` component is used to generate XML by invoking a script.
TYPE | Generator, Sitemap Component
BLOCK | bsf
CLASS | org.apache.cocoon.generation.ScriptGenerator
SINCE | Cocoon 2.1
CACHEABLE | no

## Description

The ScriptGenerator executes arbitrary scripts using the Apache BSF framework and additional interpreter (Rhino, Jython, etc.) as a Cocoon Generator.

## Usage

The ScriptGenerator is used primarily for prototyping a Cocoon generator. Moreover if the generator's XML output is low and the logic processing is high.

### Sitemap pipeline examples

The following sample uses the ScriptGenerator for generating XML content for URIs matching the pattern `*.js`. The output of the script invoked by ScriptGenerator is serialized by the default serializer.

    <map:match pattern="*.js">
      <map:generate type="script" src="{0}" >
      </map:generate>
      <map:serialize/>
    </map:match>

### Sitemap component configuration example

The following sample declares the ScriptGenerator in the sitemap's component section. The ScriptGenerator can be used in the sitemap's section using `type` value `script`. In this sample no extra script language is declared.

    <map:generators...
      <map:generator name="script"
        src="org.apache.cocoon.generation.ScriptGenerator"
        logger="sitemap.generation.scriptgenerator"
      />
        <!-- optional generator configuration -->
        ...
    </map:generators>
    ...

### Configuration

The ScriptGenerator allows registering additional script languages.

List the newly registered language inside the `add-language` element, each language is defined in `language` element, by specifying the name of the language using the `name` attribute, and the language BSF adaptor using the attribute `src`.

Each registered language should be mapped to at least one extension, as the language detection of ScriptGenerator is based on the script's extension.

The following sample register an new language foobar, the BSF adaptor class is `foo.bar.ScriptLanguage`, and it is associated with the extensions `foo`, and `bar`.

    <add-language>
      <language name="foobar" src="foo.bar.ScriptLanguage">
        <extension>foo</extension>
        <extension>bar</extension>
      </language>
      ...
    </add-language>

The next table lists the default mapping of BSF:

Extension | Script language
--- | ---
javascript | js
jacl | jacl
netrexx | nrx
java | java
javaclass | class
bml | bml
vbscript | vbs
jscript | jss
jscript | jss
perlscript | pls
perl | pl
jpython | py
lotusscript | lss
xslt | xslt
pnuts | pnut
beanbasic | bb

### Setup

ScriptGenerator registers following objects before invoking the script:

Name | Object Typ | Comment
--- | --- | ---
resolver | Resolver | Cocoon's resolver
source | Source | src attribute of this ScriptGenerator
objectModel | Map | Cocoon's objectModel
parameters | Parameters | paramters of this ScriptGenerator
output | StringBuffer | XML content, forwarded into Cocoon's XML pipeline
logger | Logger | logger of this ScriptGenerator

The script shall write XML content into the StringBuffer object `output.` The content of `output` is parsed by the ScriptGenerator and forwarded into the sitemap pipeline.

### Effect on Object Model and Sitemap Parameters

none

## Bugs/Caveats

ScriptGenerator expects the script to write the XML content into the passed object `output`.

Be aware to provide script language implementation, beside BSF implementation.

## History

07-24-03: initial creation

## See also

A general documentation about generators is available at generators.

Further Documentation about Apache BSF is available here.

Errors and Improvements? If you see any errors or potential improvements in this document please help us: View, Edit or comment on the latest development version (registration required).