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.
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);
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 :
To do that you 2 use cases :
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); }
// 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.