Rewriter Sone text parser to separate the parser from the generated HTML.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 8 Jun 2011 09:12:59 +0000 (11:12 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 8 Jun 2011 09:12:59 +0000 (11:12 +0200)
15 files changed:
src/main/java/net/pterodactylus/sone/template/ParserFilter.java
src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java [deleted file]
src/main/java/net/pterodactylus/sone/text/FreenetLinkParserContext.java [deleted file]
src/main/java/net/pterodactylus/sone/text/FreenetLinkPart.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/text/LinkPart.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/text/Parser.java
src/main/java/net/pterodactylus/sone/text/Part.java
src/main/java/net/pterodactylus/sone/text/PartContainer.java
src/main/java/net/pterodactylus/sone/text/PlainTextPart.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/text/PostPart.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/text/SonePart.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/text/SoneTextParser.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/text/SoneTextParserContext.java [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/text/TemplatePart.java [deleted file]
src/test/java/net/pterodactylus/sone/text/FreenetLinkParserTest.java [deleted file]

index db945b5..02d12f0 100644 (file)
@@ -19,19 +19,29 @@ package net.pterodactylus.sone.template;
 
 import java.io.IOException;
 import java.io.StringReader;
+import java.io.StringWriter;
+import java.io.Writer;
 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.FreenetLinkPart;
+import net.pterodactylus.sone.text.LinkPart;
+import net.pterodactylus.sone.text.Part;
+import net.pterodactylus.sone.text.PlainTextPart;
+import net.pterodactylus.sone.text.PostPart;
+import net.pterodactylus.sone.text.SonePart;
+import net.pterodactylus.sone.text.SoneTextParser;
+import net.pterodactylus.sone.text.SoneTextParserContext;
 import net.pterodactylus.sone.web.page.Page.Request;
 import net.pterodactylus.util.template.Filter;
+import net.pterodactylus.util.template.Template;
 import net.pterodactylus.util.template.TemplateContext;
 import net.pterodactylus.util.template.TemplateContextFactory;
+import net.pterodactylus.util.template.TemplateParser;
 
 /**
- * Filter that filters a given text through a {@link FreenetLinkParser}.
+ * Filter that filters a given text through a {@link SoneTextParser}.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
@@ -41,11 +51,20 @@ public class ParserFilter implements Filter {
        private final Core core;
 
        /** The link parser. */
-       private final FreenetLinkParser linkParser;
+       private final SoneTextParser textParser;
+
+       /** The template context factory. */
+       private final TemplateContextFactory templateContextFactory;
+
+       /** The template for {@link PlainTextPart}s. */
+       private final Template plainTextTemplate = TemplateParser.parse(new StringReader("<%text|html>"));
+
+       /** The template for {@link FreenetLinkPart}s. */
+       private final Template linkTemplate = TemplateParser.parse(new StringReader("<a class=\"<%cssClass|html>\" href=\"<%link|html>\" title=\"<%title|html>\"><%text|html></a>"));
 
        /**
-        * Creates a new filter that runs its input through a
-        * {@link FreenetLinkParser}.
+        * Creates a new filter that runs its input through a {@link SoneTextParser}
+        * .
         *
         * @param core
         *            The core
@@ -54,7 +73,8 @@ public class ParserFilter implements Filter {
         */
        public ParserFilter(Core core, TemplateContextFactory templateContextFactory) {
                this.core = core;
-               linkParser = new FreenetLinkParser(core, templateContextFactory);
+               this.templateContextFactory = templateContextFactory;
+               textParser = new SoneTextParser(core);
        }
 
        /**
@@ -71,13 +91,75 @@ public class ParserFilter implements Filter {
                if (sone == null) {
                        sone = core.getSone(soneKey, false);
                }
-               FreenetLinkParserContext context = new FreenetLinkParserContext((Request) templateContext.get("request"), sone);
+               Request request = (Request) templateContext.get("request");
+               SoneTextParserContext context = new SoneTextParserContext(request, sone);
+               StringWriter parsedTextWriter = new StringWriter();
                try {
-                       return linkParser.parse(context, new StringReader(text));
+                       render(parsedTextWriter, textParser.parse(context, new StringReader(text)));
                } catch (IOException ioe1) {
-                       /* no exceptions in a StringReader, ignore. */
+                       /* no exceptions in a StringReader or StringWriter, ignore. */
+               }
+               return parsedTextWriter.toString();
+       }
+
+       //
+       // PRIVATE METHODS
+       //
+
+       private void render(Writer writer, Iterable<Part> parts) throws IOException {
+               for (Part part : parts) {
+                       render(writer, part);
+               }
+       }
+
+       private void render(Writer writer, Part part) throws IOException {
+               if (part instanceof PlainTextPart) {
+                       render(writer, (PlainTextPart) part);
+               } else if (part instanceof FreenetLinkPart) {
+                       render(writer, (FreenetLinkPart) part);
+               }
+       }
+
+       private void render(Writer writer, PlainTextPart plainTextPart) throws IOException {
+               TemplateContext templateContext = templateContextFactory.createTemplateContext();
+               templateContext.set("text", plainTextPart.getText());
+               plainTextTemplate.render(templateContext, writer);
+       }
+
+       private void render(Writer writer, FreenetLinkPart freenetLinkPart) throws IOException {
+               renderLink(writer, "/" + freenetLinkPart.getLink(), freenetLinkPart.getText(), freenetLinkPart.getTitle(), freenetLinkPart.isTrusted() ? "freenet-trusted" : "freenet");
+       }
+
+       private void render(Writer writer, LinkPart linkPart) throws IOException {
+               renderLink(writer, "/?_CHECKED_HTTP_=" + linkPart.getLink(), linkPart.getText(), linkPart.getTitle(), "internet");
+       }
+
+       private void render(Writer writer, SonePart sonePart) throws IOException {
+               renderLink(writer, "viewSone.html?sone=" + sonePart.getSone().getId(), SoneAccessor.getNiceName(sonePart.getSone()), SoneAccessor.getNiceName(sonePart.getSone()), "in-sone");
+       }
+
+       private void render(Writer writer, PostPart postPart) throws IOException {
+               renderLink(writer, "viewPost.html?post=" + postPart.getPost().getId(), getExcerpt(postPart.getPost().getText(), 20), SoneAccessor.getNiceName(postPart.getPost().getSone()), "in-sone");
+       }
+
+       private void renderLink(Writer writer, String link, String text, String title, String cssClass) {
+               TemplateContext templateContext = templateContextFactory.createTemplateContext();
+               templateContext.set("cssClass", cssClass);
+               templateContext.set("link", link);
+               templateContext.set("text", text);
+               templateContext.set("title", title);
+               linkTemplate.render(templateContext, writer);
+       }
+
+       //
+       // STATIC METHODS
+       //
+
+       private static String getExcerpt(String text, int length) {
+               if (text.length() > length) {
+                       return text.substring(0, length) + "…";
                }
-               return null;
+               return text;
        }
 
 }
