Fixing find hastag
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/social-networking/social-util-library@176823 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
c1efacf654
commit
42e1e85992
|
@ -0,0 +1,45 @@
|
||||||
|
package org.gcube.socialnetworking.socialtoken;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class SanitizedTag {
|
||||||
|
|
||||||
|
private static final String TAG_REGEX = "^#[\\w-_]*";
|
||||||
|
|
||||||
|
private static final Pattern pattern;
|
||||||
|
|
||||||
|
static {
|
||||||
|
pattern = Pattern.compile(TAG_REGEX);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String tag;
|
||||||
|
protected String postfix;
|
||||||
|
|
||||||
|
public SanitizedTag(String string) throws Exception {
|
||||||
|
if(string==null || string.compareTo("")==0 || !string.startsWith("#")) {
|
||||||
|
throw new Exception(string + "is not a valid TAG");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Matcher matcher = SanitizedTag.pattern.matcher(string);
|
||||||
|
|
||||||
|
if(matcher.find()) {
|
||||||
|
tag = string.substring(matcher.start(), matcher.end());
|
||||||
|
postfix = string.substring(matcher.end());
|
||||||
|
}else {
|
||||||
|
throw new Exception(string + "is not a valid TAG");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTag() {
|
||||||
|
return tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPostfix() {
|
||||||
|
return postfix;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -5,7 +5,7 @@ import java.net.URL;
|
||||||
|
|
||||||
public class SanitizedURL {
|
public class SanitizedURL {
|
||||||
|
|
||||||
private static String FINAL_CHARACTERS_TO_REMOVE_FROM_LINK = "[\\.\\,\\;\\)\\:]";
|
private static String FINAL_CHARACTERS_TO_REMOVE = "[\\.\\,\\;\\)\\:]";
|
||||||
|
|
||||||
protected String prefix;
|
protected String prefix;
|
||||||
protected String postfix;
|
protected String postfix;
|
||||||
|
@ -27,7 +27,7 @@ public class SanitizedURL {
|
||||||
}
|
}
|
||||||
|
|
||||||
postfix = urlString.substring(urlString.length()-1);
|
postfix = urlString.substring(urlString.length()-1);
|
||||||
if(postfix.matches(FINAL_CHARACTERS_TO_REMOVE_FROM_LINK)) {
|
if(postfix.matches(FINAL_CHARACTERS_TO_REMOVE)) {
|
||||||
urlString = urlString.substring(0, urlString.length()-1);
|
urlString = urlString.substring(0, urlString.length()-1);
|
||||||
}else {
|
}else {
|
||||||
postfix = "";
|
postfix = "";
|
||||||
|
|
|
@ -46,10 +46,14 @@ public class SocialMessageParser {
|
||||||
String tokenString = token.getToken();
|
String tokenString = token.getToken();
|
||||||
if(tokenString.startsWith("#")) {
|
if(tokenString.startsWith("#")) {
|
||||||
TagToken tagToken = new TagToken(token);
|
TagToken tagToken = new TagToken(token);
|
||||||
tokens.add(tagToken);
|
try {
|
||||||
tagTokens.add(tagToken);
|
hashtags.add(tagToken.getTag());
|
||||||
hashtags.add(tagToken.getToken());
|
tokens.add(tagToken);
|
||||||
continue;
|
tagTokens.add(tagToken);
|
||||||
|
continue;
|
||||||
|
}catch (Exception e) {
|
||||||
|
// Not a valid tag
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
URL url = URLToken.isURL(tokenString);
|
URL url = URLToken.isURL(tokenString);
|
||||||
|
|
|
@ -5,6 +5,8 @@ import org.gcube.socialnetworking.tokenization.Token;
|
||||||
|
|
||||||
public class TagToken extends ReplaceableToken {
|
public class TagToken extends ReplaceableToken {
|
||||||
|
|
||||||
|
protected SanitizedTag sanitizedTag;
|
||||||
|
|
||||||
public TagToken(String token, String delimiter, int start, int end) {
|
public TagToken(String token, String delimiter, int start, int end) {
|
||||||
super(token, delimiter, start, end);
|
super(token, delimiter, start, end);
|
||||||
}
|
}
|
||||||
|
@ -15,11 +17,24 @@ public class TagToken extends ReplaceableToken {
|
||||||
|
|
||||||
public String getTokenReplacement() {
|
public String getTokenReplacement() {
|
||||||
if(!replaced) {
|
if(!replaced) {
|
||||||
String linkTarget = ReplaceableToken.createHref("", GCubeSocialNetworking.HASHTAG_OID, super.token);
|
try {
|
||||||
tokenReplacement = ReplaceableToken.createLink(linkTarget, token, null);
|
String tag = getTag();
|
||||||
|
String linkTarget = ReplaceableToken.createHref("", GCubeSocialNetworking.HASHTAG_OID, tag);
|
||||||
|
tokenReplacement = ReplaceableToken.createLink(linkTarget, tag, null) + sanitizedTag.getPostfix();
|
||||||
|
} catch(Exception e) {
|
||||||
|
tokenReplacement = token;
|
||||||
|
}
|
||||||
|
|
||||||
replaced = true;
|
replaced = true;
|
||||||
}
|
}
|
||||||
return tokenReplacement;
|
return tokenReplacement;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getTag() throws Exception {
|
||||||
|
if(sanitizedTag==null) {
|
||||||
|
sanitizedTag = new SanitizedTag(token);
|
||||||
|
}
|
||||||
|
return sanitizedTag.getTag();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,6 +55,10 @@ public class TestUnit {
|
||||||
public void testTest() {
|
public void testTest() {
|
||||||
String text = "Hello (https://doodle.com/poll/not-existing-poll)";
|
String text = "Hello (https://doodle.com/poll/not-existing-poll)";
|
||||||
logger.debug(findFirstLink(text));
|
logger.debug(findFirstLink(text));
|
||||||
|
|
||||||
|
text = "post \"a text with #hashtag);\"";
|
||||||
|
SocialMessageParser messageParser = new SocialMessageParser(text);
|
||||||
|
logger.debug(messageParser.getParsedMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue