User guide - RTFTempate integration - Application

RTFTemplate & Application

This section describes how you must integrate RTFTemplate into rich client application (Swing,...). But you can use this explanation in your WEB application if you don't want use RTFTemplate servlet.

XML fields available

It's adwisable to use XML file of available fields (*.fields.xml). To do that you must call setXmlFields method of RTFTemplate class.

  // Load XML fields available and set it to the RTFTemplate
  InputStream xmlFieldsStream = RTFTemplateWithVelocityAndXmlFields.class.getResourceAsStream("test.fields.xml");
  rtfTemplate.setXmlFields(xmlFieldsStream);
  

Here basic sample of RTFTemplate using :

  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 Load XML fields available and set it to the RTFTemplate
  InputStream xmlFieldsStream = ...
  rtfTemplate.setXmlFields(xmlFieldsStream);
  
  // 4. Set the RTF model source 
  rtfTemplate.setTemplate(new File(rtfSource));  
  
  // 5. Put the context           
  rtfTemplate.put("project", new Project("Jakarta Velocity project"));
  ...    
  
  // 6. Merge the RTF source model and the context  
  rtfTemplate.merge(rtfTarget);

Optimization

The first and second step of generation process of RTFTemplate (parse RTF source to get RTFDocument and transform this last switch template engine) can degradate your performance of your application if you have a lot of RTF edition.

To optimize the process, you can cache the instance of transformed RTFDocument.

Transformed RTFDocumentcan be get :

  • after calling transform method of rtfTemplate instance. This method parse and transform the RTF model source by using the XML file of available fields.
  • after calling merge method of rtfTemplate instance. This method call transform method and merge transformed RTFDocument with context.

To do that you 2 use cases :

use case 1 :

  • on first RTF edition, execute the 2 steps and get the transformed RTFDocument.
  • cache the transformed RTFDocument instance (for instance by using java.util.Map).
  • on second edition, get the cached instance to merge your context (with template engine).
      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();    
      
      // Test if transformed document exist fot this RTF model
      RTFDocument transformedDocument = (RTFDocument)staticTransformedDocumentMap.get(rtfSource);
      if (transformedDocument == null) {   
        // transformed document doesn't exist
    
        // 3 Load XML fields available and set it to the RTFTemplate
        InputStream xmlFieldsStream = ...
        rtfTemplate.setXmlFields(xmlFieldsStream);
        
        // 4. Set the RTF model source 
        rtfTemplate.setTemplate(new File(rtfSource));  
      }
      else 
        // Transformed document exist, set to the rtfTemplate
        rtfTemplate.setTransformedDocument(transformedDocument);  
    
      
      // 5. Put the context           
      rtfTemplate.put("project", new Project("Jakarta Velocity project"));
      ...    
      
      // 6. Merge the RTF source model and the context  
      rtfTemplate.merge(rtfTarget);  
      if (transformedDocument == null) {  
        / 7. Get the transformed document and cache it into the map
        transformedDocument = rtfTemplate.getTransformedDocument();
        staticTransformedDocumentMap.put(rtfSource, transformedDocument);
      }
    

use case 2 :

  • (on start application) execute the 2 steps and get the transformed RTFDocument.
  • cache the transformed RTFDocument instance (for instance by using java.util.Map).
  • on RTF edition get the cached instance to merge your context (with template engine).
  // On application start
  
  String rtfSource = "usecases/models/jakartavelocityproject/jakarta-velocity-model.rtf";

  // 1. Get default RTFtemplateBuilder
  RTFTemplateBuilder builder = RTFTemplateBuilder.newRTFTemplateBuilder();            
    
  // 2. Get RTFtemplate with default Implementation of template engine (Velocity) 
  RTFTemplate rtfTemplate = builder.newRTFTemplate();    
  
  // 3 Load XML fields available and set it to the RTFTemplate
  InputStream xmlFieldsStream = ...
  rtfTemplate.setXmlFields(xmlFieldsStream);
  
  // 4. Set the RTF model source 
  rtfTemplate.setTemplate(new File(rtfSource));  
    
  // 5. Get the transformed document and cache it into the map
  RTFDocument transformedDocument = rtfTemplate.transform();    
  staticTransformedDocumentMap.put(rtfSource, transformedDocument);
  // Edition
  
  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. Get the transformed document and NOT the rtf source
  RTFDocument transformedDocument =  (RTFDocument)transformedDocumentMap.get(rtfSource); 
  rtfTemplate.setTransformedDocument(transformedDocument);  
  
  // 4. Put the context           
  rtfTemplate.put("project", new Project("Jakarta Velocity project"));
  ...    
  
  // 5. Merge the RTF source model and the context  
  rtfTemplate.merge(rtfTarget);
  

This optimization must be done if you have problems with performance. But with cache solution, you must develop a system which is able to desactivate the cache when RTF model is updated.