Add javadoc comments.
[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 textParser;
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          */
74         public ParserFilter(Core core, TemplateContextFactory templateContextFactory) {
75                 this.core = core;
76                 this.templateContextFactory = templateContextFactory;
77                 textParser = new SoneTextParser(core);
78         }
79
80         /**
81          * {@inheritDoc}
82          */
83         @Override
84         public Object format(TemplateContext templateContext, Object data, Map<String, String> parameters) {
85                 String text = String.valueOf(data);
86                 String soneKey = parameters.get("sone");
87                 if (soneKey == null) {
88                         soneKey = "sone";
89                 }
90                 Sone sone = (Sone) templateContext.get(soneKey);
91                 if (sone == null) {
92                         sone = core.getSone(soneKey, false);
93                 }
94                 Request request = (Request) templateContext.get("request");
95                 SoneTextParserContext context = new SoneTextParserContext(request, sone);
96                 StringWriter parsedTextWriter = new StringWriter();
97                 try {
98                         render(parsedTextWriter, textParser.parse(context, new StringReader(text)));
99                 } catch (IOException ioe1) {
100                         /* no exceptions in a StringReader or StringWriter, ignore. */
101                 }
102                 return parsedTextWriter.toString();
103         }
104
105         //
106         // PRIVATE METHODS
107         //
108
109         /**
110          * Renders the given parts.
111          *
112          * @param writer
113          *            The writer to render the parts to
114          * @param parts
115          *            The parts to render
116          */
117         private void render(Writer writer, Iterable<Part> parts) {
118                 for (Part part : parts) {
119                         render(writer, part);
120                 }
121         }
122
123         /**
124          * Renders the given part.
125          *
126          * @param writer
127          *            The writer to render the part to
128          * @param part
129          *            The part to render
130          */
131         @SuppressWarnings("unchecked")
132         private void render(Writer writer, Part part) {
133                 if (part instanceof PlainTextPart) {
134                         render(writer, (PlainTextPart) part);
135                 } else if (part instanceof FreenetLinkPart) {
136                         render(writer, (FreenetLinkPart) part);
137                 } else if (part instanceof LinkPart) {
138                         render(writer, (LinkPart) part);
139                 } else if (part instanceof SonePart) {
140                         render(writer, (SonePart) part);
141                 } else if (part instanceof PostPart) {
142                         render(writer, (PostPart) part);
143                 } else if (part instanceof Iterable<?>) {
144                         render(writer, (Iterable<Part>) part);
145                 }
146         }
147
148         /**
149          * Renders the given plain-text part.
150          *
151          * @param writer
152          *            The writer to render the part to
153          * @param plainTextPart
154          *            The part to render
155          */
156         private void render(Writer writer, PlainTextPart plainTextPart) {
157                 TemplateContext templateContext = templateContextFactory.createTemplateContext();
158                 templateContext.set("text", plainTextPart.getText());
159                 plainTextTemplate.render(templateContext, writer);
160         }
161
162         /**
163          * Renders the given freenet link part.
164          *
165          * @param writer
166          *            The writer to render the part to
167          * @param freenetLinkPart
168          *            The part to render
169          */
170         private void render(Writer writer, FreenetLinkPart freenetLinkPart) {
171                 renderLink(writer, "/" + freenetLinkPart.getLink(), freenetLinkPart.getText(), freenetLinkPart.getTitle(), freenetLinkPart.isTrusted() ? "freenet-trusted" : "freenet");
172         }
173
174         /**
175          * Renders the given link part.
176          *
177          * @param writer
178          *            The writer to render the part to
179          * @param linkPart
180          *            The part to render
181          */
182         private void render(Writer writer, LinkPart linkPart) {
183                 renderLink(writer, "/?_CHECKED_HTTP_=" + linkPart.getLink(), linkPart.getText(), linkPart.getTitle(), "internet");
184         }
185
186         /**
187          * Renders the given Sone part.
188          *
189          * @param writer
190          *            The writer to render the part to
191          * @param sonePart
192          *            The part to render
193          */
194         private void render(Writer writer, SonePart sonePart) {
195                 renderLink(writer, "viewSone.html?sone=" + sonePart.getSone().getId(), SoneAccessor.getNiceName(sonePart.getSone()), SoneAccessor.getNiceName(sonePart.getSone()), "in-sone");
196         }
197
198         /**
199          * Renders the given post part.
200          *
201          * @param writer
202          *            The writer to render the part to
203          * @param postPart
204          *            The part to render
205          */
206         private void render(Writer writer, PostPart postPart) {
207                 renderLink(writer, "viewPost.html?post=" + postPart.getPost().getId(), getExcerpt(postPart.getPost().getText(), 20), SoneAccessor.getNiceName(postPart.getPost().getSone()), "in-sone");
208         }
209
210         /**
211          * Renders the given link.
212          *
213          * @param writer
214          *            The writer to render the link to
215          * @param link
216          *            The link to render
217          * @param text
218          *            The text of the link
219          * @param title
220          *            The title of the link
221          * @param cssClass
222          *            The CSS class of the link
223          */
224         private void renderLink(Writer writer, String link, String text, String title, String cssClass) {
225                 TemplateContext templateContext = templateContextFactory.createTemplateContext();
226                 templateContext.set("cssClass", cssClass);
227                 templateContext.set("link", link);
228                 templateContext.set("text", text);
229                 templateContext.set("title", title);
230                 linkTemplate.render(templateContext, writer);
231         }
232
233         //
234         // STATIC METHODS
235         //
236
237         /**
238          * Returns up to {@code length} characters from the given text, appending
239          * “…” if the text is longer.
240          *
241          * @param text
242          *            The text to get an excerpt from
243          * @param length
244          *            The maximum length of the excerpt (without the ellipsis)
245          * @return The excerpt of the text
246          */
247         private static String getExcerpt(String text, int length) {
248                 if (text.length() > length) {
249                         return text.substring(0, length) + "…";
250                 }
251                 return text;
252         }
253
254 }