Store in link type whether a link is an internet link.
[Sone.git] / src / main / java / net / pterodactylus / sone / text / SoneTextParser.java
index 6d81c0d..6086918 100644 (file)
@@ -28,15 +28,15 @@ import java.util.regex.Pattern;
 
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.database.PostProvider;
-import net.pterodactylus.sone.database.SoneProvider;
+import net.pterodactylus.sone.data.impl.DefaultSone;
+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 freenet.keys.FreenetURI;
 
+import com.google.common.base.Optional;
+
 /**
  * {@link Parser} implementation that can recognize Freenet URIs.
  *
@@ -58,40 +58,45 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
        private enum LinkType {
 
                /** Link is a KSK. */
-               KSK("KSK@"),
+               KSK("KSK@", true, false),
 
                /** Link is a CHK. */
-               CHK("CHK@"),
+               CHK("CHK@", true, false),
 
                /** Link is an SSK. */
-               SSK("SSK@"),
+               SSK("SSK@", true, false),
 
                /** Link is a USK. */
-               USK("USK@"),
+               USK("USK@", true, false),
 
                /** Link is HTTP. */
-               HTTP("http://"),
+               HTTP("http://", false, true),
 
                /** Link is HTTPS. */
-               HTTPS("https://"),
+               HTTPS("https://", false, true),
 
                /** Link is a Sone. */
-               SONE("sone://"),
+               SONE("sone://", false, false),
 
                /** Link is a post. */
-               POST("post://");
+               POST("post://", false, false);
 
                /** The scheme identifying this link type. */
                private final String scheme;
+               private final boolean freenetLink;
+               private final boolean internetLink;
 
                /**
                 * Creates a new link type identified by the given scheme.
                 *
                 * @param scheme
                 *            The scheme of the link type
+                * @param freenetLink
                 */
-               private LinkType(String scheme) {
+               private LinkType(String scheme, boolean freenetLink, boolean internetLink) {
                        this.scheme = scheme;
+                       this.freenetLink = freenetLink;
+                       this.internetLink = internetLink;
                }
 
                /**
@@ -103,34 +108,31 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                        return scheme;
                }
 
-       }
+               public boolean isFreenetLink() {
+                       return freenetLink;
+               }
 
-       /** The Sone provider. */
-       private final SoneProvider soneProvider;
+               public boolean isInternetLink() {
+                       return internetLink;
+               }
+
+       }
 
-       /** The post provider. */
-       private final PostProvider postProvider;
+       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();
@@ -209,7 +211,7 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                        }
 
                                        /* 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:"))) {
+                                       if (linkType.isFreenetLink() && (next >= 8) && (line.substring(next - 8, next).equals("freenet:"))) {
                                                next -= 8;
                                                line = line.substring(0, next) + line.substring(next + 8);
                                        }
@@ -221,7 +223,6 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                        if (next > 0) {
                                                parts.add(new PlainTextPart(line.substring(0, next)));
                                                line = line.substring(next);
-                                               next = 0;
                                        }
                                        lineComplete = false;
 
@@ -242,13 +243,13 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                        if (linkType == LinkType.SONE) {
                                                if (line.length() >= (7 + 43)) {
                                                        String soneId = line.substring(7, 50);
-                                                       Optional<Sone> sone = soneProvider.getSone(soneId);
+                                                       Optional<Sone> sone = database.getSone(soneId);
                                                        if (!sone.isPresent()) {
                                                                /*
                                                                 * don’t use create=true above, we don’t want
                                                                 * the empty shell.
                                                                 */
-                                                               sone = Optional.fromNullable(new Sone(soneId, false));
+                                                               sone = Optional.<Sone>of(new DefaultSone(database, soneId, false, null));
                                                        }
                                                        parts.add(new SonePart(sone.get()));
                                                        line = line.substring(50);
@@ -261,7 +262,7 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                        if (linkType == LinkType.POST) {
                                                if (line.length() >= (7 + 36)) {
                                                        String postId = line.substring(7, 43);
-                                                       Optional<Post> post = postProvider.getPost(postId);
+                                                       Optional<Post> post = database.getPost(postId);
                                                        if (post.isPresent()) {
                                                                parts.add(new PostPart(post.get()));
                                                        } else {
@@ -275,7 +276,7 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                                continue;
                                        }
 
-                                       if ((linkType == LinkType.KSK) || (linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) {
+                                       if (linkType.isFreenetLink()) {
                                                FreenetURI uri;
                                                if (name.indexOf('?') > -1) {
                                                        name = name.substring(0, name.indexOf('?'));
@@ -304,7 +305,7 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                                        /* oh, and these, too. */
                                                        parts.add(new PlainTextPart(link));
                                                }
-                                       } else if ((linkType == LinkType.HTTP) || (linkType == LinkType.HTTPS)) {
+                                       } else if (linkType.isInternetLink()) {
                                                name = link.substring(linkType == LinkType.HTTP ? 7 : 8);
                                                int firstSlash = name.indexOf('/');
                                                int lastSlash = name.lastIndexOf('/');
@@ -333,7 +334,7 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                }
                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);