|
|
|
The Model-Driven System Builder
|
|
JeeWiz Architect's Guide
|
|
|
|
Contents >
8. Templates and Velocity Features
|
|
|
8.10 Using Velocity For Scripting
8.10.1 Local Methods Compared to Local Macros
|
You can use Velocity as a scripting language.
To be honest, the implementation is a hack for now ... but a useful one and it gets the job done.
We tend to write some complex jobs using JeeWiz/Velocity because it saves having to learn or remember another script environment.
In addition, the JeeWiz environment provides significant features for scripting that help in specific ways:
- Easy file handling: readFileAsString/Lines/XML, writeStringToFile
- Complex string handling made easy.
e.g. see the helper methods:
getPieces*, CSV handling, exciseString(), ordering using TreeMap.
- Access to Java classes for specialised processing.
- XML process: the convert()
feature for filtering and changing element tags, utilities for writing attributes and CDATA,
formatXML() for easy reformatting of XML.
Our (hard-won) experience is that, compared to XSL-T,
JeeWiz is much faster to develop and runs dramatically faster.
- Easy to define methods (normal JeeWiz #method()...#end)
- divert/revert for massaging output text; easy to write reports.
8.10.2 jwscript batch job
|
The key to using JeeWiz/Velocity for scripting is to use jwscript rather than ant
to run the script.
See jeewiz/external/ant1.7.0/bin/jwscript.bat for the Windows batch job that runs this.
(There is currently no UNIX equivalent. Please feel free to contribute!
If so, also check out the implementatin file jeewiz/engine/src/uk/co/nte/jw/engine/JwScript.java.
)
This runs the Ant process in the normal way,
but using the class uk.co.nte.jw.engine.JwScript as the main program for Ant.
You can implement extensions to the scheme described here
by copying and amending jeewiz/external/ant1.7.0/bin/jwscript.bat (but best to keep it in the Ant directory).
8.10.3 Syntax and Operation
|
The complete syntax for a script invocation is:
jwscript [-x XmlFileName] scriptfile [arg | antSwitch]...
|
In words:
- The specification.
You do not need to input a model/specification to a script.
-
If you do not input a specification, "" is used;
this is taken from the file jeewiz/resources/jwscript/specification/assembly.xml.
-
If you want to input a specification, specify "-x" and the file as the first arguments
There is no default extension (like ".xml").
This must be an XML format file.
Any valid XML file is allowed: there is no specific meta-model checking
because this uses the "any" to run, which will accept any valid XML.
- The script file.
A Velocity script (by convention with a ".vs" extension) must be specified.
This is what will be run!
The script is copied to jeewiz/resources/jwscript/control/start.vm,
which will automatically run with the top component (i.e. the root XML element) as its context.
- Arguments
Arguments (intended for the script) and Ant switches (like '-v') are specified after the scriptfile.
They can be in any order.
The Velocity variable
$args is always defined for a script.
It is an ArrayList.
If no arguments are specified, $args is empty - size()==0.
If arguments are specified, they are added to the ArrayList in the order they appear on the command line.
The first argument is accessed by $args.get(0); the second by $args.get(1).
- For example,
currently it is not possible to use local files (i.e. in the same directory as the Velocity script file) in #parse().
These will have to be copied into the target area for the script file
(i.e. make it a peer of jeewiz/resources/jwscript/control).
And if you do this, you might want to change the location to your own specific subdirectory of jeewiz/examples/any rather than use the
jwscript/ directory.
This is straightforward to do in your own enhanced batch job.
8.10.4 Piping input
|
If a file is piped into the cmd (e.g. jwscript < foo.txt ),
the input stream is available in the top-level Velocity variable $in.
This is read in using the default character encoding for JeeWiz.
Copyright (c) 2001-2008 New Technology/enterprise Ltd.
| |