public class RuntimeInstance extends Object implements RuntimeConstants, RuntimeServices
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, which can make all needed setProperty() calls before calling 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. -----------------------------------------------------------------------
RuntimeConstants.SpaceGobbling
CHECK_EMPTY_OBJECTS, CONTEXT_AUTOREFERENCE_KEY, CONTEXT_SCOPE_CONTROL, CONVERSION_HANDLER_CLASS, CONVERSION_HANDLER_INSTANCE, CUSTOM_DIRECTIVES, DEFAULT_PARSER_CLASS, DEFAULT_RUNTIME_DIRECTIVES, DEFAULT_RUNTIME_LOG_NAME, DEFAULT_RUNTIME_PROPERTIES, DEFINE_DIRECTIVE_MAXDEPTH, DS_RESOURCE_LOADER_DATASOURCE, DS_RESOURCE_LOADER_KEY_COLUMN, DS_RESOURCE_LOADER_TABLE, DS_RESOURCE_LOADER_TEMPLATE_COLUMN, DS_RESOURCE_LOADER_TIMESTAMP_COLUMN, ENCODING_DEFAULT, ERRORMSG_END, ERRORMSG_START, EVENTHANDLER_INCLUDE, EVENTHANDLER_INVALIDREFERENCES, EVENTHANDLER_INVALIDREFERENCES_NULL, EVENTHANDLER_INVALIDREFERENCES_QUIET, EVENTHANDLER_INVALIDREFERENCES_TESTED, EVENTHANDLER_METHODEXCEPTION, EVENTHANDLER_REFERENCEINSERTION, FILE_RESOURCE_LOADER_CACHE, FILE_RESOURCE_LOADER_PATH, INPUT_ENCODING, INTERPOLATE_STRINGLITERALS, INTROSPECTOR_RESTRICT_CLASSES, INTROSPECTOR_RESTRICT_PACKAGES, MAX_NUMBER_LOOPS, NUMBER_OF_PARSERS, PARSE_DIRECTIVE_MAXDEPTH, PARSER_CLASS, PARSER_HYPHEN_ALLOWED, PARSER_POOL_CLASS, PARSER_POOL_SIZE, PROVIDE_SCOPE_CONTROL, RESOURCE_LOADER, RESOURCE_LOADER_CACHE, RESOURCE_LOADER_CHECK_INTERVAL, RESOURCE_LOADER_CLASS, RESOURCE_LOADER_IDENTIFIER, RESOURCE_LOADER_INSTANCE, RESOURCE_LOADER_PATHS, RESOURCE_LOADERS, RESOURCE_MANAGER_CACHE_CLASS, RESOURCE_MANAGER_CLASS, RESOURCE_MANAGER_DEFAULTCACHE_SIZE, RESOURCE_MANAGER_INSTANCE, RESOURCE_MANAGER_LOGWHENFOUND, RUNTIME_LOG_INSTANCE, RUNTIME_LOG_METHOD_CALL_LOG_INVALID, RUNTIME_LOG_NAME, RUNTIME_LOG_REFERENCE_LOG_INVALID, RUNTIME_LOG_TRACK_LOCATION, RUNTIME_REFERENCES_STRICT, RUNTIME_REFERENCES_STRICT_ESCAPE, RUNTIME_STRING_INTERNING, SKIP_INVALID_ITERATOR, SPACE_GOBBLING, STRICT_MATH, UBERSPECT_CLASSNAME, VM_ARGUMENTS_STRICT, VM_BODY_REFERENCE, VM_ENABLE_BC_MODE, VM_LIBRARY, VM_LIBRARY_AUTORELOAD, VM_LIBRARY_DEFAULT, VM_MAX_DEPTH, VM_MESSAGES_ON, VM_PERM_ALLOW_INLINE, VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL, VM_PERM_INLINE_LOCAL
OLD_CHECK_EMPTY_OBJECTS, OLD_CONTEXT_AUTOREFERENCE_KEY, OLD_CONVERSION_HANDLER_CLASS, OLD_CUSTOM_DIRECTIVES, OLD_DEFINE_DIRECTIVE_MAXDEPTH, OLD_DS_RESOURCE_LOADER_DATASOURCE, OLD_DS_RESOURCE_LOADER_KEY_COLUMN, OLD_DS_RESOURCE_LOADER_TEMPLATE_COLUMN, OLD_DS_RESOURCE_LOADER_TIMESTAMP_COLUMN, OLD_ERRORMSG_END, OLD_ERRORMSG_START, OLD_EVENTHANDLER_INCLUDE, OLD_EVENTHANDLER_INVALIDREFERENCES, OLD_EVENTHANDLER_METHODEXCEPTION, OLD_EVENTHANDLER_REFERENCEINSERTION, OLD_FILE_RESOURCE_LOADER_CACHE, OLD_FILE_RESOURCE_LOADER_PATH, OLD_INPUT_ENCODING, OLD_INTERPOLATE_STRINGLITERALS, OLD_MAX_NUMBER_LOOPS, OLD_PARSE_DIRECTIVE_MAXDEPTH, OLD_RESOURCE_LOADER_CHECK_INTERVAL, OLD_RESOURCE_LOADERS, OLD_RESOURCE_MANAGER_DEFAULTCACHE_SIZE, OLD_RESOURCE_MANAGER_LOGWHENFOUND, OLD_RUNTIME_LOG_REFERENCE_LOG_INVALID, OLD_RUNTIME_REFERENCES_STRICT, OLD_RUNTIME_REFERENCES_STRICT_ESCAPE, OLD_SKIP_INVALID_ITERATOR, OLD_SPACE_GOBBLING, OLD_STRICT_MATH, OLD_UBERSPECT_CLASSNAME, OLD_VM_BODY_REFERENCE, OLD_VM_ENABLE_BC_MODE, OLD_VM_LIBRARY, OLD_VM_LIBRARY_DEFAULT, OLD_VM_MAX_DEPTH, OLD_VM_PERM_ALLOW_INLINE, OLD_VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL, OLD_VM_PERM_INLINE_LOCAL
Constructor and Description |
---|
RuntimeInstance()
Creates a new RuntimeInstance object.
|
Modifier and Type | Method and Description |
---|---|
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,
List<Macro.MacroArg> macroArgs,
Template definingTemplate)
Adds a new Velocimacro.
|
void |
clearProperty(String key)
Clear the values pertaining to a particular
property.
|
Parser |
createNewParser()
Returns a JavaCC generated Parser.
|
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.
|
ExtProperties |
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.
|
String |
getLoaderNameForResource(String resourceName)
Determines if a template exists and returns name of the loader that
provides it.
|
org.slf4j.Logger |
getLog()
Returns the configured logger.
|
org.slf4j.Logger |
getLog(String childNamespace)
Get a logger for the specified child namespace.
|
LogContext |
getLogContext()
Get the LogContext object used to tack locations in templates.
|
ParserConfiguration |
getParserConfiguration()
Get the replacement characters configured for this runtime service's parser
|
Object |
getProperty(String key)
Allows an external caller to get a property.
|
RuntimeConstants.SpaceGobbling |
getSpaceGobbling()
get space gobbling mode
|
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,
Template renderingTemplate,
Template template)
Returns the appropriate VelocimacroProxy object if vmName
is a valid current Velocimacro.
|
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 a
properties file path.
|
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 |
isHyphenAllowedInIdentifiers()
get whether hyphens are allowed in identifiers
|
boolean |
isInitialized()
Returns true if the RuntimeInstance has been successfully initialized.
|
boolean |
isScopeControlEnabled(String scopeName)
Get whether to provide a scope control object for this scope
|
boolean |
isVelocimacro(String vmName,
Template template)
Checks to see if a VM exists
|
void |
loadDirective(String directiveClass)
instantiates and loads the directive with some basic checks
|
SimpleNode |
parse(Reader reader,
Template template)
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. |
void |
reset()
Resets the instance, so Velocity can be re-initialized again.
|
Object |
setApplicationAttribute(Object key,
Object o)
Sets the application attribute for the given key
|
void |
setConfiguration(ExtProperties configuration)
Allow an external system to set an ExtProperties
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.
|
boolean |
useStringInterning()
Whether to use string interning
|
public void init()
init
in interface RuntimeServices
public void reset()
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)
fileName
- public void setProperties(Properties props)
props
- public void setConfiguration(ExtProperties 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 returnpublic 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 directiveDirective
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(Reader reader, Template template) throws ParseException
parse
in interface RuntimeServices
reader
- Reader retrieved by a resource loadertemplate
- template being parsedParseException
- 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 renderedParseErrorException
- 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 renderedParseErrorException
- 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 renderedParseErrorException
- 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 streampublic 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 resource.default_encoding
property. The default is UTF-8.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 managergetTemplate
in interface RuntimeServices
name
- The name of the desired template.encoding
- Character encoding of the templateResourceNotFoundException
- 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 getResourceNotFoundException
- 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 useResourceNotFoundException
- 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 resourcepublic org.slf4j.Logger getLog()
getLog
in interface RuntimeServices
public org.slf4j.Logger getLog(String childNamespace)
getLog
in interface RuntimeServices
childNamespace
- public LogContext getLogContext()
getLogContext
in interface RuntimeServices
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, Template renderingTemplate, Template template)
getVelocimacro
in interface RuntimeServices
vmName
- Name of velocimacro requestedrenderingTemplate
- 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.template
- Template which acts as the host for the macropublic boolean addVelocimacro(String name, Node macro, List<Macro.MacroArg> macroArgs, Template definingTemplate)
addVelocimacro
in interface RuntimeServices
name
- Name of velocimacromacro
- root AST node of the parsed macromacroArgs
- Array of macro arguments, containing the
#macro() arguments and default values. the 0th is the name.definingTemplate
- Template containing the source of the macropublic boolean isVelocimacro(String vmName, Template template)
isVelocimacro
in interface RuntimeServices
vmName
- Name of the Velocimacro.template
- Template on which to look for the Macro.public String getString(String key)
getString
in interface RuntimeServices
key
- property keypublic int getInt(String key)
getInt
in interface RuntimeServices
key
- Property keypublic 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 ExtProperties getConfiguration()
getConfiguration
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
public boolean useStringInterning()
useStringInterning
in interface RuntimeServices
public RuntimeConstants.SpaceGobbling getSpaceGobbling()
getSpaceGobbling
in interface RuntimeServices
public boolean isHyphenAllowedInIdentifiers()
isHyphenAllowedInIdentifiers
in interface RuntimeServices
public boolean isScopeControlEnabled(String scopeName)
isScopeControlEnabled
in interface RuntimeServices
scopeName
- public ParserConfiguration getParserConfiguration()
RuntimeServices
getParserConfiguration
in interface RuntimeServices
Copyright © 2000–2020 The Apache Software Foundation. All rights reserved.