View Javadoc
1   package com.exsoinn.util;
2   
3   import java.util.ArrayList;
4   import java.util.Collections;
5   import java.util.List;
6   import java.util.regex.Matcher;
7   import java.util.regex.Pattern;
8   
9   /**
10   * Encapsulate logic to "escape" characters that create issues when trying to form JSON from strings that contain
11   * such characters. This class contains methods for both escaping and unescaping, {@link EscapeUtil#escapeSpecialCharacters(String)}
12   * and {@link EscapeUtil#unescapeSpecialCharacters(String)} respectively.
13   *
14   * Created by QuijadaJ on 8/15/2017.
15   */
16  public class EscapeUtil {
17      /*
18       * Escape character substitutes
19       */
20      public static final String ESCAPE_SPACE = "__SPACE__";
21      public static final String ESCAPE_EQ_SIGN = "__EQ__";
22      public static final String ESCAPE_SEMI_COLON = "__SC__";
23      public static final String ESCAPE_DOUBLE_PIPE = "__DP__";
24      public static final String ESCAPE_COLON = "__COL__";
25      public static final String ESCAPE_DASH = "__DASH__";
26      public static final String ESCAPE_SQR_BRCK_OPEN = "__SBO__";
27      public static final String ESCAPE_SQR_BRCK_CLOSE = "__SBC__";
28      public static final String ESCAPE_BACK_SLASH = "__BS__";
29      public static final String ESCAPE_FORWARD_SLASH = "__FS__";
30  
31      //Newly Added
32      public static final String ESCAPE_QUESTION_MARK = "__QM__";
33      public static final String ESCAPE_ASTERISK = "__ASTRK__";
34      public static final String ESCAPE_CURLY_BRCK_OPEN = "__CRBO__";
35      public static final String ESCAPE_CURLY_BRCK_CLOSE = "__CRBC__";
36      public static final String ESCAPE_OPEN_BRCK = "__OPB__";
37      public static final String ESCAPE_CLS_BRCK = "__CLB__";
38      public static final String ESCAPE_COMMA = "__CM__";
39      public static final String ESCAPE_AMPERSAND = "__AMBR__";
40      public static final String ESCAPE_PLUS = "__PLS__";
41      public static final String ESCAPE_AT_MRK = "__ATM__";
42      public static final String ESCAPE_HASH_MRK = "__HSH__";
43      public static final String ESCAPE_SINGLE_PIPE = "__SPP__";
44      public static final String ESCAPE_TILT_MRK = "__TLT__";
45      public static final String ESCAPE_PRCNTG_MRK = "__PRCN__";
46      public static final String ESCAPE_CAP_MRK = "__CAP__";
47  
48  
49      /**
50       * Stores characters and what they're replacement should be within the Rule API. Escaping
51       * characters is sometimes required in order to be able to build a {@link com.exsoinn.util.epf.Context} object,
52       * which the Rule API heavily depends on.
53       */
54      public enum EscapeToken {
55          SPACE(" ", ESCAPE_SPACE, " "),
56          EQUAL_SIGN("=", ESCAPE_EQ_SIGN, "="),
57          SEMI_COLON(";", ESCAPE_SEMI_COLON, ";"),
58          DOUBLE_PIPE(Pattern.quote("||"), ESCAPE_DOUBLE_PIPE, "||"),
59          COLON(":", ESCAPE_COLON, ":"),
60          DASH("-", ESCAPE_DASH, "-"),
61          SQR_BRCK_OPEN(Pattern.quote("["), ESCAPE_SQR_BRCK_OPEN, "["),
62          SQR_BRCK_CLOSE(Pattern.quote("]"), ESCAPE_SQR_BRCK_CLOSE, "]"),
63          BACK_SLASH(Pattern.quote("\\"), ESCAPE_BACK_SLASH, "\\\\"),
64          FORWARD_SLASH(Pattern.quote("/"), ESCAPE_FORWARD_SLASH, "/"),
65  
66          //Newly Added
67          QUESTION_MARK(Pattern.quote("?"), ESCAPE_QUESTION_MARK, "?"),
68          ASTERISK(Pattern.quote("*"), ESCAPE_ASTERISK, "*"),
69          CURLY_BRCK_OPEN(Pattern.quote("{"), ESCAPE_CURLY_BRCK_OPEN, "{"),
70          CURLY_BRCK_CLOSE(Pattern.quote("}"), ESCAPE_CURLY_BRCK_CLOSE, "}"),
71          OPEN_BRCK(Pattern.quote("("), ESCAPE_OPEN_BRCK, "("),
72          CLS_BRCK(Pattern.quote(")"), ESCAPE_CLS_BRCK, ")"),
73          COMMA(Pattern.quote(","), ESCAPE_COMMA, ","),
74          AMPERSAND(Pattern.quote("&"), ESCAPE_AMPERSAND, "&"),
75          PLUS(Pattern.quote("+"), ESCAPE_PLUS, "+"),
76          AT_MRK(Pattern.quote("@"), ESCAPE_AT_MRK, "@"),
77          HASH_MRK(Pattern.quote("#"), ESCAPE_HASH_MRK, "#"),
78          SINGLE_PIPE(Pattern.quote("|"), ESCAPE_SINGLE_PIPE, "|"),
79          TILT_MRK(Pattern.quote("~"), ESCAPE_TILT_MRK, "~"),
80          PRCNTG_MRK(Pattern.quote("%"), ESCAPE_PRCNTG_MRK, "%"),
81          CAP_MRK(Pattern.quote("^"), ESCAPE_CAP_MRK, "^");
82  
83  
84          final private String targetString;
85          final private String replacement;
86          final private String restoreValue;
87  
88          public String targetString() {
89              return targetString;
90          }
91  
92          public String replacement() {
93              return replacement;
94          }
95  
96          public String restoreValue() {
97              return restoreValue;
98          }
99  
100         EscapeToken(String pStr, String pReplacement, String pRestVal) {
101             targetString = pStr;
102             replacement = pReplacement;
103             restoreValue = pRestVal;
104         }
105 
106 
107         /**
108          * Given a passed in string, returns what the replacement should be
109          * for escaping purposes. If not replacement is found, the passed in
110          * string is returned as is.
111          *
112          * @param pStr - pStr
113          * @return - TODO
114          */
115         public static String replacementFromString(String pStr)  {
116             for (EscapeToken c: EscapeToken.values()) {
117                 if (c.targetString.equals(pStr)) {
118                     return c.replacement();
119                 }
120             }
121 
122             return pStr;
123         }
124     }
125 
126 
127 
128     /**
129      * Un-escape any special characters contained in {pInStr.
130      * @param pInStr - String to un-escape
131      * @return - TODO
132      */
133     public static String unescapeSpecialCharacters(String pInStr) {
134         /**
135          * Change back any "escaped" (I.e. replaced) strings back to their originals
136          */
137         for (EscapeUtil.EscapeToken t : EscapeUtil.EscapeToken.values()) {
138             pInStr = pInStr.replaceAll(t.replacement(), t.restoreValue());
139         }
140 
141         return pInStr;
142     }
143 
144 
145     /**
146      * Escape any special characters contained in pInStr.
147      * @param pInStr - String to escape
148      * * @return - TODO
149      */
150     public static String escapeSpecialCharacters(String pInStr) {
151         for (EscapeUtil.EscapeToken t : EscapeUtil.EscapeToken.values()) {
152             pInStr = pInStr.replaceAll(t.targetString(), t.replacement());
153         }
154 
155         return pInStr;
156     }
157 
158 
159     /**
160      * Make this class non-instantiable to public
161      */
162     private EscapeUtil() {}
163 
164 }