From 7a4fb1a910d463d4b39784ea0f16ebdd89e103b1 Mon Sep 17 00:00:00 2001 From: janos erdos Date: Wed, 15 Nov 2023 20:25:29 +0100 Subject: [PATCH] simplify java api --- .../erdos/stencil/PreparedTemplate.java | 13 ++- .../stencil/impl/CachingTemplateFactory.java | 2 +- .../stencil/impl/NativeTemplateFactory.java | 84 ++----------------- .../impl/DirWatcherTemplateFactoryTest.java | 2 +- src/stencil/api.clj | 4 +- src/stencil/process.clj | 35 +++++--- 6 files changed, 38 insertions(+), 102 deletions(-) diff --git a/java-src/io/github/erdos/stencil/PreparedTemplate.java b/java-src/io/github/erdos/stencil/PreparedTemplate.java index 4a018b3b..3174e86a 100644 --- a/java-src/io/github/erdos/stencil/PreparedTemplate.java +++ b/java-src/io/github/erdos/stencil/PreparedTemplate.java @@ -50,10 +50,12 @@ default TemplateDocumentFormats getTemplateFormat() { TemplateVariables getVariables(); /** - * Makes the template clean up any resources allocated for it. Subsequential invocations of this method have no - * effects. Rendering the template after this method call will throw an IllegalStateException. + * Makes the template clean up any resources allocated for it. + * Subsequent invocations of this method have no effects. + * Rendering the template after this method call will throw an IllegalStateException. */ - void cleanup(); + @Override + void close(); /** * Renders the current prepared template file with the given template data. @@ -61,9 +63,4 @@ default TemplateDocumentFormats getTemplateFormat() { default EvaluatedDocument render(TemplateData templateData) { return API.render(this, templateData); } - - @Override - default void close() { - cleanup(); - } } \ No newline at end of file diff --git a/java-src/io/github/erdos/stencil/impl/CachingTemplateFactory.java b/java-src/io/github/erdos/stencil/impl/CachingTemplateFactory.java index a3853ce7..9ab82dae 100644 --- a/java-src/io/github/erdos/stencil/impl/CachingTemplateFactory.java +++ b/java-src/io/github/erdos/stencil/impl/CachingTemplateFactory.java @@ -37,7 +37,7 @@ public PreparedTemplate prepareTemplateFile(File templateFile, PrepareOptions op PreparedTemplate stored = cache.get(templateFile); if (stored.creationDateTime().toEpochSecond(ZoneOffset.UTC) <= templateFile.lastModified()) { // TODO: this is so not thread safe. - stored.cleanup(); + stored.close(); stored = templateFactory.prepareTemplateFile(templateFile, options); cache.put(templateFile, stored); } diff --git a/java-src/io/github/erdos/stencil/impl/NativeTemplateFactory.java b/java-src/io/github/erdos/stencil/impl/NativeTemplateFactory.java index a2d88981..38895d6d 100644 --- a/java-src/io/github/erdos/stencil/impl/NativeTemplateFactory.java +++ b/java-src/io/github/erdos/stencil/impl/NativeTemplateFactory.java @@ -6,12 +6,8 @@ import io.github.erdos.stencil.exceptions.ParsingException; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; -import java.time.LocalDateTime; import java.util.*; -import java.util.concurrent.atomic.AtomicBoolean; import static io.github.erdos.stencil.TemplateDocumentFormats.ofExtension; import static io.github.erdos.stencil.impl.FileHelper.forceDeleteOnExit; @@ -33,8 +29,12 @@ public PreparedTemplate prepareTemplateFile(final File inputTemplateFile, Prepar throw new IllegalArgumentException("Template preparation options are missing!"); } - try (InputStream input = new FileInputStream(inputTemplateFile)) { - return prepareTemplateImpl(templateDocFormat.get(), input, inputTemplateFile, options); + try { + return (PreparedTemplate) ClojureHelper.findFunction("prepare-template").invoke(inputTemplateFile, options); + } catch (ParsingException e) { + throw e; + } catch (Exception e) { + throw ParsingException.wrapping("Could not parse template file!", e); } } @@ -85,76 +85,4 @@ private static Set fragmentNames(Map prepared) { ? unmodifiableSet(new HashSet<>((Collection) prepared.get(ClojureHelper.Keywords.FRAGMENTS.kw))) : emptySet(); } - - @SuppressWarnings("unchecked") - private static PreparedTemplate prepareTemplateImpl(TemplateDocumentFormats templateDocFormat, InputStream input, File originalFile, PrepareOptions options) { - final IFn prepareFunction = ClojureHelper.findFunction("prepare-template"); - - final String format = templateDocFormat.name(); - final Map prepared; - - try { - prepared = (Map) prepareFunction.invoke(input, options); - } catch (ParsingException e) { - throw e; - } catch (Exception e) { - throw ParsingException.wrapping("Could not parse template file!", e); - } - - final TemplateVariables vars = TemplateVariables.fromPaths(variableNames(prepared), fragmentNames(prepared)); - - final File zipDirResource = (File) prepared.get(ClojureHelper.Keywords.SOURCE_FOLDER.kw); - if (zipDirResource != null) { - forceDeleteOnExit(zipDirResource); - } - - return new PreparedTemplate() { - final LocalDateTime now = LocalDateTime.now(); - final AtomicBoolean valid = new AtomicBoolean(true); - - @Override - public File getTemplateFile() { - return originalFile; - } - - @Override - public TemplateDocumentFormats getTemplateFormat() { - return templateDocFormat; - } - - @Override - public LocalDateTime creationDateTime() { - return now; - } - - @Override - public Object getSecretObject() { - if (!valid.get()) { - throw new IllegalStateException("Can not render destroyed template!"); - } else { - return prepared; - } - } - - @Override - public TemplateVariables getVariables() { - return vars; - } - - @Override - public void cleanup() { - if (valid.compareAndSet(true, false)) { - // deletes unused temporary zip directory - if (zipDirResource != null) { - FileHelper.forceDelete(zipDirResource); - } - } - } - - @Override - public String toString() { - return "