2019-01-24 11:47:03 +01:00
|
|
|
package org.gcube.socialnetworking.socialtoken;
|
2019-01-22 18:22:17 +01:00
|
|
|
|
|
|
|
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 18:01:42 +01:00
|
|
|
public URLToken(String token, String delimiter, int start, int end) {
|
|
|
|
super(token, delimiter, start, end);
|
|
|
|
}
|
|
|
|
|
2019-01-23 17:57:50 +01:00
|
|
|
public URLToken(Token token) {
|
2019-01-22 18:22:17 +01:00
|
|
|
super(token);
|
|
|
|
}
|
|
|
|
|
2019-01-24 11:38:34 +01:00
|
|
|
public static URL isURL(String url) {
|
2019-01-22 18:22:17 +01:00
|
|
|
try {
|
2019-01-24 11:38:34 +01:00
|
|
|
SanitizedURL sanitizedURL = new SanitizedURL(url);
|
|
|
|
return sanitizedURL.getURL();
|
2019-01-22 18:22:17 +01:00
|
|
|
} catch(MalformedURLException e) {
|
|
|
|
// not an URL
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-01-23 14:58:59 +01:00
|
|
|
public String getTokenReplacement() {
|
|
|
|
if(!replaced) {
|
2019-01-24 11:38:34 +01:00
|
|
|
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;
|
2019-01-23 17:57:50 +01:00
|
|
|
}
|
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
|
|
|
}
|