|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| StringSplitUtils.java | 100% | 100% | 100% | 100% |
|
||||||||||||||
| 1 | /* Copyright 2004, 2005 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 | ||
| 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 | //~ Methods ================================================================ | |
| 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 | 180 | public static String[] split(String toSplit, String delimiter) { |
| 51 | 180 | Assert.hasLength(toSplit, "Cannot split a null or empty string"); |
| 52 | 178 | Assert.hasLength(delimiter, |
| 53 | "Cannot use a null or empty delimiter to split a string"); | |
| 54 | ||
| 55 | 176 | if (delimiter.length() != 1) { |
| 56 | 1 | throw new IllegalArgumentException( |
| 57 | "Delimiter can only be one character in length"); | |
| 58 | } | |
| 59 | ||
| 60 | 175 | int offset = toSplit.indexOf(delimiter); |
| 61 | ||
| 62 | 175 | if (offset < 0) { |
| 63 | 3 | return null; |
| 64 | } | |
| 65 | ||
| 66 | 172 | String beforeDelimiter = toSplit.substring(0, offset); |
| 67 | 172 | String afterDelimiter = toSplit.substring(offset + 1); |
| 68 | ||
| 69 | 172 | 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 | 34 | public static Map splitEachArrayElementAndCreateMap(String[] array, |
| 95 | String delimiter, String removeCharacters) { | |
| 96 | 34 | if ((array == null) || (array.length == 0)) { |
| 97 | 2 | return null; |
| 98 | } | |
| 99 | ||
| 100 | 32 | Map map = new HashMap(); |
| 101 | ||
| 102 | 32 | for (int i = 0; i < array.length; i++) { |
| 103 | 170 | String postRemove; |
| 104 | ||
| 105 | 170 | if (removeCharacters == null) { |
| 106 | 8 | postRemove = array[i]; |
| 107 | } else { | |
| 108 | 162 | postRemove = StringUtils.replace(array[i], removeCharacters, ""); |
| 109 | } | |
| 110 | ||
| 111 | 170 | String[] splitThisArrayElement = split(postRemove, delimiter); |
| 112 | ||
| 113 | 170 | if (splitThisArrayElement == null) { |
| 114 | 2 | continue; |
| 115 | } | |
| 116 | ||
| 117 | 168 | map.put(splitThisArrayElement[0].trim(), |
| 118 | splitThisArrayElement[1].trim()); | |
| 119 | } | |
| 120 | ||
| 121 | 32 | return map; |
| 122 | } | |
| 123 | } |
|
||||||||||