From 54e3312d42e82c36da964874b03bf69b9068359c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 16 Oct 2010 16:08:48 +0200 Subject: [PATCH] Add classpath-based template provider for template inclusions. --- .../net/pterodactylus/sone/web/WebInterface.java | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 83a942a..cbd8b42 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -44,6 +44,9 @@ import net.pterodactylus.util.template.DateFilter; import net.pterodactylus.util.template.DefaultTemplateFactory; import net.pterodactylus.util.template.ReflectionAccessor; import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateException; +import net.pterodactylus.util.template.TemplateFactory; +import net.pterodactylus.util.template.TemplateProvider; import freenet.clients.http.LinkEnabledCallback; import freenet.clients.http.SessionManager; import freenet.clients.http.ToadletContainer; @@ -148,6 +151,7 @@ public class WebInterface extends AbstractService { templateFactory.addAccessor(Post.class, new PostAccessor(core())); templateFactory.addFilter("date", new DateFilter()); templateFactory.addFilter("l10n", new L10nFilter(l10n())); + templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory)); String formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword(); @@ -266,4 +270,47 @@ public class WebInterface extends AbstractService { } } + /** + * Template provider implementation that uses + * {@link WebInterface#createReader(String)} to load templates for + * inclusion. + * + * @author David ‘Bombe’ Roden + */ + private class ClassPathTemplateProvider implements TemplateProvider { + + /** The template factory. */ + private final TemplateFactory templateFactory; + + /** + * Creates a new template provider that locates templates on the + * classpath. + * + * @param templateFactory + * The template factory to create the templates + */ + public ClassPathTemplateProvider(TemplateFactory templateFactory) { + this.templateFactory = templateFactory; + } + + /** + * {@inheritDoc} + */ + @Override + @SuppressWarnings("synthetic-access") + public Template getTemplate(String templateName) { + Reader templateReader = createReader("/templates/" + templateName); + if (templateReader == null) { + return null; + } + Template template = templateFactory.createTemplate(templateReader); + try { + template.parse(); + } catch (TemplateException te1) { + logger.log(Level.WARNING, "Could not parse template “" + templateName + "” for inclusion!", te1); + } + return template; + } + + } } -- 2.7.4