From 1127a71127c6488152e5cbe3b8c6d941eaf14e83 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 14 Oct 2010 10:01:14 +0200 Subject: [PATCH 1/1] Move L10nFilter to own file, remove Sone template factory, use DefaultTemplateFactory. --- .../net/pterodactylus/sone/freenet/L10nFilter.java | 55 ++++++++ .../sone/web/SoneTemplateFactory.java | 147 --------------------- .../net/pterodactylus/sone/web/WebInterface.java | 11 +- 3 files changed, 64 insertions(+), 149 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java delete mode 100644 src/main/java/net/pterodactylus/sone/web/SoneTemplateFactory.java diff --git a/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java b/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java new file mode 100644 index 0000000..4d6a744 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/freenet/L10nFilter.java @@ -0,0 +1,55 @@ +/* + * FreenetSone - L10nFilter.java - Copyright © 2010 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.freenet; + +import java.util.Map; + +import net.pterodactylus.util.template.DataProvider; +import net.pterodactylus.util.template.Filter; +import freenet.l10n.BaseL10n; + +/** + * {@link Filter} implementation replaces {@link String} values with their + * translated equivalents. + * + * @author David ‘Bombe’ Roden + */ +public class L10nFilter implements Filter { + + /** The l10n handler. */ + private final BaseL10n l10n; + + /** + * Creates a new L10n filter. + * + * @param l10n + * The l10n handler + */ + public L10nFilter(BaseL10n l10n) { + this.l10n = l10n; + } + + /** + * {@inheritDoc} + */ + @Override + public String format(DataProvider dataProvider, Object data, Map parameters) { + return l10n.getString(String.valueOf(data)); + } + +} diff --git a/src/main/java/net/pterodactylus/sone/web/SoneTemplateFactory.java b/src/main/java/net/pterodactylus/sone/web/SoneTemplateFactory.java deleted file mode 100644 index 51a6691..0000000 --- a/src/main/java/net/pterodactylus/sone/web/SoneTemplateFactory.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * shortener - L10nTemplateFactory.java - Copyright © 2010 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.web; - -import java.io.Reader; -import java.text.DateFormat; -import java.util.Date; -import java.util.Map; - -import net.pterodactylus.util.template.DataProvider; -import net.pterodactylus.util.template.DefaultTemplateFactory; -import net.pterodactylus.util.template.Filter; -import net.pterodactylus.util.template.ReflectionAccessor; -import net.pterodactylus.util.template.Template; -import net.pterodactylus.util.template.TemplateFactory; -import freenet.l10n.BaseL10n; - -/** - * {@link TemplateFactory} implementation that creates {@link Template}s that - * have an {@link L10nFilter} added. - * - * @author David ‘Bombe’ Roden - */ -public class SoneTemplateFactory implements TemplateFactory { - - /** The base template factory. */ - private final TemplateFactory templateFactory; - - /** The L10n filter. */ - private final L10nFilter l10nFilter; - - /** The date filter. */ - private final DateFilter dateFilter; - - /** The reflection accessor. */ - private final ReflectionAccessor reflectionAccessor = new ReflectionAccessor(); - - /** - * Creates a new Freetalk template factory. - * - * @param l10n - * The L10n handler - */ - public SoneTemplateFactory(BaseL10n l10n) { - this(DefaultTemplateFactory.getInstance(), l10n); - } - - /** - * Creates a new Freetalk template factory, retrieving templates from the - * given template factory, then adding all filters used by Freetalk to them. - * - * @param templateFactory - * The base template factory - * @param l10n - * The L10n handler - */ - public SoneTemplateFactory(TemplateFactory templateFactory, BaseL10n l10n) { - this.templateFactory = templateFactory; - this.l10nFilter = new L10nFilter(l10n); - this.dateFilter = new DateFilter(); - } - - /** - * {@inheritDoc} - */ - @Override - public Template createTemplate(Reader templateSource) { - Template template = templateFactory.createTemplate(templateSource); - template.addAccessor(Object.class, reflectionAccessor); - template.addFilter("l10n", l10nFilter); - template.addFilter("date", dateFilter); - return template; - } - - /** - * {@link Filter} implementation replaces {@link String} values with their - * translated equivalents. - * - * @author David ‘Bombe’ Roden - */ - public static class L10nFilter implements Filter { - - /** The l10n handler. */ - private final BaseL10n l10n; - - /** - * Creates a new L10n filter. - * - * @param l10n - * The l10n handler - */ - public L10nFilter(BaseL10n l10n) { - this.l10n = l10n; - } - - /** - * {@inheritDoc} - */ - @Override - public String format(DataProvider dataProvider, Object data, Map parameters) { - return l10n.getString(String.valueOf(data)); - } - - } - - /** - * {@link Filter} implementation that formats a date. The date may be given - * either as a {@link Date} or a {@link Long} object. - * - * @author David ‘Bombe’ Roden - */ - public static class DateFilter implements Filter { - - /** The date formatter. */ - private final DateFormat dateFormat = DateFormat.getInstance(); - - /** - * {@inheritDoc} - */ - @Override - public String format(DataProvider dataProvider, Object data, Map parameters) { - if (data instanceof Date) { - return dateFormat.format((Date) data); - } else if (data instanceof Long) { - return dateFormat.format(new Date((Long) data)); - } - return ""; - } - - } - -} diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index d312029..e85b066 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -29,14 +29,17 @@ import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.freenet.L10nFilter; import net.pterodactylus.sone.main.SonePlugin; import net.pterodactylus.sone.web.page.CSSPage; import net.pterodactylus.sone.web.page.PageToadlet; import net.pterodactylus.sone.web.page.PageToadletFactory; import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.service.AbstractService; +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.TemplateFactory; import freenet.clients.http.LinkEnabledCallback; import freenet.clients.http.SessionManager; import freenet.clients.http.ToadletContainer; @@ -135,7 +138,11 @@ public class WebInterface extends AbstractService { * Register all toadlets. */ private void registerToadlets() { - TemplateFactory templateFactory = new SoneTemplateFactory(l10n()); + DefaultTemplateFactory templateFactory = new DefaultTemplateFactory(); + templateFactory.addAccessor(Object.class, new ReflectionAccessor()); + templateFactory.addFilter("date", new DateFilter()); + templateFactory.addFilter("l10n", new L10nFilter(l10n())); + String formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword(); Template loginTemplate = templateFactory.createTemplate(createReader("/templates/login.html")); -- 2.7.4