001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.util;
017:
018: import org.springframework.util.Assert;
019: import org.springframework.util.StringUtils;
020:
021: import java.util.HashMap;
022: import java.util.Map;
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: /**
027: * Provides several <code>String</code> manipulation methods.
028: *
029: * @author Ben Alex
030: * @version $Id: StringSplitUtils.java 1966 2007-08-28 00:31:30Z luke_t $
031: */
032: public final class StringSplitUtils {
033: //~ Static fields/initializers =====================================================================================
034: private static final String[] EMPTY_STRING_ARRAY = new String[0];
035:
036: //~ Constructors ===================================================================================================
037:
038: private StringSplitUtils() {
039: }
040:
041: //~ Methods ========================================================================================================
042:
043: /**
044: * Splits a <code>String</code> at the first instance of the delimiter.<p>Does not include the delimiter in
045: * the response.</p>
046: *
047: * @param toSplit the string to split
048: * @param delimiter to split the string up with
049: * @return a two element array with index 0 being before the delimiter, and index 1 being after the delimiter
050: * (neither element includes the delimiter)
051: * @throws IllegalArgumentException if an argument was invalid
052: */
053: public static String[] split(String toSplit, String delimiter) {
054: Assert
055: .hasLength(toSplit,
056: "Cannot split a null or empty string");
057: Assert
058: .hasLength(delimiter,
059: "Cannot use a null or empty delimiter to split a string");
060:
061: if (delimiter.length() != 1) {
062: throw new IllegalArgumentException(
063: "Delimiter can only be one character in length");
064: }
065:
066: int offset = toSplit.indexOf(delimiter);
067:
068: if (offset < 0) {
069: return null;
070: }
071:
072: String beforeDelimiter = toSplit.substring(0, offset);
073: String afterDelimiter = toSplit.substring(offset + 1);
074:
075: return new String[] { beforeDelimiter, afterDelimiter };
076: }
077:
078: /**
079: * Takes an array of <code>String</code>s, and for each element removes any instances of
080: * <code>removeCharacter</code>, and splits the element based on the <code>delimiter</code>. A <code>Map</code> is
081: * then generated, with the left of the delimiter providing the key, and the right of the delimiter providing the
082: * value.<p>Will trim both the key and value before adding to the <code>Map</code>.</p>
083: *
084: * @param array the array to process
085: * @param delimiter to split each element using (typically the equals symbol)
086: * @param removeCharacters one or more characters to remove from each element prior to attempting the split
087: * operation (typically the quotation mark symbol) or <code>null</code> if no removal should occur
088: * @return a <code>Map</code> representing the array contents, or <code>null</code> if the array to process was
089: * null or empty
090: */
091: public static Map splitEachArrayElementAndCreateMap(String[] array,
092: String delimiter, String removeCharacters) {
093: if ((array == null) || (array.length == 0)) {
094: return null;
095: }
096:
097: Map map = new HashMap();
098:
099: for (int i = 0; i < array.length; i++) {
100: String postRemove;
101:
102: if (removeCharacters == null) {
103: postRemove = array[i];
104: } else {
105: postRemove = StringUtils.replace(array[i],
106: removeCharacters, "");
107: }
108:
109: String[] splitThisArrayElement = split(postRemove,
110: delimiter);
111:
112: if (splitThisArrayElement == null) {
113: continue;
114: }
115:
116: map.put(splitThisArrayElement[0].trim(),
117: splitThisArrayElement[1].trim());
118: }
119:
120: return map;
121: }
122:
123: public static String substringBeforeLast(String str,
124: String separator) {
125: if (str == null || separator == null || str.length() == 0
126: || separator.length() == 0) {
127: return str;
128: }
129: int pos = str.lastIndexOf(separator);
130: if (pos == -1) {
131: return str;
132: }
133: return str.substring(0, pos);
134: }
135:
136: public static String substringAfterLast(String str, String separator) {
137: if (str == null || str.length() == 0) {
138: return str;
139: }
140: if (separator == null || separator.length() == 0) {
141: return "";
142: }
143: int pos = str.lastIndexOf(separator);
144: if (pos == -1 || pos == (str.length() - separator.length())) {
145: return "";
146: }
147: return str.substring(pos + separator.length());
148: }
149:
150: /**
151: * Splits a given string on the given separator character, skips the contents of quoted substrings
152: * when looking for separators.
153: * Introduced for use in DigestProcessingFilter (see SEC-506).
154: * <p/>
155: * This was copied and modified from commons-lang StringUtils
156: */
157: public static String[] splitIgnoringQuotes(String str,
158: char separatorChar) {
159: if (str == null) {
160: return null;
161: }
162:
163: int len = str.length();
164:
165: if (len == 0) {
166: return EMPTY_STRING_ARRAY;
167: }
168:
169: List list = new ArrayList();
170: int i = 0;
171: int start = 0;
172: boolean match = false;
173:
174: while (i < len) {
175: if (str.charAt(i) == '"') {
176: i++;
177: while (i < len) {
178: if (str.charAt(i) == '"') {
179: i++;
180: break;
181: }
182: i++;
183: }
184: match = true;
185: continue;
186: }
187: if (str.charAt(i) == separatorChar) {
188: if (match) {
189: list.add(str.substring(start, i));
190: match = false;
191: }
192: start = ++i;
193: continue;
194: }
195: match = true;
196: i++;
197: }
198: if (match) {
199: list.add(str.substring(start, i));
200: }
201:
202: return (String[]) list.toArray(new String[list.size()]);
203: }
204:
205: }
|