51 lines
1.2 KiB
Java
51 lines
1.2 KiB
Java
package org.gcube.socialnetworking.token;
|
|
|
|
import java.net.MalformedURLException;
|
|
import java.net.URL;
|
|
import java.util.HashMap;
|
|
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);
|
|
}
|
|
|
|
public URLToken(Token token) {
|
|
super(token);
|
|
}
|
|
|
|
public static URL isURL(String string) {
|
|
try {
|
|
if(string.startsWith("www.")) {
|
|
string = "http://" + string;
|
|
}
|
|
return new URL(string);
|
|
} catch(MalformedURLException e) {
|
|
// not an URL
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|
|
public String getTokenReplacement() {
|
|
if(!replaced) {
|
|
Map<String,String> anchorAttibutes = new HashMap<>(1);
|
|
anchorAttibutes.put("target", "_blank");
|
|
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;
|
|
replaced = true;
|
|
}
|
|
return tokenReplacement;
|
|
}
|
|
|
|
}
|