Module jamal.tools

Class ScriptingTools


  • public class ScriptingTools
    extends java.lang.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 Summary

      Constructors 
      Constructor Description
      ScriptingTools()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object evaluate​(javax.script.ScriptEngine engine, java.lang.String content)
      Evaluate the content using the scripting engine.
      static javax.script.ScriptEngine getEngine​(java.lang.String scriptType)
      Get the scripting engine based on the name of the script type e.g.: JavaScript.
      static void populate​(javax.script.ScriptEngine engine, java.lang.String key, java.lang.String value)
      Populate the scripting engine with the key/value pair.
      static java.lang.String resultToString​(java.lang.Object obj)
      Return the string representation of the object.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ScriptingTools

        public ScriptingTools()
    • Method Detail

      • populate

        public static void populate​(javax.script.ScriptEngine engine,
                                    java.lang.String key,
                                    java.lang.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
      • resultToString

        public static java.lang.String resultToString​(java.lang.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 javax.script.ScriptEngine getEngine​(java.lang.String scriptType)
        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
      • evaluate

        public static java.lang.Object evaluate​(javax.script.ScriptEngine engine,
                                                java.lang.String content)
                                         throws javax.script.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:
        javax.script.ScriptException - when there is an error executing the script