From 05f91b3345e9a6fcba39b805a1b34217f7372eee Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 4 Nov 2010 11:49:19 +0100 Subject: [PATCH] Add accessor for Collections. --- .../sone/template/CollectionAccessor.java | 73 ++++++++++++++++++++++ .../net/pterodactylus/sone/web/WebInterface.java | 3 + 2 files changed, 76 insertions(+) create mode 100644 src/main/java/net/pterodactylus/sone/template/CollectionAccessor.java diff --git a/src/main/java/net/pterodactylus/sone/template/CollectionAccessor.java b/src/main/java/net/pterodactylus/sone/template/CollectionAccessor.java new file mode 100644 index 0000000..2b91592 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/template/CollectionAccessor.java @@ -0,0 +1,73 @@ +/* + * Sone - CollectionAccessor.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.template; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.util.template.Accessor; +import net.pterodactylus.util.template.DataProvider; +import net.pterodactylus.util.template.ReflectionAccessor; + +/** + * {@link Accessor} for {@link Collection}s that adds a couple of specialized + * properties that only work for collections that contain the right types. + *
+ *
soneNames
+ *
Returns the nice names of all {@link Sone}s in the collection, sorted + * ascending by their nice names.
+ *
+ * + * @author David ‘Bombe’ Roden + */ +public class CollectionAccessor extends ReflectionAccessor { + + /** + * {@inheritDoc} + */ + @Override + public Object get(DataProvider dataProvider, Object object, String member) { + if (object == null) { + return null; + } + Collection collection = (Collection) object; + if (member.equals("soneNames")) { + List sones = new ArrayList(); + for (Object sone : collection) { + if (!(sone instanceof Sone)) { + continue; + } + sones.add((Sone) sone); + } + Collections.sort(sones, Sone.NICE_NAME_COMPARATOR); + StringBuilder soneNames = new StringBuilder(); + for (Sone sone : sones) { + if (soneNames.length() > 0) { + soneNames.append('\n'); + } + soneNames.append(SoneAccessor.getNiceName(sone)); + } + return soneNames.toString(); + } + return super.get(dataProvider, object, member); + } + +} diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index c518a87..fce52c4 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -24,6 +24,7 @@ import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -35,6 +36,7 @@ import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.L10nFilter; import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.sone.main.SonePlugin; +import net.pterodactylus.sone.template.CollectionAccessor; import net.pterodactylus.sone.template.GetPagePlugin; import net.pterodactylus.sone.template.IdentityAccessor; import net.pterodactylus.sone.template.PostAccessor; @@ -177,6 +179,7 @@ public class WebInterface extends AbstractService { private void registerToadlets() { DefaultTemplateFactory templateFactory = new DefaultTemplateFactory(); templateFactory.addAccessor(Object.class, new ReflectionAccessor()); + templateFactory.addAccessor(Collection.class, new CollectionAccessor()); templateFactory.addAccessor(Sone.class, new SoneAccessor(core())); templateFactory.addAccessor(Post.class, new PostAccessor(core())); templateFactory.addAccessor(Reply.class, new ReplyAccessor(core())); -- 2.7.4