Module jamal.tools

Class ScriptingTools

java.lang.Object
javax0.jamal.tools.ScriptingTools

public class ScriptingTools
extends Object
Utility class containing static methods handling scripting. Scripting is handled through the JSR223 defined interface and no interpreter specific API is used, thus the scripts can be written in any scripting implementation that is on the classpath runtime.
  • Constructor Details

  • Method Details

    • populate

      public static void populate​(ScriptEngine engine, String key, String value)
      Populate the scripting engine with the key/value pair. This means that the global variable that has the name specified in the parameter key will have the value specified in the parameter value.

      If the string given in the parameter value can be interpreted as an integer then the population converts the value to Integer. If the string can be interpreted as a floating point number then it will be converted to a Double. If the value if "true" (any case, even mixed case) then the value will be Boolean true. Likewise if the value is "false" then it is converted to false.

      In any other cases the string is stored and assigned to the global variable.

      Parameters:
      engine - that stores the global variables for later execution
      key - the name of the global variable
      value - the value to be assigned to the global variable
    • populateJShell

      public static void populateJShell​(javax0.jamal.api.JShellEngine engine, String key, String value) throws javax0.jamal.api.BadSyntax
      Throws:
      javax0.jamal.api.BadSyntax
    • unescape

      public static String unescape​(String s)
    • resultToString

      public static String resultToString​(Object obj)
      Return the string representation of the object. If the object is a Double then the trailing .0 is chopped off. This is to help the macro evaluation and use in case the scripting implementation returns a Double even for integer values.
      Parameters:
      obj - to convert to string
      Returns:
      the converted string
    • getEngine

      public static ScriptEngine getEngine​(String scriptType) throws javax0.jamal.api.BadSyntax
      Get the scripting engine based on the name of the script type e.g.: JavaScript.
      Parameters:
      scriptType - the name of the scripting language
      Returns:
      the engine for the specified script type
      Throws:
      javax0.jamal.api.BadSyntax - when there is no scripting engine with the given name
    • evaluate

      public static Object evaluate​(ScriptEngine engine, String content) throws ScriptException
      Evaluate the content using the scripting engine.
      Parameters:
      engine - to be used to execute the evaluation
      content - the program code to be evaluated
      Returns:
      the result of the evaluation
      Throws:
      ScriptException - when there is an error executing the script