From a8d0c63f09f1c99c9b407e5e0da68e6a6d31595c Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 27 Nov 2010 11:27:38 +0100 Subject: [PATCH] =?utf8?q?Store=20the=20recognized=20link=20type,=20don?= =?utf8?q?=E2=80=99t=20rely=20on=20positions.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/sone/text/FreenetLinkParser.java | 28 +++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java b/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java index 7749b00..34edc48 100644 --- a/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java +++ b/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java @@ -42,6 +42,27 @@ public class FreenetLinkParser implements Parser { /** Pattern to detect whitespace. */ private static final Pattern whitespacePattern = Pattern.compile("[\\p{javaWhitespace}]"); + /** + * Enumeration for all recognized link types. + * + * @author David ‘Bombe’ Roden + */ + private enum LinkType { + + /** Link is a KSK. */ + KSK, + + /** Link is a CHK. */ + CHK, + + /** Link is an SSK. */ + SSK, + + /** Link is a USK. */ + USK + + } + /** The template factory. */ private final TemplateFactory templateFactory; @@ -79,17 +100,22 @@ public class FreenetLinkParser implements Parser { 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; } Matcher matcher = whitespacePattern.matcher(line); int nextSpace = matcher.find(next) ? matcher.start() : line.length(); @@ -99,7 +125,7 @@ public class FreenetLinkParser implements Parser { String name = link; logger.log(Level.FINER, "Found link: " + link); logger.log(Level.FINEST, "Next: %d, CHK: %d, SSK: %d, USK: %d", new Object[] { next, nextChk, nextSsk, nextUsk }); - if (((next == nextChk) || (next == nextSsk) || (next == nextUsk)) && (link.length() > 98) && (link.charAt(47) == ',') && (link.charAt(91) == ',') && (link.charAt(99) == '/')) { + if (((linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) && (link.length() > 98) && (link.charAt(47) == ',') && (link.charAt(91) == ',') && (link.charAt(99) == '/')) { name = link.substring(0, 47) + "…" + link.substring(99); } parts.add(createLinkPart(link, name)); -- 2.7.4