Package jmri.script
Class JmriScriptEngineManager
java.lang.Object
jmri.script.JmriScriptEngineManager
- All Implemented Interfaces:
InstanceManagerAutoDefault
Provide a manager for
ScriptEngines. The following
methods are the only mechanisms for evaluating a Python script that respect
the jython.exec property in the python.properties file:
eval(java.io.File)eval(java.io.File, javax.script.Bindings)eval(java.io.File, javax.script.ScriptContext)eval(java.lang.String, javax.script.ScriptEngine)runScript(java.io.File)
getEngine*(java.lang.String).eval(...)
methods will not respect the jython.exec property, although all
methods will respect all other properties of that file.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionEvaluate a script contained in a file.Evaluate a script contained in a file given a set ofBindingsto add to the script's context.eval(File file, ScriptContext context) Evaluate a script contained in a file given a special context for the script.eval(String script, ScriptEngine engine) Evaluate a script using the given ScriptEngine.static StringfileForLanguage(String engineName, String languageName) Service routine to make engine-type strings to a human-readable promptstatic JmriScriptEngineManagerGet the default instance of a JmriScriptEngineManager.Get the defaultScriptContextfor allScriptEngines.Get a ScriptEngine by its name(s), mime type, or supported extensions.getEngineByExtension(String extension) Given a file extension, get the ScriptEngine registered to handle that extension.getEngineByMimeType(String mimeType) Given a mime type, get the ScriptEngine registered to handle that mime type.getEngineByName(String shortName) Given a short name, get the ScriptEngine registered by that name.getFactory(String name) Get a ScriptEngineFactory by its name(s), mime types, or supported extensions.getFactoryByExtension(String extension) Given a file extension, get the ScriptEngineFactory registered to handle that extension.getFactoryByMimeType(String mimeType) Given a mime type, get the ScriptEngineFactory registered to handle that mime type.getFactoryByName(String shortName) Given a short name, get the ScriptEngineFactory registered by that name.Get the Java ScriptEngineManager that this object contains.(package private) org.python.util.PythonInterpretervoidInitialize all ScriptEngines.voidThe Python ScriptEngine can be configured using a custom python.properties file and will run jmri_defaults.py if found in the user's configuration profile or settings directory.org.python.util.PythonInterpreterCreate a new PythonInterpreter with the default bindings.voidRun a script, suppressing common errors.
-
Field Details
-
JYTHON_DEFAULTS
- See Also:
-
JYTHON
- See Also:
-
dontWarnJavaScript
-
-
Constructor Details
-
JmriScriptEngineManager
public JmriScriptEngineManager()Create a JmriScriptEngineManager. In most cases, it is preferable to usegetDefault()to get existingScriptEngineinstances.
-
-
Method Details
-
getDefault
Get the default instance of a JmriScriptEngineManager. Using the default instance ensures that a script retains the context of the prior script.- Returns:
- the default JmriScriptEngineManager
-
getManager
Get the Java ScriptEngineManager that this object contains.- Returns:
- the ScriptEngineManager
-
getEngineByExtension
Given a file extension, get the ScriptEngine registered to handle that extension.- Parameters:
extension- a file extension- Returns:
- a ScriptEngine or null
- Throws:
ScriptException- if unable to get a matching ScriptEngine
-
getEngineByMimeType
Given a mime type, get the ScriptEngine registered to handle that mime type.- Parameters:
mimeType- a mimeType for a script- Returns:
- a ScriptEngine or null
- Throws:
ScriptException- if unable to get a matching ScriptEngine
-
getEngineByName
Given a short name, get the ScriptEngine registered by that name.- Parameters:
shortName- the short name for the ScriptEngine- Returns:
- a ScriptEngine or null
- Throws:
ScriptException- if unable to get a matching ScriptEngine
-
getEngine
Get a ScriptEngine by its name(s), mime type, or supported extensions.- Parameters:
name- the complete name, mime type, or extension for the ScriptEngine- Returns:
- a ScriptEngine or null if matching engine not found
-
eval
Evaluate a script using the given ScriptEngine.- Parameters:
script- The script.engine- The script engine.- Returns:
- The results of evaluating the script.
- Throws:
ScriptException- if there is an error in the script.
-
eval
Evaluate a script contained in a file. Uses the extension of the file to determine which ScriptEngine to use.- Parameters:
file- the script file to evaluate.- Returns:
- the results of the evaluation.
- Throws:
ScriptException- if there is an error evaluating the script.FileNotFoundException- if the script file cannot be found.IOException- if the script file cannot be read.
-
eval
Evaluate a script contained in a file given a set ofBindingsto add to the script's context. Uses the extension of the file to determine which ScriptEngine to use.- Parameters:
file- the script file to evaluate.bindings- script bindings to evaluate against.- Returns:
- the results of the evaluation.
- Throws:
ScriptException- if there is an error evaluating the script.FileNotFoundException- if the script file cannot be found.IOException- if the script file cannot be read.
-
eval
Evaluate a script contained in a file given a special context for the script. Uses the extension of the file to determine which ScriptEngine to use.- Parameters:
file- the script file to evaluate.context- script context to evaluate within.- Returns:
- the results of the evaluation.
- Throws:
ScriptException- if there is an error evaluating the script.FileNotFoundException- if the script file cannot be found.IOException- if the script file cannot be read.
-
runScript
Run a script, suppressing common errors. Note that the file needs to have a registered extension, or a NullPointerException will be thrown.Note: this will eventually be deprecated in favor of using
eval(File)and having callers handle exceptions.- Parameters:
file- the script to run.
-
initializeAllEngines
Initialize all ScriptEngines. This can be used to prevent the on-demand initialization of a ScriptEngine from causing a pause in JMRI. -
getDefaultContext
Get the defaultScriptContextfor allScriptEngines.- Returns:
- the default ScriptContext;
-
getFactoryByExtension
Given a file extension, get the ScriptEngineFactory registered to handle that extension.- Parameters:
extension- a file extension- Returns:
- a ScriptEngineFactory or null
- Throws:
ScriptException- if unable to get a matching ScriptEngineFactory
-
getFactoryByMimeType
Given a mime type, get the ScriptEngineFactory registered to handle that mime type.- Parameters:
mimeType- the script mimeType- Returns:
- a ScriptEngineFactory or null
- Throws:
ScriptException- if unable to get a matching ScriptEngineFactory
-
getFactoryByName
Given a short name, get the ScriptEngineFactory registered by that name.- Parameters:
shortName- the short name for the factory- Returns:
- a ScriptEngineFactory or null
- Throws:
ScriptException- if unable to get a matching ScriptEngineFactory
-
getFactory
Get a ScriptEngineFactory by its name(s), mime types, or supported extensions.- Parameters:
name- the complete name, mime type, or extension for a factory- Returns:
- a ScriptEngineFactory or null
-
initializePython
The Python ScriptEngine can be configured using a custom python.properties file and will run jmri_defaults.py if found in the user's configuration profile or settings directory. See python.properties in the JMRI installation directory for details of how to configure the Python ScriptEngine. -
newPythonInterpreter
Create a new PythonInterpreter with the default bindings.- Returns:
- a new interpreter
-
getPythonInterpreter
-
fileForLanguage
@Nonnull public static String fileForLanguage(@Nonnull String engineName, @Nonnull String languageName) Service routine to make engine-type strings to a human-readable prompt- Parameters:
engineName- Self-provided name of the enginelanguageName- Names of language supported by the engine- Returns:
- Human readable string, i.e. Jython Files
-