Split text parsing and rendering into two filters
[Sone.git] / src / main / java / net / pterodactylus / sone / template / ParserFilter.java
index 477e972..2625441 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - ParserFilter.java - Copyright © 2011 David Roden
+ * Sone - ParserFilter.java - Copyright © 2011–2013 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 package net.pterodactylus.sone.template;
 
+import static java.lang.String.valueOf;
+
 import java.io.IOException;
 import java.io.StringReader;
+import java.util.Collections;
 import java.util.Map;
 
 import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.text.FreenetLinkParser;
-import net.pterodactylus.sone.text.FreenetLinkParserContext;
+import net.pterodactylus.sone.text.Part;
+import net.pterodactylus.sone.text.SoneTextParser;
+import net.pterodactylus.sone.text.SoneTextParserContext;
+import net.pterodactylus.sone.web.page.FreenetRequest;
 import net.pterodactylus.util.template.Filter;
 import net.pterodactylus.util.template.TemplateContext;
-import net.pterodactylus.util.template.TemplateContextFactory;
 
 /**
- * Filter that filters a given text through a {@link FreenetLinkParser}.
+ * Filter that filters a given text through a {@link SoneTextParser} and returns the parsed {@link Part}s.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 public class ParserFilter implements Filter {
 
-       /** The core. */
        private final Core core;
+       private final SoneTextParser soneTextParser;
 
-       /** The link parser. */
-       private final FreenetLinkParser linkParser;
-
-       /**
-        * Creates a new filter that runs its input through a
-        * {@link FreenetLinkParser}.
-        *
-        * @param core
-        *            The core
-        * @param templateContextFactory
-        *            The context factory for rendering the parts
-        */
-       public ParserFilter(Core core, TemplateContextFactory templateContextFactory) {
+       public ParserFilter(Core core, SoneTextParser soneTextParser) {
                this.core = core;
-               linkParser = new FreenetLinkParser(core, templateContextFactory);
+               this.soneTextParser = soneTextParser;
        }
 
        /**
         * {@inheritDoc}
         */
        @Override
-       public Object format(TemplateContext templateContext, Object data, Map<String, String> parameters) {
-               String text = String.valueOf(data);
-               String soneKey = parameters.get("sone");
-               if (soneKey == null) {
-                       soneKey = "sone";
-               }
-               Sone sone = (Sone) templateContext.get(soneKey);
-               if (sone == null) {
-                       sone = core.getSone(soneKey, false);
+       public Object format(TemplateContext templateContext, Object data, Map<String, Object> parameters) {
+               String text = valueOf(data);
+               Object sone = parameters.get("sone");
+               if (sone instanceof String) {
+                       sone = core.getSone((String) sone).orNull();
                }
-               FreenetLinkParserContext context = new FreenetLinkParserContext(sone);
+               FreenetRequest request = (FreenetRequest) templateContext.get("request");
+               SoneTextParserContext context = new SoneTextParserContext(request, (Sone) sone);
                try {
-                       return linkParser.parse(context, new StringReader(text));
+                       return soneTextParser.parse(context, new StringReader(text));
                } catch (IOException ioe1) {
                        /* no exceptions in a StringReader, ignore. */
+                       return Collections.<Part>emptyList();
                }
-               return null;
        }
 
 }