diff --git a/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java b/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java
deleted file mode 100644 (file)
index 770a484..0000000
+++ /dev/null
@@ -1,394 +0,0 @@
-/*
- * Sone - FreenetLinkParser.java - Copyright © 2010 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
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.text;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.Reader;
-import java.io.StringReader;
-import java.net.MalformedURLException;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import net.pterodactylus.sone.core.Core;
-import net.pterodactylus.sone.data.Post;
-import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.template.SoneAccessor;
-import net.pterodactylus.util.logging.Logging;
-import net.pterodactylus.util.template.TemplateContextFactory;
-import net.pterodactylus.util.template.TemplateParser;
-import freenet.keys.FreenetURI;
-
-/**
- * {@link Parser} implementation that can recognize Freenet URIs.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class FreenetLinkParser implements Parser<FreenetLinkParserContext> {
-
-       /** The logger. */
-       private static final Logger logger = Logging.getLogger(FreenetLinkParser.class);
-
-       /** Pattern to detect whitespace. */
-       private static final Pattern whitespacePattern = Pattern.compile("[\\u000a\u0020\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u200c\u200d\u202f\u205f\u2060\u2800\u3000]");
-
-       /**
-        * Enumeration for all recognized link types.
-        *
-        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
-        */
-       private enum LinkType {
-
-               /** Link is a KSK. */
-               KSK,
-
-               /** Link is a CHK. */
-               CHK,
-
-               /** Link is an SSK. */
-               SSK,
-
-               /** Link is a USK. */
-               USK,
-
-               /** Link is HTTP. */
-               HTTP,
-
-               /** Link is HTTPS. */
-               HTTPS,
-
-               /** Link is a Sone. */
-               SONE,
-
-               /** Link is a post. */
-               POST,
-
-       }
-
-       /** The core. */
-       private final Core core;
-
-       /** The template factory. */
-       private final TemplateContextFactory templateContextFactory;
-
-       /**
-        * Creates a new freenet link parser.
-        *
-        * @param core
-        *            The core
-        * @param templateContextFactory
-        *            The template context factory
-        */
-       public FreenetLinkParser(Core core, TemplateContextFactory templateContextFactory) {
-               this.core = core;
-               this.templateContextFactory = templateContextFactory;
-       }
-
-       //
-       // PART METHODS
-       //
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public Part parse(FreenetLinkParserContext context, Reader source) throws IOException {
-               PartContainer parts = new PartContainer();
-               BufferedReader bufferedReader = (source instanceof BufferedReader) ? (BufferedReader) source : new BufferedReader(source);
-               String line;
-               boolean lastLineEmpty = true;
-               int emptyLines = 0;
-               while ((line = bufferedReader.readLine()) != null) {
-                       if (line.trim().length() == 0) {
-                               if (lastLineEmpty) {
-                                       continue;
-                               }
-                               parts.add(createPlainTextPart("\n"));
-                               ++emptyLines;
-                               lastLineEmpty = emptyLines == 2;
-                               continue;
-                       }
-                       emptyLines = 0;
-                       boolean lineComplete = true;
-                       while (line.length() > 0) {
-                               int nextKsk = line.indexOf("KSK@");
-                               int nextChk = line.indexOf("CHK@");
-                               int nextSsk = line.indexOf("SSK@");
-                               int nextUsk = line.indexOf("USK@");
-                               int nextHttp = line.indexOf("http://");
-                               int nextHttps = line.indexOf("https://");
-                               int nextSone = line.indexOf("sone://");
-                               int nextPost = line.indexOf("post://");
-                               if ((nextKsk == -1) && (nextChk == -1) && (nextSsk == -1) && (nextUsk == -1) && (nextHttp == -1) && (nextHttps == -1) && (nextSone == -1) && (nextPost == -1)) {
-                                       if (lineComplete && !lastLineEmpty) {
-                                               parts.add(createPlainTextPart("\n" + line));
-                                       } else {
-                                               parts.add(createPlainTextPart(line));
-                                       }
-                                       break;
-                               }
-                               int next = Integer.MAX_VALUE;
-                               LinkType linkType = null;
-                               if ((nextKsk > -1) && (nextKsk < next)) {
-                                       next = nextKsk;
-                                       linkType = LinkType.KSK;
-                               }
-                               if ((nextChk > -1) && (nextChk < next)) {
-                                       next = nextChk;
-                                       linkType = LinkType.CHK;
-                               }
-                               if ((nextSsk > -1) && (nextSsk < next)) {
-                                       next = nextSsk;
-                                       linkType = LinkType.SSK;
-                               }
-                               if ((nextUsk > -1) && (nextUsk < next)) {
-                                       next = nextUsk;
-                                       linkType = LinkType.USK;
-                               }
-                               if ((nextHttp > -1) && (nextHttp < next)) {
-                                       next = nextHttp;
-                                       linkType = LinkType.HTTP;
-                               }
-                               if ((nextHttps > -1) && (nextHttps < next)) {
-                                       next = nextHttps;
-                                       linkType = LinkType.HTTPS;
-                               }
-                               if ((nextSone > -1) && (nextSone < next)) {
-                                       next = nextSone;
-                                       linkType = LinkType.SONE;
-                               }
-                               if ((nextPost > -1) && (nextPost < next)) {
-                                       next = nextPost;
-                                       linkType = LinkType.POST;
-                               }
-                               if (linkType == LinkType.SONE) {
-                                       if (next > 0) {
-                                               parts.add(createPlainTextPart(line.substring(0, next)));
-                                       }
-                                       if (line.length() >= (next + 7 + 43)) {
-                                               String soneId = line.substring(next + 7, next + 50);
-                                               Sone sone = core.getSone(soneId, false);
-                                               if (sone != null) {
-                                                       parts.add(createInSoneLinkPart("viewSone.html?sone=" + soneId, SoneAccessor.getNiceName(sone)));
-                                               } else {
-                                                       parts.add(createPlainTextPart(line.substring(next, next + 50)));
-                                               }
-                                               line = line.substring(next + 50);
-                                       } else {
-                                               parts.add(createPlainTextPart(line.substring(next)));
-                                               line = "";
-                                       }
-                                       continue;
-                               }
-                               if (linkType == LinkType.POST) {
-                                       if (next > 0) {
-                                               parts.add(createPlainTextPart(line.substring(0, next)));
-                                       }
-                                       if (line.length() >= (next + 7 + 36)) {
-                                               String postId = line.substring(next + 7, next + 43);
-                                               Post post = core.getPost(postId, false);
-                                               if ((post != null) && (post.getSone() != null)) {
-                                                       String postText = post.getText();
-                                                       postText = postText.substring(0, Math.min(postText.length(), 20)) + "…";
-                                                       Sone postSone = post.getSone();
-                                                       parts.add(createInSoneLinkPart("viewPost.html?post=" + postId, postText, (postSone == null) ? postText : SoneAccessor.getNiceName(post.getSone())));
-                                               } else {
-                                                       parts.add(createPlainTextPart(line.substring(next, next + 43)));
-                                               }
-                                               line = line.substring(next + 43);
-                                       } else {
-                                               parts.add(createPlainTextPart(line.substring(next)));
-                                               line = "";
-                                       }
-                                       continue;
-                               }
-                               if ((next >= 8) && (line.substring(next - 8, next).equals("freenet:"))) {
-                                       next -= 8;
-                                       line = line.substring(0, next) + line.substring(next + 8);
-                               }
-                               Matcher matcher = whitespacePattern.matcher(line);
-                               int nextSpace = matcher.find(next) ? matcher.start() : line.length();
-                               if (nextSpace > (next + 4)) {
-                                       if (!lastLineEmpty && lineComplete) {
-                                               parts.add(createPlainTextPart("\n" + line.substring(0, next)));
-                                       } else {
-                                               parts.add(createPlainTextPart(line.substring(0, next)));
-                                       }
-                                       String link = line.substring(next, nextSpace);
-                                       String name = link;
-                                       logger.log(Level.FINER, "Found link: %s", link);
-                                       logger.log(Level.FINEST, "Next: %d, CHK: %d, SSK: %d, USK: %d", new Object[] { next, nextChk, nextSsk, nextUsk });
-
-                                       if ((linkType == LinkType.KSK) || (linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) {
-                                               FreenetURI uri;
-                                               if (name.indexOf('?') > -1) {
-                                                       name = name.substring(0, name.indexOf('?'));
-                                               }
-                                               if (name.endsWith("/")) {
-                                                       name = name.substring(0, name.length() - 1);
-                                               }
-                                               try {
-                                                       uri = new FreenetURI(name);
-                                                       name = uri.lastMetaString();
-                                                       if (name == null) {
-                                                               name = uri.getDocName();
-                                                       }
-                                                       if (name == null) {
-                                                               name = link.substring(0, Math.min(9, link.length()));
-                                                       }
-                                                       boolean fromPostingSone = ((linkType == LinkType.SSK) || (linkType == LinkType.USK)) && link.substring(4, Math.min(link.length(), 47)).equals(context.getPostingSone().getId());
-                                                       parts.add(fromPostingSone ? createTrustedFreenetLinkPart(link, name) : createFreenetLinkPart(link, name));
-                                               } catch (MalformedURLException mue1) {
-                                                       /* not a valid link, insert as plain text. */
-                                                       parts.add(createPlainTextPart(link));
-                                               } catch (NullPointerException npe1) {
-                                                       /* FreenetURI sometimes throws these, too. */
-                                                       parts.add(createPlainTextPart(link));
-                                               } catch (ArrayIndexOutOfBoundsException aioobe1) {
-                                                       /* oh, and these, too. */
-                                                       parts.add(createPlainTextPart(link));
-                                               }
-                                       } else if ((linkType == LinkType.HTTP) || (linkType == LinkType.HTTPS)) {
-                                               name = link.substring(linkType == LinkType.HTTP ? 7 : 8);
-                                               int firstSlash = name.indexOf('/');
-                                               int lastSlash = name.lastIndexOf('/');
-                                               if ((lastSlash - firstSlash) > 3) {
-                                                       name = name.substring(0, firstSlash + 1) + "…" + name.substring(lastSlash);
-                                               }
-                                               if (name.endsWith("/")) {
-                                                       name = name.substring(0, name.length() - 1);
-                                               }
-                                               if (((name.indexOf('/') > -1) && (name.indexOf('.') < name.lastIndexOf('.', name.indexOf('/'))) || ((name.indexOf('/') == -1) && (name.indexOf('.') < name.lastIndexOf('.')))) && name.startsWith("www.")) {
-                                                       name = name.substring(4);
-                                               }
-                                               if (name.indexOf('?') > -1) {
-                                                       name = name.substring(0, name.indexOf('?'));
-                                               }
-                                               link = "?_CHECKED_HTTP_=" + link;
-                                               parts.add(createInternetLinkPart(link, name));
-                                       }
-                                       line = line.substring(nextSpace);
-                               } else {
-                                       if (!lastLineEmpty && lineComplete) {
-                                               parts.add(createPlainTextPart("\n" + line.substring(0, next + 4)));
-                                       } else {
-                                               parts.add(createPlainTextPart(line.substring(0, next + 4)));
-                                       }
-                                       line = line.substring(next + 4);
-                               }
-                               lineComplete = false;
-                       }
-                       lastLineEmpty = false;
-               }
-               for (int partIndex = parts.size() - 1; partIndex >= 0; --partIndex) {
-                       if (!parts.getPart(partIndex).toString().equals("\n")) {
-                               break;
-                       }
-                       parts.removePart(partIndex);
-               }
-               return parts;
-       }
-
-       //
-       // PRIVATE METHODS
-       //
-
-       /**
-        * Creates a new plain text part based on a template.
-        *
-        * @param text
-        *            The text to display
-        * @return The part that displays the given text
-        */
-       private Part createPlainTextPart(String text) {
-               return new TemplatePart(templateContextFactory, TemplateParser.parse(new StringReader("<% text|html>"))).set("text", text);
-       }
-
-       /**
-        * Creates a new part based on a template that links to a site within the
-        * normal internet.
-        *
-        * @param link
-        *            The target of the link
-        * @param name
-        *            The name of the link
-        * @return The part that displays the link
-        */
-       private Part createInternetLinkPart(String link, String name) {
-               return new TemplatePart(templateContextFactory, TemplateParser.parse(new StringReader("<a class=\"internet\" href=\"/<% link|html>\" title=\"<% link|html>\"><% name|html></a>"))).set("link", link).set("name", name);
-       }
-
-       /**
-        * Creates a new part based on a template that links to a site within
-        * freenet.
-        *
-        * @param link
-        *            The target of the link
-        * @param name
-        *            The name of the link
-        * @return The part that displays the link
-        */
-       private Part createFreenetLinkPart(String link, String name) {
-               return new TemplatePart(templateContextFactory, TemplateParser.parse(new StringReader("<a class=\"freenet\" href=\"/<% link|html>\" title=\"<% link|html>\"><% name|html></a>"))).set("link", link).set("name", name);
-       }
-
-       /**
-        * Creates a new part based on a template that links to a page in the
-        * poster’s keyspace.
-        *
-        * @param link
-        *            The target of the link
-        * @param name
-        *            The name of the link
-        * @return The part that displays the link
-        */
-       private Part createTrustedFreenetLinkPart(String link, String name) {
-               return new TemplatePart(templateContextFactory, TemplateParser.parse(new StringReader("<a class=\"freenet-trusted\" href=\"/<% link|html>\" title=\"<% link|html>\"><% name|html></a>"))).set("link", link).set("name", name);
-       }
-
-       /**
-        * Creates a new part based on a template that links to a page in Sone.
-        *
-        * @param link
-        *            The target of the link
-        * @param name
-        *            The name of the link
-        * @return The part that displays the link
-        */
-       private Part createInSoneLinkPart(String link, String name) {
-               return createInSoneLinkPart(link, name, name);
-       }
-
-       /**
-        * Creates a new part based on a template that links to a page in Sone.
-        *
-        * @param link
-        *            The target of the link
-        * @param name
-        *            The name of the link
-        * @param title
-        *            The title attribute of the link
-        * @return The part that displays the link
-        */
-       private Part createInSoneLinkPart(String link, String name, String title) {
-               return new TemplatePart(templateContextFactory, TemplateParser.parse(new StringReader("<a class=\"in-sone\" href=\"<%link|html>\" title=\"<%title|html>\"><%name|html></a>"))).set("link", link).set("name", name).set("title", title);
-       }
-
-}
diff --git a/src/main/java/net/pterodactylus/sone/text/FreenetLinkParserContext.java b/src/main/java/net/pterodactylus/sone/text/FreenetLinkParserContext.java
deleted file mode 100644 (file)
index 0712fc0..0000000
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * Sone - FreenetLinkParserContext.java - Copyright © 2011 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
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.text;
-
-import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.web.page.Page.Request;
-
-/**
- * {@link ParserContext} implementation for the {@link FreenetLinkParser}. It
- * stores the {@link Sone} that provided the parsed text so that certain links
- * can be marked in a different way.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class FreenetLinkParserContext implements ParserContext {
-
-       /** The request being processed. */
-       private final Request request;
-
-       /** The posting Sone. */
-       private final Sone postingSone;
-
-       /**
-        * Creates a new link parser context.
-        *
-        * @param request
-        *            The request being processed
-        * @param postingSone
-        *            The posting Sone
-        */
-       public FreenetLinkParserContext(Request request, Sone postingSone) {
-               this.request = request;
-               this.postingSone = postingSone;
-       }
-
-       /**
-        * Returns the request that is currently being processed.
-        *
-        * @return The request being processed
-        */
-       public Request getRequest() {
-               return request;
-       }
-
-       /**
-        * Returns the Sone that provided the text that is being parsed.
-        *
-        * @return The posting Sone
-        */
-       public Sone getPostingSone() {
-               return postingSone;
-       }
-
-}
diff --git a/src/main/java/net/pterodactylus/sone/text/FreenetLinkPart.java b/src/main/java/net/pterodactylus/sone/text/FreenetLinkPart.java
new file mode 100644 (file)
index 0000000..2efb380
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Sone - FreenetLinkPart.java - Copyright © 2011 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.text;
+
+/**
+ * TODO
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class FreenetLinkPart extends LinkPart {
+
+       private final boolean trusted;
+
+       public FreenetLinkPart(String link, String text, boolean trusted) {
+               this(link, text, text, trusted);
+       }
+
+       public FreenetLinkPart(String link, String text, String title, boolean trusted) {
+               super(link, text, title);
+               this.trusted = trusted;
+       }
+
+       public boolean isTrusted() {
+               return trusted;
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/sone/text/LinkPart.java b/src/main/java/net/pterodactylus/sone/text/LinkPart.java
new file mode 100644 (file)
index 0000000..d911bbd
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Sone - LinkPart.java - Copyright © 2011 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.text;
+
+/**
+ * TODO
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class LinkPart implements Part {
+
+       private final String link;
+       private final String text;
+       private final String title;
+
+       public LinkPart(String link, String text) {
+               this(link, text, text);
+       }
+
+       public LinkPart(String link, String text, String title) {
+               this.link = link;
+               this.text = text;
+               this.title = title;
+       }
+
+       //
+       // ACCESSORS
+       //
+
+       public String getLink() {
+               return link;
+       }
+
+       public String getText() {
+               return text;
+       }
+
+       public String getTitle() {
+               return title;
+       }
+
+}
index e43ed47..3fef8ae 100644 (file)
@@ -41,6 +41,6 @@ public interface Parser<C extends ParserContext> {
         * @throws IOException
         *             if an I/O error occurs
         */
