Skip to content

Add multiple reader support#27

Merged
AuPath merged 3 commits intomainfrom
feat-multiple-reader
Sep 17, 2024
Merged

Add multiple reader support#27
AuPath merged 3 commits intomainfrom
feat-multiple-reader

Conversation

@AuPath
Copy link
Collaborator

@AuPath AuPath commented Jul 23, 2024

The TemplateExecutor class now supports multiple readers. The same class also has been refactored so that the VelocityContext and VelocityEngine are no longer created every time one of the executeMappings methods is run.

private final VelocityContext velocityContext;
private final VelocityEngine velocityEngine;

public TemplateExecutor(Map<String, Reader> readers, TemplateFunctions templateFunctions, boolean failInvalidRef, boolean trimTemplate, boolean templateInResourcesFolder, TemplateMap templateMap, Formatter formatter) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reader(s) should probably not be in the constructor but passed as parameter when the execution of a template is requested. Via CLI this is not important, but if used as a library can facilitate the reuse of the same TemplateExecutor instance.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public TemplateExecutor(Reader reader, TemplateFunctions templateFunctions, boolean failInvalidRef, boolean trimTemplate, boolean templateInResourcesFolder, TemplateMap templateMap, Formatter formatter) {
this.readers = null;
if(reader != null)
this.readers = Map.of("reader", reader);
this.templateFunctions = templateFunctions;
this.failInvalidRef = failInvalidRef;
this.trimTemplate = trimTemplate;
this.templateInResourcesFolder = templateInResourcesFolder;
this.templateMap = templateMap;
this.formatter = formatter;
this.velocityContext = this.templateFunctions == null ?
Util.createVelocityContext(this.readers, templateMap) : Util.createVelocityContext(readers, templateMap,templateFunctions);
this.velocityEngine = Util.createVelocityEngine(this.templateInResourcesFolder,failInvalidRef);

Because the VelocityContext (line 73) depends on the Reader(s), passing in the Reader(s) to the executor method would mean recreating the VelocityContext at each execution, which was the initial problem

Copy link
Member

@marioscrock marioscrock Jul 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is not initialising the VelocityEngine twice; this is why readers should not be in the constructor. It is ok to create different VelocityContext when requesting the VelocityEngine to execute a template.

VelocityEngine velocityEngine = this.velocityEngine;
velocityEngine.evaluate(this.velocityContext, writer, "TemplateExecutor", templateReader);

@AuPath AuPath requested a review from marioscrock July 30, 2024 13:39
@AuPath AuPath merged commit 5b700a5 into main Sep 17, 2024
@AuPath AuPath deleted the feat-multiple-reader branch March 10, 2025 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants