Before integrating RTFTemplate, you must understand generation process of RTFtemplate.
Here the two "well practices" to integrate RTFTemplate :
The 3 steps of generation process of RTFtemplate can be configured ou re-implemented. The 3 steps are represnted by interfaces :
RTFTemplate can be configurated with Spring descriptor.
RTFTemplate can be integrated into an application type of rich client or type of WEB.
Here basic JAVA code to use RTFTemplate :
String rtfSource = "usecases/models/jakartavelocityproject/jakarta-velocity-model.rtf"; String rtfTarget = "jakarta-velocity-model.rtf.rtf"; // 1. Get default RTFtemplateBuilder RTFTemplateBuilder builder = RTFTemplateBuilder.newRTFTemplateBuilder(); // 2. Get RTFtemplate with default Implementation of template engine (Velocity) RTFTemplate rtfTemplate = builder.newRTFTemplate(); // 3. Set the RTF model source rtfTemplate.setTemplate(new File(rtfSource)); // 4. Put the context rtfTemplate.put("project", new Project("Jakarta Velocity project")); ... // 5. Merge the RTF source model and the context rtfTemplate.merge(rtfTarget);
Here dscription of this code :
rtfTarget file is the result of process generation of RTFTemplate(merge RTF source model with JAVA object context).
If you use RTFTemplate into WEB application, click here, otherwise click here.
If you want optimize RTFTemplate into WEB application, click here, otherwise click here.
RTFTemplate is modular, each steps of the process generation can be implemented with class from RTFTemplate or with your implementation.
IRTFDocumentParser interface must parse a RTF model source to build a RTFDocument. Default implementation of the RTFTemplate is the class net.sourceforge.rtf.handler.RTFDocumentHandler.
IRTFDocumentTransformer interface must transform the RTFDocument object in order to that the template engine use it. Some macro switch selected template engine are added (#foreach for Velocity and #list for Freemarker). RTFTemplate implement two type of transformer :
RTFTemplate use by default the Velocity implementation.
ITemplateEngine interface must merge data comming from the JAVA context with the content of the transformed RTFDocument. Merege is done with the template engine. RTTemplate implements 2 template engine :
RTFTemplate use by default the Velocity implementation.
Each step of the generation process can be selected or re implemented. You can configure RTFTemplate with Spring (IoC mecanism) or with JAVA code.
RTFTemplate use by default the Spring file rtftemplate-config.xml : To select the Freemarker implementation you must do like this :
// 2. Get RTFtemplate with default Implementation of template engine (Freemarker) RTFTemplate rtfTemplate = builder.newRTFTemplate(RTFTemplateBuilder. DEFAULT_FREEMARKER_RTFTEMPLATE);
Here the Spring configuration (by default) of RTFtemplate :
<beans> <!-- ********************************************************************** ********************* RTFTEMPLTE IMPLEMENTATION ********************* ********************************************************************** --> <!-- Defautlt RTFTemplate implementation with freemarker template engine --> <bean id="ftlRTFTemplate" class="net.sourceforge.rtf.RTFTemplate" singleton="false" > <property name="parser" ref="defaultRTFParser" /> <property name="transformer" ref="ftlTransformer" /> <property name="templateEngine" ref="ftl" /> </bean> <!-- Defautlt RTFTemplate implementation with velocity template engine --> <bean id="vmRTFTemplate" class="net.sourceforge.rtf.RTFTemplate" singleton="false" > <property name="parser" ref="defaultRTFParser" /> <property name="transformer" ref="vmTransformer" /> <property name="templateEngine" ref="vm" /> </bean> <!-- ********************************************************************** ********************* RTFDOCUMENT PARSER ********************* ********************************************************************** --> <!-- Defautlt RTFDocument Parser --> <bean id="defaultRTFParser" class="net.sourceforge.rtf.handler.RTFDocumentHandler" singleton="true" > </bean> <!-- ********************************************************************** ********************* FREEMARKER TEMPLATE ENGINE ********************* ********************************************************************** --> <!-- Freemarker template engine --> <bean id="ftl" class="net.sourceforge.rtf.template.freemarker.FreemarkerTemplateEngineImpl" singleton="false" > <property name="freemarkerConfiguration" ref="ftlConfiguration" /> </bean> <!-- Freemarker Configuration --> <bean id="ftlConfiguration" class="freemarker.template.Configuration" singleton="true" > </bean> <!-- Freemarker RTF Document Transformer --> <bean id="ftlTransformer" class="net.sourceforge.rtf.template.freemarker.RTFFreemarkerTransformerImpl" singleton="true" > </bean> <!-- ********************************************************************** ********************* VELOCITY TEMPLATE ENGINE ********************* ********************************************************************** --> <bean id="vm" class="net.sourceforge.rtf.template.velocity.VelocityTemplateEngineImpl" singleton="false" > <property name="velocityEngine" ref="vmEngine" /> </bean> <!-- VelocityEngine Configuration --> <bean id="vmEngine" class="org.apache.velocity.app.VelocityEngine" singleton="true" > </bean> <!-- Velocity RTF Document Transformer --> <bean id="vmTransformer" class="net.sourceforge.rtf.template.velocity.RTFVelocityTransformerImpl" singleton="true" > </bean> </beans>
The definition of the bean with id vmRTFTemplate is the configuration for RTFtemplate to use :
RTFDocumentBuilder class is a helper which is able to load Spring configuration of RTFtemplate.
It's possible to use your own Spring configuration (to implement interfaces with your own implementations) :
// Load rtftemplate-config Spring configuration String key = "net/sourceforge/rtf/usecases/config/rtftemplate-config.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(key); // 1. Get RTFtemplateBuilder with spring config RTFTemplateBuilder builder = RTFTemplateBuilder.newRTFTemplateBuilder(key, applicationContext);
Spring configuration is cached. To reuse your configuration :
String key = "net/sourceforge/rtf/usecases/config/rtftemplate-config.xml"; // 1. Get RTFtemplateBuilder with spring config RTFTemplateBuilder builder = RTFTemplateBuilder.newRTFTemplateBuilder(key);
This last configuration can be done without Spring, with JAVA code like this :
RTFTemplate rtfTemplate = new RTFTemplate(); // Parser RTFDocumentHandler parser = new RTFDocumentHandler(); rtfTemplate.setParser(parser); // Transformer IRTFDocumentTransformer transformer = new RTFVelocityTransformerImpl(); rtfTemplate.setTransformer(transformer); // Template engine VelocityTemplateEngineImpl templateEngine = new VelocityTemplateEngineImpl(); // Initialize velocity engine VelocityEngine velocityEngine = new VelocityEngine(); templateEngine.setVelocityEngine(velocityEngine); rtfTemplate.setTemplateEngine(templateEngine);