Extract functions to detect long-enough links.
[Sone.git] / src / main / java / net / pterodactylus / sone / text / SoneTextParser.java
index 4f9900a..efb36c4 100644 (file)
@@ -194,8 +194,7 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                        }
                                        lineComplete = false;
 
-                                       Matcher matcher = whitespacePattern.matcher(line);
-                                       int nextSpace = matcher.find(0) ? matcher.start() : line.length();
+                                       int nextSpace = findNextWhitespace(line);
                                        String link = line.substring(0, nextSpace);
                                        String name = link;
                                        logger.log(Level.FINER, String.format("Found link: %s", link));
@@ -208,7 +207,7 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                        }
 
                                        if (linkType == LinkType.SONE) {
-                                               if (line.length() >= (7 + 43)) {
+                                               if (lineIsLongEnoughToContainASoneLink(line)) {
                                                        String soneId = line.substring(7, 50);
                                                        Optional<Sone> sone = database.getSone(soneId);
                                                        if (!sone.isPresent()) {
@@ -227,7 +226,7 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                                continue;
                                        }
                                        if (linkType == LinkType.POST) {
-                                               if (line.length() >= (7 + 36)) {
+                                               if (lineIsLongEnoughToContainAPostLink(line)) {
                                                        String postId = line.substring(7, 43);
                                                        Optional<Post> post = database.getPost(postId);
                                                        if (post.isPresent()) {
@@ -260,7 +259,7 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                                        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());
+                                                       boolean fromPostingSone = ((linkType == LinkType.SSK) || (linkType == LinkType.USK)) && linkMatchesPostingSone(context, link);
                                                        parts.add(new FreenetLinkPart(link, name, fromPostingSone));
                                                } catch (MalformedURLException mue1) {
                                                        /* not a valid link, insert as plain text. */
@@ -299,6 +298,11 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                Closer.close(bufferedReader);
                        }
                }
+               removeTrailingWhitespaceParts(parts);
+               return parts;
+       }
+
+       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(part.getText())) {
@@ -306,7 +310,23 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                        }
                        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 boolean lineIsLongEnoughToContainASoneLink(String line) {
+               return line.length() >= (7 + 43);
+       }
+
+       private int findNextWhitespace(String line) {
+               Matcher matcher = whitespacePattern.matcher(line);
+               return matcher.find(0) ? matcher.start() : line.length();
        }
 
        private Optional<NextLink> findNextLink(String line) {