GenericTools is the set of classes that provide basic infrastructure for using tools in standard Java SE Velocity projects, as well as a set of tools for use in generic Velocity templates. These tools have no Java EE dependencies and are often safe to use as "singletons". Some of them are not thread-safe to allow both a no-arg constructor and configurability, but the state-changing methods are declared protected with the exception of configure(Map) which is--by default--rendered useless after being used just once. If you require strict thread-safety, be cautious in using any configurable tools and consider restricting the abilities of template authors to prevent circumvention of the lockdown placed on configuration methods.
Initialization with only standard generic tools, using the default configuration file:
/* Create tools initialization properties */ Map<String, Object> properties = new HashMap<String, Object>(); /* RenderTool needs to know the VelocityEngine. Setting the engine will also set the logger. */ properties.put("engine", engine); /* If the render tool is not to be used, you can instead just set the logger if needed as follow: * * properties.put("log", logger); * * Otherwise, the engine logger will be used. * */ /* Create the manager. autoConfigure and includeDefaults are both booleans which default to true. */ ToolManager manager = new ToolManager(autoConfigure, includeDefaults);
Initialization using a provided configuration file (to customize tools or tune standard tools configuration):
/* Create tools initialization properties */ Map<String, Object> properties = new HashMap<String, Object>(); /* RenderTool needs to know the VelocityEngine. Setting the engine will also set the logger. */ properties.put("engine", engine); /* If the render tool is not to be used, you can instead just set the logger if needed as follow: * * properties.put("log", logger); * * Otherwise, the engine logger will be used. * */ /* Create the manager. includeDefaults is a boolean which defaults to true. */ ToolManager manager = new ToolManager(false, includeDefaults); /* Configure the manager * toolsConfiguration is a filesystem- or class- path towards your tools configuration, * which can be a .xml, a .properties, or a .class of a configuration factory which * has a getConfiguration() method returning a FactoryConfiguration object."); */ manager.configure(toolsConfiguration);
You can then create Velocity contexts with:
:::java Context context = manager.createContext();
The dependencies required for GenericTools vary somewhat depending on which tools you use, whether you will rely on core tool management infrastructure, and if so, how you choose to configure your toolbox. More details can be found on the dependencies chart.
- AlternatorTool - For creating Alternators to easily alternate over a set of values.
- ClassTool - For simplifying reflective lookup of information about Classes and their fields, methods and constructors.
- ContextTool - For convenient access to Context data and meta-data.
- ConversionTool - For converting String values to richer object types.
- DateTool & ComparisonDateTool - For manipulating, formatting, and comparing dates.
- DisplayTool - For controlling display of references (e.g. truncating values, "pretty printing" lists, and displaying alternates when a reference is null).
- FieldTool - For (easy) access to static fields in a class, such as string constants.
- LoopTool - A convenience tool to use with #foreach loops. It wraps a list with a custom iterator to provide greater control, allowing loops to end early, skip ahead and more.
- LinkTool - For creating and manipulating URIs and URLs. The API for this tool is designed to closely resemble that of the VelocityView tool of the same name.
- ListTool - For working with arrays and lists, treats both transparently the same.
- MathTool - For performing math functions.
- NumberTool - For formatting and converting numbers.
- RenderTool - To evaluate and render arbitrary strings of VTL, including recursive rendering.
- ResourceTool - For simplified access to ResourceBundles for internationalization or other dynamic content needs.
- SortTool - Used to sort collections (or arrays, iterators, etc) on any arbitary set of properties exposed by the objects contained within the collection.
- XmlTool - For reading/navigating XML files. This uses dom4j under the covers and provides complete XPath support.