1 16 17 package org.apache.axis.utils; 18 19 import org.apache.axis.InternalException; 20 21 import java.util.ArrayList ; 22 import java.util.List ; 23 import java.io.Writer ; 24 import java.io.IOException ; 25 import java.io.StringWriter ; 26 27 public class StringUtils { 28 private StringUtils() { 29 } 30 31 34 public static final String [] EMPTY_STRING_ARRAY = new String [0]; 35 36 42 public static boolean startsWithIgnoreWhitespaces(String prefix, String string) { 43 int index1 = 0; 44 int index2 = 0; 45 int length1 = prefix.length(); 46 int length2 = string.length(); 47 char ch1 = ' '; 48 char ch2 = ' '; 49 while (index1 < length1 && index2 < length2) { 50 while (index1 < length1 && Character.isWhitespace(ch1 = prefix.charAt(index1))) { 51 index1++; 52 } 53 while (index2 < length2 && Character.isWhitespace(ch2 = string.charAt(index2))) { 54 index2++; 55 } 56 if (index1 == length1 && index2 == length2) { 57 return true; 58 } 59 if (ch1 != ch2) { 60 return false; 61 } 62 index1++; 63 index2++; 64 } 65 if(index1 < length1 && index2 >= length2) 66 return false; 67 return true; 68 } 69 70 94 public static String [] split(String str, char separatorChar) { 95 if (str == null) { 96 return null; 97 } 98 int len = str.length(); 99 if (len == 0) { 100 return EMPTY_STRING_ARRAY; 101 } 102 List list = new ArrayList (); 103 int i = 0, start = 0; 104 boolean match = false; 105 while (i < len) { 106 if (str.charAt(i) == separatorChar) { 107 if (match) { 108 list.add(str.substring(start, i)); 109 match = false; 110 } 111 start = ++i; 112 continue; 113 } 114 match = true; 115 i++; 116 } 117 if (match) { 118 list.add(str.substring(start, i)); 119 } 120 return (String []) list.toArray(new String [list.size()]); 121 } 122 123 143 public static boolean isEmpty(String str) { 144 return (str == null || str.length() == 0); 145 } 146 147 171 public static String strip(String str) { 172 return strip(str, null); 173 } 174 175 201 public static String strip(String str, String stripChars) { 202 if (str == null) { 203 return str; 204 } 205 int len = str.length(); 206 if (len == 0) { 207 return str; 208 } 209 int start = getStripStart(str, stripChars); 210 if (start == len) { 211 return ""; 212 } 213 int end = getStripEnd(str, stripChars); 214 return (start == 0 && end == len) ? str : str.substring(start, end); 215 } 216 217 241 public static String stripStart(String str, String stripChars) { 242 int start = getStripStart(str, stripChars); 243 return (start <= 0) ? str : str.substring(start); 244 } 245 246 private static int getStripStart(String str, String stripChars) { 247 int strLen; 248 if (str == null || (strLen = str.length()) == 0) { 249 return -1; 250 } 251 int start = 0; 252 if (stripChars == null) { 253 while ((start != strLen) && Character.isWhitespace(str.charAt(start))) { 254 start++; 255 } 256 } else if (stripChars.length() == 0) { 257 return start; 258 } else { 259 while ((start != strLen) && (stripChars.indexOf(str.charAt(start)) != -1)) { 260 start++; 261 } 262 } 263 return start; 264 } 265 266 290 public static String stripEnd(String str, String stripChars) { 291 int end = getStripEnd(str, stripChars); 292 return (end < 0) ? str : str.substring(0, end); 293 } 294 295 private static int getStripEnd(String str, String stripChars) { 296 int end; 297 if (str == null || (end = str.length()) == 0) { 298 return -1; 299 } 300 if (stripChars == null) { 301 while ((end != 0) && Character.isWhitespace(str.charAt(end - 1))) { 302 end--; 303 } 304 } else if (stripChars.length() == 0) { 305 return end; 306 } else { 307 while ((end != 0) && (stripChars.indexOf(str.charAt(end - 1)) != -1)) { 308 end--; 309 } 310 } 311 return end; 312 } 313 314 320 public static String escapeNumericChar(String str) { 321 if (str == null) { 322 return null; 323 } 324 try { 325 StringWriter writer = new StringWriter (str.length()); 326 escapeNumericChar(writer, str); 327 return writer.toString(); 328 } catch (IOException ioe) { 329 ioe.printStackTrace(); 331 return null; 332 } 333 } 334 335 341 public static void escapeNumericChar(Writer out, String str) 342 throws IOException { 343 if (str == null) { 344 return; 345 } 346 int length = str.length(); 347 char character; 348 for (int i = 0; i < length; i++) { 349 character = str.charAt( i ); 350 if (character > 0x7F) { 351 out.write("&#x"); 352 out.write(Integer.toHexString(character).toUpperCase()); 353 out.write(";"); 354 } else { 355 out.write(character); 356 } 357 } 358 } 359 360 369 public static String unescapeNumericChar(String str) { 370 if (str == null) { 371 return null; 372 } 373 try { 374 StringWriter writer = new StringWriter (str.length()); 375 unescapeNumericChar(writer, str); 376 return writer.toString(); 377 } catch (IOException ioe) { 378 ioe.printStackTrace(); 380 return null; 381 } 382 } 383 384 398 public static void unescapeNumericChar(Writer out, String str) throws IOException { 399 if (out == null) { 400 throw new IllegalArgumentException ("The Writer must not be null"); 401 } 402 if (str == null) { 403 return; 404 } 405 406 int sz = str.length(); 407 StringBuffer unicode = new StringBuffer (4); 408 StringBuffer escapes = new StringBuffer (3); 409 boolean inUnicode = false; 410 411 for (int i = 0; i < sz; i++) { 412 char ch = str.charAt(i); 413 if (inUnicode) { 414 unicode.append(ch); 417 if (unicode.length() == 4) { 418 try { 421 int value = Integer.parseInt(unicode.toString(), 16); 422 out.write((char) value); 423 unicode.setLength(0); 424 i = i + 1; 426 inUnicode = false; 427 } catch (NumberFormatException nfe) { 428 throw new InternalException(nfe); 429 } 430 } 431 continue; 432 } else if (ch=='&') { 433 if (i+7 <= sz) { 437 escapes.append(ch); 438 escapes.append(str.charAt(i+1)); 439 escapes.append(str.charAt(i+2)); 440 if (escapes.toString().equals("&#x") && str.charAt(i+7)==';') { 441 inUnicode = true; 442 } else { 443 out.write(escapes.toString()); 444 } 445 escapes.setLength(0); 446 i = i + 2; 448 } else { 449 out.write(ch); 450 } 451 continue; 452 } 453 out.write(ch); 454 } 455 } 456 } 457 | Popular Tags |