1 /* 2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net 3 * Copyright (C) 2003-2008 Frederico Caldeira Knabben 4 * 5 * == BEGIN LICENSE == 6 * 7 * Licensed under the terms of any of the following licenses at your 8 * choice: 9 * 10 * - GNU General Public License Version 2 or later (the "GPL") 11 * http://www.gnu.org/licenses/gpl.html 12 * 13 * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") 14 * http://www.gnu.org/licenses/lgpl.html 15 * 16 * - Mozilla Public License Version 1.1 or later (the "MPL") 17 * http://www.mozilla.org/MPL/MPL-1.1.html 18 * 19 * == END LICENSE == 20 */ 21 package net.fckeditor.tool; 22 23 import java.util.HashSet; 24 import java.util.Set; 25 import java.util.StringTokenizer; 26 27 28 29 30 /** 31 * Some static helper methods. 32 * 33 * @version $Id: Utils.java 1709 2008-03-17 10:10:14Z th-schwarz $ 34 */ 35 public class Utils { 36 37 /** 38 * Constructs a set of uppercased strings from a 'delimiter' separated 39 * string. 40 * 41 * @param stringList 42 * @param delimiter 43 * The delimiter. It shouldn't be empty! 44 * @return An emtpy list, if 'stringList' is empty, or an lowercased set of 45 * strings. 46 * @throws IllegalArgumentException 47 * if 'delimiter' is empty. 48 */ 49 public static Set<String> getSet(final String stringList, 50 final String delimiter) { 51 if (isEmpty(delimiter)) 52 throw new IllegalArgumentException( 53 "Argument 'delimiter' shouldn't be empty!"); 54 if (isEmpty(stringList)) 55 return new HashSet<String>(); 56 57 Set<String> set = new HashSet<String>(); 58 StringTokenizer st = new StringTokenizer(stringList, delimiter); 59 while (st.hasMoreTokens()) { 60 String tmp = st.nextToken(); 61 if (isNotEmpty(tmp)) // simple empty filter 62 set.add(tmp.toLowerCase()); 63 } 64 return set; 65 } 66 67 /** 68 * Just a wrapper to {@link #getSet(String, String)} for using '|' as 69 * delimiter. 70 */ 71 public static Set<String> getSet(final String stringlist) { 72 return getSet(stringlist, "|"); 73 } 74 75 /** 76 * Checks if a string is null or empty. 77 * 78 * @param str 79 * to check 80 * @return <code>true</code> if the String is empty or null 81 */ 82 public static boolean isEmpty(final String str) { 83 return str == null || str.length() == 0; 84 } 85 86 /** 87 * Just a wrapper to {@link #isEmpty(String)}. 88 * 89 * @param str 90 * @return <code>true</code> if the String is not empty and not null. 91 */ 92 public static boolean isNotEmpty(final String str) { 93 return !isEmpty(str); 94 } 95 96 /** 97 * Replaces all 'search' with 'replacement' in 'string'.<br> 98 * Usage: 99 * 100 * <pre> 101 * Utils.replaceAll(null, *, *) = "" 102 * Utils.replaceAll("", *, *) = "" 103 * Utils.replaceAll("foo", null, *) = "foo" 104 * Utils.replaceAll("foo", "o", "a") = "faa" 105 * </pre> 106 * 107 * @param string 108 * @param search 109 * @param replacement 110 * @return replaced String 111 */ 112 public static String replaceAll(final String string, final String search, 113 final String replacement) { 114 if (isEmpty(string)) 115 return ""; 116 if (isEmpty(search)) 117 return string; 118 if (string.indexOf(search) == -1) 119 return string; 120 StringBuffer sb = new StringBuffer(string); 121 int pos = sb.indexOf(search); 122 123 while (pos != -1) { 124 sb.replace(pos, pos + search.length(), replacement); 125 pos = sb.indexOf(search); 126 } 127 return sb.toString(); 128 } 129 }