From 18338415117c61708e349222af566a09a9b490d6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 13 Jan 2011 14:36:29 +0100 Subject: [PATCH] Improve link parser. --- .../pterodactylus/sone/text/FreenetLinkParser.java | 43 +++++++++------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java b/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java index 63f68b8..73b33be 100644 --- a/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java +++ b/src/main/java/net/pterodactylus/sone/text/FreenetLinkParser.java @@ -147,36 +147,29 @@ public class FreenetLinkParser implements Parser { String name = link; logger.log(Level.FINER, "Found link: %s", link); logger.log(Level.FINEST, "Next: %d, CHK: %d, SSK: %d, USK: %d", new Object[] { next, nextChk, nextSsk, nextUsk }); - if (linkType == LinkType.KSK) { - name = link.substring(4); - } else if ((linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) { - if (name.indexOf('/') > -1) { - if (!name.endsWith("/")) { - name = name.substring(name.lastIndexOf('/') + 1); - } else { - if (name.indexOf('/') != name.lastIndexOf('/')) { - name = name.substring(name.lastIndexOf('/', name.lastIndexOf('/') - 1)); - } else { - /* shorten to 5 chars. */ - name = name.substring(4, Math.min(9, name.length())); - } - } - } + + 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('?')); } - boolean fromPostingSone = false; - if ((linkType == LinkType.SSK) || (linkType == LinkType.USK)) { - try { - new FreenetURI(link); - fromPostingSone = link.substring(4, Math.min(link.length(), 47)).equals(context.getPostingSone().getId()); - parts.add(fromPostingSone ? createTrustedFreenetLinkPart(link, name) : createFreenetLinkPart(link, name)); - } catch (MalformedURLException mue1) { - /* it’s not a valid link. */ - parts.add(createPlainTextPart(link)); + 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())); } - } else { + boolean fromPostingSone = ((linkType == LinkType.SSK) || (linkType == LinkType.USK)) && link.substring(4, Math.min(link.length(), 47)).equals(context.getPostingSone().getId()); parts.add(fromPostingSone ? createTrustedFreenetLinkPart(link, name) : createFreenetLinkPart(link, name)); + } catch (MalformedURLException mue1) { + /* not a valid link, insert as plain text. */ + parts.add(createPlainTextPart(link)); } } else if ((linkType == LinkType.HTTP) || (linkType == LinkType.HTTPS)) { name = link.substring(linkType == LinkType.HTTP ? 7 : 8); -- 2.7.4