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 classVelocityScriptEngine.SingleResourceReader
-
Field Summary
Fields Modifier and Type Field Description private javax.script.ScriptEngineFactoryfactorystatic java.lang.StringVELOCITY_PROPERTIES_KEYKey used to provide this engine with the pathname of the Velocity properties file.private org.apache.velocity.runtime.RuntimeInstancevelocityEngine
-
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.CompiledScriptcompile(java.io.Reader script)Compile a templatejavax.script.CompiledScriptcompile(java.lang.String script)Compile a templatejavax.script.BindingscreateBindings()creates a new Bindings to be used with this scriptjava.lang.Objecteval(java.io.Reader reader, javax.script.ScriptContext ctx)Evaluate the given script.java.lang.Objecteval(java.lang.String str, javax.script.ScriptContext ctx)Evaluate the given script.javax.script.ScriptEngineFactorygetFactory()get the factory used to create this script engineprotected static java.lang.StringgetFilename(javax.script.ScriptContext ctx)protected static org.apache.velocity.VelocityContextgetVelocityContext(javax.script.ScriptContext ctx)protected org.apache.velocity.runtime.RuntimeInstancegetVelocityEngine()get the internal Velocity RuntimeInstanceprotected static java.util.PropertiesgetVelocityProperties(javax.script.ScriptContext ctx)private voidinitVelocityEngine(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.ScriptExceptionEvaluate 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:
evalin 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.ScriptExceptionEvaluate 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:
evalin 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:
getFactoryin interfacejavax.script.ScriptEngine- Returns:
- factory
-
createBindings
public javax.script.Bindings createBindings()
creates a new Bindings to be used with this script- Specified by:
createBindingsin 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.ScriptExceptionCompile a template- Specified by:
compilein 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.ScriptExceptionCompile a template- Specified by:
compilein interfacejavax.script.Compilable- Parameters:
script- template source- Returns:
- compiled template
- Throws:
javax.script.ScriptException
-
-