1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.util;
17
18 import org.springframework.util.Assert;
19 import org.springframework.util.StringUtils;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24
25 /***
26 * Provides several <code>String</code> manipulation methods.
27 *
28 * @author Ben Alex
29 * @version $Id: StringSplitUtils.java,v 1.3 2005/11/17 00:56:09 benalex Exp $
30 */
31 public class StringSplitUtils {
32
33
34 /***
35 * Splits a <code>String</code> at the first instance of the delimiter.
36 *
37 * <p>
38 * Does not include the delimiter in the response.
39 * </p>
40 *
41 * @param toSplit the string to split
42 * @param delimiter to split the string up with
43 *
44 * @return a two element array with index 0 being before the delimiter, and
45 * index 1 being after the delimiter (neither element includes the
46 * delimiter)
47 *
48 * @throws IllegalArgumentException if an argument was invalid
49 */
50 public static String[] split(String toSplit, String delimiter) {
51 Assert.hasLength(toSplit, "Cannot split a null or empty string");
52 Assert.hasLength(delimiter,
53 "Cannot use a null or empty delimiter to split a string");
54
55 if (delimiter.length() != 1) {
56 throw new IllegalArgumentException(
57 "Delimiter can only be one character in length");
58 }
59
60 int offset = toSplit.indexOf(delimiter);
61
62 if (offset < 0) {
63 return null;
64 }
65
66 String beforeDelimiter = toSplit.substring(0, offset);
67 String afterDelimiter = toSplit.substring(offset + 1);
68
69 return new String[] {beforeDelimiter, afterDelimiter};
70 }
71
72 /***
73 * Takes an array of <code>String</code>s, and for each element removes any
74 * instances of <code>removeCharacter</code>, and splits the element based
75 * on the <code>delimiter</code>. A <code>Map</code> is then generated,
76 * with the left of the delimiter providing the key, and the right of the
77 * delimiter providing the value.
78 *
79 * <p>
80 * Will trim both the key and value before adding to the <code>Map</code>.
81 * </p>
82 *
83 * @param array the array to process
84 * @param delimiter to split each element using (typically the equals
85 * symbol)
86 * @param removeCharacters one or more characters to remove from each
87 * element prior to attempting the split operation (typically the
88 * quotation mark symbol) or <code>null</code> if no removal should
89 * occur
90 *
91 * @return a <code>Map</code> representing the array contents, or
92 * <code>null</code> if the array to process was null or empty
93 */
94 public static Map splitEachArrayElementAndCreateMap(String[] array,
95 String delimiter, String removeCharacters) {
96 if ((array == null) || (array.length == 0)) {
97 return null;
98 }
99
100 Map map = new HashMap();
101
102 for (int i = 0; i < array.length; i++) {
103 String postRemove;
104
105 if (removeCharacters == null) {
106 postRemove = array[i];
107 } else {
108 postRemove = StringUtils.replace(array[i], removeCharacters, "");
109 }
110
111 String[] splitThisArrayElement = split(postRemove, delimiter);
112
113 if (splitThisArrayElement == null) {
114 continue;
115 }
116
117 map.put(splitThisArrayElement[0].trim(),
118 splitThisArrayElement[1].trim());
119 }
120
121 return map;
122 }
123 }