Rework the text parser.
[Sone.git] / src / main / java / net / pterodactylus / sone / text / SoneTextParser.java
index baa3dc9..fa61c91 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - FreenetLinkParser.java - Copyright © 2010–2012 David Roden
+ * Sone - SoneTextParser.java - Copyright © 2010–2013 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 package net.pterodactylus.sone.text;
 
+import static com.google.common.base.Optional.absent;
+import static com.google.common.base.Optional.of;
+import static com.google.common.collect.FluentIterable.from;
+import static com.google.common.collect.Lists.newArrayList;
+
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.Reader;
 import java.net.MalformedURLException;
-import java.util.logging.Level;
+import java.util.Comparator;
+import java.util.EnumMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 import java.util.logging.Logger;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import net.pterodactylus.sone.core.PostProvider;
-import net.pterodactylus.sone.core.SoneProvider;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.logging.Logging;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.ImmutableMap;
 import freenet.keys.FreenetURI;
 
 /**
@@ -54,258 +65,360 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
         */
        private enum LinkType {
 
-               /** Link is a KSK. */
-               KSK,
-
-               /** Link is a CHK. */
-               CHK,
-
-               /** Link is an SSK. */
-               SSK,
-
-               /** Link is a USK. */
-               USK,
+               KSK("KSK@"),
+               CHK("CHK@"),
+               SSK("SSK@") {
+                       @Override
+                       public boolean isSigned() {
+                               return true;
+                       }
+               },
+               USK("USK@") {
+                       @Override
+                       public boolean isSigned() {
+                               return true;
+                       }
+               },
+               HTTP("http://"),
+               HTTPS("https://"),
+               SONE("sone://"),
+               POST("post://");
 
-               /** Link is HTTP. */
-               HTTP,
+               private final String scheme;
 
-               /** Link is HTTPS. */
-               HTTPS,
+               private LinkType(String scheme) {
+                       this.scheme = scheme;
+               }
 
-               /** Link is a Sone. */
-               SONE,
+               public String getScheme() {
+                       return scheme;
+               }
 
-               /** Link is a post. */
-               POST,
+               public boolean isSigned() {
+                       return false;
+               }
 
        }
 
-       /** The Sone provider. */
-       private final SoneProvider soneProvider;
-
-       /** The post provider. */
-       private final PostProvider postProvider;
+       private final PartCreators partCreators = new PartCreators();
+       private final Database database;
 
        /**
         * Creates a new freenet link parser.
         *
-        * @param soneProvider
-        *            The Sone provider
-        * @param postProvider
-        *            The post provider
+        * @param database
         */