-       public Part parse(C context, Reader source) throws IOException;
+       public Iterable<Part> parse(C context, Reader source) throws IOException;
 
 }
index b9083b7..9c07e3d 100644 (file)
 
 package net.pterodactylus.sone.text;
 
-import net.pterodactylus.util.io.Renderable;
-
 /**
  * A part is a single piece of information that can be displayed as a single
  * element.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public interface Part extends Renderable {
-
-       /* all required methods are inherited from {@link Renderable}. */
+public interface Part {
 
 }
index d52658e..34383a5 100644 (file)
@@ -20,8 +20,12 @@ package net.pterodactylus.sone.text;
 import java.io.IOException;
 import java.io.StringWriter;
 import java.io.Writer;
+import java.util.ArrayDeque;
 import java.util.ArrayList;
+import java.util.Deque;
+import java.util.Iterator;
 import java.util.List;
+import java.util.NoSuchElementException;
 
 /**
  * Part implementation that can contain an arbitrary amount of other parts.
@@ -30,7 +34,7 @@ import java.util.List;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public class PartContainer implements Part {
+public class PartContainer implements Iterable<Part> {
 
        /** The parts to render. */
        private final List<Part> parts = new ArrayList<Part>();
@@ -80,35 +84,69 @@ public class PartContainer implements Part {
        }
 
        //
