- java.lang.Object
-
- javax0.jamal.tools.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.
-
-
-
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 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
-
resultToString
public static java.lang.String resultToString(java.lang.Object obj)
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
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 evaluationcontent
- the program code to be evaluated- Returns:
- the result of the evaluation
- Throws:
javax.script.ScriptException
- when there is an error executing the script
-
-