Package org.apache.velocity.script
Class VelocityScriptEngine
- java.lang.Object
-
- javax.script.AbstractScriptEngine
-
- org.apache.velocity.script.VelocityScriptEngine
-
- All Implemented Interfaces:
javax.script.Compilable
,javax.script.ScriptEngine
public class VelocityScriptEngine extends javax.script.AbstractScriptEngine implements javax.script.Compilable
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
VelocityScriptEngine.SingleResourceReader
-
Field Summary
Fields Modifier and Type Field Description private javax.script.ScriptEngineFactory
factory
static java.lang.String
VELOCITY_PROPERTIES_KEY
Key used to provide this engine with the pathname of the Velocity properties file.private org.apache.velocity.runtime.RuntimeInstance
velocityEngine
-
Constructor Summary
Constructors Constructor Description VelocityScriptEngine()
constructs a new standalone Velocity script engineVelocityScriptEngine(javax.script.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 javax.script.CompiledScript
compile(java.io.Reader script)
Compile a templatejavax.script.CompiledScript
compile(java.lang.String script)
Compile a templatejavax.script.Bindings
createBindings()
creates a new Bindings to be used with this scriptjava.lang.Object
eval(java.io.Reader reader, javax.script.ScriptContext ctx)
Evaluate the given script.java.lang.Object
eval(java.lang.String str, javax.script.ScriptContext ctx)
Evaluate the given script.javax.script.ScriptEngineFactory
getFactory()
get the factory used to create this script engineprotected static java.lang.String
getFilename(javax.script.ScriptContext ctx)
protected static org.apache.velocity.VelocityContext
getVelocityContext(javax.script.ScriptContext ctx)
protected org.apache.velocity.runtime.RuntimeInstance
getVelocityEngine()
get the internal Velocity RuntimeInstanceprotected static java.util.Properties
getVelocityProperties(javax.script.ScriptContext ctx)
private void
initVelocityEngine(javax.script.ScriptContext ctx)
-
-
-
Field Detail
-
VELOCITY_PROPERTIES_KEY
public static final java.lang.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
-
factory
private volatile javax.script.ScriptEngineFactory factory
-
velocityEngine
private volatile org.apache.velocity.runtime.RuntimeInstance velocityEngine
-
-
Constructor Detail
-
VelocityScriptEngine
public VelocityScriptEngine(javax.script.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 org.apache.velocity.runtime.RuntimeInstance getVelocityEngine()
get the internal Velocity RuntimeInstance- Returns:
- the internal Velocity RuntimeInstance
-
eval
public java.lang.Object eval(java.lang.String str, javax.script.ScriptContext ctx) throws javax.script.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 interfacejavax.script.ScriptEngine
- Parameters:
str
- script sourcectx
- script context- Returns:
- the script context writer (by default a PrintWriter towards System.out)
- Throws:
javax.script.ScriptException
-
eval
public java.lang.Object eval(java.io.Reader reader, javax.script.ScriptContext ctx) throws javax.script.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 interfacejavax.script.ScriptEngine
- Parameters:
reader
- script source readerctx
- script context- Returns:
- the script context writer (by default a PrintWriter towards System.out)
- Throws:
javax.script.ScriptException
-
getFactory
public javax.script.ScriptEngineFactory getFactory()
get the factory used to create this script engine- Specified by:
getFactory
in interfacejavax.script.ScriptEngine
- Returns:
- factory
-
createBindings
public javax.script.Bindings createBindings()
creates a new Bindings to be used with this script- Specified by:
createBindings
in interfacejavax.script.ScriptEngine
- Returns:
- new bindings
-
initVelocityEngine
private void initVelocityEngine(javax.script.ScriptContext ctx)
-
getVelocityContext
protected static org.apache.velocity.VelocityContext getVelocityContext(javax.script.ScriptContext ctx)
-
getFilename
protected static java.lang.String getFilename(javax.script.ScriptContext ctx)
-
getVelocityProperties
protected static java.util.Properties getVelocityProperties(javax.script.ScriptContext ctx)
-
compile
public javax.script.CompiledScript compile(java.lang.String script) throws javax.script.ScriptException
Compile a template- Specified by:
compile
in interfacejavax.script.Compilable
- Parameters:
script
- template source- Returns:
- compiled template
- Throws:
javax.script.ScriptException
-
compile
public javax.script.CompiledScript compile(java.io.Reader script) throws javax.script.ScriptException
Compile a template- Specified by:
compile
in interfacejavax.script.Compilable
- Parameters:
script
- template source- Returns:
- compiled template
- Throws:
javax.script.ScriptException
-
-