Skip to content

Commit 2cb1f89

Browse files
committed
Fix FreeMarker utilities to accept custom class loaders
1 parent 23e06a4 commit 2cb1f89

File tree

5 files changed

+39
-30
lines changed

5 files changed

+39
-30
lines changed

compiler/core/src/zserio/extension/common/FreeMarkerUtil.java

+26-20
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,20 @@ public final class FreeMarkerUtil
2727
/**
2828
* Processes FreeMarker template with the provided data model and generates output.
2929
*
30-
* @param templateName The template name with the path relatively to "/FREEMARKER_LOCATION" directory.
30+
* @param templateName The template name with the path relatively to "/FREEMARKER_LOCATION" directory.
3131
* @param templateDataModel The template data model to apply.
32-
* @param outputWriter The writer to use for generated output.
32+
* @param outputWriter The writer to use for generated output.
33+
* @param classForTemplateLoading The class which is used to get class loader for templates.
3334
*
3435
* @throws ZserioExtensionException In case of any template error.
3536
*/
36-
public static void processTemplate(String templateName, Object templateDataModel, Writer outputWriter)
37-
throws ZserioExtensionException
37+
public static void processTemplate(String templateName, Object templateDataModel, Writer outputWriter,
38+
Class<?> classForTemplateLoading) throws ZserioExtensionException
3839
{
3940
if (freeMarkerConfig == null)
4041
{
4142
final Configuration newFreeMarkerConfig = new Configuration(Configuration.VERSION_2_3_28);
42-
newFreeMarkerConfig.setClassForTemplateLoading(FreeMarkerUtil.class, '/' + FREEMARKER_LOCATION);
43+
newFreeMarkerConfig.setClassForTemplateLoading(classForTemplateLoading, '/' + FREEMARKER_LOCATION);
4344
newFreeMarkerConfig.setOutputEncoding("UTF-8");
4445

4546
freeMarkerConfig = newFreeMarkerConfig;
@@ -63,30 +64,32 @@ public static void processTemplate(String templateName, Object templateDataModel
6364
/**
6465
* Processes FreeMarker template with the provided data model and generates output.
6566
*
66-
* @param templateName The template name with the path relatively to "/FREEMARKER_LOCATION" directory.
67+
* @param templateName The template name with the path relatively to "/FREEMARKER_LOCATION" directory.
6768
* @param templateDataModel The template data model to apply.
68-
* @param outputFile The output to be generated.
69+
* @param outputFile The output to be generated.
70+
* @param classForTemplateLoading The class which is used to get class loader for templates.
6971
*
7072
* @throws ZserioExtensionException In case of any template error.
7173
*/
72-
public static void processTemplate(String templateName, Object templateDataModel, File outputFile)
73-
throws ZserioExtensionException
74+
public static void processTemplate(String templateName, Object templateDataModel, File outputFile,
75+
Class<?> classForTemplateLoading) throws ZserioExtensionException
7476
{
75-
processTemplate(templateName, templateDataModel, outputFile, false);
77+
processTemplate(templateName, templateDataModel, outputFile, classForTemplateLoading, false);
7678
}
7779

7880
/**
7981
* Processes FreeMarker template with the provided data model and generates output.
8082
*
81-
* @param templateName The template name with the path relatively to "/FREEMARKER_LOCATION" directory.
83+
* @param templateName The template name with the path relatively to "/FREEMARKER_LOCATION" directory.
8284
* @param templateDataModel The template data model to apply.
83-
* @param outputFile The output to be generated.
84-
* @param amalgamate True if the generated output will be amalgamated to the output file.
85+
* @param outputFile The output to be generated.
86+
* @param amalgamate True if the generated output will be amalgamated to the output file.
87+
* @param classForTemplateLoading The class which is used to get class loader for templates.
8588
*
8689
* @throws ZserioExtensionException In case of any template error.
8790
*/
8891
public static void processTemplate(String templateName, Object templateDataModel, File outputFile,
89-
boolean amalgamate) throws ZserioExtensionException
92+
Class<?> classForTemplateLoading, boolean amalgamate) throws ZserioExtensionException
9093
{
9194
FileUtil.createOutputDirectory(outputFile);
9295

@@ -108,7 +111,7 @@ public static void processTemplate(String templateName, Object templateDataModel
108111

109112
if (append)
110113
bufferedWriter.newLine();
111-
processTemplate(templateName, templateDataModel, bufferedWriter);
114+
processTemplate(templateName, templateDataModel, bufferedWriter, classForTemplateLoading);
112115
}
113116
catch (IOException exception)
114117
{
@@ -122,14 +125,17 @@ public static void processTemplate(String templateName, Object templateDataModel
122125
* @param templateName Name of the FreeMarker template to read.
123126
*
124127
* @return List of lines read from FreeMarker template.
128+
* @param classForTemplateLoading The class which is used to get class loader for templates.
125129
*
126130
* @throws ZserioExtensionException When the template is not available.
127131
*/
128-
public static List<String> readFreemarkerTemplate(String templateName) throws ZserioExtensionException
132+
public static List<String> readFreemarkerTemplate(String templateName, Class<?> classForTemplateLoading)
133+
throws ZserioExtensionException
129134
{
130135
final String fullTemplateName = FREEMARKER_LOCATION + templateName;
131136
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(
132-
getFreemarkerTemplateStream(fullTemplateName), StandardCharsets.UTF_8)))
137+
getFreemarkerTemplateStream(fullTemplateName, classForTemplateLoading),
138+
StandardCharsets.UTF_8)))
133139
{
134140
final List<String> lines = new ArrayList<String>();
135141
while (reader.ready())
@@ -143,13 +149,13 @@ public static List<String> readFreemarkerTemplate(String templateName) throws Zs
143149
}
144150
}
145151

