Merge branch 'next' into new-database-38
[Sone.git] / src / main / java / net / pterodactylus / sone / text / SoneTextParser.java
index 19db65b..2c209d5 100644 (file)
@@ -56,28 +56,50 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
        private enum LinkType {
 
                /** Link is a KSK. */
-               KSK,
+               KSK("KSK@"),
 
                /** Link is a CHK. */
-               CHK,
+               CHK("CHK@"),
 
                /** Link is an SSK. */
-               SSK,
+               SSK("SSK@"),
 
                /** Link is a USK. */
-               USK,
+               USK("USK@"),
 
                /** Link is HTTP. */
-               HTTP,
+               HTTP("http://"),
 
                /** Link is HTTPS. */
-               HTTPS,
+               HTTPS("https://"),
 
                /** Link is a Sone. */
-               SONE,
+               SONE("sone://"),
 
                /** Link is a post. */
-               POST,
+               POST("post://");
+
+               /** The scheme identifying this link type. */
+               private final String scheme;
+
+               /**
+                * Creates a new link type identified by the given scheme.
+                *
+                * @param scheme
+                *            The scheme of the link type
+                */
+               private LinkType(String scheme) {
+                       this.scheme = scheme;
+               }
+
+               /**
+                * Returns the scheme of this link type.
+                *
+                * @return The scheme of this link type
+                */
+               public String getScheme() {
+                       return scheme;
+               }
 
        }
 
@@ -201,6 +223,20 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                        }
                                        lineComplete = false;
 
+                                       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 there is no text after the scheme, it’s not a link! */
+                                       if (link.equals(linkType.getScheme())) {
+                                               parts.add(new PlainTextPart(linkType.getScheme()));
+                                               line = line.substring(linkType.getScheme().length());
+                                               continue;
+                                       }
+
                                        if (linkType == LinkType.SONE) {
                                                if (line.length() >= (7 + 43)) {
                                                        String soneId = line.substring(7, 50);
@@ -236,12 +272,6 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                                }
                                                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;