From d09e14849bc490b2abe69fd4758d1204828d52fc Mon Sep 17 00:00:00 2001 From: "yinan.liu" Date: Mon, 14 Nov 2022 14:41:26 +0100 Subject: [PATCH] Add inline template support in TemplateEngine This commit provides the inline template support in TemplateEgine, so that the template can be initiated not only from a file, but could be also from a template string. --- .../bdk/template/api/TemplateEngine.java | 9 +++++++++ .../template/freemarker/FreeMarkerEngine.java | 15 +++++++++++++- .../freemarker/FreeMarkerTemplateTest.java | 11 ++++++++++ .../template/handlebars/HandlebarsEngine.java | 20 +++++++++++++++++-- .../handlebars/HandlebarsEngineTest.java | 7 +++++++ 5 files changed, 59 insertions(+), 3 deletions(-) diff --git a/symphony-bdk-template/symphony-bdk-template-api/src/main/java/com/symphony/bdk/template/api/TemplateEngine.java b/symphony-bdk-template/symphony-bdk-template-api/src/main/java/com/symphony/bdk/template/api/TemplateEngine.java index d3d370fda..dfb79f820 100644 --- a/symphony-bdk-template/symphony-bdk-template-api/src/main/java/com/symphony/bdk/template/api/TemplateEngine.java +++ b/symphony-bdk-template/symphony-bdk-template-api/src/main/java/com/symphony/bdk/template/api/TemplateEngine.java @@ -31,6 +31,15 @@ public interface TemplateEngine { */ Template newTemplateFromClasspath(String templatePath); + /** + * Create a {@link Template} instance from a string + * + * @param template inline template string + * @return a new {@link Template} instantiated from the provided inline string + * @throws TemplateException when template cannot be loaded. + */ + Template newTemplateFromString(String template); + static TemplateEngine getDefaultImplementation() { final ServiceLoader engineServiceLoader = ServiceLoader.load(TemplateEngine.class); diff --git a/symphony-bdk-template/symphony-bdk-template-freemarker/src/main/java/com/symphony/bdk/template/freemarker/FreeMarkerEngine.java b/symphony-bdk-template/symphony-bdk-template-freemarker/src/main/java/com/symphony/bdk/template/freemarker/FreeMarkerEngine.java index d4a08cac6..85d490d52 100644 --- a/symphony-bdk-template/symphony-bdk-template-freemarker/src/main/java/com/symphony/bdk/template/freemarker/FreeMarkerEngine.java +++ b/symphony-bdk-template/symphony-bdk-template-freemarker/src/main/java/com/symphony/bdk/template/freemarker/FreeMarkerEngine.java @@ -17,7 +17,7 @@ * FreeMarker specific implementation of {@link TemplateEngine}. Instantiates {@link FreeMarkerTemplate} objects. * *

- * This class is thread-safe. + * This class is thread-safe. *

*/ @API(status = API.Status.INTERNAL) @@ -53,6 +53,19 @@ public Template newTemplateFromClasspath(String templatePath) { } } + /** + * {@inheritDoc} + */ + @Override + public Template newTemplateFromString(String template) { + try { + final Configuration configuration = createConfiguration(); + return new FreeMarkerTemplate(new freemarker.template.Template(null, template, configuration)); + } catch (IOException e) { + throw new TemplateException("Unable to load template from string", e); + } + } + private static Configuration createConfiguration() { final Configuration cfg = new Configuration(Configuration.VERSION_2_3_29); cfg.setDefaultEncoding("UTF-8"); diff --git a/symphony-bdk-template/symphony-bdk-template-freemarker/src/test/java/com/symphony/bdk/template/freemarker/FreeMarkerTemplateTest.java b/symphony-bdk-template/symphony-bdk-template-freemarker/src/test/java/com/symphony/bdk/template/freemarker/FreeMarkerTemplateTest.java index 2b035ab1a..5496de45e 100644 --- a/symphony-bdk-template/symphony-bdk-template-freemarker/src/test/java/com/symphony/bdk/template/freemarker/FreeMarkerTemplateTest.java +++ b/symphony-bdk-template/symphony-bdk-template-freemarker/src/test/java/com/symphony/bdk/template/freemarker/FreeMarkerTemplateTest.java @@ -90,6 +90,17 @@ public void testFromClasspathWithLong() { "Hello \n"); } + @Test + public void testFromInlineString() { + Long userId = 12345678L; + Map parameters = new HashMap<>(); + parameters.put("userId", userId); + + Template freeMarkerTemplate = new FreeMarkerEngine().newTemplateFromString("Hello \n"); + assertTemplateProducesOutput(freeMarkerTemplate, parameters, + "Hello \n"); + } + @Test public void testWithNotFoundResource() { assertThrows(TemplateException.class, () -> new FreeMarkerEngine().newTemplateFromClasspath("./not/found.ftl")); diff --git a/symphony-bdk-template/symphony-bdk-template-handlebars/src/main/java/com/symphony/bdk/template/handlebars/HandlebarsEngine.java b/symphony-bdk-template/symphony-bdk-template-handlebars/src/main/java/com/symphony/bdk/template/handlebars/HandlebarsEngine.java index b02b8c008..cd0a61d29 100644 --- a/symphony-bdk-template/symphony-bdk-template-handlebars/src/main/java/com/symphony/bdk/template/handlebars/HandlebarsEngine.java +++ b/symphony-bdk-template/symphony-bdk-template-handlebars/src/main/java/com/symphony/bdk/template/handlebars/HandlebarsEngine.java @@ -17,13 +17,15 @@ * {@link Handlebars} implementation of the {@link TemplateEngine} interface. * *

- * This class is thread-safe. + * This class is thread-safe. *

*/ @API(status = API.Status.INTERNAL) public class HandlebarsEngine implements TemplateEngine { - /** Handlebars for classpath loading. Ok for thread-safety. */ + /** + * Handlebars for classpath loading. Ok for thread-safety. + */ private static final Handlebars HANDLEBARS = createHandlebars(new ClassPathTemplateLoader()); /** @@ -54,6 +56,20 @@ public Template newTemplateFromClasspath(String templatePath) { } } + /** + * {@inheritDoc} + */ + @Override + public Template newTemplateFromString(String template) { + try { + return new HandlebarsTemplate(HANDLEBARS.compileInline(template)); + } catch (IOException e) { + throw new TemplateException("Unable to compile Handlebars template from inline string: " + template, e); + } + } + + + /** * Creates a new {@link Handlebars} object with suffix set to "" to make this {@link TemplateEngine} implementation * consistent with other ones (e.g. developers have to specify the template resource extension). diff --git a/symphony-bdk-template/symphony-bdk-template-handlebars/src/test/java/com/symphony/bdk/template/handlebars/HandlebarsEngineTest.java b/symphony-bdk-template/symphony-bdk-template-handlebars/src/test/java/com/symphony/bdk/template/handlebars/HandlebarsEngineTest.java index 69753f089..ff85c5a37 100644 --- a/symphony-bdk-template/symphony-bdk-template-handlebars/src/test/java/com/symphony/bdk/template/handlebars/HandlebarsEngineTest.java +++ b/symphony-bdk-template/symphony-bdk-template-handlebars/src/test/java/com/symphony/bdk/template/handlebars/HandlebarsEngineTest.java @@ -49,6 +49,13 @@ void should_load_template_from_file(@TempDir Path tempDir) throws Exception { assertEquals(EXPECTED_TEST_HBS, content); } + @Test + void should_load_template_from_inline_string() throws Exception { + final Template template = this.engine.newTemplateFromString("\n{{message}}\n\n"); + final String content = template.process(Collections.singletonMap("message", "hello")); + assertEquals(EXPECTED_TEST_HBS, content); + } + @Test void should_load_complex_template_from_file(@TempDir Path tempDir) throws Exception {