1 23 24 29 30 package com.sun.jdo.spi.persistence.support.sqlstore.utility; 31 32 import java.util.*; 33 34 39 public class StringScanner 41 { 42 private static String intStrTable[] = new String [2048]; 43 44 static int skipWhite(String s, int offset) 45 { 46 int end = s.length(); 47 48 if (offset < 0) 49 return end; 50 while (offset < end && Character.isWhitespace(s.charAt(offset))) 51 offset++; 52 return offset; 53 } 54 55 static int skipTo(String s, int offset, String delim) 56 { 57 int end = s.length(); 58 59 if (offset < 0) 60 return end; 61 while (offset < end && delim.indexOf(s.charAt(offset)) == -1) 62 offset++; 63 return offset; 64 } 65 66 static int skipTo(String s, int offset, char delim) 67 { 68 int end = s.length(); 69 70 if (offset < 0) 71 return end; 72 while (offset < end && delim != s.charAt(offset)) 73 offset++; 74 return offset; 75 } 76 77 static char charAt(String s, int offset) 78 { 79 if (offset < 0 || offset >= s.length()) 80 return '\0'; 81 return s.charAt(offset); 82 } 83 84 static int skipInt(String s, int offset) 85 { 86 int end = s.length(); 87 88 if (offset < 0 || !Character.isDigit(s.charAt(offset))) 89 return end; 90 while (offset < end && Character.isDigit(s.charAt(offset))) 96 offset++; 97 return offset; 98 } 99 100 public static String format(StringBuffer buf, String fmt, Object o1) 101 { 102 return format(buf, fmt, 1, o1, null, null, null, null, null, null, 103 null, null); 104 } 105 106 public static String format(StringBuffer buf, String fmt, 107 Object o1, 108 Object o2) 109 { 110 return format(buf, fmt, 2, o1, o2, null, null, null, null, null, 111 null, null); 112 } 113 114 public static String format(StringBuffer buf, String fmt, 115 Object o1, 116 Object o2, 117 Object o3) 118 { 119 return format(buf, fmt, 3, o1, o2, o3, null, null, null, null, 120 null, null); 121 } 122 123 public static String format(StringBuffer buf, String fmt, 124 Object o1, 125 Object o2, 126 Object o3, 127 Object o4) 128 { 129 return format(buf, fmt, 4, o1, o2, o3, o4, null, null, null, 130 null, null); 131 } 132 133 public static String format(StringBuffer buf, String fmt, 134 Object o1, 135 Object o2, 136 Object o3, 137 Object o4, 138 Object o5) 139 { 140 return format(buf, fmt, 5, o1, o2, o3, o4, o5, null, null, null, null); 141 } 142 143 public static String format(StringBuffer buf, String fmt, 144 Object o1, 145 Object o2, 146 Object o3, 147 Object o4, 148 Object o5, 149 Object o6) 150 { 151 return format(buf, fmt, 6, o1, o2, o3, o4, o5, o6, null, null, null); 152 } 153 154 public static String format(StringBuffer buf, String fmt, 155 Object o1, 156 Object o2, 157 Object o3, 158 Object o4, 159 Object o5, 160 Object o6, 161 Object o7) 162 { 163 return format(buf, fmt, 7, o1, o2, o3, o4, o5, o6, o7, null, null); 164 } 165 166 public static String format(StringBuffer buf, String fmt, 167 Object o1, 168 Object o2, 169 Object o3, 170 Object o4, 171 Object o5, 172 Object o6, 173 Object o7, 174 Object o8) 175 { 176 return format(buf, fmt, 8, o1, o2, o3, o4, o5, o6, o7, o8, null); 177 } 178 179 public static String format(StringBuffer buf, String fmt, 180 Object o1, 181 Object o2, 182 Object o3, 183 Object o4, 184 Object o5, 185 Object o6, 186 Object o7, 187 Object o8, 188 Object o9) 189 { 190 return format(buf, fmt, 9, o1, o2, o3, o4, o5, o6, o7, o8, o9); 191 } 192 193 public static String format(StringBuffer buf, String fmt, 194 int argcnt, 195 Object o1, 196 Object o2, 197 Object o3, 198 Object o4, 199 Object o5, 200 Object o6, 201 Object o7, 202 Object o8, 203 Object o9) 204 { 205 Object [] params = {o1, o2, o3, o4, o5, o6, o7, o8, o9}; 206 207 int i = 0; 208 int begSubstr = 0; 209 int percent; 210 int nextParam = 0; 211 StringBuffer msg; 212 int fmtLen; 213 214 if (buf == null) 215 msg = new StringBuffer (); 216 else 217 msg = buf; 218 219 fmtLen = fmt.length(); 220 221 while ((percent = fmt.indexOf('%', i)) >= 0 && percent < (fmtLen - 1)) 222 { 223 char c = fmt.charAt(percent + 1); 224 boolean leftJustify = false; 225 boolean raw = false; 226 int nextChar = percent; 227 228 if (c == '-') 229 { 230 nextChar++; 231 leftJustify = true; 232 if (nextChar + 1 < fmtLen) 233 c = fmt.charAt(nextChar + 1); 234 } 235 236 if (Character.isDigit(c)) 237 { 238 int endInt = StringScanner.skipInt(fmt, nextChar + 1); 239 int size = Integer.parseInt(fmt.substring(nextChar + 1,endInt)); 240 241 if (size == 0) 242 raw = true; 243 244 msg.append(fmt.substring(begSubstr, percent)); 245 if (nextParam < argcnt) 246 { 247 Object p = params[nextParam++]; 248 String val = p.toString(); 249 int len = val.length(); 250 251 if (!raw) 252 { 253 if (!leftJustify && len < size) 254 { 255 for (int j = 0; j < size - len; j++) 256 msg.append(" "); } 258 else if (len > size) 259 { 260 val = val.substring(0, size); 261 } 262 } 263 msg.append(val); 264 if (leftJustify && len < size) 265 { 266 for (int j = 0; j < size - len; j++) 267 msg.append(" "); } 269 } 270 else 271 { 272 280 } 281 i = endInt; 282 begSubstr = i; 283 } 284 else 285 { 286 i = nextChar + 1; 287 } 288 } 289 msg.append(fmt.substring(begSubstr, fmtLen)); 290 return msg.toString(); 291 } 292 293 static String createParamString(String message, 294 int argcnt, 295 Object param1, 296 Object param2, 297 Object param3, 298 Object param4, 299 Object param5, 300 Object param6, 301 Object param7, 302 Object param8, 303 Object param9) 304 { 305 if (argcnt < 1 || argcnt > 9) 306 return message; 307 308 Object [] p = null; 309 if (argcnt == 1) 310 { 311 Object [] params = {param1}; 312 p = params; 313 } 314 else if (argcnt == 2) 315 { 316 Object [] params = {param1, param2}; 317 p = params; 318 } 319 else if (argcnt == 3) 320 { 321 Object [] params = {param1, param2, param3}; 322 p = params; 323 } 324 else if (argcnt == 4) 325 { 326 Object [] params = {param1, param2, param3, param4}; 327 p = params; 328 } 329 else if (argcnt == 5) 330 { 331 Object [] params = {param1, param2, param3, param4, param5}; 332 p = params; 333 } 334 else if (argcnt == 6) 335 { 336 Object [] params = {param1, param2, param3, param4, param5, 337 param6}; 338 p = params; 339 } 340 else if (argcnt == 7) 341 { 342 Object [] params = {param1, param2, param3, param4, param5, 343 param6, param7}; 344 p = params; 345 } 346 else if (argcnt == 8) 347 { 348 Object [] params = {param1, param2, param3, param4, param5, 349 param6, param7, param8}; 350 p = params; 351 } 352 else if (argcnt == 9) 353 { 354 Object [] params = {param1, param2, param3, param4, param5, 355 param6, param7, param8, param9}; 356 p = params; 357 } 358 359 boolean useJava = false; 368 int lbrace, rbrace = 0; 369 int i = 0; 370 371 while ((lbrace = message.indexOf('{', i)) >= 0) 373 { 374 if ((rbrace = message.indexOf('}', lbrace + 1)) < 0 || 378 (rbrace - lbrace) > 2) 379 { 380 useJava = true; 381 break; 382 } 383 384 i = rbrace + 1; 385 } 386 387 if (useJava) 388 { 389 return java.text.MessageFormat.format(message, p); 390 } 391 392 int esc; 395 boolean foundEsc = false; 396 StringBuffer msg = new StringBuffer (); 397 398 i = 0; 399 while ((esc = message.indexOf('\'', i)) >= 0) 400 { 401 msg.append(message.substring(i, esc)); 402 msg.append(message.substring(esc + 1, esc + 2)); 403 i = esc + 2; 404 foundEsc = true; 405 } 406 407 if (foundEsc) 408 { 409 msg.append(message.substring(i, message.length())); 410 message = msg.toString(); 411 } 412 413 char c; 414 i = 0; 415 int msglen = message.length(); 416 msg = new StringBuffer (); 417 while ( (lbrace = message.indexOf('{', i)) >= 0 418 && lbrace < msglen-1) 419 { 420 c = message.charAt(lbrace + 1); 421 422 if (Character.isDigit(c)) 423 { 424 int pnum = c - '0'; 425 426 if (0 <= pnum && pnum < argcnt) 427 { 428 Object pp = p[pnum]; 429 430 msg.append(message.substring(i, lbrace)); 431 432 if (pp == null) 433 { 434 msg.append("<null>"); } 436 else 437 { 438 msg.append(pp.toString()); 439 } 440 } 441 else 442 { 443 if (argcnt > 0) 444 { 445 453 } 454 else 455 { 456 463 } 464 } 465 i = lbrace + 3; 466 } 467 else 468 { 469 i = lbrace + 1; 470 } 471 } 472 msg.append(message.substring(i, msglen)); 473 return msg.toString(); 474 } 475 476 484 public static String levelString(int level) 491 { 492 int i; 493 String str; 494 495 if (level <= 0) 496 return ""; 498 switch (level) 499 { 500 case 1: 501 return " "; case 2: 503 return " "; case 3: 505 return " "; case 4: 507 return " "; case 5: 509 return " "; case 6: 511 return " "; default: 513 str = new String (" "); for (i = 6; i < level; i++) 515 str = str.concat(" "); return str; 517 } 518 } 519 520 528 public static Vector splitString(String str, String delimeter) 537 { 538 int mark; 539 int start; 540 int delLen; 541 Vector list; 542 543 list = new Vector(); 544 545 if (str != null) 546 { 547 mark = 0; 548 start = 0; 549 550 delLen = delimeter.length(); 551 while ((start = str.indexOf(delimeter, mark)) != -1) 552 { 553 if (start != 0) 554 list.addElement((Object ) str.substring(mark, start)); 555 mark = start + delLen; 556 } 557 558 if (mark < str.length()) 560 list.addElement((Object ) str.substring(mark)); 561 } 562 return list; 563 } 564 565 575 public static String fill(String str, int len, boolean left) 582 { 583 String nstr; 584 int startLen; 585 int diff; 586 587 if (str == null) 588 str = "null"; 590 startLen = str.length(); 591 nstr = str; 592 while (startLen < len) 593 { 594 diff = len - startLen; 595 if (diff >= 10) 596 { 597 startLen += 10; 598 if (left) 599 nstr = nstr + " "; else 601 nstr = " " + nstr; continue; 603 } 604 else if (diff >= 8) 605 { 606 startLen += 8; 607 if (left) 608 nstr = nstr + " "; else 610 nstr = " " + nstr; continue; 612 } 613 else if (diff >= 4) 614 { 615 startLen += 4; 616 if (left) 617 nstr = nstr + " "; else 619 nstr = " " + nstr; continue; 621 } 622 else if (diff >= 2) 623 { 624 startLen += 2; 625 if (left) 626 nstr = nstr + " "; else 628 nstr = " " + nstr; continue; 630 } 631 else 632 { 633 startLen++; 634 if (left) 635 nstr = nstr + " "; else 637 nstr = " " + nstr; continue; 639 } 640 } 641 return nstr; 642 } 643 644 public static String createParamString(String fmt) 645 { 646 return fmt; 647 } 648 public static String createParamString(String fmt, Object obj1) 649 { 650 return StringScanner.createParamString(fmt, 1, obj1, null, null, null, 651 null, null, null, null, null); 652 } 653 public static String createParamString(String fmt, Object obj1, Object obj2) 654 { 655 return StringScanner.createParamString(fmt, 2, obj1, obj2, null, null, 656 null, null, null, null, null); 657 } 658 public static String createParamString(String fmt, Object obj1, Object obj2, 659 Object obj3) 660 { 661 return StringScanner.createParamString(fmt, 3, obj1, obj2, obj3, null, 662 null, null, null, null, null); 663 } 664 public static String createParamString(String fmt, Object obj1, Object obj2, 665 Object obj3, Object obj4) 666 { 667 return StringScanner.createParamString(fmt, 4, obj1, obj2, obj3, obj4, 668 null, null, null, null, null); 669 } 670 public static String createParamString(String fmt, Object obj1, Object obj2, 671 Object obj3, Object obj4, 672 Object obj5) 673 { 674 return StringScanner.createParamString(fmt, 5, obj1, obj2, obj3, obj4, 675 obj5, null, null, null, null); 676 } 677 public static String createParamString(String fmt, Object obj1, Object obj2, 678 Object obj3, Object obj4, 679 Object obj5, Object obj6) 680 { 681 return StringScanner.createParamString(fmt, 6, obj1, obj2, obj3, obj4, 682 obj5, obj6, null, null, null); 683 } 684 public static String createParamString(String fmt, Object obj1, Object obj2, 685 Object obj3, Object obj4, 686 Object obj5, Object obj6, 687 Object obj7) 688 { 689 return StringScanner.createParamString(fmt, 7, obj1, obj2, obj3, obj4, 690 obj5, obj6, obj7, null, null); 691 } 692 public static String createParamString(String fmt, Object obj1, Object obj2, 693 Object obj3, Object obj4, 694 Object obj5, Object obj6, 695 Object obj7, Object obj8) 696 { 697 return StringScanner.createParamString(fmt, 8, obj1, obj2, obj3, obj4, 698 obj5, obj6, obj7, obj8, null); 699 } 700 public static String createParamString(String fmt, Object obj1, Object obj2, 701 Object obj3, Object obj4, 702 Object obj5, Object obj6, 703 Object obj7, Object obj8, 704 Object obj9) 705 { 706 return StringScanner.createParamString(fmt, 9, obj1, obj2, obj3, obj4, 707 obj5, obj6, obj7, obj8, obj9); 708 } 709 710 711 public static String getIntStr(int num) 712 { 713 String str; 714 boolean big; 715 716 try 717 { 718 str = (String ) StringScanner.intStrTable[num]; 719 big = false; 720 } 721 catch (ArrayIndexOutOfBoundsException e) 722 { 723 str = null; 724 big = true; 725 } 726 727 if (str == null) 728 { 729 str = Integer.toString(num); 730 if (!big) 731 StringScanner.intStrTable[num] = str; 732 } 733 return str; 734 } 735 } 736 | Popular Tags |