Class JavaScriptTool

java.lang.Object
java.util.Observable
com.nomagic.magicreport.engine.Tool
com.nomagic.reportwizard.tools.script.JavaScriptTool
All Implemented Interfaces:
ITool, IVariable, Serializable, Cloneable

@OpenApiAll public class JavaScriptTool extends Tool
Provide functions to execute JavaScript. The JavaScript can be executed during runtime inside Velocity template.

Example:

    #foreach ($class in $Class)
    $js.eval("'Class name is ' + c.getName();", "c", $class)
    #end
The output will be:
    Class name is A
    Class name is B
    Class name is C

Example:
JS file

    // declare function foo()
    function foo()
    {
       return "Foo";
    }
    // call foo method then return
    foo();
 
Template file
    $js.execute("jsfile.js")
The output will be:
    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 :

  1. 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"; }")
  2. JS script can access only implicit variables, runtime variable is not accessible. For example:
     #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)
     
The Java Scripting reference guide:
Since:
Sep 9, 2008
See Also:
  • Field Details

    • GLOBAL_SCOPE

      public static final int GLOBAL_SCOPE
      GlobalScope attributes are visible to all method call.
      See Also:
    • ENGINE_SCOPE

      public static final 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.
      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 is GLOBAL_SCOPE)
      Parameters:
      scope - define scope of attributes.
    • init

      public void init(int scope, boolean implicitImportFunction)
      Initialize JavaScript engine. The 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.
      Parameters:
      scope - define scope of attributes.
      implicitImportFunction - true if you want to uses built-in functions importPackage and importClass (Default is true).
    • eval

      public Object eval(String script)
      Evaluate the specified script.

      For example:

          $js.eval("context.put('var', 'hello world')")
          And the var value is $var
       
      The output will be:
          hello 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.VOID if return value is null
    • eval

      public Object eval(String script, Object bindingObject)
      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.VOID if return value is null
    • eval

      public Object eval(String script, String bindingName, Object bindingObject)
      Evaluate the script using the bindings argument.

      For example:

          #foreach ($class in $Class)
          $js.eval("'Class name is ' + c.getName();", "c", $class)
          #end
       
      The output will be:
          Class 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.VOID if return value is null
    • eval

      public Object eval(String script, Map<String,Object> bindingMap)
      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:

          #set ($map = $map.createHashMap())
          #set ($void = $map.put("name1", "foo"))
          #set ($void = $map.put("name2", "bar"))
          $js.eval("'Name is ' + name1 + ' ' + name2;", $map)
       
      The output will be:
          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.VOID if return value is null
    • execute

      public Object execute(String fileName)
      Execute a file as JavaScript source. All characters of the reader are consumed.

      For example:

          $js.execute("script.js")
       
      Or
          $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.VOID if return value is null
    • execute

      public Object execute(String fileName, Object bindingObject)
      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.VOID if return value is null
    • execute

      public Object execute(String fileName, String bindingName, Object bindingObject)
      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.VOID if return value is null
    • execute

      public Object execute(String fileName, Map<String,Object> bindingMap)
      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.VOID if return value is null
    • call

      public Object call(String function)
      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.VOID if return value is null
    • call

      public Object call(String function, Object bindingObject)
      Call a JavaScript function. The "importer" name will be used as an binding name for this argument.
      Parameters:
      function - the JavaScript function
      bindingObject - the bindings of attribute object to be used for script execution.
      Returns:
      the value returned from the execution of the script or ITool.VOID if return value is null
    • call

      public Object call(String function, String bindingName, Object bindingObject)
      Call a JavaScript function. The function must be defined before calling this method. Function can be defined either by method eval or execute. The binding object will be pass as context variable under binding name.

      For example:

          $js.eval("function calc(var1, var2) { var f = factor ? factor : 0; return f + var1 + var2; }")
          $js.call("calc(1, 3)", "factor", 10)
       
      Note : The curly braces character '{' and '}' is not allowed to uses in RTF template. This curly braces is special characters uses by RTF syntax.
      Parameters:
      function - the JavaScript function
      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.VOID if return value is null
    • call

      public Object call(String function, Map<String,Object> bindingMap)
      Call a JavaScript function. The function must be defined before calling this method. Function can be defined either by method eval or execute. The binding map consists of key-value pairs for binding name and binding object.
      Parameters:
      function - the JavaScript function
      bindingMap - the key-value pairs for binding name and binding object.
      Returns:
      the value returned from the execution of the script or ITool.VOID if return value is null
    • 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.