public class DefaultScriptInterpreter extends Object implements ScriptInterpreter
ScriptInterpreter
.
Credit to Jason Sachs for the multi-line evaluation (see his post on StackOverflow).
MORE_INPUT_PENDING
Constructor and Description |
---|
DefaultScriptInterpreter(PrefService prefs,
ScriptService scriptService,
ScriptLanguage language)
Deprecated.
Use
DefaultScriptInterpreter(ScriptLanguage) instead. |
DefaultScriptInterpreter(ScriptLanguage language)
Creates a new script interpreter for the given script language.
|
DefaultScriptInterpreter(ScriptLanguage language,
ScriptEngine engine)
Creates a new script interpreter for the given script language, using the
specified script engine.
|
Modifier and Type | Method and Description |
---|---|
Object |
eval(String command)
Evaluates a command.
|
Bindings |
getBindings()
|
ScriptEngine |
getEngine()
Returns the associated
ScriptEngine . |
ScriptLanguage |
getLanguage()
Returns the associated
ScriptLanguage . |
Object |
interpret(String line)
Interprets the given line of code, which might be part of a multi-line
statement.
|
boolean |
isExpectingMoreInput() |
boolean |
isReady() |
void |
readHistory()
Reads the persisted history of the current script interpreter.
|
void |
reset()
Clears the buffer of not-yet-evaluated lines of code, accumulated from
previous calls to
ScriptInterpreter.interpret(java.lang.String) . |
String |
walkHistory(String currentCommand,
boolean forward)
Obtains the next/previous command in the command history.
|
void |
writeHistory()
Persists the history of the current script interpreter.
|
@Deprecated public DefaultScriptInterpreter(PrefService prefs, ScriptService scriptService, ScriptLanguage language)
DefaultScriptInterpreter(ScriptLanguage)
instead.public DefaultScriptInterpreter(ScriptLanguage language)
language
- ScriptLanguage
of the interpreterpublic DefaultScriptInterpreter(ScriptLanguage language, ScriptEngine engine)
language
- ScriptLanguage
of the interpreterengine
- ScriptEngine
to use, or null for the specified
language's default enginepublic void readHistory()
ScriptInterpreter
readHistory
in interface ScriptInterpreter
public void writeHistory()
ScriptInterpreter
writeHistory
in interface ScriptInterpreter
public String walkHistory(String currentCommand, boolean forward)
ScriptInterpreter
walkHistory
in interface ScriptInterpreter
currentCommand
- the current command (will be stored in the history)forward
- if true, the next history entry is returned (more recent),
if false, the previous onepublic Object eval(String command) throws ScriptException
ScriptInterpreter
eval
in interface ScriptInterpreter
command
- the command to evaluateScriptException
public Object interpret(String line) throws ScriptException
This implementation from Jason Sachs uses the following strategy:
This helps avoid certain problems:
For further details, see SO #5584674.
interpret
in interface ScriptInterpreter
line
- line of code to interpretScriptInterpreter.MORE_INPUT_PENDING
if there is still
pending inputScriptException
- in case of an exceptionpublic void reset()
ScriptInterpreter
ScriptInterpreter.interpret(java.lang.String)
. In other words: start over with a new
(potentially multi-line) statement, discarding the current partial one.reset
in interface ScriptInterpreter
ScriptInterpreter.interpret(java.lang.String)
public ScriptLanguage getLanguage()
ScriptInterpreter
ScriptLanguage
.getLanguage
in interface ScriptInterpreter
public ScriptEngine getEngine()
ScriptInterpreter
ScriptEngine
.getEngine
in interface ScriptInterpreter
public Bindings getBindings()
ScriptInterpreter
getBindings
in interface ScriptInterpreter
public boolean isReady()
isReady
in interface ScriptInterpreter
ScriptInterpreter.interpret(String)
public boolean isExpectingMoreInput()
isExpectingMoreInput
in interface ScriptInterpreter
ScriptInterpreter.interpret(String)
Copyright © 2015–2022 SciJava. All rights reserved.