Cette section décrit comment vous devez intégrer RTFTemplate dans une application de type client lourd. Mais ceci est aussi valable dans une application WEB si vous décidez de ne pas utiliser la servlet RTFTemplate.
Il est conseillé d'utiliser le fichier XML des champs disponibles (*.fields.xml). Pour effectuer ceci vous devez utiliser la méthode setXmlFields de la classe RTFTemplate.
// Load XML fields available and set it to the RTFTemplate
InputStream xmlFieldsStream = RTFTemplateWithVelocityAndXmlFields.class.getResourceAsStream("test.fields.xml");
rtfTemplate.setXmlFields(xmlFieldsStream);
L'exemple basique donnerait :
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);
Les deux premières étapes du processus de génération de RTFTemplate (parser le fichier pour obtenir un RTFDocument et transformer ce dernier en fonction du moteur de template) peuvent être coûteuse si votre application suscite de nombreuses éditions RTF.
Pour optimiser le processus, le principe est de mettre en cache l'instance RTFDocument transformé.
Le document RTFDocument transformé peut être récupéré :
Pour cela vous avez deux scénarios :
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);
Cette optimisation n'est pas négligeable lorsque vous avez de nombreuses éditions RTF à effectuer. Il vous faudra cependant mettre en place un système qui permettent de supprimer du cache cette instance lorsque le modèle RTF est modifié.