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:
parent
77bb3403db
commit
93e88121de
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -7,8 +7,6 @@ import java.util.Map;
|
||||||
|
|
||||||
public class URLToken extends ReplaceableToken {
|
public class URLToken extends ReplaceableToken {
|
||||||
|
|
||||||
private static String FINAL_CHARACTERS_TO_REMOVE_FROM_LINK = "[\\.\\,\\;\\)\\:]";
|
|
||||||
|
|
||||||
public URLToken(String token, String delimiter, int start, int end) {
|
public URLToken(String token, String delimiter, int start, int end) {
|
||||||
super(token, delimiter, start, end);
|
super(token, delimiter, start, end);
|
||||||
}
|
}
|
||||||
|
@ -17,15 +15,10 @@ public class URLToken extends ReplaceableToken {
|
||||||
super(token);
|
super(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static URL isURL(String string) {
|
public static URL isURL(String url) {
|
||||||
try {
|
try {
|
||||||
if(string.startsWith("(")) {
|
SanitizedURL sanitizedURL = new SanitizedURL(url);
|
||||||
string = string.substring(1);
|
return sanitizedURL.getURL();
|
||||||
}
|
|
||||||
if(string.startsWith("www.")) {
|
|
||||||
string = "http://" + string;
|
|
||||||
}
|
|
||||||
return new URL(string);
|
|
||||||
} catch(MalformedURLException e) {
|
} catch(MalformedURLException e) {
|
||||||
// not an URL
|
// not an URL
|
||||||
return null;
|
return null;
|
||||||
|
@ -35,23 +28,15 @@ public class URLToken extends ReplaceableToken {
|
||||||
|
|
||||||
public String getTokenReplacement() {
|
public String getTokenReplacement() {
|
||||||
if(!replaced) {
|
if(!replaced) {
|
||||||
// Testing if it is an URL preceded by (
|
try {
|
||||||
String prefix = "";
|
Map<String,String> anchorAttibutes = new HashMap<>(1);
|
||||||
if(tokenReplacement.startsWith("(")) {
|
anchorAttibutes.put("target", "_blank");
|
||||||
tokenReplacement = token.substring(1);
|
SanitizedURL sanitizedURL = new SanitizedURL(token);
|
||||||
prefix = "(";
|
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;
|
replaced = true;
|
||||||
}
|
}
|
||||||
return tokenReplacement;
|
return tokenReplacement;
|
||||||
|
|
Loading…
Reference in New Issue