| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| StringSplitUtils |
|
| 6.0;6 |
| 1 | /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited |
|
| 2 | * |
|
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 4 | * you may not use this file except in compliance with the License. |
|
| 5 | * You may obtain a copy of the License at |
|
| 6 | * |
|
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 8 | * |
|
| 9 | * Unless required by applicable law or agreed to in writing, software |
|
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 12 | * See the License for the specific language governing permissions and |
|
| 13 | * limitations under the License. |
|
| 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 | import java.util.ArrayList; |
|
| 24 | import java.util.List; |
|
| 25 | ||
| 26 | ||
| 27 | /** |
|
| 28 | * Provides several <code>String</code> manipulation methods. |
|
| 29 | * |
|
| 30 | * @author Ben Alex |
|
| 31 | * @version $Id: StringSplitUtils.java 1966 2007-08-28 00:31:30Z luke_t $ |
|
| 32 | */ |
|
| 33 | public final class StringSplitUtils { |
|
| 34 | //~ Static fields/initializers ===================================================================================== |
|
| 35 | 1 | private static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 36 | ||
| 37 | //~ Constructors =================================================================================================== |
|
| 38 | ||
| 39 | 0 | private StringSplitUtils() { |
| 40 | 0 | } |
| 41 | ||
| 42 | //~ Methods ======================================================================================================== |
|
| 43 | ||
| 44 | /** |
|
| 45 | * Splits a <code>String</code> at the first instance of the delimiter.<p>Does not include the delimiter in |
|
| 46 | * the response.</p> |
|
| 47 | * |
|
| 48 | * @param toSplit the string to split |
|
| 49 | * @param delimiter to split the string up with |
|
| 50 | * @return a two element array with index 0 being before the delimiter, and index 1 being after the delimiter |
|
| 51 | * (neither element includes the delimiter) |
|
| 52 | * @throws IllegalArgumentException if an argument was invalid |
|
| 53 | */ |
|
| 54 | public static String[] split(String toSplit, String delimiter) { |
|
| 55 | 145 | Assert.hasLength(toSplit, "Cannot split a null or empty string"); |
| 56 | 143 | Assert.hasLength(delimiter, "Cannot use a null or empty delimiter to split a string"); |
| 57 | ||
| 58 | 141 | if (delimiter.length() != 1) { |
| 59 | 1 | throw new IllegalArgumentException("Delimiter can only be one character in length"); |
| 60 | } |
|
| 61 | ||
| 62 | 140 | int offset = toSplit.indexOf(delimiter); |
| 63 | ||
| 64 | 140 | if (offset < 0) { |
| 65 | 4 | return null; |
| 66 | } |
|
| 67 | ||
| 68 | 136 | String beforeDelimiter = toSplit.substring(0, offset); |
| 69 | 136 | String afterDelimiter = toSplit.substring(offset + 1); |
| 70 | ||
| 71 | 136 | return new String[]{beforeDelimiter, afterDelimiter}; |
| 72 | } |
|
| 73 | ||
| 74 | /** |
|
| 75 | * Takes an array of <code>String</code>s, and for each element removes any instances of |
|
| 76 | * <code>removeCharacter</code>, and splits the element based on the <code>delimiter</code>. A <code>Map</code> is |
|
| 77 | * then generated, with the left of the delimiter providing the key, and the right of the delimiter providing the |
|
| 78 | * value.<p>Will trim both the key and value before adding to the <code>Map</code>.</p> |
|
| 79 | * |
|
| 80 | * @param array the array to process |
|
| 81 | * @param delimiter to split each element using (typically the equals symbol) |
|
| 82 | * @param removeCharacters one or more characters to remove from each element prior to attempting the split |
|
| 83 | * operation (typically the quotation mark symbol) or <code>null</code> if no removal should occur |
|
| 84 | * @return a <code>Map</code> representing the array contents, or <code>null</code> if the array to process was |
|
| 85 | * null or empty |
|
| 86 | */ |
|
| 87 | public static Map splitEachArrayElementAndCreateMap(String[] array, String delimiter, String removeCharacters) { |
|
| 88 | 22 | if ((array == null) || (array.length == 0)) { |
| 89 | 2 | return null; |
| 90 | } |
|
| 91 | ||
| 92 | 20 | Map map = new HashMap(); |
| 93 | ||
| 94 | 155 | for (int i = 0; i < array.length; i++) { |
| 95 | String postRemove; |
|
| 96 | ||
| 97 | 135 | if (removeCharacters == null) { |
| 98 | 8 | postRemove = array[i]; |
| 99 | } else { |
|
| 100 | 127 | postRemove = StringUtils.replace(array[i], removeCharacters, ""); |
| 101 | } |
|
| 102 | ||
| 103 | 135 | String[] splitThisArrayElement = split(postRemove, delimiter); |
| 104 | ||
| 105 | 135 | if (splitThisArrayElement == null) { |
| 106 | 3 | continue; |
| 107 | } |
|
| 108 | ||
| 109 | 132 | map.put(splitThisArrayElement[0].trim(), splitThisArrayElement[1].trim()); |
| 110 | } |
|
| 111 | ||
| 112 | 20 | return map; |
| 113 | } |
|
| 114 | ||
| 115 | public static String substringBeforeLast(String str, String separator) { |
|
| 116 | 81 | if (str == null || separator == null || str.length() == 0 || separator.length() == 0) { |
| 117 | 0 | return str; |
| 118 | } |
|
| 119 | 81 | int pos = str.lastIndexOf(separator); |
| 120 | 81 | if (pos == -1) { |
| 121 | 0 | return str; |
| 122 | } |
|
| 123 | 81 | return str.substring(0, pos); |
| 124 | } |
|
| 125 | ||
| 126 | public static String substringAfterLast(String str, String separator) { |
|
| 127 | 81 | if (str == null || str.length() == 0) { |
| 128 | 0 | return str; |
| 129 | } |
|
| 130 | 81 | if (separator == null || separator.length() == 0) { |
| 131 | 0 | return ""; |
| 132 | } |
|
| 133 | 81 | int pos = str.lastIndexOf(separator); |
| 134 | 81 | if (pos == -1 || pos == (str.length() - separator.length())) { |
| 135 | 0 | return ""; |
| 136 | } |
|
| 137 | 81 | return str.substring(pos + separator.length()); |
| 138 | } |
|
| 139 | ||
| 140 | /** |
|
| 141 | * Splits a given string on the given separator character, skips the contents of quoted substrings |
|
| 142 | * when looking for separators. |
|
| 143 | * Introduced for use in DigestProcessingFilter (see SEC-506). |
|
| 144 | * <p/> |
|
| 145 | * This was copied and modified from commons-lang StringUtils |
|
| 146 | */ |
|
| 147 | public static String[] splitIgnoringQuotes(String str, char separatorChar) { |
|
| 148 | 16 | if (str == null) { |
| 149 | 0 | return null; |
| 150 | } |
|
| 151 | ||
| 152 | 16 | int len = str.length(); |
| 153 | ||
| 154 | 16 | if (len == 0) { |
| 155 | 0 | return EMPTY_STRING_ARRAY; |
| 156 | } |
|
| 157 | ||
| 158 | 16 | List list = new ArrayList(); |
| 159 | 16 | int i = 0; |
| 160 | 16 | int start = 0; |
| 161 | 16 | boolean match = false; |
| 162 | ||
| 163 | 1198 | while (i < len) { |
| 164 | 1182 | if (str.charAt(i) == '"') { |
| 165 | 84 | i++; |
| 166 | 2212 | while (i < len) { |
| 167 | 2212 | if (str.charAt(i) == '"') { |
| 168 | 84 | i++; |
| 169 | 84 | break; |
| 170 | } |
|
| 171 | 2128 | i++; |
| 172 | } |
|
| 173 | 84 | match = true; |
| 174 | 84 | continue; |
| 175 | } |
|
| 176 | 1098 | if (str.charAt(i) == separatorChar) { |
| 177 | 98 | if (match) { |
| 178 | 98 | list.add(str.substring(start, i)); |
| 179 | 98 | match = false; |
| 180 | } |
|
| 181 | 98 | start = ++i; |
| 182 | 98 | continue; |
| 183 | } |
|
| 184 | 1000 | match = true; |
| 185 | 1000 | i++; |
| 186 | } |
|
| 187 | 16 | if (match) { |
| 188 | 16 | list.add(str.substring(start, i)); |
| 189 | } |
|
| 190 | ||
| 191 | 16 | return (String[]) list.toArray(new String[list.size()]); |
| 192 | } |
|
| 193 | ||
| 194 | } |