From 2820c84cf18ec1662f5ab81c38e40f197c316d5c Mon Sep 17 00:00:00 2001 From: Miki Rozloznik Date: Tue, 3 Dec 2024 17:08:42 +0100 Subject: [PATCH] Fix FreeMarker utilities to accept custom class loaders --- .../extension/common/FreeMarkerUtil.java | 46 +++++++++++-------- .../extension/cpp/CppDefaultEmitter.java | 8 ++-- .../extension/doc/DocFreeMarkerUtil.java | 6 ++- .../extension/java/JavaDefaultEmitter.java | 2 +- .../python/PythonDefaultEmitter.java | 7 +-- 5 files changed, 39 insertions(+), 30 deletions(-) diff --git a/compiler/core/src/zserio/extension/common/FreeMarkerUtil.java b/compiler/core/src/zserio/extension/common/FreeMarkerUtil.java index 3dee95ba5..a5c45eca1 100644 --- a/compiler/core/src/zserio/extension/common/FreeMarkerUtil.java +++ b/compiler/core/src/zserio/extension/common/FreeMarkerUtil.java @@ -27,19 +27,20 @@ public final class FreeMarkerUtil /** * Processes FreeMarker template with the provided data model and generates output. * - * @param templateName The template name with the path relatively to "/FREEMARKER_LOCATION" directory. + * @param templateName The template name with the path relatively to "/FREEMARKER_LOCATION" directory. * @param templateDataModel The template data model to apply. - * @param outputWriter The writer to use for generated output. + * @param outputWriter The writer to use for generated output. + * @param classForTemplateLoading The class which is used to get class loader for templates. * * @throws ZserioExtensionException In case of any template error. */ - public static void processTemplate(String templateName, Object templateDataModel, Writer outputWriter) - throws ZserioExtensionException + public static void processTemplate(String templateName, Object templateDataModel, Writer outputWriter, + Class classForTemplateLoading) throws ZserioExtensionException { if (freeMarkerConfig == null) { final Configuration newFreeMarkerConfig = new Configuration(Configuration.VERSION_2_3_28); - newFreeMarkerConfig.setClassForTemplateLoading(FreeMarkerUtil.class, '/' + FREEMARKER_LOCATION); + newFreeMarkerConfig.setClassForTemplateLoading(classForTemplateLoading, '/' + FREEMARKER_LOCATION); newFreeMarkerConfig.setOutputEncoding("UTF-8"); freeMarkerConfig = newFreeMarkerConfig; @@ -63,30 +64,32 @@ public static void processTemplate(String templateName, Object templateDataModel /** * Processes FreeMarker template with the provided data model and generates output. * - * @param templateName The template name with the path relatively to "/FREEMARKER_LOCATION" directory. + * @param templateName The template name with the path relatively to "/FREEMARKER_LOCATION" directory. * @param templateDataModel The template data model to apply. - * @param outputFile The output to be generated. + * @param outputFile The output to be generated. + * @param classForTemplateLoading The class which is used to get class loader for templates. * * @throws ZserioExtensionException In case of any template error. */ - public static void processTemplate(String templateName, Object templateDataModel, File outputFile) - throws ZserioExtensionException + public static void processTemplate(String templateName, Object templateDataModel, File outputFile, + Class classForTemplateLoading) throws ZserioExtensionException { - processTemplate(templateName, templateDataModel, outputFile, false); + processTemplate(templateName, templateDataModel, outputFile, classForTemplateLoading, false); } /** * Processes FreeMarker template with the provided data model and generates output. * - * @param templateName The template name with the path relatively to "/FREEMARKER_LOCATION" directory. + * @param templateName The template name with the path relatively to "/FREEMARKER_LOCATION" directory. * @param templateDataModel The template data model to apply. - * @param outputFile The output to be generated. - * @param amalgamate True if the generated output will be amalgamated to the output file. + * @param outputFile The output to be generated. + * @param amalgamate True if the generated output will be amalgamated to the output file. + * @param classForTemplateLoading The class which is used to get class loader for templates. * * @throws ZserioExtensionException In case of any template error. */ public static void processTemplate(String templateName, Object templateDataModel, File outputFile, - boolean amalgamate) throws ZserioExtensionException + Class classForTemplateLoading, boolean amalgamate) throws ZserioExtensionException { FileUtil.createOutputDirectory(outputFile); @@ -108,7 +111,7 @@ public static void processTemplate(String templateName, Object templateDataModel if (append) bufferedWriter.newLine(); - processTemplate(templateName, templateDataModel, bufferedWriter); + processTemplate(templateName, templateDataModel, bufferedWriter, classForTemplateLoading); } catch (IOException exception) { @@ -122,14 +125,17 @@ public static void processTemplate(String templateName, Object templateDataModel * @param templateName Name of the FreeMarker template to read. * * @return List of lines read from FreeMarker template. + * @param classForTemplateLoading The class which is used to get class loader for templates. * * @throws ZserioExtensionException When the template is not available. */ - public static List readFreemarkerTemplate(String templateName) throws ZserioExtensionException + public static List readFreemarkerTemplate(String templateName, Class classForTemplateLoading) + throws ZserioExtensionException { final String fullTemplateName = FREEMARKER_LOCATION + templateName; try (final BufferedReader reader = new BufferedReader(new InputStreamReader( - getFreemarkerTemplateStream(fullTemplateName), StandardCharsets.UTF_8))) + getFreemarkerTemplateStream(fullTemplateName, classForTemplateLoading), + StandardCharsets.UTF_8))) { final List lines = new ArrayList(); while (reader.ready()) @@ -143,13 +149,13 @@ public static List readFreemarkerTemplate(String templateName) throws Zs } } - private static InputStream getFreemarkerTemplateStream(String templateName) throws ZserioExtensionException + private static InputStream getFreemarkerTemplateStream( + String templateName, Class classForTemplateLoading) throws ZserioExtensionException { InputStream resourceStream = null; try { - final ClassLoader classLoader = FreeMarkerUtil.class.getClassLoader(); - resourceStream = classLoader.getResourceAsStream(templateName); + resourceStream = classForTemplateLoading.getClassLoader().getResourceAsStream(templateName); } catch (Exception e) { diff --git a/compiler/extensions/cpp/src/zserio/extension/cpp/CppDefaultEmitter.java b/compiler/extensions/cpp/src/zserio/extension/cpp/CppDefaultEmitter.java index 45d6ef952..131103bba 100644 --- a/compiler/extensions/cpp/src/zserio/extension/cpp/CppDefaultEmitter.java +++ b/compiler/extensions/cpp/src/zserio/extension/cpp/CppDefaultEmitter.java @@ -104,8 +104,8 @@ private void processTemplate(String templateName, Object templateData, PackageNa { if (fileInfo) // not skipped { - FreeMarkerUtil.processTemplate( - CPP_TEMPLATE_LOCATION + templateName, templateData, outputFile, amalgamate); + FreeMarkerUtil.processTemplate(CPP_TEMPLATE_LOCATION + templateName, templateData, + outputFile, CppDefaultEmitter.class, amalgamate); } return; } @@ -116,8 +116,8 @@ private void processTemplate(String templateName, Object templateData, PackageNa !outputFileManager.checkTimestamps(outputFile) || !checkGeneratorDescription(outputFile); if (generate) { - FreeMarkerUtil.processTemplate( - CPP_TEMPLATE_LOCATION + templateName, templateData, outputFile, amalgamate); + FreeMarkerUtil.processTemplate(CPP_TEMPLATE_LOCATION + templateName, templateData, outputFile, + CppDefaultEmitter.class, amalgamate); } outputFileManager.registerOutputFile(outputFile, generate); diff --git a/compiler/extensions/doc/src/zserio/extension/doc/DocFreeMarkerUtil.java b/compiler/extensions/doc/src/zserio/extension/doc/DocFreeMarkerUtil.java index 995b29473..6ae042fc9 100644 --- a/compiler/extensions/doc/src/zserio/extension/doc/DocFreeMarkerUtil.java +++ b/compiler/extensions/doc/src/zserio/extension/doc/DocFreeMarkerUtil.java @@ -16,13 +16,15 @@ final class DocFreeMarkerUtil public static void processTemplate(String templateName, Object templateData, File outputFile) throws ZserioExtensionException { - FreeMarkerUtil.processTemplate(DOC_TEMPLATE_LOCATION + templateName, templateData, outputFile); + FreeMarkerUtil.processTemplate( + DOC_TEMPLATE_LOCATION + templateName, templateData, outputFile, DocFreeMarkerUtil.class); } public static void processTemplate(String templateName, Object templateData, Writer outputWriter) throws ZserioExtensionException { - FreeMarkerUtil.processTemplate(DOC_TEMPLATE_LOCATION + templateName, templateData, outputWriter); + FreeMarkerUtil.processTemplate( + DOC_TEMPLATE_LOCATION + templateName, templateData, outputWriter, DocFreeMarkerUtil.class); } public static final String DOC_TEMPLATE_LOCATION = "doc/"; diff --git a/compiler/extensions/java/src/zserio/extension/java/JavaDefaultEmitter.java b/compiler/extensions/java/src/zserio/extension/java/JavaDefaultEmitter.java index 505e1b387..5f7c790a3 100644 --- a/compiler/extensions/java/src/zserio/extension/java/JavaDefaultEmitter.java +++ b/compiler/extensions/java/src/zserio/extension/java/JavaDefaultEmitter.java @@ -94,7 +94,7 @@ private void processTemplate(String templateName, Object templateData, PackageNa if (generate) { FreeMarkerUtil.processTemplate( - JAVA_TEMPLATE_LOCATION + templateName, templateData, outputFile, false); + JAVA_TEMPLATE_LOCATION + templateName, templateData, outputFile, JavaDefaultEmitter.class); } outputFileManager.registerOutputFile(outputFile, generate); diff --git a/compiler/extensions/python/src/zserio/extension/python/PythonDefaultEmitter.java b/compiler/extensions/python/src/zserio/extension/python/PythonDefaultEmitter.java index 654d60cbc..9ca8a2bdb 100644 --- a/compiler/extensions/python/src/zserio/extension/python/PythonDefaultEmitter.java +++ b/compiler/extensions/python/src/zserio/extension/python/PythonDefaultEmitter.java @@ -81,8 +81,8 @@ protected void processTemplate(String templateName, Object templateData, Package !outputFileManager.checkTimestamps(outputFile) || !checkGeneratorDescription(outputFile); if (generate) { - FreeMarkerUtil.processTemplate( - PYTHON_TEMPLATE_LOCATION + templateName, templateData, outputFile, false); + FreeMarkerUtil.processTemplate(PYTHON_TEMPLATE_LOCATION + templateName, templateData, outputFile, + PythonDefaultEmitter.class); } outputFileManager.registerOutputFile(outputFile, generate); @@ -90,7 +90,8 @@ protected void processTemplate(String templateName, Object templateData, Package protected static List readFreemarkerTemplate(String templateName) throws ZserioExtensionException { - return FreeMarkerUtil.readFreemarkerTemplate(PYTHON_TEMPLATE_LOCATION + templateName); + return FreeMarkerUtil.readFreemarkerTemplate( + PYTHON_TEMPLATE_LOCATION + templateName, PythonDefaultEmitter.class); } static String getOutputFileName(String outFileNameRoot)