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
This commit is contained in:
Luca Frosini 2019-01-24 10:38:34 +00:00
parent 77bb3403db
commit 93e88121de
2 changed files with 58 additions and 26 deletions

View File

@ -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;
}
}

View File

@ -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<String,String> 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<String,String> 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;