-       public SoneTextParser(SoneProvider soneProvider, PostProvider postProvider) {
-               this.soneProvider = soneProvider;
-               this.postProvider = postProvider;
+       public SoneTextParser(Database database) {
+               this.database = database;
        }
 
        //
        // 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);
                try {
                        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;
-                               /*
-                                * lineComplete tracks whether the block you are parsing is the
-                                * first block of the line. this is important because sometimes
-                                * you have to add an additional line break.
-                                */
-                               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;
-                                       }
-
-                                       /* cut off “freenet:” from before keys. */
-                                       if (((linkType == LinkType.KSK) || (linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) && (next >= 8) && (line.substring(next - 8, next).equals("freenet:"))) {
-                                               next -= 8;
-                                               line = line.substring(0, next) + line.substring(next + 8);
-                                       }
-
-                                       /* if there is text before the next item, write it out. */
-                                       if (lineComplete && !lastLineEmpty) {
-                                               parts.add(new PlainTextPart("\n"));
-                                       }
-                                       if (next > 0) {
-                                               parts.add(new PlainTextPart(line.substring(0, next)));
-                                               line = line.substring(next);
-                                               next = 0;
-                                       }
-                                       lineComplete = false;
-
-                                       if (linkType == LinkType.SONE) {
-                                               if (line.length() >= (7 + 43)) {
-                                                       String soneId = line.substring(7, 50);
-                                                       Sone sone = soneProvider.getSone(soneId, false);
-                                                       if (sone == null) {
-                                                               /*
-                                                                * don’t use create=true above, we don’t want
-                                                                * the empty shell.
-                                                                */
-                                                               sone = new Sone(soneId);
-                                                       }
-                                                       parts.add(new SonePart(sone));
-                                                       line = line.substring(50);
-                                               } else {
-                                                       parts.add(new PlainTextPart(line));
-                                                       line = "";
-                                               }
-                                               continue;
-                                       }
-                                       if (linkType == LinkType.POST) {
-                                               if (line.length() >= (7 + 36)) {
-                                                       String postId = line.substring(7, 43);
-                                                       Post post = postProvider.getPost(postId, false);
-                                                       if ((post != null) && (post.getSone() != null)) {
-                                                               parts.add(new PostPart(post));
-                                                       } else {
-                                                               parts.add(new PlainTextPart(line.substring(0, 43)));
-                                                       }
-                                                       line = line.substring(43);
-                                               } else {
-                                                       parts.add(new PlainTextPart(line));
-                                                       line = "";
-                                               }
-                                               continue;
-                                       }
-                                       Matcher matcher = whitespacePattern.matcher(line);
-                                       int nextSpace = matcher.find(0) ? matcher.start() : line.length();
-                                       String link = line.substring(0, nextSpace);
-                                       String name = link;
-                                       logger.log(Level.FINER, String.format("Found link: %s", link));
-                                       logger.log(Level.FINEST, String.format("CHK: %d, SSK: %d, USK: %d", 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);
+                               for (String pieceOfLine : splitLine(line)) {
+                                       parts.add(createPart(pieceOfLine, context));
                                }
-                               lastLineEmpty = false;
                        }
                } finally {
                        if (bufferedReader != source) {
                                Closer.close(bufferedReader);
                        }
                }
+               removeTrailingWhitespaceParts(parts);
+               return optimizeParts(parts);
+       }
+
+       private Iterable<Part> optimizeParts(PartContainer partContainer) {
+               PartContainer parts = new PartContainer();
+               boolean firstPart = true;
+               Part lastPart = null;
+               int emptyLines = 0;
+               for (Part part : partContainer) {
+                       if (firstPart) {
+                               if ("\n".equals(part.getText())) {
+                                       continue;
+                               }
+                               firstPart = false;
+                       }
+                       if ("\n".equals(part.getText())) {
+                               emptyLines++;
+                               if (emptyLines > 2) {
+                                       continue;
+                               }
+                       } else {
+                               emptyLines = 0;
+                       }
+                       if ((lastPart != null) && lastPart.isPlainText() && part.isPlainText()) {
+                               parts.removePart(parts.size() - 1);
+                               PlainTextPart combinedPart = new PlainTextPart(lastPart.getText() + part.getText());
+                               parts.add(combinedPart);
+                               lastPart = combinedPart;
+                       } else if ((lastPart != null) && part.isFreenetLink() && lastPart.isPlainText() && lastPart.getText().endsWith("freenet:")) {
+                               parts.removePart(parts.size() - 1);
+                               String lastPartText = lastPart.getText();
+                               lastPartText = lastPartText.substring(0, lastPartText.length() - "freenet:".length());
+                               if (lastPartText.length() > 0) {
+                                       parts.add(new PlainTextPart(lastPartText));
+                               }
+                               lastPart = part;
+                               parts.add(part);
+                       } else {
+                               lastPart = part;
+                               parts.add(part);
+                       }
+               }
+               return parts;
+       }
+
+       private Part createPart(String line, SoneTextParserContext context) {
+               Optional<Part> linkPart = createLinkPart(line, context);
+               return linkPart.or(new PlainTextPart(line));
+       }
+
+       private Optional<Part> createLinkPart(String line, SoneTextParserContext context) {
+               Optional<NextLink> nextLink = findNextLink(line);
+               if (!nextLink.isPresent()) {
+                       return absent();
+               }
+               return partCreators.createPart(nextLink.get().getLinkType(), line, context);
+       }
+
+       private List<String> splitLine(String line) {
+               List<String> linePieces = newArrayList();
+               int currentIndex = 0;
+               while (currentIndex < line.length()) {
+                       Optional<NextLink> nextLink = findNextLink(line.substring(currentIndex));
+                       if (!nextLink.isPresent()) {
+                               linePieces.add(line.substring(currentIndex));
+                               break;
+                       }
+                       int nextIndex = currentIndex + nextLink.get().getNextIndex();
+                       if (nextIndex > currentIndex) {
+                               linePieces.add(line.substring(currentIndex, nextIndex));
+                       }
+                       int nextWhitespace = nextIndex + findNextWhitespaceOrEndOfLine(line.substring(nextIndex));
+                       linePieces.add(line.substring(nextIndex, nextWhitespace));
+                       currentIndex = nextWhitespace;
+               }
+               linePieces.add("\n");
+               return linePieces;
+       }
+
+       private void removeTrailingWhitespaceParts(PartContainer parts) {
                for (int partIndex = parts.size() - 1; partIndex >= 0; --partIndex) {
                        Part part = parts.getPart(partIndex);
-                       if (!(part instanceof PlainTextPart) || !"\n".equals(((PlainTextPart) part).getText())) {
+                       if (!(part instanceof PlainTextPart) || !"\n".equals(part.getText())) {
                                break;
                        }
                        parts.removePart(partIndex);
                }
-               return parts;
+       }
+
+       private boolean linkMatchesPostingSone(SoneTextParserContext context, String link) {
+               return (context != null) && (context.getPostingSone() != null) && link.substring(4, Math.min(link.length(), 47)).equals(context.getPostingSone().getId());
+       }
+
+       private boolean lineIsLongEnoughToContainAPostLink(String line) {
+               return line.length() >= (7 + 36);
+       }
+
+       private static boolean lineIsLongEnoughToContainASoneLink(String line) {
+               return line.length() >= (7 + 43);
+       }
+
+       private int findNextWhitespaceOrEndOfLine(String line) {
+               Matcher matcher = whitespacePattern.matcher(line);
+               return matcher.find(0) ? matcher.start() : line.length();
+       }
+
+       private Optional<NextLink> findNextLink(String line) {
+               EnumMap<LinkType, Integer> linkTypeIndexes = new EnumMap<LinkType, Integer>(LinkType.class);
+               for (LinkType linkType : LinkType.values()) {
+                       int index = line.indexOf(linkType.getScheme());
+                       if (index != -1) {
+                               linkTypeIndexes.put(linkType, index);
+                       }
+               }
+               if (linkTypeIndexes.isEmpty()) {
+                       return absent();
+               }
+               Entry<LinkType, Integer> smallestEntry = from(linkTypeIndexes.entrySet()).toSortedList(locateSmallestIndex()).get(0);
+               return of(new NextLink(smallestEntry.getValue(), smallestEntry.getKey()));
+       }
+
+       private Comparator<Entry<LinkType, Integer>> locateSmallestIndex() {
+               return new Comparator<Entry<LinkType, Integer>>() {
+                       @Override
+                       public int compare(Entry<LinkType, Integer> leftEntry, Entry<LinkType, Integer> rightEntry) {
+                               return leftEntry.getValue() - rightEntry.getValue();
+                       }
+               };
+       }
+
+       private class PartCreators {
+
+               private final Map<LinkType, PartCreator> partCreators = ImmutableMap.<LinkType, PartCreator>builder()
+                               .put(LinkType.SONE, new SonePartCreator())
+                               .put(LinkType.POST, new PostPartCreator())
+                               .put(LinkType.KSK, new FreenetLinkPartCreator(LinkType.KSK))
+                               .put(LinkType.CHK, new FreenetLinkPartCreator(LinkType.CHK))
+                               .put(LinkType.SSK, new FreenetLinkPartCreator(LinkType.SSK))
+                               .put(LinkType.USK, new FreenetLinkPartCreator(LinkType.USK))
+                               .put(LinkType.HTTP, new InternetLinkPartCreator(LinkType.HTTP))
+                               .put(LinkType.HTTPS, new InternetLinkPartCreator(LinkType.HTTPS))
+                               .build();
+
+               public Optional<Part> createPart(LinkType linkType, String line, SoneTextParserContext context) {
+                       if (line.equals(linkType.getScheme())) {
+                               return of((Part) new PlainTextPart(line));
+                       }
+                       return partCreators.get(linkType).createPart(line, context);
+               }
+
+       }
+
+       private class SonePartCreator implements PartCreator {
+
+               @Override
+               public Optional<Part> createPart(String line, SoneTextParserContext context) {
+                       if (!lineIsLongEnoughToContainASoneLink(line)) {
+                               return absent();
+                       }
+                       String soneId = line.substring(7, 50);
+                       Optional<Sone> sone = database.getSone(soneId);
+                       if (!sone.isPresent()) {
+                               return absent();
+                       }
+                       return Optional.<Part>of(new SonePart(sone.get()));
+               }
+
+       }
+
+       private class PostPartCreator implements PartCreator {
+
+               @Override
+               public Optional<Part> createPart(String line, SoneTextParserContext context) {
+                       if (!lineIsLongEnoughToContainAPostLink(line)) {
+                               return absent();
+                       }
+                       String postId = line.substring(7, 43);
+                       Optional<Post> post = database.getPost(postId);
+                       if (!post.isPresent()) {
+                               return absent();
+                       }
+                       return Optional.<Part>of(new PostPart(post.get()));
+               }
+
+       }
+
+       private class FreenetLinkPartCreator implements PartCreator {
+
+               private final LinkType linkType;
+
+               protected FreenetLinkPartCreator(LinkType linkType) {
+                       this.linkType = linkType;
+               }
+
+               @Override
+               public Optional<Part> createPart(String link, SoneTextParserContext context) {
+                       String name = link;
+                       if (name.indexOf('?') > -1) {
+                               name = name.substring(0, name.indexOf('?'));
+                       }
+                       if (name.endsWith("/")) {
+                               name = name.substring(0, name.length() - 1);
+                       }
+                       try {
+                               FreenetURI 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.isSigned() && linkMatchesPostingSone(context, link);
+                               return Optional.<Part>of(new FreenetLinkPart(link, name, fromPostingSone));
+                       } catch (MalformedURLException mue1) {
+                               /* ignore. */
+                       } catch (NullPointerException npe1) {
+                               /* ignore. */
+                       } catch (ArrayIndexOutOfBoundsException aioobe1) {
+                               /* ignore. */
+                       }
+                       return absent();
+               }
+
+       }
+
+       private class InternetLinkPartCreator implements PartCreator {
+
+               private final LinkType linkType;
+
+               private InternetLinkPartCreator(LinkType linkType) {
+                       this.linkType = linkType;
+               }
+
+               @Override
+               public Optional<Part> createPart(String link, SoneTextParserContext context) {
+                       String name = link;
+                       name = link.substring(linkType.getScheme().length());
+                       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('?'));
+                       }
+                       return Optional.<Part>of(new LinkPart(link, name));
+               }
+
+       }
+
+       private interface PartCreator {
+
+               Optional<Part> createPart(String line, SoneTextParserContext context);
+
+       }
+
+       /**
+        * Container for position and type of the next link in a line.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       private static class NextLink {
+
+               private final int nextIndex;
+               private final LinkType linkType;
+
+               private NextLink(int nextIndex, LinkType linkType) {
+                       this.nextIndex = nextIndex;
+                       this.linkType = linkType;
+               }
+
+               private int getNextIndex() {
+                       return nextIndex;
+               }
+
+               private LinkType getLinkType() {
+                       return linkType;
+               }
+
        }
 
 }