View Javadoc
1   package org.springframework.security.oauth.common;
2   
3   import org.springframework.util.StringUtils;
4   import org.springframework.util.Assert;
5   
6   import java.util.HashMap;
7   import java.util.Map;
8   import java.util.ArrayList;
9   import java.util.List;
10  
11  /**
12   * Provides several <code>String</code> manipulation methods. Copied from deleted org.springframework.security.util.StringSplitUtils
13   */
14  public class StringSplitUtils {
15  
16    private static final String[] EMPTY_STRING_ARRAY = new String[0];
17  
18    /**
19     * Splits a <code>String</code> at the first instance of the delimiter. Does not include the delimiter in
20     * the response.
21     *
22     * @param toSplit   the string to split
23     * @param delimiter to split the string up with
24     * @return a two element array with index 0 being before the delimiter, and index 1 being after the delimiter
25     *         (neither element includes the delimiter)
26     * @throws IllegalArgumentException if an argument was invalid
27     */
28    public static String[] split(String toSplit, String delimiter) {
29      Assert.hasLength(toSplit, "Cannot split a null or empty string");
30      Assert.hasLength(delimiter, "Cannot use a null or empty delimiter to split a string");
31  
32      if (delimiter.length() != 1) {
33        throw new IllegalArgumentException("Delimiter can only be one character in length");
34      }
35  
36      int offset = toSplit.indexOf(delimiter);
37  
38      if (offset < 0) {
39        return null;
40      }
41  
42      String beforeDelimiter = toSplit.substring(0, offset);
43      String afterDelimiter = toSplit.substring(offset + 1);
44  
45      return new String[]{beforeDelimiter, afterDelimiter};
46    }
47  
48    /**
49     * Takes an array of <code>String</code>s, and for each element removes any instances of
50     * <code>removeCharacter</code>, and splits the element based on the <code>delimiter</code>. A <code>Map</code> is
51     * then generated, with the left of the delimiter providing the key, and the right of the delimiter providing the
52     * value. Will trim both the key and value before adding to the <code>Map</code>.
53     *
54     * @param array            the array to process
55     * @param delimiter        to split each element using (typically the equals symbol)
56     * @param removeCharacters one or more characters to remove from each element prior to attempting the split
57     *                         operation (typically the quotation mark symbol) or <code>null</code> if no removal should occur
58     * @return a <code>Map</code> representing the array contents, or <code>null</code> if the array to process was
59     *         null or empty
60     */
61    public static Map<String, String> splitEachArrayElementAndCreateMap(String[] array, String delimiter, String removeCharacters) {
62      if ((array == null) || (array.length == 0)) {
63        return null;
64      }
65  
66      Map<String, String> map = new HashMap<String, String>();
67  
68      for (int i = 0; i < array.length; i++) {
69        String postRemove;
70  
71        if (removeCharacters == null) {
72          postRemove = array[i];
73        } else {
74          postRemove = StringUtils.replace(array[i], removeCharacters, "");
75        }
76  
77        String[] splitThisArrayElement = split(postRemove, delimiter);
78  
79        if (splitThisArrayElement == null) {
80          continue;
81        }
82  
83        map.put(splitThisArrayElement[0].trim(), splitThisArrayElement[1].trim());
84      }
85  
86      return map;
87    }
88  
89    /**
90     * Splits a given string on the given separator character, skips the contents of quoted substrings
91     * when looking for separators.
92     * Introduced for use in DigestProcessingFilter (see SEC-506).
93     *
94     * This was copied and modified from commons-lang StringUtils
95     * @param str the string to split
96     * @param separatorChar the character by which to split the string
97     * @return String array containing split string
98     */
99    public static String[] splitIgnoringQuotes(String str, char separatorChar) {
100     if (str == null) {
101       return null;
102     }
103 
104     int len = str.length();
105 
106     if (len == 0) {
107       return EMPTY_STRING_ARRAY;
108     }
109 
110     List<String> list = new ArrayList<String>();
111     int i = 0;
112     int start = 0;
113     boolean match = false;
114 
115     while (i < len) {
116       if (str.charAt(i) == '"') {
117         i++;
118         while (i < len) {
119           if (str.charAt(i) == '"') {
120             i++;
121             break;
122           }
123           i++;
124         }
125         match = true;
126         continue;
127       }
128       if (str.charAt(i) == separatorChar) {
129         if (match) {
130           list.add(str.substring(start, i));
131           match = false;
132         }
133         start = ++i;
134         continue;
135       }
136       match = true;
137       i++;
138     }
139     if (match) {
140       list.add(str.substring(start, i));
141     }
142 
143     return list.toArray(new String[list.size()]);
144   }
145 
146 }