2 * Sone - ImageLinkFilter.java - Copyright © 2011 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.template;
20 import java.io.StringReader;
21 import java.io.StringWriter;
24 import net.pterodactylus.sone.data.Image;
25 import net.pterodactylus.util.number.Numbers;
26 import net.pterodactylus.util.template.Filter;
27 import net.pterodactylus.util.template.Template;
28 import net.pterodactylus.util.template.TemplateContext;
29 import net.pterodactylus.util.template.TemplateContextFactory;
30 import net.pterodactylus.util.template.TemplateParser;
33 * Template filter that turns an {@link Image} into an HTML <img> tag,
34 * using some parameters to influence parameters of the image.
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38 public class ImageLinkFilter implements Filter {
40 /** The template to render for the <img%gt; tag. */
41 private static final Template linkTemplate = TemplateParser.parse(new StringReader("<img<%ifnull !class> class=\"<%class|css>\"<%/if> src=\"<%src|html>\" alt=\"<%alt|html>\" title=\"<%title|html>\" width=\"<%width|html>\" height=\"<%height|html>\" />"));
43 /** The template context factory. */
44 private final TemplateContextFactory templateContextFactory;
47 * Creates a new image link filter.
49 * @param templateContextFactory
50 * The template context factory
52 public ImageLinkFilter(TemplateContextFactory templateContextFactory) {
53 this.templateContextFactory = templateContextFactory;
60 public Object format(TemplateContext templateContext, Object data, Map<String, String> parameters) {
61 Image image = (Image) data;
62 String imageClass = parameters.get("class");
63 int maxWidth = Numbers.safeParseInteger(parameters.get("max-width"), Integer.MAX_VALUE);
64 int maxHeight = Numbers.safeParseInteger(parameters.get("max-height"), Integer.MAX_VALUE);
66 TemplateContext linkTemplateContext = templateContextFactory.createTemplateContext();
67 linkTemplateContext.set("class", imageClass);
68 if (image.isInserted()) {
69 linkTemplateContext.set("src", "/" + image.getKey());
71 linkTemplateContext.set("src", "getImage.html?image=" + image.getId());
73 int imageWidth = image.getWidth();
74 int imageHeight = image.getHeight();
76 if ((imageWidth > maxWidth) || (imageHeight > maxHeight)) {
77 scale = Math.min(maxWidth / (double) imageWidth, maxHeight / (double) imageHeight);
79 linkTemplateContext.set("width", (int) (imageWidth * scale + 0.5));
80 linkTemplateContext.set("height", (int) (imageHeight * scale + 0.5));
81 linkTemplateContext.set("alt", image.getDescription());
82 linkTemplateContext.set("title", image.getTitle());
84 StringWriter stringWriter = new StringWriter();
85 linkTemplate.render(linkTemplateContext, stringWriter);
86 return stringWriter.toString();