From 2e353c5053ac5b251c021e6326e291686606bf48 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 28 Jul 2012 01:37:10 +0200 Subject: [PATCH] Add accessor for track derivatives. --- .../demoscenemusic/core/TemplateServlet.java | 3 ++ .../template/TrackDerivativeAccessor.java | 47 ++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/main/java/net/pterodactylus/demoscenemusic/template/TrackDerivativeAccessor.java diff --git a/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java b/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java index c1090f2..df753f1 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java @@ -41,9 +41,11 @@ import net.pterodactylus.demoscenemusic.data.Artist; import net.pterodactylus.demoscenemusic.data.Properties; import net.pterodactylus.demoscenemusic.data.Style; import net.pterodactylus.demoscenemusic.data.Track; +import net.pterodactylus.demoscenemusic.data.TrackDerivative; import net.pterodactylus.demoscenemusic.data.User; import net.pterodactylus.demoscenemusic.page.ServletRequest; import net.pterodactylus.demoscenemusic.template.PropertiesAccessor; +import net.pterodactylus.demoscenemusic.template.TrackDerivativeAccessor; import net.pterodactylus.demoscenemusic.template.UserAccessor; import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.io.StreamCopier; @@ -85,6 +87,7 @@ public class TemplateServlet extends HttpServlet { templateContextFactory.addAccessor(Object.class, new ReflectionAccessor()); templateContextFactory.addAccessor(User.class, new UserAccessor()); templateContextFactory.addAccessor(Properties.class, new PropertiesAccessor()); + templateContextFactory.addAccessor(TrackDerivative.class, new TrackDerivativeAccessor()); templateContextFactory.addFilter("html", new HtmlFilter()); CollectionSortFilter sortFilter = new CollectionSortFilter(); diff --git a/src/main/java/net/pterodactylus/demoscenemusic/template/TrackDerivativeAccessor.java b/src/main/java/net/pterodactylus/demoscenemusic/template/TrackDerivativeAccessor.java new file mode 100644 index 0000000..66424f5 --- /dev/null +++ b/src/main/java/net/pterodactylus/demoscenemusic/template/TrackDerivativeAccessor.java @@ -0,0 +1,47 @@ +/* + * DemosceneMusic - TrackDerivativeAccessor.java - Copyright © 2012 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.demoscenemusic.template; + +import net.pterodactylus.demoscenemusic.core.Core; +import net.pterodactylus.demoscenemusic.data.TrackDerivative; +import net.pterodactylus.util.template.ReflectionAccessor; +import net.pterodactylus.util.template.TemplateContext; + +/** + * {@link ReflectionAccessor} implementation that adds “size” and “path” + * properties to a {@link TrackDerivative}. + * + * @author David ‘Bombe’ Roden + */ +public class TrackDerivativeAccessor extends ReflectionAccessor { + + /** + * {@inheritDoc} + */ + @Override + public Object get(TemplateContext templateContext, Object object, String member) { + TrackDerivative trackDerivative = (TrackDerivative) object; + if ("path".equals(member)) { + return ((Core) templateContext.get("core")).getDataDirectory().getPath(trackDerivative.getId()); + } else if ("size".equals(member)) { + return ((Core) templateContext.get("core")).getDataDirectory().getFile(trackDerivative.getId()).length(); + } + return super.get(templateContext, object, member); + } + +} -- 2.7.4