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
13
14 public class StringSplitUtils {
15
16 private static final String[] EMPTY_STRING_ARRAY = new String[0];
17
18
19
20
21
22
23
24
25
26
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
50
51
52
53
54
55
56
57
58
59
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
91
92
93
94
95
96
97
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 }