Extending terp programmatically

Extending terp programmatically

You can do quite a bit with custom transformers inside terp, but at some point, you will probably want to extend terp's built-in functionality with a set of features that require coding. This example demonstrates how easy programmatic extension of terp is for anyone who can write Java code.

The example consists of the following files:

Example Source Files
Name Description
build.xml The ANT build script for the example. Change into this directory and invoke ant without any arguments to build.
Double.java A custom transformer that doubles the numeric value of its target object. It is registered with the terp framework under the timestwo alias.
Initializer.java The registration callback. This callback is invoked by the terp launcher when you run terp from the commandline. To make sure that it is automatically invoked, use the terp launcher for your own embedded use as well.

How to build the example

You need

  • a JDK5 or higher to compile the Java source files
  • ANT 1.6.5 or higher. While the example does not require ANT 1.7 like the terp ANT tasks, we recommend that you use it here also to prevent configuration problems.

Building the example is easy: just change into the doc/examples/extend directory, type ant, and hit return. This will add a jar file named terp-extend.jar to the distribution's lib directory. Please make sure that this file exists before testing the extension features.

How to test the example

As already mentioned, the extension functionality consists of a transformer that doubles the numeric value it is given and it is registered under the timestwo name. Try it out as follows:

C:\terp\bin>terp -e "4.1[timestwo]"
C:\terp\bin>terp -e "{0,1,2}[timestwo][join(',')]"

Everything works as expected.

Copyright 2006-${now['%tY']} by Codemesh, Inc., ALL RIGHTS RESERVED

extending terp
codemesh.com home expressions templates ant about us contact us download