1 16 19 package org.apache.xpath.objects; 20 21 import org.apache.xml.dtm.DTM; 22 import org.apache.xml.dtm.DTMIterator; 23 import org.apache.xml.dtm.DTMManager; 24 import org.apache.xml.utils.XMLString; 25 import org.apache.xpath.NodeSetDTM; 26 import org.apache.xpath.axes.NodeSequence; 27 28 import org.w3c.dom.NodeList ; 29 import org.w3c.dom.traversal.NodeIterator; 30 31 36 public class XNodeSet extends NodeSequence 37 { 38 41 protected XNodeSet() 42 { 43 } 44 45 50 public XNodeSet(DTMIterator val) 51 { 52 super(); 53 if(val instanceof XNodeSet) 54 { 55 setIter(((XNodeSet)val).m_iter); 56 m_dtmMgr = ((XNodeSet)val).m_dtmMgr; 57 m_last = ((XNodeSet)val).m_last; 58 if(!((XNodeSet)val).hasCache()) 59 ((XNodeSet)val).setShouldCacheNodes(true); 60 m_obj = ((XNodeSet)val).m_obj; 61 } 62 else 63 setIter(val); 64 } 65 66 71 public XNodeSet(XNodeSet val) 72 { 73 super(); 74 setIter(val.m_iter); 75 m_dtmMgr = val.m_dtmMgr; 76 m_last = val.m_last; 77 if(!val.hasCache()) 78 val.setShouldCacheNodes(true); 79 m_obj = val.m_obj; 80 } 81 82 83 87 public XNodeSet(DTMManager dtmMgr) 88 { 89 this(DTM.NULL,dtmMgr); 90 } 91 92 97 public XNodeSet(int n, DTMManager dtmMgr) 98 { 99 100 super(new NodeSetDTM(dtmMgr)); 101 m_dtmMgr = dtmMgr; 102 103 if (DTM.NULL != n) 104 { 105 ((NodeSetDTM) m_obj).addNode(n); 106 m_last = 1; 107 } 108 else 109 m_last = 0; 110 } 111 112 117 public int getType() 118 { 119 return CLASS_NODESET; 120 } 121 122 128 public String getTypeString() 129 { 130 return "#NODESET"; 131 } 132 133 140 public double getNumberFromNode(int n) 141 { 142 XMLString xstr = m_dtmMgr.getDTM(n).getStringValue(n); 143 return xstr.toDouble(); 144 } 145 146 152 public double num() 153 { 154 155 int node = item(0); 156 return (node != DTM.NULL) ? getNumberFromNode(node) : Double.NaN; 157 } 158 159 166 public double numWithSideEffects() 167 { 168 int node = nextNode(); 169 170 return (node != DTM.NULL) ? getNumberFromNode(node) : Double.NaN; 171 } 172 173 174 179 public boolean bool() 180 { 181 return (item(0) != DTM.NULL); 182 } 183 184 190 public boolean boolWithSideEffects() 191 { 192 return (nextNode() != DTM.NULL); 193 } 194 195 196 203 public XMLString getStringFromNode(int n) 204 { 205 if(DTM.NULL != n) 208 { 209 return m_dtmMgr.getDTM(n).getStringValue(n); 210 } 211 else 212 { 213 return org.apache.xpath.objects.XString.EMPTYSTRING; 214 } 215 } 216 217 228 public void dispatchCharactersEvents(org.xml.sax.ContentHandler ch) 229 throws org.xml.sax.SAXException 230 { 231 int node = item(0); 232 233 if(node != DTM.NULL) 234 { 235 m_dtmMgr.getDTM(node).dispatchCharactersEvents(node, ch, false); 236 } 237 238 } 239 240 245 public XMLString xstr() 246 { 247 int node = item(0); 248 return (node != DTM.NULL) ? getStringFromNode(node) : XString.EMPTYSTRING; 249 } 250 251 256 public void appendToFsb(org.apache.xml.utils.FastStringBuffer fsb) 257 { 258 XString xstring = (XString)xstr(); 259 xstring.appendToFsb(fsb); 260 } 261 262 263 269 public String str() 270 { 271 int node = item(0); 272 return (node != DTM.NULL) ? getStringFromNode(node).toString() : ""; 273 } 274 275 281 public Object object() 282 { 283 if(null == m_obj) 284 return this; 285 else 286 return m_obj; 287 } 288 289 317 324 public NodeIterator nodeset() throws javax.xml.transform.TransformerException 325 { 326 return new org.apache.xml.dtm.ref.DTMNodeIterator(iter()); 327 } 328 329 336 public NodeList nodelist() throws javax.xml.transform.TransformerException 337 { 338 org.apache.xml.dtm.ref.DTMNodeList nodelist = new org.apache.xml.dtm.ref.DTMNodeList(this); 339 XNodeSet clone = (XNodeSet)nodelist.getDTMIterator(); 344 SetVector(clone.getVector()); 345 return nodelist; 346 } 347 348 349 360 363 public DTMIterator iterRaw() 364 { 365 return this; 366 } 367 368 public void release(DTMIterator iter) 369 { 370 } 371 372 377 public DTMIterator iter() 378 { 379 try 380 { 381 if(hasCache()) 382 return cloneWithReset(); 383 else 384 return this; } 386 catch (CloneNotSupportedException cnse) 387 { 388 throw new RuntimeException (cnse.getMessage()); 389 } 390 } 391 392 397 public XObject getFresh() 398 { 399 try 400 { 401 if(hasCache()) 402 return (XObject)cloneWithReset(); 403 else 404 return this; } 406 catch (CloneNotSupportedException cnse) 407 { 408 throw new RuntimeException (cnse.getMessage()); 409 } 410 } 411 412 417 public NodeSetDTM mutableNodeset() 418 { 419 NodeSetDTM mnl; 420 421 if(m_obj instanceof NodeSetDTM) 422 { 423 mnl = (NodeSetDTM) m_obj; 424 } 425 else 426 { 427 mnl = new NodeSetDTM(iter()); 428 m_obj = mnl; 429 setCurrentPos(0); 430 } 431 432 return mnl; 433 } 434 435 436 static LessThanComparator S_LT = new LessThanComparator(); 437 438 439 static LessThanOrEqualComparator S_LTE = new LessThanOrEqualComparator(); 440 441 442 static GreaterThanComparator S_GT = new GreaterThanComparator(); 443 444 445 static GreaterThanOrEqualComparator S_GTE = 446 new GreaterThanOrEqualComparator(); 447 448 449 static EqualComparator S_EQ = new EqualComparator(); 450 451 452 static NotEqualComparator S_NEQ = new NotEqualComparator(); 453 454 464 public boolean compare(XObject obj2, Comparator comparator) 465 throws javax.xml.transform.TransformerException 466 { 467 468 boolean result = false; 469 int type = obj2.getType(); 470 471 if (XObject.CLASS_NODESET == type) 472 { 473 475 DTMIterator list1 = iterRaw(); 487 DTMIterator list2 = ((XNodeSet) obj2).iterRaw(); 488 int node1; 489 java.util.Vector node2Strings = null; 490 491 while (DTM.NULL != (node1 = list1.nextNode())) 492 { 493 XMLString s1 = getStringFromNode(node1); 494 495 if (null == node2Strings) 496 { 497 int node2; 498 499 while (DTM.NULL != (node2 = list2.nextNode())) 500 { 501 XMLString s2 = getStringFromNode(node2); 502 503 if (comparator.compareStrings(s1, s2)) 504 { 505 result = true; 506 507 break; 508 } 509 510 if (null == node2Strings) 511 node2Strings = new java.util.Vector (); 512 513 node2Strings.addElement(s2); 514 } 515 } 516 else 517 { 518 int n = node2Strings.size(); 519 520 for (int i = 0; i < n; i++) 521 { 522 if (comparator.compareStrings(s1, (XMLString)node2Strings.elementAt(i))) 523 { 524 result = true; 525 526 break; 527 } 528 } 529 } 530 } 531 list1.reset(); 532 list2.reset(); 533 } 534 else if (XObject.CLASS_BOOLEAN == type) 535 { 536 537 double num1 = bool() ? 1.0 : 0.0; 544 double num2 = obj2.num(); 545 546 result = comparator.compareNumbers(num1, num2); 547 } 548 else if (XObject.CLASS_NUMBER == type) 549 { 550 551 DTMIterator list1 = iterRaw(); 559 double num2 = obj2.num(); 560 int node; 561 562 while (DTM.NULL != (node = list1.nextNode())) 563 { 564 double num1 = getNumberFromNode(node); 565 566 if (comparator.compareNumbers(num1, num2)) 567 { 568 result = true; 569 570 break; 571 } 572 } 573 list1.reset(); 574 } 575 else if (XObject.CLASS_RTREEFRAG == type) 576 { 577 XMLString s2 = obj2.xstr(); 578 DTMIterator list1 = iterRaw(); 579 int node; 580 581 while (DTM.NULL != (node = list1.nextNode())) 582 { 583 XMLString s1 = getStringFromNode(node); 584 585 if (comparator.compareStrings(s1, s2)) 586 { 587 result = true; 588 589 break; 590 } 591 } 592 list1.reset(); 593 } 594 else if (XObject.CLASS_STRING == type) 595 { 596 597 XMLString s2 = obj2.xstr(); 604 DTMIterator list1 = iterRaw(); 605 int node; 606 607 while (DTM.NULL != (node = list1.nextNode())) 608 { 609 XMLString s1 = getStringFromNode(node); 610 if (comparator.compareStrings(s1, s2)) 611 { 612 result = true; 613 614 break; 615 } 616 } 617 list1.reset(); 618 } 619 else 620 { 621 result = comparator.compareNumbers(this.num(), obj2.num()); 622 } 623 624 return result; 625 } 626 627 636 public boolean lessThan(XObject obj2) throws javax.xml.transform.TransformerException 637 { 638 return compare(obj2, S_LT); 639 } 640 641 650 public boolean lessThanOrEqual(XObject obj2) throws javax.xml.transform.TransformerException 651 { 652 return compare(obj2, S_LTE); 653 } 654 655 664 public boolean greaterThan(XObject obj2) throws javax.xml.transform.TransformerException 665 { 666 return compare(obj2, S_GT); 667 } 668 669 678 public boolean greaterThanOrEqual(XObject obj2) 679 throws javax.xml.transform.TransformerException 680 { 681 return compare(obj2, S_GTE); 682 } 683 684 693 public boolean equals(XObject obj2) 694 { 695 try 696 { 697 return compare(obj2, S_EQ); 698 } 699 catch(javax.xml.transform.TransformerException te) 700 { 701 throw new org.apache.xml.utils.WrappedRuntimeException(te); 702 } 703 } 704 705 714 public boolean notEquals(XObject obj2) throws javax.xml.transform.TransformerException 715 { 716 return compare(obj2, S_NEQ); 717 } 718 } 719 720 723 abstract class Comparator 724 { 725 726 735 abstract boolean compareStrings(XMLString s1, XMLString s2); 736 737 746 abstract boolean compareNumbers(double n1, double n2); 747 } 748 749 752 class LessThanComparator extends Comparator 753 { 754 755 764 boolean compareStrings(XMLString s1, XMLString s2) 765 { 766 return (s1.toDouble() < s2.toDouble()); 767 } 769 770 779 boolean compareNumbers(double n1, double n2) 780 { 781 return n1 < n2; 782 } 783 } 784 785 788 class LessThanOrEqualComparator extends Comparator 789 { 790 791 800 boolean compareStrings(XMLString s1, XMLString s2) 801 { 802 return (s1.toDouble() <= s2.toDouble()); 803 } 805 806 815 boolean compareNumbers(double n1, double n2) 816 { 817 return n1 <= n2; 818 } 819 } 820 821 824 class GreaterThanComparator extends Comparator 825 { 826 827 836 boolean compareStrings(XMLString s1, XMLString s2) 837 { 838 return (s1.toDouble() > s2.toDouble()); 839 } 841 842 851 boolean compareNumbers(double n1, double n2) 852 { 853 return n1 > n2; 854 } 855 } 856 857 860 class GreaterThanOrEqualComparator extends Comparator 861 { 862 863 872 boolean compareStrings(XMLString s1, XMLString s2) 873 { 874 return (s1.toDouble() >= s2.toDouble()); 875 } 877 878 887 boolean compareNumbers(double n1, double n2) 888 { 889 return n1 >= n2; 890 } 891 } 892 893 896 class EqualComparator extends Comparator 897 { 898 899 908 boolean compareStrings(XMLString s1, XMLString s2) 909 { 910 return s1.equals(s2); 911 } 912 913 922 boolean compareNumbers(double n1, double n2) 923 { 924 return n1 == n2; 925 } 926 } 927 928 931 class NotEqualComparator extends Comparator 932 { 933 934 943 boolean compareStrings(XMLString s1, XMLString s2) 944 { 945 return !s1.equals(s2); 946 } 947 948 957 boolean compareNumbers(double n1, double n2) 958 { 959 return n1 != n2; 960 } 961 } 962 | Popular Tags |