-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonStringUtils.java
More file actions
80 lines (72 loc) · 2.74 KB
/
CommonStringUtils.java
File metadata and controls
80 lines (72 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
@Slf4j
public class CommonStringUtils {
private static final String DEFAULT_ENCODING = "UTF-8";
private static final String NON_ALPHA_NUMERIC_CHARACTERS_LEAVING_SPACE_REGEX = "[^a-zA-Z0-9\\s]";
private SearchStringUtils() {
/**
* This is a Utility Class
*/
}
/**
* @param encodedString Encoded String in UTF-8 format
* @return decoded or empty string(in case of error)
*/
public static String getDecodedString(String encodedString) {
try {
String decodedString = StringEscapeUtils.unescapeHtml4(
URLDecoder.decode(encodedString, CommonConstants.DEFAULT_ENCODING));
log.debug("[{}] is decoded to [{}]", encodedString, decodedString);
return decodedString;
} catch (UnsupportedEncodingException e) {
log.error("Error while decoding string", e);
return "";
}catch(Exception e){
log.info("String [{}] is not properly encoded", encodedString, e);
String unescapedString = StringEscapeUtils.unescapeHtml4(encodedString);
log.debug("[{}] is decoded to [{}]", encodedString, unescapedString);
return unescapedString;
}
}
/**
* @param anyString any string.
* @return String where diactrictices are converted to normal character.
*/
public static String getStrippedAccentString(String anyString) {
return StringUtils.stripAccents(anyString);
}
/**
* @param anyString any string
* @return get string with alpha numeric characters and space
*/
public static String getAlphanumericString(String anyString) {
String stringStrippedOfSpecialCharacter = anyString
.replaceAll(NON_ALPHA_NUMERIC_CHARACTERS_LEAVING_SPACE_REGEX, "");
log.debug("The string [{}] became [{}]", anyString, stringStrippedOfSpecialCharacter);
return stringStrippedOfSpecialCharacter;
}
/**
* @param anyString any string
* @return converts entire string to lower case.
*/
public static String getLowerCaseString(String anyString) {
return anyString.toLowerCase();
}
/**
* @param anyString any string
* @return removes leading and trailing white spaces and extra white spaces in b/w words.
*/
public static String getTrimString(String anyString) {
return anyString.trim().replaceAll(" +", " ");
}
public static String processSearchTerm(String searchTerm) {
String processedSearchTerm = getLowerCaseString(getTrimString(getAlphanumericString(
getStrippedAccentString(getDecodedString(searchTerm)))));
log.debug("[{}] is processed to [{}]", searchTerm, processedSearchTerm);
return processedSearchTerm;
}
}