-       // PART METHODS
+       // ITERABLE METHODS
        //
 
        /**
         * {@inheritDoc}
         */
        @Override
-       public void render(Writer writer) throws IOException {
-               for (Part part : parts) {
-                       part.render(writer);
-               }
-       }
+       @SuppressWarnings("synthetic-access")
+       public Iterator<Part> iterator() {
+               return new Iterator<Part>() {
 
-       //
-       // OBJECT METHODS
-       //
+                       private Deque<Iterator<Part>> partStack = new ArrayDeque<Iterator<Part>>();
+                       private Part nextPart;
+                       private boolean foundNextPart;
+                       private boolean noNextPart;
 
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public String toString() {
-               StringWriter stringWriter = new StringWriter();
-               try {
-                       render(stringWriter);
-               } catch (IOException ioe1) {
-                       /* should never throw, ignore. */
-               }
-               return stringWriter.toString();
+                       {
+                               partStack.push(parts.iterator());
+                       }
+
+                       private void findNext() {
+                               if (foundNextPart) {
+                                       return;
+                               }
+                               noNextPart = true;
+                               while (!partStack.isEmpty()) {
+                                       Iterator<Part> parts = partStack.pop();
+                                       if (parts.hasNext()) {
+                                               nextPart = parts.next();
+                                               partStack.push(parts);
+                                               if (nextPart instanceof PartContainer) {
+                                                       partStack.push(((PartContainer) nextPart).iterator());
+                                               } else {
+                                                       noNextPart = false;
+                                                       break;
+                                               }
+                                       }
+                               }
+                               foundNextPart = true;
+                       }
+
+                       @Override
+                       public boolean hasNext() {
+                               findNext();
+                               return !noNextPart;
+                       }
+
+                       @Override
+                       public Part next() {
+                               findNext();
+                               if (noNextPart) {
+                                       throw new NoSuchElementException();
+                               }
+                               foundNextPart = false;
+                               return nextPart;
+                       }
+
+                       @Override
+                       public void remove() {
+                               /* ignore. */
+                       }
+
+               };
        }
 
 }
