Velocity API
APIs to evaluate content with Velocity |
Type | JAR |
Category | |
Developed by | |
Rating | |
License | GNU Lesser General Public License 2.1 |
Bundled With | XWiki Standard |
Compatibility | XWiki 3.0M3+ |
Table of contents
Description
This module offers APIs to evaluate content with Velocity.
The following concepts are exposed:
- VelocityManager: This is the main entry point for anyone who need to manipulate Velocity scripts
- VelocityTemplate: XWiki 15.8+ This is a helper to manipulate/cache/reuse a "compiled" version of a Velocity script
- VelocityEngine: This is the object on which you call evaluate(). In XWiki Standard, each skin has an associated ScriptEngine. This allows each Skin to provide its own set of global Velocity macros.
- VelocityConfiguration: Default Velocity configuration. Sets default configuration parameters and default tools (see below)
- VelocityContextFactory: Allows to create Velocity Context. Context are used when evaluating in order to expose some variable to the script content
- VelocityFactory: Allows to create several VelocityEngine based on a key you defined. XWiki 15.9+ Deprecated, used only VelocityManager.
- VelocityContextInitializer: A role to implement when you need to inject Velocity specific binding in the VelocityContext. In most cases, it's recommended to implement org.xwiki.script.ScriptContextInitializer so that the binding you inject is exposed to all scripting languages and not just Velocity.
Velocity Tools
For security reasons, we're not exposing some APIs to Velocity scripting (such as Reflection or Threading APIs). In addition, Velocity is a template language and doesn't have the full support a complete development language has (for example, you cannot instantiate new classes). Thus, we need to offer some Tools (i.e. API) that are meant to be used from Velocity to make it easy to create powerful scripts in Velocity code.
The following Velocity tools are made available:
- Collections Tool: $collectionstool (since 4.0M1 and )
- Comparison Date Tool (extends the standard Velocity Comparison Date Tool, which in turn extends the Date Tool): $datetool
- Escape Tool (extends the standard Velocity escape tool): $escapetool (since 2.7RC1)
- JSON Tool: $jsontool (since 4.0M2)
- Math Tool: $mathtool
- Number Tool: $numbertool
- Regex Tool: $regextool (since 2.0RC2)
- Sort Tool: $sorttool ( )
- String Tool: $stringtool (since 3.4M1). Also provides the following APIs (since 6.3M1).
- URL Tool: $urltool allows to parse URL's; currently only a parseQuery method is supported to parse the query string of a given URL (since 6.3M1)
- Exception Tool: $exceptiontool provides useful helpers to manipulate exceptions (get root cause, transform into a String, etc) (since 6.3M1)
- NIO Tool: $niotool allows to use some non-dangerous NIO2 API from Velocity (since 7.4M2). Useful along with the VFS API.
- Log Tool: $logtool allows logging from Velocity scripts (since 12.0)
- Object Tool: $objecttool offer object-related convenience APIs (such as checking for null and more) (since 12.1). Also provides the following APIs.
- Collection Tool (extends Velocity's CollectionTool): $collectiontool (since 12.1)
See more available variables at XWiki Scripting Reference Documentation
Velocity Macros
See available Velocity macros.
Velocity Uberspectors
The introspection behaviour in Velocity can be customized by implementing an uberspector. Velocity supports by default only one uberspector. To overcome this limitation we have implemented an uberspector that forwards all calls to a chain of uberspectors.
Method Arguments Uberspector
A chainable uberspector that tries to convert method arguments to formal parameter types when the passed arguments don't match the method signature. In other words, it looks for a method matching the passed arguments and if none is found then it tries the convert the arguments to match the available method signatures (the available methods with the same name and the same number of parameters but with different parameter types). E.g.:
// will forward to
obj.someMethod(SomeEnum.VALUE)
// if obj has a method with signature someMethod(SomeEnum) and not someMethod(String)
But this is not limited to enums. The conversion is done using the Properties Module which means you can create and register custom converters for data types specific to your application domain.
XWiki 13.7+
The strategies to select the right method when none exactly matches the passed parameters has been greatly improved:
- in case of different method name case, give priority to same case
- calculate a weight based on the compatibility between passed parameters and each method parameters:
- exact same type worth 10 per argument
- compatible type (the passed argument can be cast to the method argument type) worth 1 per argument
- this includes unboxing (for example Boolean to boolean)
- two numbers of different types (which is a very common use case) worth 0.5
- as last resort compare the toString() of each method to make sure the list is always stable across environments to reduce the "it worked on my machine" effect
Velocity Directives
XWiki provides a Velocity Directive to catch Exceptions from Velocity.
For example:
... velocity code here...
#end
...
#if ("$!exception" != '')
#displayException('some summary to display', $exception)
#else
...
#end
...
XWiki 8.3M2+, 7.4.5+, 8.1.2+
It's possible to name the Velocity variable in which the exception will be saved. For example:
#try("myexception")
$services.query.xwql("zzzzz").execute()
#end
#if ($myexception != '')
... do something...
#end
{{/velocity}}
Prerequisites & Installation Instructions
We recommend using the Extension Manager to install this extension (Make sure that the text "Installable with the Extension Manager" is displayed at the top right location on this page to know if this extension can be installed with the Extension Manager).
You can also use the manual method which involves dropping the JAR file and all its dependencies into the WEB-INF/lib folder and restarting XWiki.
Dependencies
Dependencies for this extension (org.xwiki.commons:xwiki-commons-velocity 16.9.0):
- org.xwiki.commons:xwiki-commons-component-api 16.9.0
- org.xwiki.commons:xwiki-commons-logging-api 16.9.0
- org.xwiki.commons:xwiki-commons-configuration-api 16.9.0
- org.xwiki.commons:xwiki-commons-script 16.9.0
- org.xwiki.commons:xwiki-commons-xml 16.9.0
- org.xwiki.commons:xwiki-commons-management 16.9.0
- org.xwiki.commons:xwiki-commons-properties 16.9.0
- org.apache.velocity:velocity-engine-core 2.3
- org.apache.velocity.tools:velocity-tools-generic 3.1
- org.apache.commons:commons-lang3 3.17.0
- org.apache.commons:commons-collections4 4.4
- org.apache.commons:commons-text 1.12.0
- commons-codec:commons-codec 1.17.1
- com.fasterxml.jackson.core:jackson-core 2.18.0
- com.fasterxml.jackson.core:jackson-databind 2.18.0
- net.sf.json-lib:json-lib:jdk15 2.4
- org.json:json 20240303
- org.apache.httpcomponents:httpclient 4.5.14