Class GroovyTool

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

@OpenApiAll public class GroovyTool extends Tool
Provide functions to execute Groovy script. The script will be executed during runtime with Velocity template.

Example:

    #import ('groovy', 'com.nomagic.reportwizard.tools.script.GroovyTool')
    #foreach ($class in $Class)
       $groovy.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:
Groovy file "Text.groovy"

    def toCap(word) {
       println 'toCap $word'
       return word[0].toUpperCase() + word[1..-1] 
    }
 
Template file
    $groovy.execute("Text.groovy")
    #foreach ($c in $Class)
       $groovy.eval('new Text().toCap('+$c.name+')');
    #end
    
If the $Class contains class name 'foo' and 'bar'. The output will be:
    Foo
    Bar

All implicit variables such as $Class or $sorter will be automatically imported into JS Script with the same variable name Example:

    def getSupplier()
    {
       def supplierList = [];
       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 in eval() function in RTF file. The curly braces are special characters use by RTF syntax. When you want to evaluate the Groovy function, you should put the function on separate groovy file.
    For example: this statement will not be executed in RTF template. The curly braces are prohibited.
     $groovy.eval("def foo() { return "Foo"; }")
The Groovy reference guide:
Since:
Mar 16, 2010
See Also:
  • Constructor Details

    • GroovyTool

      public GroovyTool()
      Create a GroovyTool.
  • Method Details

    • setShowError

      public void setShowError(boolean showError)
    • setLoaderMode

      public void setLoaderMode(int loaderMode)
    • eval

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

      For example:

          $groovy.eval("println 'Hello World!'; x = 123; return x * 10")
       
      The output will be:
      [on message view]
          Hello World!
       
      [on output report]
          1230
       
      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, String bindingName, Object bindingObject)
      Evaluate the script using the bindings argument.

      For example:

          #foreach ($class in $Class)
             $groovy.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"))
          $groovy.eval("'Name is ' + name1 + ' ' + name2;", $map)
       
      The output will be:
          Name is foo bar
       
      For example:
          $groovy.eval("'Name is ' + name1 + ' ' + name2;", {"name1":"foo","name2":"bar"})
       
      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 Groovy source. All characters of the reader are consumed.

      For example:

          $groovy.execute("script.groovy")
       
      Or
          $groovy.execute("c:/myfolder/script.groovy")
       
      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, String bindingName, Object bindingObject)
      Execute a file as Groovy 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 Groovy 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