Class JavaScriptTool
- All Implemented Interfaces:
ITool,IVariable,Serializable,Cloneable
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 :
- Curly braces character '{' and '}' cannot be used as JS script in RTF file. The curly braces are special
characters use by RTF syntax. When you want to evaluate the JavaScript function, you should put the function on
separate JS file.
This statement will not be executed in RTF template. The curly braces are prohibited.$js.eval("function foo() { return "Foo"; }") - JS script can access only implicit variables, runtime variable is not accessible. For example:
The#set ($runtime = "hello world")
$runtimeis 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)
- Since:
- Sep 9, 2008
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.nomagic.magicreport.engine.ITool
ITool.HTMLString, ITool.RetainedString, ITool.Void -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intEngineScope attributes are visible during the lifetime of a single call and a set of attributes is maintained for each method call.static final intGlobalScope attributes are visible to all method call.Fields inherited from class com.nomagic.magicreport.engine.Tool
context, properties -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCall a JavaScript function.Call a JavaScript function.Call a JavaScript function.Call a JavaScript function.voiddestroy()Called by the engine to inform this tool is no longer use and that it should destroy any resources that it has allocated.Evaluate the specified script.Evaluate the script using the bindings argument.Evaluate the script using the bindings argument.Evaluate the script using the set of bindings argument.Execute a file as JavaScript source.Execute a file as JavaScript source using the bindings argument.Execute a file as JavaScript source using the bindings argument.Execute a file as JavaScript source.voidinit(int scope) Initialize JavaScript engine.voidinit(int scope, boolean implicitImportFunction) Initialize JavaScript engine.voidsetShowError(boolean showError) Methods inherited from class com.nomagic.magicreport.engine.Tool
clone, getContext, getProperties, getProperty, getProperty, notifyObservers, setContext, setPropertiesMethods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, setChanged
-
Field Details
-
GLOBAL_SCOPE
public static final int GLOBAL_SCOPEGlobalScope attributes are visible to all method call.- See Also:
-
ENGINE_SCOPE
public static final int ENGINE_SCOPEEngineScope attributes are visible during the lifetime of a single call and a set of attributes is maintained for each method call.- See Also:
-
-
Constructor Details
-
JavaScriptTool
public JavaScriptTool()Create a JavaScriptTool. The constructor will initialize all necessary variable using in this tool. This constructor will be called by template engine.
-
-
Method Details
-
setShowError
public void setShowError(boolean showError) -
init
public void init(int scope) Initialize JavaScript engine. (Default mode isGLOBAL_SCOPE)- Parameters:
scope- define scope of attributes.
-
init
public void init(int scope, boolean implicitImportFunction) Initialize JavaScript engine. Thescopedefines the scope of JS code. TheimplicitImportFunctionallow you to useimportPackage()andimportClass()inside the JS file. TheimportPackageandimportClassfunctions "pollute" the global variable scope of JavaScript. To avoid that, you may turn this option and useJavaImporter.- Parameters:
scope- define scope of attributes.implicitImportFunction- true if you want to uses built-in functions importPackage and importClass (Default istrue).
-
eval
Evaluate the specified script.For example:
The output will be:$js.eval("context.put('var', 'hello world')") And the var value is $varhello world And the var value is hello world- Parameters:
script- the script language source to be executed.- Returns:
- the value returned from the execution of the script or
ITool.VOIDif return value isnull
-
eval
Evaluate the script using the bindings argument. The "importer" name will be used as an binding name for this argument.- Parameters:
script- the script language source to be executed.bindingObject- the bindings of attribute object to be used for script execution.- Returns:
- the value returned from the execution of the script or
ITool.VOIDif return value isnull
-
eval
Evaluate the script using the bindings argument.For example:
The output will be:#foreach ($class in $Class) $js.eval("'Class name is ' + c.getName();", "c", $class) #endClass name is A Class name is B Class name is C- Parameters:
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.- Returns:
- the value returned from the execution of the script or
ITool.VOIDif return value isnull
-
eval
Evaluate the script using the set of bindings argument. The binding map consists of key-value pairs for binding name and binding object.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- Parameters:
script- the script language source to be executed.bindingMap- the key-value pairs for binding name and binding object.- Returns:
- the value returned from the execution of the script or
ITool.VOIDif return value isnull
-
execute
Execute a file as JavaScript source. All characters of the reader are consumed.For example:
Or$js.execute("script.js")$js.execute("c:/myfolder/script.js")- Parameters:
fileName- the source of the script.- Returns:
- the value returned from the execution of the script or
ITool.VOIDif return value isnull
-
execute
Execute a file as JavaScript source using the bindings argument. The "importer" name will be used as an binding name for this argument. All characters from the file are consumed.- Parameters:
fileName- the source of the script.bindingObject- the bindings of attribute object to be used for script execution.- Returns:
- the value returned from the execution of the script or
ITool.VOIDif return value isnull
-
execute
Execute a file as JavaScript source using the bindings argument. All characters from the file are consumed.- Parameters:
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.- Returns:
- the value returned from the execution of the script or
ITool.VOIDif return value isnull
-
execute
Execute a file as JavaScript source. The binding map consists of key-value pairs for binding name and binding object. All characters from the file are consumed.- Parameters:
fileName- the source of the script.bindingMap- the key-value pairs for binding name and binding object.- Returns:
- the value returned from the execution of the script or
ITool.VOIDif return value isnull
-
call
Call a JavaScript function.For example:
$js.eval("function calc(var1, var2) { return var1 + var2; }") $js.call("calc(1, 3)")- Parameters:
function- the JavaScript function- Returns:
- the value returned from the execution of the script or
ITool.VOIDif return value isnull
-
call
Call a JavaScript function. The "importer" name will be used as an binding name for this argument.- Parameters:
function- the JavaScript functionbindingObject- the bindings of attribute object to be used for script execution.- Returns:
- the value returned from the execution of the script or
ITool.VOIDif return value isnull
-
call
Call a JavaScript function. The function must be defined before calling this method. Function can be defined either by methodevalorexecute. 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)- Parameters:
function- the JavaScript functionbindingName- the name being used with binding object.bindingObject- the bindings of attribute object to be used for script execution.- Returns:
- the value returned from the execution of the script or
ITool.VOIDif return value isnull
-
call
Call a JavaScript function. The function must be defined before calling this method. Function can be defined either by methodevalorexecute. The binding map consists of key-value pairs for binding name and binding object.- Parameters:
function- the JavaScript functionbindingMap- the key-value pairs for binding name and binding object.- Returns:
- the value returned from the execution of the script or
ITool.VOIDif return value isnull
-
destroy
public 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.
-