Package org.apache.velocity.script
Class VelocityScriptEngine
- java.lang.Object
-
- javax.script.AbstractScriptEngine
-
- org.apache.velocity.script.VelocityScriptEngine
-
- All Implemented Interfaces:
Compilable
,ScriptEngine
public class VelocityScriptEngine extends AbstractScriptEngine implements Compilable
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
VelocityScriptEngine.SingleResourceReader
-
Field Summary
Fields Modifier and Type Field Description static String
VELOCITY_PROPERTIES_KEY
Key used to provide this engine with the pathname of the Velocity properties file.-
Fields inherited from class javax.script.AbstractScriptEngine
context
-
Fields inherited from interface javax.script.ScriptEngine
ARGV, ENGINE, ENGINE_VERSION, FILENAME, LANGUAGE, LANGUAGE_VERSION, NAME
-
-
Constructor Summary
Constructors Constructor Description VelocityScriptEngine()
constructs a new standalone Velocity script engineVelocityScriptEngine(ScriptEngineFactory factory)
constructs a new Velocity script engine, linked to the given factory
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CompiledScript
compile(Reader script)
Compile a templateCompiledScript
compile(String script)
Compile a templateBindings
createBindings()
creates a new Bindings to be used with this scriptObject
eval(Reader reader, ScriptContext ctx)
Evaluate the given script.Object
eval(String str, ScriptContext ctx)
Evaluate the given script.ScriptEngineFactory
getFactory()
get the factory used to create this script engineprotected static String
getFilename(ScriptContext ctx)
protected static VelocityContext
getVelocityContext(ScriptContext ctx)
protected RuntimeInstance
getVelocityEngine()
get the internal Velocity RuntimeInstanceprotected static Properties
getVelocityProperties(ScriptContext ctx)
-
Methods inherited from class javax.script.AbstractScriptEngine
eval, eval, eval, eval, get, getBindings, getContext, getScriptContext, put, setBindings, setContext
-
-
-
-
Field Detail
-
VELOCITY_PROPERTIES_KEY
public static final String VELOCITY_PROPERTIES_KEY
Key used to provide this engine with the pathname of the Velocity properties file. This key is first searched in the ScriptContext attributes, then as a System property- See Also:
- Constant Field Values
-
-
Constructor Detail
-
VelocityScriptEngine
public VelocityScriptEngine(ScriptEngineFactory factory)
constructs a new Velocity script engine, linked to the given factory- Parameters:
factory
-
-
VelocityScriptEngine
public VelocityScriptEngine()
constructs a new standalone Velocity script engine
-
-
Method Detail
-
getVelocityEngine
protected RuntimeInstance getVelocityEngine()
get the internal Velocity RuntimeInstance- Returns:
- the internal Velocity RuntimeInstance
-
eval
public Object eval(String str, ScriptContext ctx) throws ScriptException
Evaluate the given script. If you wish to get a resulting string, call getContext().setWriter(new StringWriter()) then call toString() on the returned writer.- Specified by:
eval
in interfaceScriptEngine
- Parameters:
str
- script sourcectx
- script context- Returns:
- the script context writer (by default a PrintWriter towards System.out)
- Throws:
ScriptException
-
eval
public Object eval(Reader reader, ScriptContext ctx) throws ScriptException
Evaluate the given script. If you wish to get a resulting string, call getContext().setWriter(new StringWriter()) then call toString() on the returned writer.- Specified by:
eval
in interfaceScriptEngine
- Parameters:
reader
- script source readerctx
- script context- Returns:
- the script context writer (by default a PrintWriter towards System.out)
- Throws:
ScriptException
-
getFactory
public ScriptEngineFactory getFactory()
get the factory used to create this script engine- Specified by:
getFactory
in interfaceScriptEngine
- Returns:
- factory
-
createBindings
public Bindings createBindings()
creates a new Bindings to be used with this script- Specified by:
createBindings
in interfaceScriptEngine
- Returns:
- new bindings
-
getVelocityContext
protected static VelocityContext getVelocityContext(ScriptContext ctx)
-
getFilename
protected static String getFilename(ScriptContext ctx)
-
getVelocityProperties
protected static Properties getVelocityProperties(ScriptContext ctx)
-
compile
public CompiledScript compile(String script) throws ScriptException
Compile a template- Specified by:
compile
in interfaceCompilable
- Parameters:
script
- template source- Returns:
- compiled template
- Throws:
ScriptException
-
compile
public CompiledScript compile(Reader script) throws ScriptException
Compile a template- Specified by:
compile
in interfaceCompilable
- Parameters:
script
- template source- Returns:
- compiled template
- Throws:
ScriptException
-
-