From 5d18f62236a3771c4efa05d96c2851b95366a7dd Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 16 Jan 2011 20:35:36 +0100 Subject: [PATCH] Add filter that replaces a zero date with a translated text. This is needed for #20. --- .../sone/template/UnknownDateFilter.java | 66 ++++++++++++++++++++++ .../net/pterodactylus/sone/web/WebInterface.java | 2 + 2 files changed, 68 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sone/template/UnknownDateFilter.java diff --git a/src/main/java/net/pterodactylus/sone/template/UnknownDateFilter.java b/src/main/java/net/pterodactylus/sone/template/UnknownDateFilter.java new file mode 100644 index 0000000..4fc95f7 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/template/UnknownDateFilter.java @@ -0,0 +1,66 @@ +/* + * Sone - UnknownDateFilter.java - Copyright © 2011 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.template; + +import java.util.Map; + +import net.pterodactylus.util.template.DataProvider; +import net.pterodactylus.util.template.Filter; +import freenet.l10n.BaseL10n; + +/** + * {@link Filter} implementation that replaces a {@link Long} with a value of + * {@code 0} by a {@link String} from an {@link BaseL10n l10n handler}. + * + * @author David ‘Bombe’ Roden + */ +public class UnknownDateFilter implements Filter { + + /** The l10n handler. */ + private BaseL10n l10nHandler; + + /** The key for the text to show. */ + private final String unknownKey; + + /** + * Creates a new unknown date filter. + * + * @param l10nHandler + * The l10n handler + * @param unknownKey + * The key of the text to show + */ + public UnknownDateFilter(BaseL10n l10nHandler, String unknownKey) { + this.l10nHandler = l10nHandler; + this.unknownKey = unknownKey; + } + + /** + * {@inheritDoc} + */ + @Override + public Object format(DataProvider dataProvider, Object data, Map parameters) { + if (data instanceof Long) { + if ((Long) data == 0) { + return l10nHandler.getString(unknownKey); + } + } + return data; + } + +} diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index 8eb35e3..d08cc17 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -57,6 +57,7 @@ import net.pterodactylus.sone.template.RequestChangeFilter; import net.pterodactylus.sone.template.SoneAccessor; import net.pterodactylus.sone.template.SubstringFilter; import net.pterodactylus.sone.template.TrustAccessor; +import net.pterodactylus.sone.template.UnknownDateFilter; import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage; import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage; import net.pterodactylus.sone.web.ajax.DeletePostAjaxPage; @@ -184,6 +185,7 @@ public class WebInterface implements CoreListener { templateFactory.addFilter("css", new CssClassNameFilter()); templateFactory.addFilter("js", new JavascriptFilter()); templateFactory.addFilter("parse", new ParserFilter(templateFactory)); + templateFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate")); templateFactory.addPlugin("getpage", new GetPagePlugin()); templateFactory.addPlugin("paginate", new PaginationPlugin()); templateFactory.setTemplateProvider(new ClassPathTemplateProvider(templateFactory)); -- 2.7.4