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 Summary
Constructors Constructor Description ScriptingTools()
-
Method Summary
Modifier and Type Method Description static Object
evaluate(ScriptEngine engine, String content)
Evaluate the content using the scripting engine.static ScriptEngine
getEngine(String scriptType)
Get the scripting engine based on the name of the script type e.g.:JavaScript
.static void
populate(ScriptEngine engine, String key, String value)
Populate the scripting engine with the key/value pair.static void
populateJShell(javax0.jamal.api.JShellEngine engine, String key, String value)
static String
resultToString(Object obj)
Return the string representation of the object.static String
unescape(String s)
-
Constructor Details
-
ScriptingTools
public ScriptingTools()
-
-
Method Details
-
populate
Populate the scripting engine with the key/value pair. This means that the global variable that has the name specified in the parameterkey
will have the value specified in the parametervalue
.If the string given in the parameter
value
can be interpreted as an integer then the population converts the value toInteger
. If the string can be interpreted as a floating point number then it will be converted to aDouble
. If the value if"true"
(any case, even mixed case) then the value will beBoolean
true
. Likewise if the value is"false"
then it is converted tofalse
.In any other cases the string is stored and assigned to the global variable.
- Parameters:
engine
- that stores the global variables for later executionkey
- the name of the global variablevalue
- 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
-
resultToString
Return the string representation of the object. If the object is aDouble
then the trailing.0
is chopped off. This is to help the macro evaluation and use in case the scripting implementation returns aDouble
even for integer values.- Parameters:
obj
- to convert to string- Returns:
- the converted string
-
getEngine
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
Evaluate the content using the scripting engine.- Parameters:
engine
- to be used to execute the evaluationcontent
- the program code to be evaluated- Returns:
- the result of the evaluation
- Throws:
ScriptException
- when there is an error executing the script
-