1 16 19 package org.apache.xpath.objects; 20 21 import org.apache.xalan.res.XSLMessages; 22 import org.apache.xml.utils.FastStringBuffer; 23 import org.apache.xml.utils.XMLCharacterRecognizer; 24 import org.apache.xml.utils.XMLString; 25 import org.apache.xml.utils.XMLStringFactory; 26 import org.apache.xpath.res.XPATHErrorResources; 27 28 31 public class XStringForFSB extends XString 32 { 33 34 35 int m_start; 36 37 38 int m_length; 39 40 41 protected String m_strCache = null; 42 43 44 protected int m_hash = 0; 45 46 53 public XStringForFSB(FastStringBuffer val, int start, int length) 54 { 55 56 super(val); 57 58 m_start = start; 59 m_length = length; 60 61 if (null == val) 62 throw new IllegalArgumentException ( 63 XSLMessages.createXPATHMessage(XPATHErrorResources.ER_FASTSTRINGBUFFER_CANNOT_BE_NULL, null)); 64 } 65 66 71 private XStringForFSB(String val) 72 { 73 74 super(val); 75 76 throw new IllegalArgumentException ( 77 XSLMessages.createXPATHMessage(XPATHErrorResources.ER_FSB_CANNOT_TAKE_STRING, null)); } 79 80 85 public FastStringBuffer fsb() 86 { 87 return ((FastStringBuffer) m_obj); 88 } 89 90 95 public void appendToFsb(org.apache.xml.utils.FastStringBuffer fsb) 96 { 97 fsb.append(str()); 99 } 100 101 106 public boolean hasString() 107 { 108 return (null != m_strCache); 109 } 110 111 117 123 public Object object() 124 { 125 return str(); 126 } 127 128 133 public String str() 134 { 135 136 if (null == m_strCache) 137 { 138 m_strCache = fsb().getString(m_start, m_length); 139 140 159 } 163 164 return m_strCache; 165 } 166 167 178 public void dispatchCharactersEvents(org.xml.sax.ContentHandler ch) 179 throws org.xml.sax.SAXException 180 { 181 fsb().sendSAXcharacters(ch, m_start, m_length); 182 } 183 184 193 public void dispatchAsComment(org.xml.sax.ext.LexicalHandler lh) 194 throws org.xml.sax.SAXException 195 { 196 fsb().sendSAXComment(lh, m_start, m_length); 197 } 198 199 205 public int length() 206 { 207 return m_length; 208 } 209 210 223 public char charAt(int index) 224 { 225 return fsb().charAt(m_start + index); 226 } 227 228 249 public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) 250 { 251 252 int n = srcEnd - srcBegin; 255 256 if (n > m_length) 257 n = m_length; 258 259 if (n > (dst.length - dstBegin)) 260 n = (dst.length - dstBegin); 261 262 int end = srcBegin + m_start + n; 263 int d = dstBegin; 264 FastStringBuffer fsb = fsb(); 265 266 for (int i = srcBegin + m_start; i < end; i++) 267 { 268 dst[d++] = fsb.charAt(i); 269 } 270 } 271 272 287 public boolean equals(XMLString obj2) 288 { 289 290 if (this == obj2) 291 { 292 return true; 293 } 294 295 int n = m_length; 296 297 if (n == obj2.length()) 298 { 299 FastStringBuffer fsb = fsb(); 300 int i = m_start; 301 int j = 0; 302 303 while (n-- != 0) 304 { 305 if (fsb.charAt(i) != obj2.charAt(j)) 306 { 307 return false; 308 } 309 310 i++; 311 j++; 312 } 313 314 return true; 315 } 316 317 return false; 318 } 319 320 329 public boolean equals(XObject obj2) 330 { 331 332 if (this == obj2) 333 { 334 return true; 335 } 336 if(obj2.getType() == XObject.CLASS_NUMBER) 337 return obj2.equals(this); 338 339 String str = obj2.str(); 340 int n = m_length; 341 342 if (n == str.length()) 343 { 344 FastStringBuffer fsb = fsb(); 345 int i = m_start; 346 int j = 0; 347 348 while (n-- != 0) 349 { 350 if (fsb.charAt(i) != str.charAt(j)) 351 { 352 return false; 353 } 354 355 i++; 356 j++; 357 } 358 359 return true; 360 } 361 362 return false; 363 } 364 365 376 public boolean equals(String anotherString) 377 { 378 379 int n = m_length; 380 381 if (n == anotherString.length()) 382 { 383 FastStringBuffer fsb = fsb(); 384 int i = m_start; 385 int j = 0; 386 387 while (n-- != 0) 388 { 389 if (fsb.charAt(i) != anotherString.charAt(j)) 390 { 391 return false; 392 } 393 394 i++; 395 j++; 396 } 397 398 return true; 399 } 400 401 return false; 402 } 403 404 419 public boolean equals(Object obj2) 420 { 421 422 if (null == obj2) 423 return false; 424 425 if(obj2 instanceof XNumber) 426 return obj2.equals(this); 427 428 else if (obj2 instanceof XNodeSet) 432 return obj2.equals(this); 433 else if (obj2 instanceof XStringForFSB) 434 return equals((XMLString) this); 435 else 436 return equals(obj2.toString()); 437 } 438 439 454 public boolean equalsIgnoreCase(String anotherString) 455 { 456 return (m_length == anotherString.length()) 457 ? str().equalsIgnoreCase(anotherString) : false; 458 } 459 460 474 public int compareTo(XMLString xstr) 475 { 476 477 int len1 = m_length; 478 int len2 = xstr.length(); 479 int n = Math.min(len1, len2); 480 FastStringBuffer fsb = fsb(); 481 int i = m_start; 482 int j = 0; 483 484 while (n-- != 0) 485 { 486 char c1 = fsb.charAt(i); 487 char c2 = xstr.charAt(j); 488 489 if (c1 != c2) 490 { 491 return c1 - c2; 492 } 493 494 i++; 495 j++; 496 } 497 498 return len1 - len2; 499 } 500 501 521 public int compareToIgnoreCase(XMLString xstr) 522 { 523 524 int len1 = m_length; 525 int len2 = xstr.length(); 526 int n = Math.min(len1, len2); 527 FastStringBuffer fsb = fsb(); 528 int i = m_start; 529 int j = 0; 530 531 while (n-- != 0) 532 { 533 char c1 = Character.toLowerCase(fsb.charAt(i)); 534 char c2 = Character.toLowerCase(xstr.charAt(j)); 535 536 if (c1 != c2) 537 { 538 return c1 - c2; 539 } 540 541 i++; 542 j++; 543 } 544 545 return len1 - len2; 546 } 547 548 561 public int hashCode() 562 { 563 569 588 589 return super.hashCode(); } 591 592 611 public boolean startsWith(XMLString prefix, int toffset) 612 { 613 614 FastStringBuffer fsb = fsb(); 615 int to = m_start + toffset; 616 int tlim = m_start + m_length; 617 int po = 0; 618 int pc = prefix.length(); 619 620 if ((toffset < 0) || (toffset > m_length - pc)) 622 { 623 return false; 624 } 625 626 while (--pc >= 0) 627 { 628 if (fsb.charAt(to) != prefix.charAt(po)) 629 { 630 return false; 631 } 632 633 to++; 634 po++; 635 } 636 637 return true; 638 } 639 640 655 public boolean startsWith(XMLString prefix) 656 { 657 return startsWith(prefix, 0); 658 } 659 660 677 public int indexOf(int ch) 678 { 679 return indexOf(ch, 0); 680 } 681 682 710 public int indexOf(int ch, int fromIndex) 711 { 712 713 int max = m_start + m_length; 714 FastStringBuffer fsb = fsb(); 715 716 if (fromIndex < 0) 717 { 718 fromIndex = 0; 719 } 720 else if (fromIndex >= m_length) 721 { 722 723 return -1; 725 } 726 727 for (int i = m_start + fromIndex; i < max; i++) 728 { 729 if (fsb.charAt(i) == ch) 730 { 731 return i - m_start; 732 } 733 } 734 735 return -1; 736 } 737 738 755 public XMLString substring(int beginIndex) 756 { 757 758 int len = m_length - beginIndex; 759 760 if (len <= 0) 761 return XString.EMPTYSTRING; 762 else 763 { 764 int start = m_start + beginIndex; 765 766 return new XStringForFSB(fsb(), start, len); 767 } 768 } 769 770 786 public XMLString substring(int beginIndex, int endIndex) 787 { 788 789 int len = endIndex - beginIndex; 790 791 if (len > m_length) 792 len = m_length; 793 794 if (len <= 0) 795 return XString.EMPTYSTRING; 796 else 797 { 798 int start = m_start + beginIndex; 799 800 return new XStringForFSB(fsb(), start, len); 801 } 802 } 803 804 814 public XMLString concat(String str) 815 { 816 817 return new XString(str().concat(str)); 819 } 820 821 826 public XMLString trim() 827 { 828 return fixWhiteSpace(true, true, false); 829 } 830 831 838 private static boolean isSpace(char ch) 839 { 840 return XMLCharacterRecognizer.isWhiteSpace(ch); } 842 843 857 public XMLString fixWhiteSpace(boolean trimHead, boolean trimTail, 858 boolean doublePunctuationSpaces) 859 { 860 861 int end = m_length + m_start; 862 char[] buf = new char[m_length]; 863 FastStringBuffer fsb = fsb(); 864 boolean edit = false; 865 866 867 int d = 0; 868 boolean pres = false; 869 870 for (int s = m_start; s < end; s++) 871 { 872 char c = fsb.charAt(s); 873 874 if (isSpace(c)) 875 { 876 if (!pres) 877 { 878 if (' ' != c) 879 { 880 edit = true; 881 } 882 883 buf[d++] = ' '; 884 885 if (doublePunctuationSpaces && (d != 0)) 886 { 887 char prevChar = buf[d - 1]; 888 889 if (!((prevChar == '.') || (prevChar == '!') 890 || (prevChar == '?'))) 891 { 892 pres = true; 893 } 894 } 895 else 896 { 897 pres = true; 898 } 899 } 900 else 901 { 902 edit = true; 903 pres = true; 904 } 905 } 906 else 907 { 908 buf[d++] = c; 909 pres = false; 910 } 911 } 912 913 if (trimTail && 1 <= d && ' ' == buf[d - 1]) 914 { 915 edit = true; 916 917 d--; 918 } 919 920 int start = 0; 921 922 if (trimHead && 0 < d && ' ' == buf[0]) 923 { 924 edit = true; 925 926 start++; 927 } 928 929 XMLStringFactory xsf = XMLStringFactoryImpl.getFactory(); 930 931 return edit ? xsf.newstr(buf, start, d - start) : this; 932 } 933 934 950 public double toDouble() 951 { 952 if(m_length == 0) 953 return Double.NaN; 954 int i; 955 char c; 956 String valueString = fsb().getString(m_start,m_length); 957 958 965 for (i=0;i<m_length;i++) 966 if (!XMLCharacterRecognizer.isWhiteSpace(valueString.charAt(i))) 967 break; 968 if (valueString.charAt(i) == '-') 969 i++; 970 for (;i<m_length;i++) { 971 c = valueString.charAt(i); 972 if (c != '.' && (c < '0' || c > '9')) 973 break; 974 } 975 for (;i<m_length;i++) 976 if (!XMLCharacterRecognizer.isWhiteSpace(valueString.charAt(i))) 977 break; 978 if (i != m_length) 979 return Double.NaN; 980 981 try { 982 return new Double (valueString).doubleValue(); 983 } catch (NumberFormatException nfe) { 984 return Double.NaN; 986 } 987 } 988 } 989 | Popular Tags |