2 * Sone - ParserFilter.java - Copyright © 2011–2013 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 static java.lang.String.valueOf;
21 import static net.pterodactylus.sone.utils.NumberParsers.parseInt;
23 import java.io.IOException;
24 import java.io.StringReader;
25 import java.io.StringWriter;
26 import java.io.UnsupportedEncodingException;
27 import java.io.Writer;
28 import java.net.URLEncoder;
29 import java.util.ArrayList;
30 import java.util.List;
33 import net.pterodactylus.sone.core.Core;
34 import net.pterodactylus.sone.data.Sone;
35 import net.pterodactylus.sone.text.FreenetLinkPart;
36 import net.pterodactylus.sone.text.LinkPart;
37 import net.pterodactylus.sone.text.Part;
38 import net.pterodactylus.sone.text.PlainTextPart;
39 import net.pterodactylus.sone.text.PostPart;
40 import net.pterodactylus.sone.text.SonePart;
41 import net.pterodactylus.sone.text.SoneTextParser;
42 import net.pterodactylus.sone.text.SoneTextParserContext;
43 import net.pterodactylus.sone.web.page.FreenetRequest;
44 import net.pterodactylus.util.template.Filter;
45 import net.pterodactylus.util.template.Template;
46 import net.pterodactylus.util.template.TemplateContext;
47 import net.pterodactylus.util.template.TemplateContextFactory;
48 import net.pterodactylus.util.template.TemplateParser;
51 * Filter that filters a given text through a {@link SoneTextParser}.
53 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
55 public class ParserFilter implements Filter {
58 private final Core core;
60 /** The link parser. */
61 private final SoneTextParser soneTextParser;
63 /** The template context factory. */
64 private final TemplateContextFactory templateContextFactory;
66 /** The template for {@link PlainTextPart}s. */
67 private static final Template plainTextTemplate = TemplateParser.parse(new StringReader("<%text|html>"));
69 /** The template for {@link FreenetLinkPart}s. */
70 private static final Template linkTemplate = TemplateParser.parse(new StringReader("<a class=\"<%cssClass|html>\" href=\"<%link|html>\" title=\"<%title|html>\"><%text|html></a>"));
73 * Creates a new filter that runs its input through a {@link SoneTextParser}
78 * @param templateContextFactory
79 * The context factory for rendering the parts
80 * @param soneTextParser
81 * The Sone text parser
83 public ParserFilter(Core core, TemplateContextFactory templateContextFactory, SoneTextParser soneTextParser) {
85 this.templateContextFactory = templateContextFactory;
86 this.soneTextParser = soneTextParser;
93 public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
94 String text = valueOf(data);
95 int length = parseInt(valueOf(parameters.get("length")), -1);
96 int cutOffLength = parseInt(valueOf(parameters.get("cut-off-length")), length);
97 Object sone = parameters.get("sone");
98 if (sone instanceof String) {
99 sone = core.getSone((String) sone).orNull();
101 FreenetRequest request = (FreenetRequest) templateContext.get("request");
102 SoneTextParserContext context = new SoneTextParserContext(request, (Sone) sone);
103 StringWriter parsedTextWriter = new StringWriter();
105 Iterable<Part> parts = soneTextParser.parse(context, new StringReader(text));
107 int allPartsLength = 0;
108 List<Part> shortenedParts = new ArrayList<Part>();
109 for (Part part : parts) {
110 if (part instanceof PlainTextPart) {
111 String longText = ((PlainTextPart) part).getText();
112 if (allPartsLength < cutOffLength) {
113 if ((allPartsLength + longText.length()) > cutOffLength) {
114 shortenedParts.add(new PlainTextPart(longText.substring(0, cutOffLength - allPartsLength) + "…"));
116 shortenedParts.add(part);
119 allPartsLength += longText.length();
120 } else if (part instanceof LinkPart) {
121 if (allPartsLength < cutOffLength) {
122 shortenedParts.add(part);
124 allPartsLength += ((LinkPart) part).getText().length();
126 if (allPartsLength < cutOffLength) {
127 shortenedParts.add(part);
131 if (allPartsLength >= length) {
132 parts = shortenedParts;
135 render(parsedTextWriter, parts);
136 } catch (IOException ioe1) {
137 /* no exceptions in a StringReader or StringWriter, ignore. */
139 return parsedTextWriter.toString();
147 * Renders the given parts.
150 * The writer to render the parts to
152 * The parts to render
154 private void render(Writer writer, Iterable<Part> parts) {
155 for (Part part : parts) {
156 render(writer, part);
161 * Renders the given part.
164 * The writer to render the part to
168 @SuppressWarnings("unchecked")
169 private void render(Writer writer, Part part) {
170 if (part instanceof PlainTextPart) {
171 render(writer, (PlainTextPart) part);
172 } else if (part instanceof FreenetLinkPart) {
173 render(writer, (FreenetLinkPart) part);
174 } else if (part instanceof LinkPart) {
175 render(writer, (LinkPart) part);
176 } else if (part instanceof SonePart) {
177 render(writer, (SonePart) part);
178 } else if (part instanceof PostPart) {
179 render(writer, (PostPart) part);
180 } else if (part instanceof Iterable<?>) {
181 render(writer, (Iterable<Part>) part);
186 * Renders the given plain-text part.
189 * The writer to render the part to
190 * @param plainTextPart
193 private void render(Writer writer, PlainTextPart plainTextPart) {
194 TemplateContext templateContext = templateContextFactory.createTemplateContext();
195 templateContext.set("text", plainTextPart.getText());
196 plainTextTemplate.render(templateContext, writer);
200 * Renders the given freenet link part.
203 * The writer to render the part to
204 * @param freenetLinkPart
207 private void render(Writer writer, FreenetLinkPart freenetLinkPart) {
208 renderLink(writer, "/" + freenetLinkPart.getLink(), freenetLinkPart.getText(), freenetLinkPart.getTitle(), freenetLinkPart.isTrusted() ? "freenet-trusted" : "freenet");
212 * Renders the given link part.
215 * The writer to render the part to
219 private void render(Writer writer, LinkPart linkPart) {
221 renderLink(writer, "/external-link/?_CHECKED_HTTP_=" + URLEncoder.encode(linkPart.getLink(), "UTF-8"), linkPart.getText(), linkPart.getTitle(), "internet");
222 } catch (UnsupportedEncodingException uee1) {
223 /* not possible for UTF-8. */
224 throw new RuntimeException("The JVM does not support UTF-8 encoding!", uee1);
229 * Renders the given Sone part.
232 * The writer to render the part to
236 private void render(Writer writer, SonePart sonePart) {
237 if ((sonePart.getSone() != null) && (sonePart.getSone().getName() != null)) {
238 renderLink(writer, "viewSone.html?sone=" + sonePart.getSone().getId(), SoneAccessor.getNiceName(sonePart.getSone()), SoneAccessor.getNiceName(sonePart.getSone()), "in-sone");
240 renderLink(writer, "/WebOfTrust/ShowIdentity?id=" + sonePart.getSone().getId(), sonePart.getSone().getId(), sonePart.getSone().getId(), "in-sone");
245 * Renders the given post part.
248 * The writer to render the part to
252 private void render(Writer writer, PostPart postPart) {
253 SoneTextParser parser = new SoneTextParser(core, core);
254 SoneTextParserContext parserContext = new SoneTextParserContext(null, postPart.getPost().getSone());
256 Iterable<Part> parts = parser.parse(parserContext, new StringReader(postPart.getPost().getText()));
257 StringBuilder excerpt = new StringBuilder();
258 for (Part part : parts) {
259 excerpt.append(part.getText());
260 if (excerpt.length() > 20) {
261 int lastSpace = excerpt.lastIndexOf(" ", 20);
262 if (lastSpace > -1) {
263 excerpt.setLength(lastSpace);
265 excerpt.setLength(20);
271 renderLink(writer, "viewPost.html?post=" + postPart.getPost().getId(), excerpt.toString(), SoneAccessor.getNiceName(postPart.getPost().getSone()), "in-sone");
272 } catch (IOException ioe1) {
273 /* StringReader shouldn’t throw. */
278 * Renders the given link.
281 * The writer to render the link to
285 * The text of the link
287 * The title of the link
289 * The CSS class of the link
291 private void renderLink(Writer writer, String link, String text, String title, String cssClass) {
292 TemplateContext templateContext = templateContextFactory.createTemplateContext();
293 templateContext.set("cssClass", cssClass);
294 templateContext.set("link", link);
295 templateContext.set("text", text);
296 templateContext.set("title", title);
297 linkTemplate.render(templateContext, writer);