Implementing new Solution refs #13207

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/social-networking/social-util-library@176732 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2019-01-22 17:22:17 +00:00
parent ee601e09d2
commit f69ff631eb
6 changed files with 218 additions and 0 deletions

View File

@ -0,0 +1,61 @@
package org.gcube.social_networking.socialutillibrary;
import java.io.StringWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.gcube.socialnetworking.token.ReplaceableToken;
import org.gcube.socialnetworking.token.TagToken;
import org.gcube.socialnetworking.token.Token;
import org.gcube.socialnetworking.token.URLToken;
public class MessageParser {
private final String originalMessage;
private StringWriter stringWriter;
private List<ReplaceableToken> tokens;
public MessageParser(String message) {
this.originalMessage = message;
}
public List<ReplaceableToken> getTokens() {
if(tokens==null){
tokens = new ArrayList<>();
SocialStringTokenizer socialStringTokenizer = new SocialStringTokenizer(originalMessage);
for(Token token : socialStringTokenizer.getTokens()) {
String tokenString = token.getToken();
if(tokenString.startsWith("#")) {
TagToken tagToken = new TagToken(token);
tokens.add(tagToken);
continue;
}
URL url = URLToken.isURL(tokenString);
if(url!=null) {
URLToken urlToken = new URLToken(token, url);
tokens.add(urlToken);
continue;
}
ReplaceableToken replaceableToken = new ReplaceableToken(token);
tokens.add(replaceableToken);
}
}
return tokens;
}
public String getParsedMessage() {
if(stringWriter==null) {
stringWriter = new StringWriter();
for(ReplaceableToken token : getTokens()) {
stringWriter.append(token.getTokenReplacement());
stringWriter.append(token.getDelimiter());
}
}
return stringWriter.toString();
}
}

View File

@ -0,0 +1,49 @@
package org.gcube.social_networking.socialutillibrary;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.gcube.socialnetworking.token.Token;
public class SocialStringTokenizer {
private final String originalString;
private Pattern pattern;
private Matcher matcher;
private List<Token> tokens;
public SocialStringTokenizer(String string) {
this.originalString = string;
this.pattern = Pattern.compile("\\s");
this.matcher = pattern.matcher(originalString);
}
public List<Token> getTokens() {
if(tokens==null) {
tokens = new ArrayList<>();
int tokenStart = 0;
while(matcher.find()) {
int tokenEnd = matcher.start();
String tokenString = originalString.substring(tokenStart, tokenEnd);
int delimiterStart = tokenEnd;
int delimiterEnd = matcher.end();
String delimiter = originalString.substring(delimiterStart, delimiterEnd);
Token token = new Token(tokenString, delimiter, tokenStart, tokenEnd);
tokens.add(token);
tokenStart = delimiterEnd;
}
if(tokenStart!=originalString.length()){
int tokenEnd = originalString.length();
String tokenString = originalString.substring(tokenStart, tokenEnd);
Token token = new Token(tokenString, "", tokenStart, tokenEnd);
tokens.add(token);
}
}
return tokens;
}
}

View File

@ -0,0 +1,21 @@
package org.gcube.socialnetworking.token;
public class ReplaceableToken extends Token {
protected String tokenReplacement;
public ReplaceableToken(Token token) {
super(token.token, token.delimiter, token.start, token.end);
this.tokenReplacement = token.token;
}
public ReplaceableToken(Token token, String tokenReplacement) {
super(token.token, token.delimiter, token.start, token.end);
this.tokenReplacement = tokenReplacement;
}
public String getTokenReplacement() {
return tokenReplacement;
}
}

View File

@ -0,0 +1,9 @@
package org.gcube.socialnetworking.token;
public class TagToken extends ReplaceableToken {
public TagToken(Token token) {
super(token, "#MY_MASSI_TAG");
}
}

View File

@ -0,0 +1,45 @@
package org.gcube.socialnetworking.token;
public class Token{
final String token;
final String delimiter;
final int start;
final int end;
/**
* @param token the Token String
* @param delimiter the delimiter after token
* @param start the start point in the original String
* @param end the end point in the original String
*/
public Token(String token, String delimiter, int start, int end){
this.token = token;
this.delimiter = delimiter;
this.start = start;
this.end = end;
}
public String getToken() {
return token;
}
public String getDelimiter() {
return delimiter;
}
public int getStart() {
return start;
}
public int getEnd() {
return end;
}
@Override
public String toString() {
return String.format("Token '%s', Subsequent delimiter '%s', Start '%d', End '%d'", token, delimiter, start, end);
}
}

View File

@ -0,0 +1,33 @@
package org.gcube.socialnetworking.token;
import java.net.MalformedURLException;
import java.net.URL;
public class URLToken extends ReplaceableToken {
private String getReplacement(URL url) {
StringBuilder sb = new StringBuilder();
return sb.toString();
}
public URLToken(Token token, URL url) {
super(token);
this.tokenReplacement = "___URL____";
}
public static URL isURL(String string) {
if(string.startsWith("www.")) {
string = "http://" + string;
}
try {
return new URL(string);
} catch(MalformedURLException e) {
// not an URL
return null;
}
}
}