2019-01-22 18:22:17 +01:00
|
|
|
package org.gcube.socialnetworking.token;
|
|
|
|
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2019-01-23 14:58:59 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2019-01-22 18:22:17 +01:00
|
|
|
|
|
|
|
public class URLToken extends ReplaceableToken {
|
|
|
|
|
2019-01-23 17:57:50 +01:00
|
|
|
private static String FINAL_CHARACTERS_TO_REMOVE_FROM_LINK = "[\\.\\,\\;\\)]";
|
|
|
|
|
|
|
|
public URLToken(Token token) {
|
2019-01-22 18:22:17 +01:00
|
|
|
super(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static URL isURL(String string) {
|
|
|
|
try {
|
2019-01-23 17:57:50 +01:00
|
|
|
if(string.startsWith("www.")) {
|
|
|
|
string = "http://" + string;
|
|
|
|
}
|
2019-01-22 18:22:17 +01:00
|
|
|
return new URL(string);
|
|
|
|
} catch(MalformedURLException e) {
|
|
|
|
// not an URL
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-01-23 14:58:59 +01:00
|
|
|
public String getTokenReplacement() {
|
|
|
|
if(!replaced) {
|
2019-01-23 17:57:50 +01:00
|
|
|
Map<String,String> anchorAttibutes = new HashMap<>(1);
|
2019-01-23 14:58:59 +01:00
|
|
|
anchorAttibutes.put("target", "_blank");
|
2019-01-23 17:57:50 +01:00
|
|
|
String url = token;
|
|
|
|
String lastCharacters = token.substring(token.length()-1);
|
|
|
|
if(lastCharacters.matches(FINAL_CHARACTERS_TO_REMOVE_FROM_LINK)) {
|
|
|
|
url = token.substring(0, token.length()-1);
|
|
|
|
}else {
|
|
|
|
lastCharacters = "";
|
|
|
|
}
|
|
|
|
tokenReplacement = ReplaceableToken.createLink(url, url, anchorAttibutes) + lastCharacters;
|
2019-01-23 14:58:59 +01:00
|
|
|
replaced = true;
|
|
|
|
}
|
2019-01-23 17:05:15 +01:00
|
|
|
return tokenReplacement;
|
2019-01-23 14:58:59 +01:00
|
|
|
}
|
|
|
|
|
2019-01-22 18:22:17 +01:00
|
|
|
}
|