C# Class NVelocity.App.VelocityEngine

This class provides a separate new-able instance of the Velocity template engine. The alternative model for use is using the Velocity class which employs the singleton model. Please ensure that you call one of the init() variants. This is critical for proper behavior. Coming soon : Velocity will call the parameter-less init() at the first use of this class if the init() wasn't explicitly called. While this will ensure that Velocity functions, it almost certainly won't function in the way you intend, so please make sure to call init().
显示文件 Open project: rasmus-toftdahl-olesen/NVelocity Class Usage Examples

Public Methods

Method Description
AddProperty ( String key, Object value ) : void

Add a Velocity Runtime property.

ClearProperty ( String key ) : void

Clear a Velocity Runtime property.

Debug ( Object message ) : void

Log a debug message.

Error ( Object message ) : void

Log an error message.

Evaluate ( IContext context, TextWriter writer, String logTag, String inString ) : bool

renders the input string using the context into the output writer. To be used when a template is dynamically constructed, or want to use Velocity as a token replacer.

Evaluate ( IContext context, TextWriter writer, String logTag, TextReader reader ) : bool

Renders the input reader using the context into the output writer. To be used when a template is dynamically constructed, or want to use Velocity as a token replacer.

GetProperty ( String key ) : Object

Get a Velocity Runtime property.

GetTemplate ( String name ) : Template

Returns a Template from the Velocity resource management system.

GetTemplate ( String name, String encoding ) : Template

Returns a Template from the Velocity resource management system.

Info ( Object message ) : void

Log an info message.

Init ( ) : void

initialize the Velocity runtime engine, using the default properties of the Velocity distribution

Init ( ExtendedProperties p ) : void

initialize the Velocity runtime engine, using default properties plus the properties in the passed in java.util.Properties object

Init ( String propsFilename ) : void

initialize the Velocity runtime engine, using default properties plus the properties in the properties file passed in as the arg

InvokeVelocimacro ( String vmName, String logTag, String parameters, IContext context, TextWriter writer ) : bool

Invokes a currently registered Velocimacro with the parameters provided and places the rendered stream into the writer. Note : currently only accepts args to the VM if they are in the context.

MergeTemplate ( String templateName, String encoding, IContext context, TextWriter writer ) : bool

merges a template and puts the rendered stream into the writer

SetApplicationAttribute ( Object key, Object value ) : void

Set the an ApplicationAttribute, which is an Object set by the application which is accessible from any component of the system that gets a RuntimeServices. This allows communication between the application environment and custom pluggable components of the Velocity engine, such as loaders and loggers.

Note that there is no enforcement or rules for the key used - it is up to the application developer. However, to help make the intermixing of components possible, using the target Class name (e.g. com.foo.bar ) as the key might help avoid collision.

SetExtendedProperties ( ExtendedProperties value ) : void

Set an entire configuration at once. This is useful in cases where the parent application uses the ExtendedProperties class and the velocity configuration is a subset of the parent application's configuration.

SetProperty ( String key, Object value ) : void

Set a Velocity Runtime property.

TemplateExists ( String templateName ) : bool

Determines if a template is accessible via the currently configured resource loaders.

Note that the current implementation will not change the state of the system in any real way - so this cannot be used to pre-load the resource cache, as the previous implementation did as a side-effect.

The previous implementation exhibited extreme laziness and sloth, and the author has been flogged.

VelocityEngine ( ) : System

Init-less CTOR

VelocityEngine ( ExtendedProperties p ) : System

CTOR that invokes an init(String), initializing the engine using the Properties specified

VelocityEngine ( String propsFilename ) : System

CTOR that invokes an init(String), initializing the engine using the properties file specified

Warn ( Object message ) : void

Log a warning message.

Private Methods

Method Description
Evaluate ( IContext context, TextWriter writer, String logTag, Stream instream ) : bool
MergeTemplate ( String templateName, IContext context, TextWriter writer ) : bool

Method Details

AddProperty() public method

Add a Velocity Runtime property.
public AddProperty ( String key, Object value ) : void
key String
value Object
return void

ClearProperty() public method

Clear a Velocity Runtime property.
public ClearProperty ( String key ) : void
key String key of property to clear
return void

Debug() public method

Log a debug message.
public Debug ( Object message ) : void
message Object message to log
return void

Error() public method

Log an error message.
public Error ( Object message ) : void
message Object message to log
return void

Evaluate() public method

renders the input string using the context into the output writer. To be used when a template is dynamically constructed, or want to use Velocity as a token replacer.
public Evaluate ( IContext context, TextWriter writer, String logTag, String inString ) : bool
context IContext context to use in rendering input string
writer System.IO.TextWriter Writer in which to render the output
logTag String string to be used as the template name for log messages in case of error
inString String input string containing the VTL to be rendered
return bool