diff --git a/src/main/java/net/pterodactylus/sone/text/PlainTextPart.java b/src/main/java/net/pterodactylus/sone/text/PlainTextPart.java
new file mode 100644 (file)
index 0000000..4870bc4
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Sone - PlainTextPart.java - Copyright © 2011 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.text;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Iterator;
+
+import net.pterodactylus.util.collection.ObjectIterator;
+
+/**
+ * TODO
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class PlainTextPart implements Part {
+
+       private final String text;
+
+       public PlainTextPart(String text) {
+               this.text = text;
+       }
+
+       //
+       // ACCESSORS
+       //
+
+       public String getText() {
+               return text;
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/sone/text/PostPart.java b/src/main/java/net/pterodactylus/sone/text/PostPart.java
new file mode 100644 (file)
index 0000000..1e0773d
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Sone - PostLinkPart.java - Copyright © 2011 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.text;
+
+import net.pterodactylus.sone.data.Post;
+
+/**
+ * TODO
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class PostPart implements Part {
+
+       private final Post post;
+
+       public PostPart(Post post) {
+               this.post = post;
+       }
+
+       public Post getPost() {
+               return post;
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/sone/text/SonePart.java b/src/main/java/net/pterodactylus/sone/text/SonePart.java
new file mode 100644 (file)
index 0000000..b460a29
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Sone - SoneLinkPart.java - Copyright © 2011 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.text;
+
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.template.SoneAccessor;
+
+/**
+ * TODO
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class SonePart implements Part {
+
+       private final Sone sone;
+
+       public SonePart(Sone sone) {
+               this.sone = sone;
+       }
+
+       public Sone getSone() {
+               return sone;
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java
new file mode 100644 (file)
index 0000000..6336934
--- /dev/null
@@ -0,0 +1,305 @@
+/*
+ * Sone - FreenetLinkParser.java - Copyright © 2010 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.text;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.Writer;
+import java.net.MalformedURLException;
+import java.util.Iterator;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.template.SoneAccessor;
+import net.pterodactylus.util.logging.Logging;
+import net.pterodactylus.util.template.Template;
+import net.pterodactylus.util.template.TemplateContextFactory;
+import net.pterodactylus.util.template.TemplateParser;
+import freenet.keys.FreenetURI;
+
+/**
+ * {@link Parser} implementation that can recognize Freenet URIs.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class SoneTextParser implements Parser<SoneTextParserContext> {
+
+       /** The logger. */
+       private static final Logger logger = Logging.getLogger(SoneTextParser.class);
+
+       /** Pattern to detect whitespace. */
+       private static final Pattern whitespacePattern = Pattern.compile("[\\u000a\u0020\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u200c\u200d\u202f\u205f\u2060\u2800\u3000]");
+
+       /**
+        * Enumeration for all recognized link types.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       private enum LinkType {
+
+               /** Link is a KSK. */
+               KSK,
+
+               /** Link is a CHK. */
+               CHK,
+
+               /** Link is an SSK. */
+               SSK,
+
+               /** Link is a USK. */
+               USK,
+
+               /** Link is HTTP. */
+               HTTP,
+
+               /** Link is HTTPS. */
+               HTTPS,
+
+               /** Link is a Sone. */
+               SONE,
+
+               /** Link is a post. */
+               POST,
+
+       }
+
+       /** The core. */
+       private final Core core;
+
+       /**
+        * Creates a new freenet link parser.
+        *
+        * @param core
+        *            The core
+        */
+       public SoneTextParser(Core core) {
+               this.core = core;
+       }
+
+       //
+       // PART METHODS
+       //
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       public Iterable<Part> parse(SoneTextParserContext context, Reader source) throws IOException {
+               PartContainer parts = new PartContainer();
+               BufferedReader bufferedReader = (source instanceof BufferedReader) ? (BufferedReader) source : new BufferedReader(source);
+               String line;
+               boolean lastLineEmpty = true;
+               int emptyLines = 0;
+               while ((line = bufferedReader.readLine()) != null) {
+                       if (line.trim().length() == 0) {
+                               if (lastLineEmpty) {
+                                       continue;
+                               }
+                               parts.add(new PlainTextPart("\n"));
+                               ++emptyLines;
+                               lastLineEmpty = emptyLines == 2;
+                               continue;
+                       }
+                       emptyLines = 0;
+                       boolean lineComplete = true;
+                       while (line.length() > 0) {
+                               int nextKsk = line.indexOf("KSK@");
+                               int nextChk = line.indexOf("CHK@");
+                               int nextSsk = line.indexOf("SSK@");
+                               int nextUsk = line.indexOf("USK@");
+                               int nextHttp = line.indexOf("http://");
+                               int nextHttps = line.indexOf("https://");
+                               int nextSone = line.indexOf("sone://");
+                               int nextPost = line.indexOf("post://");
+                               if ((nextKsk == -1) && (nextChk == -1) && (nextSsk == -1) && (nextUsk == -1) && (nextHttp == -1) && (nextHttps == -1) && (nextSone == -1) && (nextPost == -1)) {
+                                       if (lineComplete && !lastLineEmpty) {
+                                               parts.add(new PlainTextPart("\n" + line));
+                                       } else {
+                                               parts.add(new PlainTextPart(line));
+                                       }
+                                       break;
+                               }
+                               int next = Integer.MAX_VALUE;
+                               LinkType linkType = null;
+                               if ((nextKsk > -1) && (nextKsk < next)) {
+                                       next = nextKsk;
+                                       linkType = LinkType.KSK;
+                               }
+                               if ((nextChk > -1) && (nextChk < next)) {
+                                       next = nextChk;
+                                       linkType = LinkType.CHK;
+                               }
+                               if ((nextSsk > -1) && (nextSsk < next)) {
+                                       next = nextSsk;
+                                       linkType = LinkType.SSK;
+                               }
+                               if ((nextUsk > -1) && (nextUsk < next)) {
+                                       next = nextUsk;
+                                       linkType = LinkType.USK;
+                               }
+                               if ((nextHttp > -1) && (nextHttp < next)) {
+                                       next = nextHttp;
+                                       linkType = LinkType.HTTP;
+                               }
+                               if ((nextHttps > -1) && (nextHttps < next)) {
+                                       next = nextHttps;
+                                       linkType = LinkType.HTTPS;
+                               }
+                               if ((nextSone > -1) && (nextSone < next)) {
+                                       next = nextSone;
+                                       linkType = LinkType.SONE;
+                               }
+                               if ((nextPost > -1) && (nextPost < next)) {
+                                       next = nextPost;
+                                       linkType = LinkType.POST;
+                               }
+                               if (linkType == LinkType.SONE) {
+                                       if (next > 0) {
+                                               parts.add(new PlainTextPart(line.substring(0, next)));
+                                       }
+                                       if (line.length() >= (next + 7 + 43)) {
+                                               String soneId = line.substring(next + 7, next + 50);
+                                               Sone sone = core.getSone(soneId, false);
+                                               if (sone != null) {
+                                                       parts.add(new SonePart(sone));
+                                               } else {
+                                                       parts.add(new PlainTextPart(line.substring(next, next + 50)));
+                                               }
+                                               line = line.substring(next + 50);
+                                       } else {
+                                               parts.add(new PlainTextPart(line.substring(next)));
+                                               line = "";
+                                       }
+                                       continue;
+                               }
+                               if (linkType == LinkType.POST) {
+                                       if (next > 0) {
+                                               parts.add(new PlainTextPart(line.substring(0, next)));
+                                       }
+                                       if (line.length() >= (next + 7 + 36)) {
+                                               String postId = line.substring(next + 7, next + 43);
+                                               Post post = core.getPost(postId, false);
+                                               if ((post != null) && (post.getSone() != null)) {
+                                                       String postText = post.getText();
+                                                       postText = postText.substring(0, Math.min(postText.length(), 20)) + "…";
+                                                       parts.add(new PostPart(post));
+                                               } else {
+                                                       parts.add(new PlainTextPart(line.substring(next, next + 43)));
+                                               }
+                                               line = line.substring(next + 43);
+                                       } else {
+                                               parts.add(new PlainTextPart(line.substring(next)));
+                                               line = "";
+                                       }
+                                       continue;
+                               }
+                               if ((next >= 8) && (line.substring(next - 8, next).equals("freenet:"))) {
+                                       next -= 8;
+                                       line = line.substring(0, next) + line.substring(next + 8);
+                               }
+                               Matcher matcher = whitespacePattern.matcher(line);
+                               int nextSpace = matcher.find(next) ? matcher.start() : line.length();
+                               if (nextSpace > (next + 4)) {
+                                       if (!lastLineEmpty && lineComplete) {
+                                               parts.add(new PlainTextPart("\n" + line.substring(0, next)));
+                                       } else {
+                                               parts.add(new PlainTextPart(line.substring(0, next)));
+                                       }
+                                       String link = line.substring(next, nextSpace);
+                                       String name = link;
+                                       logger.log(Level.FINER, "Found link: %s", link);
+                                       logger.log(Level.FINEST, "Next: %d, CHK: %d, SSK: %d, USK: %d", new Object[] { next, nextChk, nextSsk, nextUsk });
+
+                                       if ((linkType == LinkType.KSK) || (linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) {
+                                               FreenetURI uri;
+                                               if (name.indexOf('?') > -1) {
+                                                       name = name.substring(0, name.indexOf('?'));
+                                               }
+                                               if (name.endsWith("/")) {
+                                                       name = name.substring(0, name.length() - 1);
+                                               }
+                                               try {
+                                                       uri = new FreenetURI(name);
+                                                       name = uri.lastMetaString();
+                                                       if (name == null) {
+                                                               name = uri.getDocName();
+                                                       }
+                                                       if (name == null) {
+                                                               name = link.substring(0, Math.min(9, link.length()));
+                                                       }
+                                                       boolean fromPostingSone = ((linkType == LinkType.SSK) || (linkType == LinkType.USK)) && (context.getPostingSone() != null) && link.substring(4, Math.min(link.length(), 47)).equals(context.getPostingSone().getId());
+                                                       parts.add(new FreenetLinkPart(link, name, fromPostingSone));
+                                               } catch (MalformedURLException mue1) {
+                                                       /* not a valid link, insert as plain text. */
+                                                       parts.add(new PlainTextPart(link));
+                                               } catch (NullPointerException npe1) {
+                                                       /* FreenetURI sometimes throws these, too. */
+                                                       parts.add(new PlainTextPart(link));
+                                               } catch (ArrayIndexOutOfBoundsException aioobe1) {
+                                                       /* oh, and these, too. */
+                                                       parts.add(new PlainTextPart(link));
+                                               }
+                                       } else if ((linkType == LinkType.HTTP) || (linkType == LinkType.HTTPS)) {
+                                               name = link.substring(linkType == LinkType.HTTP ? 7 : 8);
+                                               int firstSlash = name.indexOf('/');
+                                               int lastSlash = name.lastIndexOf('/');
+                                               if ((lastSlash - firstSlash) > 3) {
+                                                       name = name.substring(0, firstSlash + 1) + "…" + name.substring(lastSlash);
+                                               }
+                                               if (name.endsWith("/")) {
+                                                       name = name.substring(0, name.length() - 1);
+                                               }
+                                               if (((name.indexOf('/') > -1) && (name.indexOf('.') < name.lastIndexOf('.', name.indexOf('/'))) || ((name.indexOf('/') == -1) && (name.indexOf('.') < name.lastIndexOf('.')))) && name.startsWith("www.")) {
+                                                       name = name.substring(4);
+                                               }
+                                               if (name.indexOf('?') > -1) {
+                                                       name = name.substring(0, name.indexOf('?'));
+                                               }
+                                               parts.add(new LinkPart(link, name));
+                                       }
+                                       line = line.substring(nextSpace);
+                               } else {
+                                       if (!lastLineEmpty && lineComplete) {
+                                               parts.add(new PlainTextPart("\n" + line.substring(0, next + 4)));
+                                       } else {
+                                               parts.add(new PlainTextPart(line.substring(0, next + 4)));
+                                       }
+                                       line = line.substring(next + 4);
+                               }
+                               lineComplete = false;
+                       }
+                       lastLineEmpty = false;
+               }
+               for (int partIndex = parts.size() - 1; partIndex >= 0; --partIndex) {
+                       Part part = parts.getPart(partIndex);
+                       if ((part instanceof PlainTextPart) && !"\n".equals(((PlainTextPart) part).getText())) {
+                               break;
+                       }
+                       parts.removePart(partIndex);
+               }
+               return parts;
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/sone/text/SoneTextParserContext.java b/src/main/java/net/pterodactylus/sone/text/SoneTextParserContext.java
new file mode 100644 (file)
index 0000000..4a89016
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * Sone - SoneTextParserContext.java - Copyright © 2011 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.text;
+
+import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.web.page.Page.Request;
+
+/**
+ * {@link ParserContext} implementation for the {@link SoneTextParser}. It
+ * stores the {@link Sone} that provided the parsed text so that certain links
+ * can be marked in a different way.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class SoneTextParserContext implements ParserContext {
+
+       /** The request being processed. */
+       private final Request request;
+
+       /** The posting Sone. */
+       private final Sone postingSone;
+
+       /**
+        * Creates a new link parser context.
+        *
+        * @param request
+        *            The request being processed
+        * @param postingSone
+        *            The posting Sone
+        */
+       public SoneTextParserContext(Request request, Sone postingSone) {
+               this.request = request;
+               this.postingSone = postingSone;
+       }
+
+       /**
+        * Returns the request that is currently being processed.
+        *
+        * @return The request being processed
+        */
+       public Request getRequest() {
+               return request;
+       }
+
+       /**
+        * Returns the Sone that provided the text that is being parsed.
+        *
+        * @return The posting Sone
+        */
+       public Sone getPostingSone() {
+               return postingSone;
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/sone/text/TemplatePart.java b/src/main/java/net/pterodactylus/sone/text/TemplatePart.java
deleted file mode 100644 (file)
index ac5694c..0000000
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- * Sone - TemplatePart.java - Copyright © 2010 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
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.text;
-
-import java.io.IOException;
-import java.io.StringWriter;
-import java.io.Writer;
-
-import net.pterodactylus.util.template.Template;
-import net.pterodactylus.util.template.TemplateContext;
-import net.pterodactylus.util.template.TemplateContextFactory;
-import net.pterodactylus.util.template.TemplateException;
-
-/**
- * {@link Part} implementation that is rendered using a {@link Template}.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class TemplatePart implements Part, net.pterodactylus.util.template.Part {
-
-       /** The template context factory. */
-       private final TemplateContextFactory templateContextFactory;
-
-       /** The template to render for this part. */
-       private final Template template;
-
-       /**
-        * Creates a new template part.
-        *
-        * @param templateContextFactory
-        *            The template context factory
-        * @param template
-        *            The template to render
-        */
-       public TemplatePart(TemplateContextFactory templateContextFactory, Template template) {
-               this.templateContextFactory = templateContextFactory;
-               this.template = template;
-       }
-
-       //
-       // ACTIONS
-       //
-
-       /**
-        * Sets a variable in the template.
-        *
-        * @param key
-        *            The key of the variable
-        * @param value
-        *            The value of the variable
-        * @return This template part (for method chaining)
-        */
-       public TemplatePart set(String key, Object value) {
-               template.getInitialContext().set(key, value);
-               return this;
-       }
-
-       //
-       // PART METHODS
-       //
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public void render(Writer writer) throws IOException {
-               template.render(templateContextFactory.createTemplateContext().mergeContext(template.getInitialContext()), writer);
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public void render(TemplateContext templateContext, Writer writer) throws TemplateException {
-               template.render(templateContext.mergeContext(template.getInitialContext()), writer);
-       }
-
-       //
-       // OBJECT METHODS
-       //
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public String toString() {
-               StringWriter stringWriter = new StringWriter();
-               try {
-                       render(stringWriter);
-               } catch (IOException ioe1) {
-                       /* should never throw, ignore. */
-               }
-               return stringWriter.toString();
-       }
-
-}
diff --git a/src/test/java/net/pterodactylus/sone/text/FreenetLinkParserTest.java b/src/test/java/net/pterodactylus/sone/text/FreenetLinkParserTest.java
deleted file mode 100644 (file)
index e667aeb..0000000
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- * Sone - FreenetLinkParserTest.java - Copyright © 2010 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
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package net.pterodactylus.sone.text;
-
-import java.io.IOException;
-import java.io.StringReader;
-
-import junit.framework.TestCase;
-import net.pterodactylus.util.template.HtmlFilter;
-import net.pterodactylus.util.template.TemplateContextFactory;
-
-/**
- * JUnit test case for {@link FreenetLinkParser}.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class FreenetLinkParserTest extends TestCase {
-
-       /**
-        * Tests the parser.
-        *
-        * @throws IOException
-        *             if an I/O error occurs
-        */
-       public void testParser() throws IOException {
-               TemplateContextFactory templateContextFactory = new TemplateContextFactory();
-               templateContextFactory.addFilter("html", new HtmlFilter());
-               FreenetLinkParser parser = new FreenetLinkParser(null, templateContextFactory);
-               FreenetLinkParserContext context = new FreenetLinkParserContext(null, null);
-               Part part;
-
-               part = parser.parse(context, new StringReader("Text."));
-               assertEquals("Text.", part.toString());
-
-               part = parser.parse(context, new StringReader("Text.\nText."));
-               assertEquals("Text.\nText.", part.toString());
-
-               part = parser.parse(context, new StringReader("Text.\n\nText."));
-               assertEquals("Text.\n\nText.", part.toString());
-
-               part = parser.parse(context, new StringReader("Text.\n\n\nText."));
-               assertEquals("Text.\n\nText.", part.toString());
-
-               part = parser.parse(context, new StringReader("\nText.\n\n\nText."));
-               assertEquals("Text.\n\nText.", part.toString());
-
-               part = parser.parse(context, new StringReader("\nText.\n\n\nText.\n"));
-               assertEquals("Text.\n\nText.", part.toString());
-
-               part = parser.parse(context, new StringReader("\nText.\n\n\nText.\n\n"));
-               assertEquals("Text.\n\nText.", part.toString());
-
-               part = parser.parse(context, new StringReader("\nText.\n\n\n\nText.\n\n\n"));
-               assertEquals("Text.\n\nText.", part.toString());
-
-               part = parser.parse(context, new StringReader("\n\nText.\n\n\n\nText.\n\n\n"));
-               assertEquals("Text.\n\nText.", part.toString());
-
-               part = parser.parse(context, new StringReader("\n\nText. KSK@a text.\n\n\n\nText.\n\n\n"));
-               assertEquals("Text. <a class=\"freenet\" href=\"/KSK@a\" title=\"KSK@a\">a</a> text.\n\nText.", part.toString());
-       }
-
-}