2 * Sone - ParserFilter.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.IOException;
21 import java.io.StringReader;
22 import java.io.StringWriter;
23 import java.io.Writer;
26 import net.pterodactylus.sone.core.Core;
27 import net.pterodactylus.sone.data.Sone;
28 import net.pterodactylus.sone.text.FreenetLinkPart;
29 import net.pterodactylus.sone.text.LinkPart;
30 import net.pterodactylus.sone.text.Part;
31 import net.pterodactylus.sone.text.PlainTextPart;
32 import net.pterodactylus.sone.text.PostPart;
33 import net.pterodactylus.sone.text.SonePart;
34 import net.pterodactylus.sone.text.SoneTextParser;
35 import net.pterodactylus.sone.text.SoneTextParserContext;
36 import net.pterodactylus.sone.web.page.Page.Request;
37 import net.pterodactylus.util.template.Filter;
38 import net.pterodactylus.util.template.Template;
39 import net.pterodactylus.util.template.TemplateContext;
40 import net.pterodactylus.util.template.TemplateContextFactory;
41 import net.pterodactylus.util.template.TemplateParser;
44 * Filter that filters a given text through a {@link SoneTextParser}.
46 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48 public class ParserFilter implements Filter {
51 private final Core core;
53 /** The link parser. */
54 private final SoneTextParser soneTextParser;
56 /** The template context factory. */
57 private final TemplateContextFactory templateContextFactory;
59 /** The template for {@link PlainTextPart}s. */
60 private final Template plainTextTemplate = TemplateParser.parse(new StringReader("<%text|html>"));
62 /** The template for {@link FreenetLinkPart}s. */
63 private final Template linkTemplate = TemplateParser.parse(new StringReader("<a class=\"<%cssClass|html>\" href=\"<%link|html>\" title=\"<%title|html>\"><%text|html></a>"));
66 * Creates a new filter that runs its input through a {@link SoneTextParser}
71 * @param templateContextFactory
72 * The context factory for rendering the parts
73 * @param soneTextParser
74 * The Sone text parser
76 public ParserFilter(Core core, TemplateContextFactory templateContextFactory, SoneTextParser soneTextParser) {
78 this.templateContextFactory = templateContextFactory;
79 this.soneTextParser = soneTextParser;
86 public Object format(TemplateContext templateContext, Object data, Map<String, String> parameters) {
87 String text = String.valueOf(data);
88 String soneKey = parameters.get("sone");
89 if (soneKey == null) {
92 Sone sone = (Sone) templateContext.get(soneKey);
94 sone = core.getSone(soneKey, false);
96 Request request = (Request) templateContext.get("request");
97 SoneTextParserContext context = new SoneTextParserContext(request, sone);
98 StringWriter parsedTextWriter = new StringWriter();
100 render(parsedTextWriter, soneTextParser.parse(context, new StringReader(text)));
101 } catch (IOException ioe1) {
102 /* no exceptions in a StringReader or StringWriter, ignore. */
104 return parsedTextWriter.toString();
112 * Renders the given parts.
115 * The writer to render the parts to
117 * The parts to render
119 private void render(Writer writer, Iterable<Part> parts) {
120 for (Part part : parts) {
121 render(writer, part);
126 * Renders the given part.
129 * The writer to render the part to
133 @SuppressWarnings("unchecked")
134 private void render(Writer writer, Part part) {
135 if (part instanceof PlainTextPart) {
136 render(writer, (PlainTextPart) part);
137 } else if (part instanceof FreenetLinkPart) {
138 render(writer, (FreenetLinkPart) part);
139 } else if (part instanceof LinkPart) {
140 render(writer, (LinkPart) part);
141 } else if (part instanceof SonePart) {
142 render(writer, (SonePart) part);
143 } else if (part instanceof PostPart) {
144 render(writer, (PostPart) part);
145 } else if (part instanceof Iterable<?>) {
146 render(writer, (Iterable<Part>) part);
151 * Renders the given plain-text part.
154 * The writer to render the part to
155 * @param plainTextPart
158 private void render(Writer writer, PlainTextPart plainTextPart) {
159 TemplateContext templateContext = templateContextFactory.createTemplateContext();
160 templateContext.set("text", plainTextPart.getText());
161 plainTextTemplate.render(templateContext, writer);
165 * Renders the given freenet link part.
168 * The writer to render the part to
169 * @param freenetLinkPart
172 private void render(Writer writer, FreenetLinkPart freenetLinkPart) {
173 renderLink(writer, "/" + freenetLinkPart.getLink(), freenetLinkPart.getText(), freenetLinkPart.getTitle(), freenetLinkPart.isTrusted() ? "freenet-trusted" : "freenet");
177 * Renders the given link part.
180 * The writer to render the part to
184 private void render(Writer writer, LinkPart linkPart) {
185 renderLink(writer, "/?_CHECKED_HTTP_=" + linkPart.getLink(), linkPart.getText(), linkPart.getTitle(), "internet");
189 * Renders the given Sone part.
192 * The writer to render the part to
196 private void render(Writer writer, SonePart sonePart) {
197 renderLink(writer, "viewSone.html?sone=" + sonePart.getSone().getId(), SoneAccessor.getNiceName(sonePart.getSone()), SoneAccessor.getNiceName(sonePart.getSone()), "in-sone");
201 * Renders the given post part.
204 * The writer to render the part to
208 private void render(Writer writer, PostPart postPart) {
209 renderLink(writer, "viewPost.html?post=" + postPart.getPost().getId(), getExcerpt(postPart.getPost().getText(), 20), SoneAccessor.getNiceName(postPart.getPost().getSone()), "in-sone");
213 * Renders the given link.
216 * The writer to render the link to
220 * The text of the link
222 * The title of the link
224 * The CSS class of the link
226 private void renderLink(Writer writer, String link, String text, String title, String cssClass) {
227 TemplateContext templateContext = templateContextFactory.createTemplateContext();
228 templateContext.set("cssClass", cssClass);
229 templateContext.set("link", link);
230 templateContext.set("text", text);
231 templateContext.set("title", title);
232 linkTemplate.render(templateContext, writer);
240 * Returns up to {@code length} characters from the given text, appending
241 * “…” if the text is longer.
244 * The text to get an excerpt from
246 * The maximum length of the excerpt (without the ellipsis)
247 * @return The excerpt of the text
249 private static String getExcerpt(String text, int length) {
250 if (text.length() > length) {
251 return text.substring(0, length) + "…";