146-
private static InputStream getFreemarkerTemplateStream(String templateName) throws ZserioExtensionException
152+
private static InputStream getFreemarkerTemplateStream(
153+
String templateName, Class<?> classForTemplateLoading) throws ZserioExtensionException
147154
{
148155
InputStream resourceStream = null;
149156
try
150157
{
151-
final ClassLoader classLoader = FreeMarkerUtil.class.getClassLoader();
152-
resourceStream = classLoader.getResourceAsStream(templateName);
158+
resourceStream = classForTemplateLoading.getClassLoader().getResourceAsStream(templateName);
153159
}
154160
catch (Exception e)
155161
{

compiler/extensions/cpp/src/zserio/extension/cpp/CppDefaultEmitter.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ private void processTemplate(String templateName, Object templateData, PackageNa
104104
{
105105
if (fileInfo) // not skipped
106106
{
107-
FreeMarkerUtil.processTemplate(
108-
CPP_TEMPLATE_LOCATION + templateName, templateData, outputFile, amalgamate);
107+
FreeMarkerUtil.processTemplate(CPP_TEMPLATE_LOCATION + templateName, templateData,
108+
outputFile, CppDefaultEmitter.class, amalgamate);
109109
}
110110
return;
111111
}
@@ -116,8 +116,8 @@ private void processTemplate(String templateName, Object templateData, PackageNa
116116
!outputFileManager.checkTimestamps(outputFile) || !checkGeneratorDescription(outputFile);
117117
if (generate)
118118
{
119-
FreeMarkerUtil.processTemplate(
120-
CPP_TEMPLATE_LOCATION + templateName, templateData, outputFile, amalgamate);
119+
FreeMarkerUtil.processTemplate(CPP_TEMPLATE_LOCATION + templateName, templateData, outputFile,
120+
CppDefaultEmitter.class, amalgamate);
121121
}
122122

123123
outputFileManager.registerOutputFile(outputFile, generate);

compiler/extensions/doc/src/zserio/extension/doc/DocFreeMarkerUtil.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ final class DocFreeMarkerUtil
1616
public static void processTemplate(String templateName, Object templateData, File outputFile)
1717
throws ZserioExtensionException
1818
{
19-
FreeMarkerUtil.processTemplate(DOC_TEMPLATE_LOCATION + templateName, templateData, outputFile);
19+
FreeMarkerUtil.processTemplate(
20+
DOC_TEMPLATE_LOCATION + templateName, templateData, outputFile, DocFreeMarkerUtil.class);
2021
}
2122

2223
public static void processTemplate(String templateName, Object templateData, Writer outputWriter)
2324
throws ZserioExtensionException
2425
{
25-
FreeMarkerUtil.processTemplate(DOC_TEMPLATE_LOCATION + templateName, templateData, outputWriter);
26+
FreeMarkerUtil.processTemplate(
27+
DOC_TEMPLATE_LOCATION + templateName, templateData, outputWriter, DocFreeMarkerUtil.class);
2628
}
2729

2830
public static final String DOC_TEMPLATE_LOCATION = "doc/";

compiler/extensions/java/src/zserio/extension/java/JavaDefaultEmitter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private void processTemplate(String templateName, Object templateData, PackageNa
9494
if (generate)
9595
{
9696
FreeMarkerUtil.processTemplate(
97-
JAVA_TEMPLATE_LOCATION + templateName, templateData, outputFile, false);
97+
JAVA_TEMPLATE_LOCATION + templateName, templateData, outputFile, JavaDefaultEmitter.class);
9898
}
9999

100100
outputFileManager.registerOutputFile(outputFile, generate);

compiler/extensions/python/src/zserio/extension/python/PythonDefaultEmitter.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,17 @@ protected void processTemplate(String templateName, Object templateData, Package
8181
!outputFileManager.checkTimestamps(outputFile) || !checkGeneratorDescription(outputFile);
8282
if (generate)
8383
{
84-
FreeMarkerUtil.processTemplate(
85-
PYTHON_TEMPLATE_LOCATION + templateName, templateData, outputFile, false);
84+
FreeMarkerUtil.processTemplate(PYTHON_TEMPLATE_LOCATION + templateName, templateData, outputFile,
85+
PythonDefaultEmitter.class);
8686
}
8787

8888
outputFileManager.registerOutputFile(outputFile, generate);
8989
}
9090

9191
protected static List<String> readFreemarkerTemplate(String templateName) throws ZserioExtensionException
9292
{
93-
return FreeMarkerUtil.readFreemarkerTemplate(PYTHON_TEMPLATE_LOCATION + templateName);
93+
return FreeMarkerUtil.readFreemarkerTemplate(
94+
PYTHON_TEMPLATE_LOCATION + templateName, PythonDefaultEmitter.class);
9495
}
9596

9697
static String getOutputFileName(String outFileNameRoot)

0 commit comments

Comments
 (0)