Show posts mentioning a local Sone by a new notification.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 8 Jun 2011 14:00:32 +0000 (16:00 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 8 Jun 2011 14:00:32 +0000 (16:00 +0200)
This fixes #188.

19 files changed:
src/main/java/net/pterodactylus/sone/data/Sone.java
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/main/java/net/pterodactylus/sone/web/WebInterface.java
src/main/resources/i18n/sone.en.properties
src/main/resources/templates/notify/mentionNotification.html [new file with mode: 0644]
src/test/java/net/pterodactylus/sone/text/FreenetLinkParserTest.java [deleted file]

index 715d95b..e1c0391 100644 (file)
@@ -27,8 +27,10 @@ import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import net.pterodactylus.sone.core.Core;
 import net.pterodactylus.sone.core.Options;
 import net.pterodactylus.sone.freenet.wot.Identity;
+import net.pterodactylus.sone.freenet.wot.OwnIdentity;
 import net.pterodactylus.sone.template.SoneAccessor;
 import net.pterodactylus.util.filter.Filter;
 import net.pterodactylus.util.logging.Logging;
@@ -67,6 +69,16 @@ public class Sone implements Fingerprintable, Comparable<Sone> {
                }
        };
 
+       /** Filter that matches all {@link Core#isLocalSone(Sone) local Sones}. */
+       public static final Filter<Sone> LOCAL_SONE_FILTER = new Filter<Sone>() {
+
+               @Override
+               public boolean filterObject(Sone sone) {
+                       return sone.getIdentity() instanceof OwnIdentity;
+               }
+
+       };
+
        /** The logger. */
        private static final Logger logger = Logging.getLogger(Sone.class);
 
