Merge branch 'release-0.6.5'
[Sone.git] / src / main / java / net / pterodactylus / sone / template / ParserFilter.java
1 /*
2  * Sone - ParserFilter.java - Copyright © 2011 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.template;
19
20 import java.io.IOException;
21 import java.io.StringReader;
22 import java.io.StringWriter;
23 import java.io.Writer;
24 import java.util.Map;
25
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;
42
43 /**
44  * Filter that filters a given text through a {@link SoneTextParser}.
45  *
46  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47  */
48 public class ParserFilter implements Filter {
49
50         /** The core. */
51         private final Core core;
52
53         /** The link parser. */
54         private final SoneTextParser soneTextParser;
55
56         /** The template context factory. */
57         private final TemplateContextFactory templateContextFactory;
58
59         /** The template for {@link PlainTextPart}s. */
60         private final Template plainTextTemplate = TemplateParser.parse(new StringReader("<%text|html>"));
61
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>"));
64
65         /**
66          * Creates a new filter that runs its input through a {@link SoneTextParser}
67          * .
68          *
69          * @param core
70          *            The core
71          * @param templateContextFactory
72          *            The context factory for rendering the parts
73          * @param soneTextParser
74          *            The Sone text parser
75          */
76         public ParserFilter(Core core, TemplateContextFactory templateContextFactory, SoneTextParser soneTextParser) {
77                 this.core = core;
78                 this.templateContextFactory = templateContextFactory;
79                 this.soneTextParser = soneTextParser;
80         }
81
82         /**
83          * {@inheritDoc}
84          */
85         @Override
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) {
90                         soneKey = "sone";
91                 }
92                 Sone sone = (Sone) templateContext.get(soneKey);
93                 if (sone == null) {
94                         sone = core.getSone(soneKey, false);
95                 }
96                 Request request = (Request) templateContext.get("request");
97                 SoneTextParserContext context = new SoneTextParserContext(request, sone);
98                 StringWriter parsedTextWriter = new StringWriter();
99                 try {
100                         render(parsedTextWriter, soneTextParser.parse(context, new StringReader(text)));
101                 } catch (IOException ioe1) {
102                         /* no exceptions in a StringReader or StringWriter, ignore. */
103                 }
104                 return parsedTextWriter.toString();
105         }
106
107         //
108         // PRIVATE METHODS
109         //
110
111         /**
112          * Renders the given parts.
113          *
114          * @param writer
115          *            The writer to render the parts to
116          * @param parts
117          *            The parts to render
118          */
119         private void render(Writer writer, Iterable<Part> parts) {
120                 for (Part part : parts) {
121                         render(writer, part);
122                 }
123         }
124
125         /**
126          * Renders the given part.
127          *
128          * @param writer
129          *            The writer to render the part to
130          * @param part
131          *            The part to render
132          */
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);
147                 }
148         }
149
150         /**
151          * Renders the given plain-text part.
152          *
153          * @param writer
154          *            The writer to render the part to
155          * @param plainTextPart
156          *            The part to render
157          */
158         private void render(Writer writer, PlainTextPart plainTextPart) {
159                 TemplateContext templateContext = templateContextFactory.createTemplateContext();
160                 templateContext.set("text", plainTextPart.getText());
161                 plainTextTemplate.render(templateContext, writer);
162         }
163
164         /**
165          * Renders the given freenet link part.
166          *
167          * @param writer
168          *            The writer to render the part to
169          * @param freenetLinkPart
170          *            The part to render
171          */
172         private void render(Writer writer, FreenetLinkPart freenetLinkPart) {
173                 renderLink(writer, "/" + freenetLinkPart.getLink(), freenetLinkPart.getText(), freenetLinkPart.getTitle(), freenetLinkPart.isTrusted() ? "freenet-trusted" : "freenet");
174         }
175
176         /**
177          * Renders the given link part.
178          *
179          * @param writer
180          *            The writer to render the part to
181          * @param linkPart
182          *            The part to render
183          */
184         private void render(Writer writer, LinkPart linkPart) {
185                 renderLink(writer, "/?_CHECKED_HTTP_=" + linkPart.getLink(), linkPart.getText(), linkPart.getTitle(), "internet");
186         }
187
188         /**
189          * Renders the given Sone part.
190          *
191          * @param writer
192          *            The writer to render the part to
193          * @param sonePart
194          *            The part to render
195          */
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");
198         }
199
200         /**
201          * Renders the given post part.
202          *
203          * @param writer
204          *            The writer to render the part to
205          * @param postPart
206          *            The part to render
207          */
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");
210         }
211
212         /**
213          * Renders the given link.
214          *
215          * @param writer
216          *            The writer to render the link to
217          * @param link
218          *            The link to render
219          * @param text
220          *            The text of the link
221          * @param title
222          *            The title of the link
223          * @param cssClass
224          *            The CSS class of the link
225          */
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);
233         }
234
235         //
236         // STATIC METHODS
237         //
238
239         /**
240          * Returns up to {@code length} characters from the given text, appending
241          * “…” if the text is longer.
242          *
243          * @param text
244          *            The text to get an excerpt from
245          * @param length
246          *            The maximum length of the excerpt (without the ellipsis)
247          * @return The excerpt of the text
248          */
249         private static String getExcerpt(String text, int length) {
250                 if (text.length() > length) {
251                         return text.substring(0, length) + "…";
252                 }
253                 return text;
254         }
255
256 }