@OpenApiAll
public class JavaScriptTool
extends com.nomagic.magicreport.engine.Tool
Example:
The output will be:
#foreach ($class in $Class)
$js.eval("'Class name is ' + c.getName();", "c", $class)
#end
Class name is A
Class name is B
Class name is C
Example:
JS file
Template file
// declare function foo()
function foo()
{
return "Foo";
}
// call foo method then return
foo();
The output will be:
$js.execute("jsfile.js")
Foo
All implicit variables such as $Class or $sorter will be automatically imported into JS Script with the same
variable name Example:
function getSupplier()
{
var supplierList = new ArrayList();
for (var i=0; i<$Dependency.size(); i++)
{
var dependency = $Dependency.get(i);
supplierList.add(dependency.getSupplier());
}
return supplierList;
}
Limitation :
$js.eval("function foo() { return "Foo"; }")
#set ($runtime = "hello world")
The $runtime
is runtime variable. This variable
maintain inside Velocity and it is not accessible from other class thus report engine will not able to pass
this variable to JS. You can pass your variable into JS file by passing them on binding arguments of JSTool.
For example:
$js.execute('jsfile.js', 'myvarname', varObj)
Modifier and Type | Field and Description |
---|---|
static int |
ENGINE_SCOPE
EngineScope attributes are visible during the lifetime of a single call and a set of attributes is
maintained for each method call.
|
static int |
GLOBAL_SCOPE
GlobalScope attributes are visible to all method call.
|
Constructor and Description |
---|
JavaScriptTool()
Create a JavaScriptTool.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
call(java.lang.String function)
Call a JavaScript function.
|
java.lang.Object |
call(java.lang.String function,
java.util.Map<java.lang.String,java.lang.Object> bindingMap)
Call a JavaScript function.
|
java.lang.Object |
call(java.lang.String function,
java.lang.Object bindingObject)
Call a JavaScript function.
|
java.lang.Object |
call(java.lang.String function,
java.lang.String bindingName,
java.lang.Object bindingObject)
Call a JavaScript function.
|
void |
destroy()
Called by the engine to inform this tool is no longer use and that it should destroy any resources that it
has allocated.
|
java.lang.Object |
eval(java.lang.String script)
Evaluate the specified script.
|
java.lang.Object |
eval(java.lang.String script,
java.util.Map<java.lang.String,java.lang.Object> bindingMap)
Evaluate the script using the set of bindings argument.
|
java.lang.Object |
eval(java.lang.String script,
java.lang.Object bindingObject)
Evaluate the script using the bindings argument.
|
java.lang.Object |
eval(java.lang.String script,
java.lang.String bindingName,
java.lang.Object bindingObject)
Evaluate the script using the bindings argument.
|
java.lang.Object |
execute(java.lang.String fileName)
Execute a file as JavaScript source.
|
java.lang.Object |
execute(java.lang.String fileName,
java.util.Map<java.lang.String,java.lang.Object> bindingMap)
Execute a file as JavaScript source.
|
java.lang.Object |
execute(java.lang.String fileName,
java.lang.Object bindingObject)
Execute a file as JavaScript source using the bindings argument.
|
java.lang.Object |
execute(java.lang.String fileName,
java.lang.String bindingName,
java.lang.Object bindingObject)
Execute a file as JavaScript source using the bindings argument.
|
void |
init(int scope)
Initialize JavaScript engine.
|
void |
init(int scope,
boolean implicitImportFunction)
Initialize JavaScript engine.
|
clone, getContext, getProperties, getProperty, getProperty, notifyObservers, setContext, setProperties
public static final int GLOBAL_SCOPE
public static final int ENGINE_SCOPE
public JavaScriptTool()
public void init(int scope)
GLOBAL_SCOPE
)scope
- define scope of attributes.public void init(int scope, boolean implicitImportFunction)
scope
defines the scope of JS code. The
implicitImportFunction
allow you to use importPackage()
and
importClass()
inside the JS file. The importPackage
and importClass
functions "pollute" the global variable scope of JavaScript. To avoid that, you may turn this option and use
JavaImporter
.scope
- define scope of attributes.implicitImportFunction
- true if you want to uses built-in functions importPackage and importClass
(Default is true
).public java.lang.Object eval(java.lang.String script)
For example:
The output will be:
$js.eval("context.put('var', 'hello world')")
And the var value is $var
hello world
And the var value is hello world
script
- the script language source to be executed.ITool.VOID
if return value is
null
public java.lang.Object eval(java.lang.String script, java.lang.Object bindingObject)
script
- the script language source to be executed.bindingObject
- the bindings of attribute object to be used for script execution.ITool.VOID
if return value is
null
public java.lang.Object eval(java.lang.String script, java.lang.String bindingName, java.lang.Object bindingObject)
For example:
The output will be:
#foreach ($class in $Class)
$js.eval("'Class name is ' + c.getName();", "c", $class)
#end
Class name is A
Class name is B
Class name is C
script
- the script language source to be executed.bindingName
- the name being used with binding object.bindingObject
- the bindings of attribute object to be used for script execution.ITool.VOID
if return value is
null
public java.lang.Object eval(java.lang.String script, java.util.Map<java.lang.String,java.lang.Object> bindingMap)
For example:
The output will be:
#set ($map = $map.createHashMap())
#set ($void = $map.put("name1", "foo"))
#set ($void = $map.put("name2", "bar"))
$js.eval("'Name is ' + name1 + ' ' + name2;", $map)
Name is foo bar
script
- the script language source to be executed.bindingMap
- the key-value pairs for binding name and binding object.ITool.VOID
if return value is
null
public java.lang.Object execute(java.lang.String fileName)
For example:
Or
$js.execute("script.js")
$js.execute("c:/myfolder/script.js")
fileName
- the source of the script.ITool.VOID
if return value is
null
public java.lang.Object execute(java.lang.String fileName, java.lang.Object bindingObject)
fileName
- the source of the script.bindingObject
- the bindings of attribute object to be used for script execution.ITool.VOID
if return value is
null
public java.lang.Object execute(java.lang.String fileName, java.lang.String bindingName, java.lang.Object bindingObject)
fileName
- the source of the script.bindingName
- the name being used with binding object.bindingObject
- the bindings of attribute object to be used for script execution.ITool.VOID
if return value is
null
public java.lang.Object execute(java.lang.String fileName, java.util.Map<java.lang.String,java.lang.Object> bindingMap)
fileName
- the source of the script.bindingMap
- the key-value pairs for binding name and binding object.ITool.VOID
if return value is
null
public java.lang.Object call(java.lang.String function)
For example:
$js.eval("function calc(var1, var2) { return var1 + var2; }")
$js.call("calc(1, 3)")
function
- the JavaScript functionITool.VOID
if return value is
null
public java.lang.Object call(java.lang.String function, java.lang.Object bindingObject)
function
- the JavaScript functionbindingObject
- the bindings of attribute object to be used for script execution.ITool.VOID
if return value is
null
public java.lang.Object call(java.lang.String function, java.lang.String bindingName, java.lang.Object bindingObject)
eval
or execute
. The binding object will be pass as context
variable under binding name.
For example:
Note : The curly braces character '{' and '}' is not allowed to uses in RTF template.
This curly braces is special characters uses by RTF syntax.
$js.eval("function calc(var1, var2) { var f = factor ? factor : 0; return f + var1 + var2; }")
$js.call("calc(1, 3)", "factor", 10)
function
- the JavaScript functionbindingName
- the name being used with binding object.bindingObject
- the bindings of attribute object to be used for script execution.ITool.VOID
if return value is
null
public java.lang.Object call(java.lang.String function, java.util.Map<java.lang.String,java.lang.Object> bindingMap)
eval
or execute
. The binding map consists of key-value pairs for
binding name and binding object.function
- the JavaScript functionbindingMap
- the key-value pairs for binding name and binding object.ITool.VOID
if return value is
null
public void destroy()
Copyright © 2015 - No Magic Asia