Skip to content

Commit

Permalink
[#674] Fix FreeMarker utilities to accept custom class loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Dec 10, 2024
1 parent 7b23ec3 commit 5a4cd34
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 30 deletions.
46 changes: 26 additions & 20 deletions compiler/core/src/zserio/extension/common/FreeMarkerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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)
{
Expand All @@ -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<String> readFreemarkerTemplate(String templateName) throws ZserioExtensionException
public static List<String> 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<String> lines = new ArrayList<String>();
while (reader.ready())
Expand All @@ -143,13 +149,13 @@ public static List<String> 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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,17 @@ 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);
}

protected static List<String> 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)
Expand Down

0 comments on commit 5a4cd34

Please sign in to comment.