index db945b5..26afe2e 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,20 +51,32 @@ public class ParserFilter implements Filter {
        private final Core core;
 
        /** The link parser. */
-       private final FreenetLinkParser linkParser;
+       private final SoneTextParser soneTextParser;
+
+       /** 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
         * @param templateContextFactory
         *            The context factory for rendering the parts
+        * @param soneTextParser
+        *            The Sone text parser
         */
-       public ParserFilter(Core core, TemplateContextFactory templateContextFactory) {
+       public ParserFilter(Core core, TemplateContextFactory templateContextFactory, SoneTextParser soneTextParser) {
                this.core = core;
-               linkParser = new FreenetLinkParser(core, templateContextFactory);
+               this.templateContextFactory = templateContextFactory;
+               this.soneTextParser = soneTextParser;
        }
 
        /**
@@ -71,13 +93,164 @@ 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, soneTextParser.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
+       //
+
+       /**
+        * Renders the given parts.
+        *
+        * @param writer
+        *            The writer to render the parts to
+        * @param parts
+        *            The parts to render
+        */
+       private void render(Writer writer, Iterable<Part> parts) {
+               for (Part part : parts) {
+                       render(writer, part);
+               }
+       }
+
+       /**
+        * Renders the given part.
+        *
+        * @param writer
+        *            The writer to render the part to
+        * @param part
+        *            The part to render
+        */
+       @SuppressWarnings("unchecked")
+       private void render(Writer writer, Part part) {
+               if (part instanceof PlainTextPart) {
+                       render(writer, (PlainTextPart) part);
+               } else if (part instanceof FreenetLinkPart) {
+                       render(writer, (FreenetLinkPart) part);
+               } else if (part instanceof LinkPart) {
+                       render(writer, (LinkPart) part);
+               } else if (part instanceof SonePart) {
+                       render(writer, (SonePart) part);
+               } else if (part instanceof PostPart) {
+                       render(writer, (PostPart) part);
+               } else if (part instanceof Iterable<?>) {
+                       render(writer, (Iterable<Part>) part);
+               }
+       }
+
+       /**
+        * Renders the given plain-text part.
+        *
+        * @param writer
+        *            The writer to render the part to
+        * @param plainTextPart
+        *            The part to render
+        */
+       private void render(Writer writer, PlainTextPart plainTextPart) {
+               TemplateContext templateContext = templateContextFactory.createTemplateContext();
+               templateContext.set("text", plainTextPart.getText());
+               plainTextTemplate.render(templateContext, writer);
+       }
+
+       /**
+        * Renders the given freenet link part.
+        *
+        * @param writer
+        *            The writer to render the part to
+        * @param freenetLinkPart
+        *            The part to render
+        */
+       private void render(Writer writer, FreenetLinkPart freenetLinkPart) {
+               renderLink(writer, "/" + freenetLinkPart.getLink(), freenetLinkPart.getText(), freenetLinkPart.getTitle(), freenetLinkPart.isTrusted() ? "freenet-trusted" : "freenet");
+       }
+
+       /**
+        * Renders the given link part.
+        *
+        * @param writer
+        *            The writer to render the part to
+        * @param linkPart
+        *            The part to render
+        */
+       private void render(Writer writer, LinkPart linkPart) {
+               renderLink(writer, "/?_CHECKED_HTTP_=" + linkPart.getLink(), linkPart.getText(), linkPart.getTitle(), "internet");
+       }
+
+       /**
+        * Renders the given Sone part.
+        *
+        * @param writer
+        *            The writer to render the part to
+        * @param sonePart
+        *            The part to render
+        */
+       private void render(Writer writer, SonePart sonePart) {
+               renderLink(writer, "viewSone.html?sone=" + sonePart.getSone().getId(), SoneAccessor.getNiceName(sonePart.getSone()), SoneAccessor.getNiceName(sonePart.getSone()), "in-sone");
+       }
+
+       /**
+        * Renders the given post part.
+        *
+        * @param writer
+        *            The writer to render the part to
+        * @param postPart
+        *            The part to render
+        */
+       private void render(Writer writer, PostPart postPart) {
+               renderLink(writer, "viewPost.html?post=" + postPart.getPost().getId(), getExcerpt(postPart.getPost().getText(), 20), SoneAccessor.getNiceName(postPart.getPost().getSone()), "in-sone");
+       }
+
+       /**
+        * Renders the given link.
+        *
+        * @param writer
+        *            The writer to render the link to
+        * @param link
+        *            The link to render
+        * @param text
+        *            The text of the link
+        * @param title
+        *            The title of the link
+        * @param cssClass
+        *            The CSS class of the link
+        */
+       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
+       //
+
+       /**
+        * Returns up to {@code length} characters from the given text, appending
+        * “…” if the text is longer.
+        *
+        * @param text
+        *            The text to get an excerpt from
+        * @param length
+        *            The maximum length of the excerpt (without the ellipsis)
+        * @return The excerpt of the text
+        */
+       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..be3aef9
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * 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;
+
+/**
+ * {@link LinkPart} implementation that stores an additional attribute: if the
+ * link is an SSK or USK link and the post was created by an identity that owns
+ * the keyspace in question.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class FreenetLinkPart extends LinkPart {
+
+       /** Whether the link is trusted. */
+       private final boolean trusted;
+
+       /**
+        * Creates a new freenet link part.
+        *
+        * @param link
+        *            The link of the part
+        * @param text
+        *            The text of the part
+        * @param trusted
+        *            {@code true} if the link is trusted, {@code false} otherwise
+        */
+       public FreenetLinkPart(String link, String text, boolean trusted) {
+               this(link, text, text, trusted);
+       }
+
+       /**
+        * Creates a new freenet link part.
+        *
+        * @param link
+        *            The link of the part
+        * @param text
+        *            The text of the part
+        * @param title
+        *            The title of the part
+        * @param trusted
+        *            {@code true} if the link is trusted, {@code false} otherwise
+        */
+       public FreenetLinkPart(String link, String text, String title, boolean trusted) {
+               super(link, text, title);
+               this.trusted = trusted;
+       }
+
+       //
+       // ACCESSORS
+       //
+
+       /**
+        * Returns whether the link is trusted.
+        *
+        * @return {@code true} if the link is trusted, {@code false} otherwise
+        */
+       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..d5da730
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * 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;
+
+/**
+ * {@link Part} implementation that can hold a link. A link contains of three
+ * attributes: the link itself, the text that is shown instead of the link, and
+ * an explanatory text that can be displayed e.g. as a tooltip.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class LinkPart implements Part {
+
+       /** The link of this part. */
+       private final String link;
+
+       /** The text of this part. */
+       private final String text;
+
+       /** The title of this part. */
+       private final String title;
+
+       /**
+        * Creates a new link part.
+        *
+        * @param link
+        *            The link of the link part
+        * @param text
+        *            The text of the link part
+        */
+       public LinkPart(String link, String text) {
+               this(link, text, text);
+       }
+
+       /**
+        * Creates a new link part.
+        *
+        * @param link
+        *            The link of the link part
+        * @param text
+        *            The text of the link part
+        * @param title
+        *            The title of the link part
+        */
+       public LinkPart(String link, String text, String title) {
+               this.link = link;
+               this.text = text;
+               this.title = title;
+       }
+
+       //
+       // ACCESSORS
+       //
+
+       /**
+        * Returns the link of this part.
+        *
+        * @return The link of this part
+        */
+       public String getLink() {
+               return link;
+       }
+
+       /**
+        * Returns the text of this part.
+        *
+        * @return The text of this part
+        */
+       public String getText() {
+               return text;
+       }
+
+       /**
+        * Returns the title of this part.
+        *
+        * @return The title of this part
+        */
+       public String getTitle() {
+               return title;
+       }
+
+}
index e43ed47..87cdab5 100644 (file)
@@ -31,16 +31,16 @@ import java.io.Reader;
 public interface Parser<C extends ParserContext> {
 
        /**
-        * Create a {@link Part} from the given text source.
+        * Create one or more {@link Part}s from the given text source.
         *
         * @param context
-        *            The parser context
+        *            The parser context (may be {@code null})
         * @param source
         *            The text source
-        * @return The parsed part
+        * @return The parsed parts
         * @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..e16dfb4 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.
+ * element. How the part is displayed is not part of the {@code Part}
+ * specification.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
-public interface Part extends Renderable {
+public interface Part {
 
-       /* all required methods are inherited from {@link Renderable}. */
+       /* no methods. */
 
 }
index d52658e..384e8ae 100644 (file)
 
 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 +31,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 Part, Iterable<Part> {
 
        /** The parts to render. */
        private final List<Part> parts = new ArrayList<Part>();
@@ -80,35 +81,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..a1f2088
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * 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;
+
+/**
+ * {@link Part} implementation that holds a single piece of text.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class PlainTextPart implements Part {
+
+       /** The text of the part. */
+       private final String text;
+
+       /**
+        * Creates a new plain-text part.
+        *
+        * @param text
+        *            The text of the part
+        */
+       public PlainTextPart(String text) {
+               this.text = text;
+       }
+
+       //
+       // ACCESSORS
+       //
+
+       /**
+        * Returns the text of this part.
+        *
+        * @return The text of this part
+        */
+       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..22acd43
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * 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;
+
+/**
+ * {@link Part} implementation that stores a reference to a {@link Post}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class PostPart implements Part {
+
+       /** The post this part refers to. */
+       private final Post post;
+
+       /**
+        * Creates a new post part.
+        *
+        * @param post
+        *            The referenced post
+        */
+       public PostPart(Post post) {
+               this.post = post;
+       }
+
+       //
+       // ACCESSORS
+       //
+
+       /**
+        * Returns the post referenced by this part.
+        *
+        * @return The post referenced by this part
+        */
+       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..5c6a63d
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * 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;
+
+/**
+ * {@link Part} implementation that stores a reference to a {@link Sone}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class SonePart implements Part {
+
+       /** The referenced {@link Sone}. */
+       private final Sone sone;
+
+       /**
+        * Creates a new Sone part.
+        *
+        * @param sone
+        *            The referenced Sone
+        */
+       public SonePart(Sone sone) {
+               this.sone = sone;
+       }
+
+       //
+       // ACCESSORS
+       //
+
+       /**
+        * Returns the referenced Sone.
+        *
+        * @return The referenced 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..98bb800
--- /dev/null
@@ -0,0 +1,298 @@
+/*
+ * 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.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.util.logging.Logging;
+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 != null) && (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();
-       }
-
-}
index 98539fe..8eb0122 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.web;
 
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
@@ -58,6 +59,9 @@ import net.pterodactylus.sone.template.SoneAccessor;
 import net.pterodactylus.sone.template.SubstringFilter;
 import net.pterodactylus.sone.template.TrustAccessor;
 import net.pterodactylus.sone.template.UnknownDateFilter;
+import net.pterodactylus.sone.text.Part;
+import net.pterodactylus.sone.text.SonePart;
+import net.pterodactylus.sone.text.SoneTextParser;
 import net.pterodactylus.sone.web.ajax.BookmarkAjaxPage;
 import net.pterodactylus.sone.web.ajax.CreatePostAjaxPage;
 import net.pterodactylus.sone.web.ajax.CreateReplyAjaxPage;
@@ -96,6 +100,7 @@ import net.pterodactylus.util.cache.CacheItem;
 import net.pterodactylus.util.cache.DefaultCacheItem;
 import net.pterodactylus.util.cache.MemoryCache;
 import net.pterodactylus.util.cache.ValueRetriever;
+import net.pterodactylus.util.filter.Filters;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.notify.Notification;
 import net.pterodactylus.util.notify.NotificationManager;
@@ -151,6 +156,9 @@ public class WebInterface implements CoreListener {
        /** The template context factory. */
        private final TemplateContextFactory templateContextFactory;
 
+       /** The Sone text parser. */
+       private final SoneTextParser soneTextParser;
+
        /** The “new Sone” notification. */
        private final ListNotification<Sone> newSoneNotification;
 
@@ -160,6 +168,9 @@ public class WebInterface implements CoreListener {
        /** The “new reply” notification. */
        private final ListNotification<Reply> newReplyNotification;
 
+       /** The “you have been mentioned” notification. */
+       private final ListNotification<Post> mentionNotification;
+
        /** The “rescuing Sone” notification. */
        private final ListNotification<Sone> rescuingSonesNotification;
 
@@ -185,6 +196,7 @@ public class WebInterface implements CoreListener {
        public WebInterface(SonePlugin sonePlugin) {
                this.sonePlugin = sonePlugin;
                formPassword = sonePlugin.pluginRespirator().getToadletContainer().getFormPassword();
+               soneTextParser = new SoneTextParser(getCore());
 
                templateContextFactory = new TemplateContextFactory();
                templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
@@ -206,7 +218,7 @@ public class WebInterface implements CoreListener {
                templateContextFactory.addFilter("match", new MatchFilter());
                templateContextFactory.addFilter("css", new CssClassNameFilter());
                templateContextFactory.addFilter("js", new JavascriptFilter());
-               templateContextFactory.addFilter("parse", new ParserFilter(getCore(), templateContextFactory));
+               templateContextFactory.addFilter("parse", new ParserFilter(getCore(), templateContextFactory, soneTextParser));
                templateContextFactory.addFilter("unknown", new UnknownDateFilter(getL10n(), "View.Sone.Text.UnknownDate"));
                templateContextFactory.addFilter("format", new FormatFilter());
                templateContextFactory.addFilter("sort", new CollectionSortFilter());
@@ -227,6 +239,9 @@ public class WebInterface implements CoreListener {
                Template newReplyNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/newReplyNotification.html"));
                newReplyNotification = new ListNotification<Reply>("new-reply-notification", "replies", newReplyNotificationTemplate, false);
 
+               Template mentionNotificationTemplate = TemplateParser.parse(createReader("/templates/notify/mentionNotification.html"));
+               mentionNotification = new ListNotification<Post>("mention-notification", "posts", mentionNotificationTemplate, false);
+
                Template rescuingSonesTemplate = TemplateParser.parse(createReader("/templates/notify/rescuingSonesNotification.html"));
                rescuingSonesNotification = new ListNotification<Sone>("sones-being-rescued-notification", "sones", rescuingSonesTemplate);
 
@@ -649,11 +664,25 @@ public class WebInterface implements CoreListener {
                try {
                        return new InputStreamReader(getClass().getResourceAsStream(resourceName), "UTF-8");
                } catch (UnsupportedEncodingException uee1) {
-                       System.out.println("  fail.");
                        return null;
                }
        }
 
+       private Set<Sone> getMentionedSones(String text) {
+               /* we need no context to find mentioned Sones. */
+               Set<Sone> mentionedSones = new HashSet<Sone>();
+               try {
+                       for (Part part : soneTextParser.parse(null, new StringReader(text))) {
+                               if (part instanceof SonePart) {
+                                       mentionedSones.add(((SonePart) part).getSone());
+                               }
+                       }
+               } catch (IOException ioe1) {
+                       logger.log(Level.WARNING, "Could not parse post text: " + text, ioe1);
+               }
+               return Filters.filteredSet(mentionedSones, Sone.LOCAL_SONE_FILTER);
+       }
+
        //
        // CORELISTENER METHODS
        //
@@ -696,6 +725,10 @@ public class WebInterface implements CoreListener {
                newPostNotification.add(post);
                if (!hasFirstStartNotification()) {
                        notificationManager.addNotification(newPostNotification);
+                       if (!getMentionedSones(post.getText()).isEmpty()) {
+                               mentionNotification.add(post);
+                               notificationManager.addNotification(mentionNotification);
+                       }
                } else {
                        getCore().markPostKnown(post);
                }
@@ -712,6 +745,10 @@ public class WebInterface implements CoreListener {
                newReplyNotification.add(reply);
                if (!hasFirstStartNotification()) {
                        notificationManager.addNotification(newReplyNotification);
+                       if (!getMentionedSones(reply.getText()).isEmpty()) {
+                               mentionNotification.add(reply.getPost());
+                               notificationManager.addNotification(mentionNotification);
+                       }
                } else {
                        getCore().markReplyKnown(reply);
                }
@@ -731,6 +768,7 @@ public class WebInterface implements CoreListener {
        @Override
        public void markPostKnown(Post post) {
                newPostNotification.remove(post);
+               mentionNotification.remove(post);
        }
 
        /**
@@ -739,6 +777,7 @@ public class WebInterface implements CoreListener {
        @Override
        public void markReplyKnown(Reply reply) {
                newReplyNotification.remove(reply);
+               mentionNotification.remove(reply.getPost());
        }
 
        /**
index de9d317..3610afc 100644 (file)
@@ -312,3 +312,5 @@ Notification.SoneRescued.Text=The following Sones have been rescued:
 Notification.SoneRescued.Text.RememberToUnlock=Please remember to control the posts and replies you have given and don’t forget to unlock your Sones!
 Notification.LockedSones.Text=The following Sones have been locked for more than 5 minutes. Please check if you really want to keep these Sones locked:
 Notification.NewVersion.Text=Version {version} of the Sone plugin was found. Download it from USK@nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI,DuQSUZiI~agF8c-6tjsFFGuZ8eICrzWCILB60nT8KKo,AQACAAE/sone/{edition}​!
+Notification.Mention.ShortText=You have been mentioned.
+Notification.Mention.Text=You have been mentioned in the following posts:
diff --git a/src/main/resources/templates/notify/mentionNotification.html b/src/main/resources/templates/notify/mentionNotification.html
new file mode 100644 (file)
index 0000000..087fc2e
--- /dev/null
@@ -0,0 +1,11 @@
+<div class="short-text hidden">
+       <%= Notification.Mention.ShortText|l10n|html>
+       <a class="link" onclick="showNotificationDetails('<%notification.id|html>'); return false;"><%= Notification.ClickHereToRead|l10n|html></a>
+</div>
+<div class="text">
+       <%= Notification.Mention.Text|l10n|html>
+       <%foreach posts post>
+               <div class="hidden post-id"><%post.id|html></div>
+               <a class="link-<% post.id|html>" href="viewPost.html?post=<% post.id|html>"><% post.sone.niceName|html></a><%notlast>,<%/notlast><%last>.<%/last>
+       <%/foreach>
+</div>
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());
-       }
-
-}