Change code to use new way to specify filter parameters.
[Sone.git] / src / main / java / net / pterodactylus / sone / template / ImageLinkFilter.java
index d7b5eea..06a11d7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - ImageLinkFilter.java - Copyright © 2011 David Roden
+ * Sone - ImageLinkFilter.java - Copyright © 2011–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
@@ -21,6 +21,7 @@ import java.io.StringReader;
 import java.io.StringWriter;
 import java.util.Map;
 
+import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.util.number.Numbers;
 import net.pterodactylus.util.object.Default;
@@ -41,16 +42,22 @@ public class ImageLinkFilter implements Filter {
        /** The template to render for the <img> tag. */
        private static final Template linkTemplate = TemplateParser.parse(new StringReader("<img<%ifnull !class> class=\"<%class|css>\"<%/if> src=\"<%src|html><%if forceDownload>?forcedownload=true<%/if>\" alt=\"<%alt|html>\" title=\"<%title|html>\" width=\"<%width|html>\" height=\"<%height|html>\" style=\"position: relative;<%ifnull ! top>top: <% top|html>;<%/if><%ifnull ! left>left: <% left|html>;<%/if>\"/>"));
 
+       /** The core. */
+       private final Core core;
+
        /** The template context factory. */
        private final TemplateContextFactory templateContextFactory;
 
        /**
         * Creates a new image link filter.
         *
+        * @param core
+        *            The core
         * @param templateContextFactory
         *            The template context factory
         */
-       public ImageLinkFilter(TemplateContextFactory templateContextFactory) {
+       public ImageLinkFilter(Core core, TemplateContextFactory templateContextFactory) {
+               this.core = core;
                this.templateContextFactory = templateContextFactory;
        }
 
@@ -58,16 +65,21 @@ public class ImageLinkFilter implements Filter {
         * {@inheritDoc}
         */
        @Override
-       public Object format(TemplateContext templateContext, Object data, Map<String, String> parameters) {
-               Image image = (Image) data;
-               String imageClass = parameters.get("class");
+       public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
+               Image image = null;
+               if (data instanceof String) {
+                       image = core.getImage((String) data, false);
+               } else if (data instanceof Image) {
+                       image = (Image) data;
+               }
+               if (image == null) {
+                       return null;
+               }
+               String imageClass = String.valueOf(parameters.get("class"));
                int maxWidth = Numbers.safeParseInteger(parameters.get("max-width"), Integer.MAX_VALUE);
                int maxHeight = Numbers.safeParseInteger(parameters.get("max-height"), Integer.MAX_VALUE);
                String mode = String.valueOf(parameters.get("mode"));
-               String title = parameters.get("title");
-               if ((title != null) && title.startsWith("=")) {
-                       title = String.valueOf(templateContext.get(title.substring(1)));
-               }
+               String title = String.valueOf(parameters.get("title"));
 
                TemplateContext linkTemplateContext = templateContextFactory.createTemplateContext();
                linkTemplateContext.set("class", imageClass);