From: David ‘Bombe’ Roden Date: Mon, 27 Jul 2015 18:28:06 +0000 (+0200) Subject: Load included templates from filesystem X-Git-Tag: 0.9.4^2~8^2 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=b7aa5c79bf610db3e44fed9e6407119c421f2458;p=Sone.git Load included templates from filesystem --- diff --git a/src/main/java/net/pterodactylus/sone/main/DebugLoaders.java b/src/main/java/net/pterodactylus/sone/main/DebugLoaders.java index 14c58b6..11755e0 100644 --- a/src/main/java/net/pterodactylus/sone/main/DebugLoaders.java +++ b/src/main/java/net/pterodactylus/sone/main/DebugLoaders.java @@ -4,7 +4,9 @@ import java.io.File; import net.pterodactylus.sone.template.FilesystemTemplate; import net.pterodactylus.sone.web.ReloadingPage; +import net.pterodactylus.util.template.FilesystemTemplateProvider; import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateProvider; import net.pterodactylus.util.web.Page; import net.pterodactylus.util.web.Request; @@ -31,4 +33,9 @@ public class DebugLoaders implements Loaders { return new ReloadingPage(basePath, new File(filesystemPath, prefix).getAbsolutePath(), mimeType); } + @Override + public TemplateProvider getTemplateProvider() { + return new FilesystemTemplateProvider(new File(filesystemPath, "/templates/").getAbsolutePath()); + } + } diff --git a/src/main/java/net/pterodactylus/sone/main/DefaultLoaders.java b/src/main/java/net/pterodactylus/sone/main/DefaultLoaders.java index 4978d7c..0f73216 100644 --- a/src/main/java/net/pterodactylus/sone/main/DefaultLoaders.java +++ b/src/main/java/net/pterodactylus/sone/main/DefaultLoaders.java @@ -7,8 +7,11 @@ import java.io.InputStreamReader; import java.io.Reader; import java.io.UnsupportedEncodingException; +import net.pterodactylus.sone.web.WebInterface; import net.pterodactylus.util.io.Closer; +import net.pterodactylus.util.template.ClassPathTemplateProvider; import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateProvider; import net.pterodactylus.util.web.Page; import net.pterodactylus.util.web.Request; import net.pterodactylus.util.web.StaticPage; @@ -41,4 +44,9 @@ public class DefaultLoaders implements Loaders { return new StaticPage(pathPrefix, basePath, mimeType); } + @Override + public TemplateProvider getTemplateProvider() { + return new ClassPathTemplateProvider(WebInterface.class, "/templates/"); + } + } diff --git a/src/main/java/net/pterodactylus/sone/main/Loaders.java b/src/main/java/net/pterodactylus/sone/main/Loaders.java index 35ae81f..34ee1b1 100644 --- a/src/main/java/net/pterodactylus/sone/main/Loaders.java +++ b/src/main/java/net/pterodactylus/sone/main/Loaders.java @@ -1,9 +1,9 @@ package net.pterodactylus.sone.main; import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateProvider; import net.pterodactylus.util.web.Page; import net.pterodactylus.util.web.Request; -import net.pterodactylus.util.web.StaticPage; import com.google.inject.ImplementedBy; @@ -17,5 +17,6 @@ public interface Loaders { Template loadTemplate(String path); Page loadStaticPage(String basePath, String prefix, String mimeType); + TemplateProvider getTemplateProvider(); } diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index f524e5f..beba513 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -129,7 +129,6 @@ import net.pterodactylus.sone.web.page.PageToadletFactory; import net.pterodactylus.util.notify.Notification; import net.pterodactylus.util.notify.NotificationManager; import net.pterodactylus.util.notify.TemplateNotification; -import net.pterodactylus.util.template.ClassPathTemplateProvider; import net.pterodactylus.util.template.CollectionSortFilter; import net.pterodactylus.util.template.ContainsFilter; import net.pterodactylus.util.template.DateFilter; @@ -285,7 +284,7 @@ public class WebInterface { templateContextFactory.addFilter("mod", new ModFilter()); templateContextFactory.addFilter("paginate", new PaginationFilter()); templateContextFactory.addProvider(TemplateProvider.TEMPLATE_CONTEXT_PROVIDER); - templateContextFactory.addProvider(new ClassPathTemplateProvider(WebInterface.class, "/templates/")); + templateContextFactory.addProvider(loaders.getTemplateProvider()); templateContextFactory.addTemplateObject("webInterface", this); templateContextFactory.addTemplateObject("formPassword", formPassword); diff --git a/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java b/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java index e9dfb71..c3fa7e9 100644 --- a/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java +++ b/src/test/java/net/pterodactylus/sone/main/DebugLoadersTest.java @@ -16,6 +16,7 @@ import java.net.URISyntaxException; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; +import net.pterodactylus.util.template.TemplateProvider; import net.pterodactylus.util.web.Method; import net.pterodactylus.util.web.Page; import net.pterodactylus.util.web.Response; @@ -50,6 +51,9 @@ public class DebugLoadersTest { loaders = new DebugLoaders(templatePath); File templateFile = new File(templatePath, "template.txt"); Files.write("<%if foo>foo<%else>bar<%/if>", templateFile, Charsets.UTF_8); + new File(templatePath, "templates").mkdir(); + File secondTemplateFile = new File(templatePath, "templates/template.txt"); + Files.write("<%if foo>foo<%else>bar<%/if>", secondTemplateFile, Charsets.UTF_8); } @Test @@ -74,4 +78,12 @@ public class DebugLoadersTest { assertThat(response.getStatusCode(), is(200)); } + @Test + public void templateProviderLocatesTemplatesInFileSystem() { + TemplateProvider templateProvider = loaders.getTemplateProvider(); + Template template = templateProvider.getTemplate(templateContext, "template.txt"); + template.render(templateContext, stringWriter); + assertThat(stringWriter.toString(), is("bar")); + } + } diff --git a/src/test/java/net/pterodactylus/sone/main/DefaultLoadersTest.java b/src/test/java/net/pterodactylus/sone/main/DefaultLoadersTest.java index 59abf0a..0f0a9f1 100644 --- a/src/test/java/net/pterodactylus/sone/main/DefaultLoadersTest.java +++ b/src/test/java/net/pterodactylus/sone/main/DefaultLoadersTest.java @@ -2,6 +2,7 @@ package net.pterodactylus.sone.main; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.startsWith; import static org.mockito.Mockito.mock; @@ -15,6 +16,7 @@ import java.net.URISyntaxException; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; +import net.pterodactylus.util.template.TemplateProvider; import net.pterodactylus.util.web.Method; import net.pterodactylus.util.web.Page; import net.pterodactylus.util.web.Response; @@ -57,4 +59,11 @@ public class DefaultLoadersTest { assertThat(response.getStatusCode(), is(200)); } + @Test + public void templateIsLocatedInClasspath() { + TemplateProvider templateProvider = loaders.getTemplateProvider(); + Template template = templateProvider.getTemplate(templateContext, "about.html"); + assertThat(template, notNullValue()); + } + }