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
  • Field Details

    • 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:
    • factory

      private volatile ScriptEngineFactory factory
    • velocityEngine

      private volatile org.apache.velocity.runtime.RuntimeInstance velocityEngine
  • Constructor Details

    • 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 Details

    • getVelocityEngine

      protected org.apache.velocity.runtime.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 interface ScriptEngine
      Parameters:
      str - script source
      ctx - 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 interface ScriptEngine
      Parameters:
      reader - script source reader
      ctx - 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 interface ScriptEngine
      Returns:
      factory
    • createBindings

      public Bindings createBindings()
      creates a new Bindings to be used with this script
      Specified by:
      createBindings in interface ScriptEngine
      Returns:
      new bindings
    • initVelocityEngine

      private void initVelocityEngine(ScriptContext ctx)
    • getVelocityContext

      protected static org.apache.velocity.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 interface Compilable
      Parameters:
      script - template source
      Returns:
      compiled template
      Throws:
      ScriptException
    • compile

      public CompiledScript compile(Reader script) throws ScriptException
      Compile a template
      Specified by:
      compile in interface Compilable
      Parameters:
      script - template source
      Returns:
      compiled template
      Throws:
      ScriptException