Evaluate() public method

Renders the input reader using the context into the output writer. To be used when a template is dynamically constructed, or want to use Velocity as a token replacer.
public Evaluate ( IContext context, TextWriter writer, String logTag, TextReader reader ) : bool
context IContext context to use in rendering input string
writer System.IO.TextWriter Writer in which to render the output
logTag String string to be used as the template name for log messages in case of error
reader TextReader Reader containing the VTL to be rendered
return bool

GetProperty() public method

Get a Velocity Runtime property.
public GetProperty ( String key ) : Object
key String property to retrieve
return Object

GetTemplate() public method

Returns a Template from the Velocity resource management system.
/// if template not found from any available source. /// /// if template cannot be parsed due /// to syntax (or other) error. /// /// if an error occurs in template initialization ///
public GetTemplate ( String name ) : Template
name String The file name of the desired template.
return Template

GetTemplate() public method

Returns a Template from the Velocity resource management system.
/// if template not found from any available source. /// /// if template cannot be parsed due /// to syntax (or other) error. /// /// if an error occurs in template initialization ///
public GetTemplate ( String name, String encoding ) : Template
name String The file name of the desired template.
encoding String The character encoding to use for the template.
return Template

Info() public method

Log an info message.
public Info ( Object message ) : void
message Object message to log
return void

Init() public method

initialize the Velocity runtime engine, using the default properties of the Velocity distribution
public Init ( ) : void
return void

Init() public method

initialize the Velocity runtime engine, using default properties plus the properties in the passed in java.util.Properties object
public Init ( ExtendedProperties p ) : void
p Commons.Collections.ExtendedProperties Properties object containing initialization properties
return void

Init() public method

initialize the Velocity runtime engine, using default properties plus the properties in the properties file passed in as the arg
public Init ( String propsFilename ) : void
propsFilename String file containing properties to use to initialize /// the Velocity runtime
return void

InvokeVelocimacro() public method

Invokes a currently registered Velocimacro with the parameters provided and places the rendered stream into the writer. Note : currently only accepts args to the VM if they are in the context.
public InvokeVelocimacro ( String vmName, String logTag, String parameters, IContext context, TextWriter writer ) : bool
vmName String name of Velocimacro to call
logTag String string to be used for template name in case of error
parameters String args used to invoke Velocimacro. In context key format : /// eg "foo","bar" (rather than "$foo","$bar") ///
context IContext Context object containing data/objects used for rendering.
writer System.IO.TextWriter Writer for output stream
return bool

MergeTemplate() public method

merges a template and puts the rendered stream into the writer
public MergeTemplate ( String templateName, String encoding, IContext context, TextWriter writer ) : bool
templateName String name of template to be used in merge
encoding String encoding used in template
context IContext filled context to be used in merge
writer System.IO.TextWriter writer to write template into
return bool

SetApplicationAttribute() public method

Set the an ApplicationAttribute, which is an Object set by the application which is accessible from any component of the system that gets a RuntimeServices. This allows communication between the application environment and custom pluggable components of the Velocity engine, such as loaders and loggers.

Note that there is no enforcement or rules for the key used - it is up to the application developer. However, to help make the intermixing of components possible, using the target Class name (e.g. com.foo.bar ) as the key might help avoid collision.

public SetApplicationAttribute ( Object key, Object value ) : void
key Object object 'name' under which the object is stored
value Object object to store under this key
return void

SetExtendedProperties() public method

Set an entire configuration at once. This is useful in cases where the parent application uses the ExtendedProperties class and the velocity configuration is a subset of the parent application's configuration.
public SetExtendedProperties ( ExtendedProperties value ) : void
value Commons.Collections.ExtendedProperties
return void

SetProperty() public method

Set a Velocity Runtime property.
public SetProperty ( String key, Object value ) : void
key String
value Object
return void

TemplateExists() public method

Determines if a template is accessible via the currently configured resource loaders.

Note that the current implementation will not change the state of the system in any real way - so this cannot be used to pre-load the resource cache, as the previous implementation did as a side-effect.

The previous implementation exhibited extreme laziness and sloth, and the author has been flogged.
public TemplateExists ( String templateName ) : bool
templateName String name of the template to search for ///
return bool

VelocityEngine() public method

Init-less CTOR
public VelocityEngine ( ) : System
return System

VelocityEngine() public method

CTOR that invokes an init(String), initializing the engine using the Properties specified
public VelocityEngine ( ExtendedProperties p ) : System
p Commons.Collections.ExtendedProperties name of properties to init with
return System

VelocityEngine() public method

CTOR that invokes an init(String), initializing the engine using the properties file specified
public VelocityEngine ( String propsFilename ) : System
propsFilename String name of properties file to init with
return System

Warn() public method

Log a warning message.
public Warn ( Object message ) : void
message Object message to log
return void