@@ -27,19 +27,20 @@ public final class FreeMarkerUtil
27
27
/**
28
28
* Processes FreeMarker template with the provided data model and generates output.
29
29
*
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.
31
31
* @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.
33
34
*
34
35
* @throws ZserioExtensionException In case of any template error.
35
36
*/
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
38
39
{
39
40
if (freeMarkerConfig == null )
40
41
{
41
42
final Configuration newFreeMarkerConfig = new Configuration (Configuration .VERSION_2_3_28 );
42
- newFreeMarkerConfig .setClassForTemplateLoading (FreeMarkerUtil . class , '/' + FREEMARKER_LOCATION );
43
+ newFreeMarkerConfig .setClassForTemplateLoading (classForTemplateLoading , '/' + FREEMARKER_LOCATION );
43
44
newFreeMarkerConfig .setOutputEncoding ("UTF-8" );
44
45
45
46
freeMarkerConfig = newFreeMarkerConfig ;
@@ -63,30 +64,32 @@ public static void processTemplate(String templateName, Object templateDataModel
63
64
/**
64
65
* Processes FreeMarker template with the provided data model and generates output.
65
66
*
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.
67
68
* @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.
69
71
*
70
72
* @throws ZserioExtensionException In case of any template error.
71
73
*/
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
74
76
{
75
- processTemplate (templateName , templateDataModel , outputFile , false );
77
+ processTemplate (templateName , templateDataModel , outputFile , classForTemplateLoading , false );
76
78
}
77
79
78
80
/**
79
81
* Processes FreeMarker template with the provided data model and generates output.
80
82
*
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.
82
84
* @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.
85
88
*
86
89
* @throws ZserioExtensionException In case of any template error.
87
90
*/
88
91
public static void processTemplate (String templateName , Object templateDataModel , File outputFile ,
89
- boolean amalgamate ) throws ZserioExtensionException
92
+ Class <?> classForTemplateLoading , boolean amalgamate ) throws ZserioExtensionException
90
93
{
91
94
FileUtil .createOutputDirectory (outputFile );
92
95
@@ -108,7 +111,7 @@ public static void processTemplate(String templateName, Object templateDataModel
108
111
109
112
if (append )
110
113
bufferedWriter .newLine ();
111
- processTemplate (templateName , templateDataModel , bufferedWriter );
114
+ processTemplate (templateName , templateDataModel , bufferedWriter , classForTemplateLoading );
112
115
}
113
116
catch (IOException exception )
114
117
{
@@ -122,14 +125,17 @@ public static void processTemplate(String templateName, Object templateDataModel
122
125
* @param templateName Name of the FreeMarker template to read.
123
126
*
124
127
* @return List of lines read from FreeMarker template.
128
+ * @param classForTemplateLoading The class which is used to get class loader for templates.
125
129
*
126
130
* @throws ZserioExtensionException When the template is not available.
127
131
*/
128
- public static List <String > readFreemarkerTemplate (String templateName ) throws ZserioExtensionException
132
+ public static List <String > readFreemarkerTemplate (String templateName , Class <?> classForTemplateLoading )
133
+ throws ZserioExtensionException
129
134
{
130
135
final String fullTemplateName = FREEMARKER_LOCATION + templateName ;
131
136
try (final BufferedReader reader = new BufferedReader (new InputStreamReader (
132
- getFreemarkerTemplateStream (fullTemplateName ), StandardCharsets .UTF_8 )))
137
+ getFreemarkerTemplateStream (fullTemplateName , classForTemplateLoading ),
138
+ StandardCharsets .UTF_8 )))
133
139
{
134
140
final List <String > lines = new ArrayList <String >();
135
141
while (reader .ready ())
@@ -143,13 +149,13 @@ public static List<String> readFreemarkerTemplate(String templateName) throws Zs
143
149
}
144
150
}
145
151
146
- private static InputStream getFreemarkerTemplateStream (String templateName ) throws ZserioExtensionException
152
+ private static InputStream getFreemarkerTemplateStream (
153
+ String templateName , Class <?> classForTemplateLoading ) throws ZserioExtensionException
147
154
{
148
155
InputStream resourceStream = null ;
149
156
try
150
157
{
151
- final ClassLoader classLoader = FreeMarkerUtil .class .getClassLoader ();
152
- resourceStream = classLoader .getResourceAsStream (templateName );
158
+ resourceStream = classForTemplateLoading .getClassLoader ().getResourceAsStream (templateName );
153
159
}
154
160
catch (Exception e )
155
161
{
0 commit comments