From 93e88121de827eb29b28d0d138b6b80690585021 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Thu, 24 Jan 2019 10:38:34 +0000 Subject: [PATCH] Better implementation of URL identification git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/social-networking/social-util-library@176778 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../socialnetworking/token/SanitizedURL.java | 47 +++++++++++++++++++ .../socialnetworking/token/URLToken.java | 37 +++++---------- 2 files changed, 58 insertions(+), 26 deletions(-) create mode 100644 src/main/java/org/gcube/socialnetworking/token/SanitizedURL.java diff --git a/src/main/java/org/gcube/socialnetworking/token/SanitizedURL.java b/src/main/java/org/gcube/socialnetworking/token/SanitizedURL.java new file mode 100644 index 0000000..96fef58 --- /dev/null +++ b/src/main/java/org/gcube/socialnetworking/token/SanitizedURL.java @@ -0,0 +1,47 @@ +package org.gcube.socialnetworking.token; + +import java.net.MalformedURLException; +import java.net.URL; + +public class SanitizedURL { + + private static String FINAL_CHARACTERS_TO_REMOVE_FROM_LINK = "[\\.\\,\\;\\)\\:]"; + + protected String prefix; + protected String postfix; + protected final URL url; + + public SanitizedURL(String urlString) throws MalformedURLException { + prefix = ""; + if(urlString.startsWith("(")) { + prefix = urlString.substring(0, 1); + urlString = urlString.substring(1); + } + + if(urlString.startsWith("www.")) { + urlString = "http://" + urlString; + } + + postfix = urlString.substring(urlString.length()-1); + if(postfix.matches(FINAL_CHARACTERS_TO_REMOVE_FROM_LINK)) { + urlString = urlString.substring(0, urlString.length()-1); + }else { + postfix = ""; + } + + url = new URL(urlString); + } + + public String getPrefix() { + return prefix; + } + + public String getPostfix() { + return postfix; + } + + public URL getURL() { + return url; + } + +} \ No newline at end of file diff --git a/src/main/java/org/gcube/socialnetworking/token/URLToken.java b/src/main/java/org/gcube/socialnetworking/token/URLToken.java index e33dcaf..856785f 100644 --- a/src/main/java/org/gcube/socialnetworking/token/URLToken.java +++ b/src/main/java/org/gcube/socialnetworking/token/URLToken.java @@ -7,8 +7,6 @@ import java.util.Map; public class URLToken extends ReplaceableToken { - private static String FINAL_CHARACTERS_TO_REMOVE_FROM_LINK = "[\\.\\,\\;\\)\\:]"; - public URLToken(String token, String delimiter, int start, int end) { super(token, delimiter, start, end); } @@ -17,15 +15,10 @@ public class URLToken extends ReplaceableToken { super(token); } - public static URL isURL(String string) { + public static URL isURL(String url) { try { - if(string.startsWith("(")) { - string = string.substring(1); - } - if(string.startsWith("www.")) { - string = "http://" + string; - } - return new URL(string); + SanitizedURL sanitizedURL = new SanitizedURL(url); + return sanitizedURL.getURL(); } catch(MalformedURLException e) { // not an URL return null; @@ -35,23 +28,15 @@ public class URLToken extends ReplaceableToken { public String getTokenReplacement() { if(!replaced) { - // Testing if it is an URL preceded by ( - String prefix = ""; - if(tokenReplacement.startsWith("(")) { - tokenReplacement = token.substring(1); - prefix = "("; + try { + Map anchorAttibutes = new HashMap<>(1); + anchorAttibutes.put("target", "_blank"); + SanitizedURL sanitizedURL = new SanitizedURL(token); + String url = sanitizedURL.getURL().toString(); + tokenReplacement = sanitizedURL.getPrefix() + ReplaceableToken.createLink(url, url, anchorAttibutes) + sanitizedURL.getPostfix(); + }catch(MalformedURLException e) { + tokenReplacement = token; } - - Map anchorAttibutes = new HashMap<>(1); - anchorAttibutes.put("target", "_blank"); - String url = tokenReplacement; - String postfix = tokenReplacement.substring(tokenReplacement.length()-1); - if(postfix.matches(FINAL_CHARACTERS_TO_REMOVE_FROM_LINK)) { - url = tokenReplacement.substring(0, token.length()-1); - }else { - postfix = ""; - } - tokenReplacement = prefix + ReplaceableToken.createLink(url, url, anchorAttibutes) + postfix; replaced = true; } return tokenReplacement;