1 24 package org.ofbiz.base.util; 25 26 import java.io.UnsupportedEncodingException ; 27 import java.net.URLDecoder ; 28 import java.net.URLEncoder ; 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Set ; 35 import java.util.StringTokenizer ; 36 37 44 public class StringUtil { 45 46 public static final String module = StringUtil.class.getName(); 47 48 55 public static String replaceString(String mainString, String oldString, String newString) { 56 if (mainString == null) { 57 return null; 58 } 59 if (oldString == null || oldString.length() == 0) { 60 return mainString; 61 } 62 if (newString == null) { 63 newString = ""; 64 } 65 66 int i = mainString.lastIndexOf(oldString); 67 68 if (i < 0) return mainString; 69 70 StringBuffer mainSb = new StringBuffer (mainString); 71 72 while (i >= 0) { 73 mainSb.replace(i, i + oldString.length(), newString); 74 i = mainString.lastIndexOf(oldString, i - 1); 75 } 76 return mainSb.toString(); 77 } 78 79 85 public static String join(List list, String delim) { 86 if (list == null || list.size() < 1) 87 return null; 88 StringBuffer buf = new StringBuffer (); 89 Iterator i = list.iterator(); 90 91 while (i.hasNext()) { 92 buf.append((String ) i.next()); 93 if (i.hasNext()) 94 buf.append(delim); 95 } 96 return buf.toString(); 97 } 98 99 105 public static List split(String str, String delim) { 106 List splitList = null; 107 StringTokenizer st = null; 108 109 if (str == null) 110 return splitList; 111 112 if (delim != null) 113 st = new StringTokenizer (str, delim); 114 else 115 st = new StringTokenizer (str); 116 117 if (st != null && st.hasMoreTokens()) { 118 splitList = new ArrayList (); 119 120 while (st.hasMoreTokens()) 121 splitList.add(st.nextToken()); 122 } 123 return splitList; 124 } 125 126 130 public static List quoteStrList(List list) { 131 List tmpList = list; 132 133 list = new ArrayList (); 134 Iterator i = tmpList.iterator(); 135 136 while (i.hasNext()) { 137 String str = (String ) i.next(); 138 139 str = "'" + str + "''"; 140 list.add(str); 141 } 142 return list; 143 } 144 145 151 public static Map strToMap(String str, boolean trim) { 152 if (str == null) return null; 153 Map decodedMap = new HashMap (); 154 List elements = split(str, "|"); 155 Iterator i = elements.iterator(); 156 157 while (i.hasNext()) { 158 String s = (String ) i.next(); 159 List e = split(s, "="); 160 161 if (e.size() != 2) { 162 continue; 163 } 164 String name = (String ) e.get(0); 165 String value = (String ) e.get(1); 166 if (trim) { 167 if (name != null) { 168 name = name.trim(); 169 } 170 if (value != null) { 171 value = value.trim(); 172 } 173 } 174 175 try { 176 decodedMap.put(URLDecoder.decode(name, "UTF-8"), URLDecoder.decode(value, "UTF-8")); 177 } catch (UnsupportedEncodingException e1) { 178 Debug.logError(e1, module); 179 } 180 } 181 return decodedMap; 182 } 183 184 189 public static Map strToMap(String str) { 190 return strToMap(str, false); 191 } 192 193 198 public static String mapToStr(Map map) { 199 if (map == null) return null; 200 StringBuffer buf = new StringBuffer (); 201 Set keySet = map.keySet(); 202 Iterator i = keySet.iterator(); 203 boolean first = true; 204 205 while (i.hasNext()) { 206 Object key = i.next(); 207 Object value = map.get(key); 208 209 if (!(key instanceof String ) || !(value instanceof String )) 210 continue; 211 String encodedName = null; 212 try { 213 encodedName = URLEncoder.encode((String ) key, "UTF-8"); 214 } catch (UnsupportedEncodingException e) { 215 Debug.logError(e, module); 216 } 217 String encodedValue = null; 218 try { 219 encodedValue = URLEncoder.encode((String ) value, "UTF-8"); 220 } catch (UnsupportedEncodingException e) { 221 Debug.logError(e, module); 222 } 223 224 if (first) 225 first = false; 226 else 227 buf.append("|"); 228 229 buf.append(encodedName); 230 buf.append("="); 231 buf.append(encodedValue); 232 } 233 return buf.toString(); 234 } 235 236 243 public static Map createMap(List keys, List values) { 244 if (keys == null || values == null || keys.size() != values.size()) { 245 throw new IllegalArgumentException ("Keys and Values cannot be null and must be the same size"); 246 } 247 Map newMap = new HashMap (); 248 for (int i = 0; i < keys.size(); i++) { 249 newMap.put(keys.get(i), values.get(i)); 250 } 251 return newMap; 252 } 253 254 255 public static String cleanUpPathPrefix(String prefix) { 256 if (prefix == null || prefix.length() == 0) return ""; 257 258 StringBuffer cppBuff = new StringBuffer (prefix.replace('\\', '/')); 259 260 if (cppBuff.charAt(0) != '/') { 261 cppBuff.insert(0, '/'); 262 } 263 if (cppBuff.charAt(cppBuff.length() - 1) == '/') { 264 cppBuff.deleteCharAt(cppBuff.length() - 1); 265 } 266 return cppBuff.toString(); 267 } 268 269 270 public static String removeSpaces(String str) { 271 StringBuffer newString = new StringBuffer (); 272 for (int i = 0; i < str.length(); i++) { 273 if (str.charAt(i) != ' ') 274 newString.append(str.charAt(i)); 275 } 276 return newString.toString(); 277 } 278 279 public static String toHexString(byte[] bytes) { 280 StringBuffer buf = new StringBuffer (bytes.length * 2); 281 for (int i = 0; i < bytes.length; i++) { 282 buf.append(hexChar[(bytes[i] & 0xf0) >>> 4]); 283 buf.append(hexChar[bytes[i] & 0x0f]); 284 } 285 return buf.toString(); 286 287 } 288 289 public static String cleanHexString(String str) { 290 StringBuffer buf = new StringBuffer (); 291 for (int i = 0; i < str.length(); i++) { 292 if (str.charAt(i) != (int) 32 && str.charAt(i) != ':') { 293 buf.append(str.charAt(i)); 294 } 295 } 296 return buf.toString(); 297 } 298 299 public static byte[] fromHexString(String str) { 300 str = cleanHexString(str); 301 int stringLength = str.length(); 302 if ((stringLength & 0x1) != 0) { 303 throw new IllegalArgumentException ("fromHexString requires an even number of hex characters"); 304 } 305 byte[] b = new byte[stringLength / 2]; 306 307 for (int i = 0, j = 0; i < stringLength; i+= 2, j++) { 308 int high = convertChar(str.charAt(i)); 309 int low = convertChar(str.charAt(i+1)); 310 b[j] = (byte) ((high << 4) | low); 311 } 312 return b; 313 } 314 315 private static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; 316 public static int convertChar(char c) { 317 if ( '0' <= c && c <= '9' ) { 318 return c - '0' ; 319 } else if ( 'a' <= c && c <= 'f' ) { 320 return c - 'a' + 0xa ; 321 } else if ( 'A' <= c && c <= 'F' ) { 322 return c - 'A' + 0xa ; 323 } else { 324 throw new IllegalArgumentException ("Invalid hex character: [" + c + "]"); 325 } 326 } 327 328 public static char[] encodeInt(int i, int j, char digestChars[]) { 329 if (i < 16) { 330 digestChars[j] = '0'; 331 } 332 j++; 333 do { 334 digestChars[j--] = hexChar[i & 0xf]; 335 i >>>= 4; 336 } while (i != 0); 337 return digestChars; 338 } 339 } 340 | Popular Tags |