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:
Luca Frosini 2019-01-25 14:27:32 +00:00
parent c1efacf654
commit 42e1e85992
5 changed files with 76 additions and 8 deletions

View File

@ -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;
}
}

View File

@ -5,7 +5,7 @@ import java.net.URL;
public class SanitizedURL {
private static String FINAL_CHARACTERS_TO_REMOVE_FROM_LINK = "[\\.\\,\\;\\)\\:]";
private static String FINAL_CHARACTERS_TO_REMOVE = "[\\.\\,\\;\\)\\:]";
protected String prefix;
protected String postfix;
@ -27,7 +27,7 @@ public class SanitizedURL {
}
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);
}else {
postfix = "";

View File

@ -46,10 +46,14 @@ public class SocialMessageParser {
String tokenString = token.getToken();
if(tokenString.startsWith("#")) {
TagToken tagToken = new TagToken(token);
tokens.add(tagToken);
tagTokens.add(tagToken);
hashtags.add(tagToken.getToken());
continue;
try {
hashtags.add(tagToken.getTag());
tokens.add(tagToken);
tagTokens.add(tagToken);
continue;
}catch (Exception e) {
// Not a valid tag
}
}
URL url = URLToken.isURL(tokenString);

View File

@ -5,6 +5,8 @@ import org.gcube.socialnetworking.tokenization.Token;
public class TagToken extends ReplaceableToken {
protected SanitizedTag sanitizedTag;
public TagToken(String token, String delimiter, int start, int end) {
super(token, delimiter, start, end);
}
@ -15,11 +17,24 @@ public class TagToken extends ReplaceableToken {
public String getTokenReplacement() {
if(!replaced) {
String linkTarget = ReplaceableToken.createHref("", GCubeSocialNetworking.HASHTAG_OID, super.token);
tokenReplacement = ReplaceableToken.createLink(linkTarget, token, null);
try {
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;
}
return tokenReplacement;
}
public String getTag() throws Exception {
if(sanitizedTag==null) {
sanitizedTag = new SanitizedTag(token);
}
return sanitizedTag.getTag();
}
}

View File

@ -55,6 +55,10 @@ public class TestUnit {
public void testTest() {
String text = "Hello (https://doodle.com/poll/not-existing-poll)";
logger.debug(findFirstLink(text));
text = "post \"a text with #hashtag);\"";
SocialMessageParser messageParser = new SocialMessageParser(text);
logger.debug(messageParser.getParsedMessage());
}
}