Who is the Tea Engine library for?
For people who want to integrate Tea code into existing Java frameworks
using the standard JSR-223 JavaTM Scripting API, without having to learn internal details of Tea Java
interfaces.
Reading of Java
1.6 scripting features and Tea
documentation is highly recommended.
top
From the
In this example, we call the
Examples
The same examples provided in the Java Scripting Programmers guide are
provided, adapted to Tea, for convinience.
"Hello, World"
ScriptEngineManager instance, we request a
Tea engine instance using
getEngineByName method. On the script engine, the eval
method is called to execute
a given String as Tea code! For brevity, in this as well as in
subsequent examples, we have not shown
exception handling. There are checked and runtime exceptions thrown
from javax.script API.
Needless to say, you have to handle the exceptions appropriately.
top
public class HelloWorld {
public static void main(String[] args) throws Exception {
// create a script engine manager
ScriptEngineManager m = new ScriptEngineManager();
// create engine
ScriptEngine engine = m.getEngineByName("tea");
// evaluate JavaScript code from string.
engine.eval("echo \"Tea says Hello Java Standard World!\"");
//((TeaScriptEngine)engine).end();
}
}
Evaluating a Script File
eval method that accepts java.io.Reader
for the input source.
The script read by the given reader is executed. This way it is
possible to execute scripts from files, URLs and
resources by wrapping the relevant input stream objects as readers.
import javax.script.*;
Let us assume that we have the file named "sample.tea" with the
following text:
public class EvalFile {
public static void main(String[] args) throws Exception {
// create a script engine manager
ScriptEngineManager factory = new ScriptEngineManager();
// create engine
ScriptEngine engine = m.getEngineByName("tea");
// evaluate Tea code from file with filename as 1st argument.
engine.eval(new java.io.FileReader(args[0]));
}
}
echo "Hello"
We can run the above Java as
When you embed script engines and scripts with your Java application,
you may want to
expose your application objects as global variables to scripts. This
example demonstrates
how you can expose your application objects as global variables to a
script. We create a
Sometimes you may want to call a specific scripting function repeatedly
- for example,
your application menu functionality might be implemented by a script.
In your menu's action
event handler you may want to call a specific script function. The
following
example demonstrates invoking a specific script function from Java
code.
Tea supports objects through the native TOS library, so
you can invoke a script method on a script object.
top
java EvalFile sample.tea
Script Variables
java.io.File in the application and
expose the same as a global variable with
the name "file". The script can access the variable - for example, it
can call public methods
on it. Note that the syntax to access Java objects, methods and fields
is dependent on the scripting language.
public class ScriptVars {
top
public static void main(String[] args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("Tea Engine");
String s = new String("test string");
// expose File object as variable to script
engine.put("aString", s);
// evaluate a script string. The script accesses "file"
// variable and calls method on it
engine.eval("echo $aString");
}
}
Invoking Script Functions and Methods
import javax.script.*;
public class InvokeScriptFunction {
public static void main(String[] args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("Tea Engine");
// evaluate a script string.
engine.eval("global hello ( arg ) { echo \"hello \" $arg }"); // javax.script.Invocable is an optional interface.
// Check whether your script engine implements or not!
// Note that the Tea Engine implements Invocable interface.
Invocable inv = (Invocable) engine;
// invoke the global function named "hello"
inv.invokeFunction("hello", "Scripting!!" );
}
}
top
import javax.script.*;
public class InvokeScriptMethod {
public static void main(String[] args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("Tea Engine");
// evaluate a script string. // TODO: load should not be needed
engine.eval("load \"com.pdmfc.tea.modules.tos.SModuleTos\"; class Test ( ) ; method Test hello ( arg ) { echo \"hello \" $arg } ; global obj [new Test]");
// javax.script.Invocable is an optional interface.
// Check whether your script engine implements or not!
// Note that the Tea Engine implements Invocable interface.
Invocable inv = (Invocable) engine;
// get script object on which we want to call the method
Object obj = engine.get("obj");
// invoke the method named "hello" on the script object "obj"
inv.invokeMethod(obj, "hello", "Script Method !!" );
}
}
Copyright 2007 PDM&FC