1 34 35 36 package com.micronova.util.codec; 37 38 import java.util.*; 39 import java.io.*; 40 import java.util.regex.*; 41 import com.micronova.util.*; 42 43 44 45 public class CodecString extends Codec 46 { 47 48 49 protected static Pattern getPattern(Object patternSpec) 50 { 51 Pattern pattern = null; 52 53 if (patternSpec instanceof Pattern) 54 { 55 pattern = (Pattern)patternSpec; 56 } 57 else if (patternSpec != null) 58 { 59 pattern = Pattern.compile(patternSpec.toString()); 60 } 61 62 return pattern; 63 } 64 65 66 67 public static final Object replaceAll(Object object, Object pattern, Object replacement) 68 { 69 if (object != null) 70 { 71 object = object.toString().replaceAll(pattern.toString(), replacement.toString()); 72 } 73 74 return object; 75 } 76 77 78 79 public static final Object replaceFirst(Object object, Object pattern, Object replacement) 80 { 81 if (object != null) 82 { 83 object = object.toString().replaceFirst(pattern.toString(), replacement.toString()); 84 } 85 86 return object; 87 } 88 89 90 91 public static final Object matchingGroups(Object object, Object patternSpec) 92 { 93 if (object != null) 94 { 95 object = StringUtil.matchingGroups(object.toString(), TypeUtil.isPattern(patternSpec)); 96 } 97 98 return object; 99 } 100 101 123 124 125 126 public static final Object trim(Object object) 127 { 128 if (object != null) 129 { 130 object = object.toString().trim(); 131 } 132 133 return object; 134 } 135 136 137 138 public static final Object toUpperCase(Object object) 139 { 140 if (object != null) 141 { 142 object = object.toString().toUpperCase(); 143 } 144 145 return object; 146 } 147 148 149 150 public static final Object toLowerCase(Object object) 151 { 152 if (object != null) 153 { 154 object = object.toString().toLowerCase(); 155 } 156 157 return object; 158 } 159 160 161 162 public static final Object capitalize(Object object) 163 { 164 if (object != null) 165 { 166 object = StringUtil.capitalize(object.toString()); 167 } 168 169 return object; 170 } 171 172 173 174 public static final Object reverse(Object object) 175 { 176 if (object != null) 177 { 178 object = StringUtil.reverse(object.toString()); 179 } 180 181 return object; 182 } 183 184 185 186 public static final Object multiply(Object object, Object countSpec) 187 { 188 if (object != null) 189 { 190 Integer countInteger = TypeUtil.isInteger(countSpec); 191 192 int count = (countInteger != null) ? countInteger.intValue() : 0; 193 194 object = StringUtil.multiply(object.toString(), count); 195 } 196 197 return object; 198 } 199 200 201 202 public static final Object append(Object object, Object appended) throws Exception 203 { 204 if (object == null) 205 { 206 object = appended; 207 } 208 else 209 { 210 if (appended != null) 211 { 212 String appendedString = appended.toString(); 213 214 if (object instanceof Writer) 215 { 216 ((Writer)object).write(appendedString); 217 } 218 else if (object instanceof OutputStream) 219 { 220 ((OutputStream)object).write(appendedString.getBytes("ISO-8859-1")); 221 } 222 else 223 { 224 object = object.toString() + appendedString; 225 } 226 } 227 } 228 229 return object; 230 } 231 232 233 234 public static final Object prepend(Object object, Object prepended) throws Exception 235 { 236 if (object != null) 237 { 238 if (prepended != null) 239 { 240 String objectString = object.toString(); 241 242 if (prepended instanceof Writer) 243 { 244 ((Writer)prepended).write(objectString); 245 } 246 else if (prepended instanceof OutputStream) 247 { 248 ((OutputStream)prepended).write(objectString.getBytes("ISO-8859-1")); 249 } 250 else 251 { 252 object = prepended.toString() + objectString; 253 } 254 } 255 } 256 else 257 { 258 object = prepended; 259 } 260 261 return object; 262 } 263 264 265 266 public static final Object substring(Object object, Object start, Object end) 267 { 268 if (object != null) 269 { 270 String string = object.toString(); 271 272 int length = string.length(); 273 274 int startIndex = TypeUtil.isInteger(start).intValue(); 275 int endIndex = TypeUtil.isInteger(end).intValue(); 276 277 object = StringUtil.substring(string, startIndex, endIndex); 278 } 279 280 return object; 281 } 282 283 284 285 public static final Object substring(Object object, Object start) 286 { 287 if (object != null) 288 { 289 String string = object.toString(); 290 291 int startIndex = TypeUtil.isInteger(start).intValue(); 292 293 object = StringUtil.substring(string, startIndex); 294 } 295 296 return object; 297 } 298 299 300 301 public static final Object indexOf(Object object, Object sub) 302 { 303 if ((object != null) && (sub != null)) 304 { 305 object = new Integer (object.toString().indexOf(sub.toString())); 306 } 307 308 return object; 309 } 310 311 312 313 public static final Object lastIndexOf(Object object, Object sub) 314 { 315 if ((object != null) && (sub != null)) 316 { 317 object = new Integer (object.toString().lastIndexOf(sub.toString())); 318 } 319 320 return object; 321 } 322 323 324 325 public static final Object length(Object object) 326 { 327 if (object != null) 328 { 329 return new Integer (object.toString().length()); 330 } 331 332 return object; 333 } 334 335 336 337 public static final Object toCharacterList(Object object) 338 { 339 if (object != null) 340 { 341 return TypeUtil.isList(object.toString().toCharArray()); 342 } 343 344 return object; 345 } 346 347 348 349 public static final Object fromCharacterList(Object object) 350 { 351 if (object != null) 352 { 353 List list = (List)TypeUtil.isList(object); 354 355 int length = list.size(); 356 357 char[] array = new char[length]; 358 359 for (int i = length; --i >= 0;) 360 { 361 array[i] = ((Character )(list.get(i))).charValue(); 362 } 363 364 object = new String (array); 365 } 366 367 return object; 368 } 369 370 371 372 373 public static final Object split(Object object, Object patternSpec, Object groupSpec) 374 { 375 if (object != null) 376 { 377 Pattern pattern = getPattern(patternSpec); 378 379 int groupIndex = 0; 380 381 Integer groupIndexInteger = TypeUtil.isInteger(groupSpec); 382 383 if (groupIndexInteger != null) 384 { 385 groupIndex = groupIndexInteger.intValue(); 386 } 387 388 object = StringUtil.split(object.toString(), pattern, groupIndex); 389 } 390 391 return object; 392 } 393 394 395 396 public static final Object split(Object object, Object patternSpec) 397 { 398 return split(object, patternSpec, null); 399 } 400 401 402 403 public static final Object split(Object object) 404 { 405 return split(object, ","); 406 } 407 408 409 410 public static final Object matchAll(Object object, Object patternSpec, Object groupSpec) 411 { 412 if (object != null) 413 { 414 Pattern pattern = getPattern(patternSpec); 415 416 int groupIndex = 0; 417 418 Integer groupIndexInteger = TypeUtil.isInteger(groupSpec); 419 420 if (groupIndexInteger != null) 421 { 422 groupIndex = groupIndexInteger.intValue(); 423 } 424 425 object = StringUtil.matchAll(object.toString(), pattern, groupIndex); 426 } 427 428 return object; 429 } 430 431 432 433 public static final Object matchAll(Object object, Object patternSpec) 434 { 435 return matchAll(object, patternSpec, null); 436 } 437 438 439 440 public static final Object decompose(Object object, Object patternSpec, Object groupSpec) 441 { 442 if (object != null) 443 { 444 Pattern pattern = getPattern(patternSpec); 445 446 int groupIndex = 0; 447 448 Integer groupIndexInteger = TypeUtil.isInteger(groupSpec); 449 450 if (groupIndexInteger != null) 451 { 452 groupIndex = groupIndexInteger.intValue(); 453 } 454 455 object = StringUtil.decompose(object.toString(), pattern, groupIndex); 456 } 457 458 return object; 459 } 460 461 462 463 public static final Object decompose(Object object, Object patternSpec) 464 { 465 return decompose(object, patternSpec, null); 466 } 467 468 469 470 471 public static final Object match(Object object, Object patternSpec, Object groupSpec) 472 { 473 if (object != null) 474 { 475 Pattern pattern = getPattern(patternSpec); 476 477 int groupIndex = 0; 478 479 Integer groupIndexInteger = TypeUtil.isInteger(groupSpec); 480 481 if (groupIndexInteger != null) 482 { 483 groupIndex = groupIndexInteger.intValue(); 484 } 485 486 object = StringUtil.match(object.toString(), pattern, groupIndex); 487 } 488 489 return object; 490 } 491 492 493 494 public static final Object match(Object object, Object patternSpec) 495 { 496 return match(object, patternSpec, null); 497 } 498 499 500 501 public static final Object join(Object object, Object glue) 502 { 503 if (object != null) 504 { 505 List list = TypeUtil.isList(object); 506 507 if ((list != null) && (glue != null)) 508 { 509 object = StringUtil.join(list, glue.toString()); 510 } 511 else 512 { 513 object = null; 514 } 515 } 516 517 return object; 518 } 519 520 521 522 public static final Object join(Object object) 523 { 524 return join(object, ","); 525 } 526 527 528 529 public static final Object toByteArray(Object object, Object encoding) throws Exception 530 { 531 if (object != null) 532 { 533 String string = object.toString(); 534 535 if (encoding != null) 536 { 537 object = string.getBytes(encoding.toString()); 538 } 539 else 540 { 541 object = string.getBytes(); 542 } 543 } 544 545 return object; 546 } 547 548 549 550 public static final Object fromByteArray(Object object, Object encoding) throws Exception 551 { 552 if (object != null) 553 { 554 byte[] byteArray = (byte[])object; 555 556 if (encoding != null) 557 { 558 object = new String (byteArray, encoding.toString()); 559 } 560 else 561 { 562 object = new String (byteArray); 563 } 564 } 565 566 return object; 567 } 568 569 570 571 public static final Object countWords(Object object, Object pattern) 572 { 573 if (object != null) 574 { 575 object = StringUtil.countWords(object.toString(), getPattern(pattern.toString())); 576 } 577 578 return object; 579 } 580 581 582 583 public static final Object countWords(Object object) 584 { 585 return countWords(object, "[ ]+"); 586 } 587 588 589 590 public static final Object replaceCharacters(Object object, Object from, Object to) 591 { 592 if (object != null) 593 { 594 object = StringUtil.replaceCharacters(object.toString(), from.toString().toCharArray(), to.toString().toCharArray()); 595 } 596 597 return object; 598 } 599 600 601 602 public static final Object splitCSV(Object object, Object spec) throws Exception 603 { 604 if (object != null) 605 { 606 NestedMap specMap = TypeUtil.isNestedMap(spec); 607 String separator = StringUtil.decodeBackslash(specMap.getString("separator", ",")); 608 String quote = StringUtil.decodeBackslash(specMap.getString("quote", "\"")); 609 String newline = StringUtil.decodeBackslash(specMap.getString("newline", "\n")); 610 611 object = StringUtil.splitCSV(new StringReader(object.toString()), separator.charAt(0), quote.charAt(0), newline.charAt(0)); 612 } 613 614 return object; 615 } 616 617 public static final Object splitCSV(Object object) throws Exception 618 { 619 return splitCSV(object, null); 620 } 621 622 623 624 public static final Object escapeUnicode(Object object, Object controlSpec) 625 { 626 if (object != null) 627 { 628 NestedMap controlMap = TypeUtil.isNestedMap(controlSpec); 629 long minCode = controlMap.getLong("minCode", 128L); 630 long maxCode = controlMap.getLong("maxCode", 65535L); 631 object = StringUtil.escapeUnicode(object.toString(), minCode, maxCode); 632 } 633 634 return object; 635 } 636 637 public static final Object escapeUnicode(Object object) 638 { 639 return escapeUnicode(object, null); 640 } 641 } 642 643 | Popular Tags |