Add filter that replaces a zero date with a translated text.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 16 Jan 2011 19:35:36 +0000 (20:35 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 16 Jan 2011 19:35:36 +0000 (20:35 +0100)
This is needed for #20.

src/main/java/net/pterodactylus/sone/template/UnknownDateFilter.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/web/WebInterface.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 (file)
index 0000000..4fc95f7
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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<String, String> parameters) {
+               if (data instanceof Long) {
+                       if ((Long) data == 0) {
+                               return l10nHandler.getString(unknownKey);
+                       }
+               }
+               return data;
+       }
+
+}
index 8eb35e3..d08cc17 100644 (file)
@@ -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));