|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.velocity.runtime.RuntimeInstance
public class RuntimeInstance
This is the Runtime system for Velocity. It is the single access point for all functionality in Velocity. It adheres to the mediator pattern and is the only structure that developers need to be familiar with in order to get Velocity to perform. The Runtime will also cooperate with external systems like Turbine. Runtime properties can set and then the Runtime is initialized. Turbine, for example, knows where the templates are to be loaded from, and where the Velocity log file should be placed. So in the case of Velocity cooperating with Turbine the code might look something like the following:
ri.setProperty(Runtime.FILE_RESOURCE_LOADER_PATH, templatePath);
ri.setProperty(Runtime.RUNTIME_LOG, pathToVelocityLog);
ri.init();
----------------------------------------------------------------------- N O T E S O N R U N T I M E I N I T I A L I Z A T I O N ----------------------------------------------------------------------- init() If init() is called by itself the RuntimeInstance will initialize with a set of default values. ----------------------------------------------------------------------- init(String/Properties) In this case the default velocity properties are layed down first to provide a solid base, then any properties provided in the given properties object will override the corresponding default property. -----------------------------------------------------------------------
Field Summary |
---|
Constructor Summary | |
---|---|
RuntimeInstance()
Creates a new RuntimeInstance object. |
Method Summary | |
---|---|
void |
addDirective(Directive directive)
Programatically add a directive. |
void |
addProperty(String key,
Object value)
Add a property to the configuration. |
boolean |
addVelocimacro(String name,
Node macro,
String[] argArray,
String sourceTemplate)
Adds a new Velocimacro. |
boolean |
addVelocimacro(String name,
String macro,
String[] argArray,
String sourceTemplate)
Deprecated. Use addVelocimacro(String, Node, String[], String) instead |
void |
clearProperty(String key)
Clear the values pertaining to a particular property. |
Parser |
createNewParser()
Returns a JavaCC generated Parser. |
void |
debug(Object message)
Deprecated. Use getLog() and call debug() on it. |
boolean |
dumpVMNamespace(String namespace)
tells the vmFactory to dump the specified namespace. |
void |
error(Object message)
Deprecated. Use getLog() and call error() on it. |
boolean |
evaluate(Context context,
Writer writer,
String logTag,
Reader reader)
Renders the input reader using the context into the output writer. |
boolean |
evaluate(Context context,
Writer out,
String logTag,
String instring)
Renders the input string using the context into the output writer. |
Object |
getApplicationAttribute(Object key)
Gets the application attribute for the given key |
EventCartridge |
getApplicationEventCartridge()
Returns the event handlers for the application. |
boolean |
getBoolean(String key,
boolean def)
Boolean property accessor method to hide the configuration implementation. |
ExtendedProperties |
getConfiguration()
Return the velocity runtime configuration object. |
ContentResource |
getContent(String name)
Returns a static content resource from the resource manager. |
ContentResource |
getContent(String name,
String encoding)
Returns a static content resource from the resource manager. |
Directive |
getDirective(String name)
Retrieve a previously instantiated directive. |
int |
getInt(String key)
Int property accessor method to hide the configuration implementation. |
int |
getInt(String key,
int defaultValue)
Int property accessor method to hide the configuration implementation. |
Introspector |
getIntrospector()
Return the Introspector for this instance |
String |
getLoaderNameForResource(String resourceName)
Determines if a template exists and returns name of the loader that provides it. |
Log |
getLog()
Returns a convenient Log instance that wraps the current LogChute. |
Object |
getProperty(String key)
Allows an external caller to get a property. |
String |
getString(String key)
String property accessor method to hide the configuration implementation |
String |
getString(String key,
String defaultValue)
String property accessor method with default to hide the configuration implementation. |
Template |
getTemplate(String name)
Returns a Template from the resource manager. |
Template |
getTemplate(String name,
String encoding)
Returns a Template from the resource manager |
Uberspect |
getUberspect()
Returns the Uberspect object for this Instance. |
Directive |
getVelocimacro(String vmName,
String templateName)
Returns the appropriate VelocimacroProxy object if vmName is a valid current Velocimacro. |
Directive |
getVelocimacro(String vmName,
String templateName,
String renderingTemplate)
Returns the appropriate VelocimacroProxy object if vmName is a valid current Velocimacro. |
void |
info(Object message)
Deprecated. Use getLog() and call info() on it. |
void |
init()
This is the primary initialization method in the Velocity Runtime. |
void |
init(Properties p)
Initialize the Velocity Runtime with a Properties object. |
void |
init(String configurationFile)
Initialize the Velocity Runtime with the name of ExtendedProperties object. |
boolean |
invokeVelocimacro(String vmName,
String logTag,
String[] params,
Context context,
Writer writer)
Invokes a currently registered Velocimacro with the params provided and places the rendered stream into the writer. |
boolean |
isInitialized()
Returns true if the RuntimeInstance has been successfully initialized. |
boolean |
isVelocimacro(String vmName,
String templateName)
Checks to see if a VM exists |
void |
loadDirective(String directiveClass)
instantiates and loads the directive with some basic checks |
SimpleNode |
parse(Reader reader,
String templateName)
Parse the input and return the root of AST node structure. |
SimpleNode |
parse(Reader reader,
String templateName,
boolean dumpNamespace)
Parse the input and return the root of the AST node structure. |
SimpleNode |
parse(String string,
String templateName)
Parse the input and return the root of AST node structure. |
void |
removeDirective(String name)
Remove a directive. |
boolean |
render(Context context,
Writer writer,
String logTag,
SimpleNode nodeTree)
Initializes and renders the AST SimpleNode using the context
into the output writer. |
Object |
setApplicationAttribute(Object key,
Object o)
Sets the application attribute for the given key |
void |
setConfiguration(ExtendedProperties configuration)
Allow an external system to set an ExtendedProperties object to use. |
void |
setProperties(Properties props)
Add all the properties in props to the RuntimeInstance properties |
void |
setProperties(String fileName)
Add all properties contained in the file fileName to the RuntimeInstance properties |
void |
setProperty(String key,
Object value)
Allows an external system to set a property in the Velocity Runtime. |
void |
warn(Object message)
Deprecated. Use getLog() and call warn() on it. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public RuntimeInstance()
Method Detail |
---|
public void init()
init
in interface RuntimeServices
public boolean isInitialized()
isInitialized
in interface RuntimeServices
public void setProperty(String key, Object value)
setProperty
in interface RuntimeServices
key
- property keyvalue
- property valuepublic void setProperties(String fileName)
public void setProperties(Properties props)
public void setConfiguration(ExtendedProperties configuration)
setConfiguration
in interface RuntimeServices
configuration
- public void addProperty(String key, Object value)
addProperty
in interface RuntimeServices
key
- value
- public void clearProperty(String key)
clearProperty
in interface RuntimeServices
key
- of property to clearpublic Object getProperty(String key)
getProperty
in interface RuntimeServices
key
- property to return
public void init(Properties p)
init
in interface RuntimeServices
p
- Velocity properties for initializationpublic void init(String configurationFile)
init
in interface RuntimeServices
configurationFile
- public void addDirective(Directive directive)
directive
- public Directive getDirective(String name)
getDirective
in interface RuntimeServices
name
- name of the directive
Directive
for that namepublic void removeDirective(String name)
name
- name of the directive.public void loadDirective(String directiveClass)
directiveClass
- classname of directive to loadpublic Parser createNewParser()
createNewParser
in interface RuntimeServices
public SimpleNode parse(String string, String templateName) throws ParseException
parse
in interface RuntimeServices
string
- String to be parsedtemplateName
- name of the template being parsed
ParseException
- When the string could not be parsed as a template.public SimpleNode parse(Reader reader, String templateName) throws ParseException
parse
in interface RuntimeServices
reader
- Reader retrieved by a resource loadertemplateName
- name of the template being parsed
ParseException
- When the template could not be parsed.public SimpleNode parse(Reader reader, String templateName, boolean dumpNamespace) throws ParseException
parse
in interface RuntimeServices
reader
- Reader retrieved by a resource loadertemplateName
- name of the template being parseddumpNamespace
- flag to dump the Velocimacro namespace for this template
ParseException
- When the template could not be parsed.public boolean evaluate(Context context, Writer out, String logTag, String instring)
evaluate
in interface RuntimeServices
context
- context to use in rendering input stringout
- Writer in which to render the outputlogTag
- string to be used as the template name for log
messages in case of errorinstring
- input string containing the VTL to be rendered
ParseErrorException
- The template could not be parsed.
MethodInvocationException
- A method on a context object could not be invoked.
ResourceNotFoundException
- A referenced resource could not be loaded.public boolean evaluate(Context context, Writer writer, String logTag, Reader reader)
evaluate
in interface RuntimeServices
context
- context to use in rendering input stringwriter
- Writer in which to render the outputlogTag
- string to be used as the template name for log messages
in case of errorreader
- Reader containing the VTL to be rendered
ParseErrorException
- The template could not be parsed.
MethodInvocationException
- A method on a context object could not be invoked.
ResourceNotFoundException
- A referenced resource could not be loaded.public boolean render(Context context, Writer writer, String logTag, SimpleNode nodeTree)
SimpleNode
using the context
into the output writer.
context
- context to use in rendering input stringwriter
- Writer in which to render the outputlogTag
- string to be used as the template name for log messages
in case of errornodeTree
- SimpleNode which is the root of the AST to be rendered
ParseErrorException
- The template could not be parsed.
MethodInvocationException
- A method on a context object could not be invoked.
ResourceNotFoundException
- A referenced resource could not be loaded.public boolean invokeVelocimacro(String vmName, String logTag, String[] params, Context context, Writer writer)
invokeVelocimacro
in interface RuntimeServices
vmName
- name of Velocimacro to calllogTag
- string to be used for template name in case of error. if null,
the vmName will be usedparams
- keys for args used to invoke Velocimacro, in java format
rather than VTL (eg "foo" or "bar" rather than "$foo" or "$bar")context
- Context object containing data/objects used for rendering.writer
- Writer for output stream
public Template getTemplate(String name) throws ResourceNotFoundException, ParseErrorException
Template
from the resource manager.
This method assumes that the character encoding of the
template is set by the input.encoding
property. The default is "ISO-8859-1"
getTemplate
in interface RuntimeServices
name
- The file name of the desired template.
ResourceNotFoundException
- if template not found
from any available source.
ParseErrorException
- if template cannot be parsed due
to syntax (or other) error.public Template getTemplate(String name, String encoding) throws ResourceNotFoundException, ParseErrorException
Template
from the resource manager
getTemplate
in interface RuntimeServices
name
- The name of the desired template.encoding
- Character encoding of the template
ResourceNotFoundException
- if template not found
from any available source.
ParseErrorException
- if template cannot be parsed due
to syntax (or other) error.public ContentResource getContent(String name) throws ResourceNotFoundException, ParseErrorException
getContent
in interface RuntimeServices
name
- Name of content resource to get
ResourceNotFoundException
- if template not found
from any available source.
ParseErrorException
- When the template could not be parsed.public ContentResource getContent(String name, String encoding) throws ResourceNotFoundException, ParseErrorException
getContent
in interface RuntimeServices
name
- Name of content resource to getencoding
- Character encoding to use
ResourceNotFoundException
- if template not found
from any available source.
ParseErrorException
- When the template could not be parsed.public String getLoaderNameForResource(String resourceName)
getLoaderNameForResource
in interface RuntimeServices
resourceName
- Name of template or content resource
public Log getLog()
getLog
in interface RuntimeServices
public void warn(Object message)
warn
in interface RuntimeLogger
message
- The message to log.Log.warn(Object)
public void info(Object message)
info
in interface RuntimeLogger
message
- The message to log.Log.info(Object)
public void error(Object message)
error
in interface RuntimeLogger
message
- The message to log.Log.error(Object)
public void debug(Object message)
debug
in interface RuntimeLogger
message
- The message to log.Log.debug(Object)
public String getString(String key, String defaultValue)
getString
in interface RuntimeServices
key
- property keydefaultValue
- default value to return if key not
found in resource manager.
public Directive getVelocimacro(String vmName, String templateName)
getVelocimacro
in interface RuntimeServices
vmName
- Name of velocimacro requestedtemplateName
- Name of the template that contains the velocimacro.
public Directive getVelocimacro(String vmName, String templateName, String renderingTemplate)
getVelocimacro
in interface RuntimeServices
vmName
- Name of velocimacro requestedtemplateName
- Name of the namespace.renderingTemplate
- Name of the template we are currently rendering. This
information is needed when VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL setting is true
and template contains a macro with the same name as the global macro library.
public boolean addVelocimacro(String name, String macro, String[] argArray, String sourceTemplate)
addVelocimacro
in interface RuntimeServices
name
- Name of velocimacromacro
- String form of macro bodyargArray
- Array of strings, containing the
#macro() arguments. the 0th is the name.sourceTemplate
- Name of the template that contains the velocimacro.
public boolean addVelocimacro(String name, Node macro, String[] argArray, String sourceTemplate)
addVelocimacro
in interface RuntimeServices
name
- Name of velocimacromacro
- root AST node of the parsed macroargArray
- Array of strings, containing the
#macro() arguments. the 0th is the name.sourceTemplate
-
public boolean isVelocimacro(String vmName, String templateName)
isVelocimacro
in interface RuntimeServices
vmName
- Name of the Velocimacro.templateName
- Template on which to look for the Macro.
public boolean dumpVMNamespace(String namespace)
dumpVMNamespace
in interface RuntimeServices
namespace
- Namespace to dump.
public String getString(String key)
getString
in interface RuntimeServices
key
- property key
public int getInt(String key)
getInt
in interface RuntimeServices
key
- Property key
public int getInt(String key, int defaultValue)
getInt
in interface RuntimeServices
key
- property keydefaultValue
- The default value.
public boolean getBoolean(String key, boolean def)
getBoolean
in interface RuntimeServices
key
- property keydef
- The default value if property not found.
public ExtendedProperties getConfiguration()
getConfiguration
in interface RuntimeServices
public Introspector getIntrospector()
getIntrospector
in interface RuntimeServices
public EventCartridge getApplicationEventCartridge()
getApplicationEventCartridge
in interface RuntimeServices
public Object getApplicationAttribute(Object key)
getApplicationAttribute
in interface RuntimeServices
key
-
public Object setApplicationAttribute(Object key, Object o)
setApplicationAttribute
in interface RuntimeServices
key
- o
- The new application attribute.
public Uberspect getUberspect()
getUberspect
in interface RuntimeServices
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |