Merge branch 'release-0.6.3'
[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.util.Map;
23
24 import net.pterodactylus.sone.core.Core;
25 import net.pterodactylus.sone.data.Sone;
26 import net.pterodactylus.sone.text.FreenetLinkParser;
27 import net.pterodactylus.sone.text.FreenetLinkParserContext;
28 import net.pterodactylus.sone.web.page.Page.Request;
29 import net.pterodactylus.util.template.Filter;
30 import net.pterodactylus.util.template.TemplateContext;
31 import net.pterodactylus.util.template.TemplateContextFactory;
32
33 /**
34  * Filter that filters a given text through a {@link FreenetLinkParser}.
35  *
36  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37  */
38 public class ParserFilter implements Filter {
39
40         /** The core. */
41         private final Core core;
42
43         /** The link parser. */
44         private final FreenetLinkParser linkParser;
45
46         /**
47          * Creates a new filter that runs its input through a
48          * {@link FreenetLinkParser}.
49          *
50          * @param core
51          *            The core
52          * @param templateContextFactory
53          *            The context factory for rendering the parts
54          */
55         public ParserFilter(Core core, TemplateContextFactory templateContextFactory) {
56                 this.core = core;
57                 linkParser = new FreenetLinkParser(core, templateContextFactory);
58         }
59
60         /**
61          * {@inheritDoc}
62          */
63         @Override
64         public Object format(TemplateContext templateContext, Object data, Map<String, String> parameters) {
65                 String text = String.valueOf(data);
66                 String soneKey = parameters.get("sone");
67                 if (soneKey == null) {
68                         soneKey = "sone";
69                 }
70                 Sone sone = (Sone) templateContext.get(soneKey);
71                 if (sone == null) {
72                         sone = core.getSone(soneKey, false);
73                 }
74                 FreenetLinkParserContext context = new FreenetLinkParserContext((Request) templateContext.get("request"), sone);
75                 try {
76                         return linkParser.parse(context, new StringReader(text));
77                 } catch (IOException ioe1) {
78                         /* no exceptions in a StringReader, ignore. */
79                 }
80                 return null;
81         }
82
83 }