1 package com.icl.saxon.expr; 2 import com.icl.saxon.Controller; 3 import com.icl.saxon.om.NodeInfo; 4 import com.icl.saxon.om.NodeEnumeration; 5 import com.icl.saxon.sort.Comparer; 6 7 10 11 public class UnionEnumeration implements NodeEnumeration { 12 13 private NodeEnumeration p1; 14 private NodeEnumeration p2; 15 private NodeEnumeration e1; 16 private NodeEnumeration e2; 17 private NodeInfo nextNode1 = null; 18 private NodeInfo nextNode2 = null; 19 private Controller controller; 20 21 public UnionEnumeration(NodeEnumeration p1, NodeEnumeration p2, 22 Controller controller) throws XPathException { 23 this.p1 = p1; 24 this.p2 = p2; 25 this.controller = controller; 26 e1 = p1; 27 e2 = p2; 28 29 if (!e1.isSorted()) { 30 e1 = (new NodeSetExtent(e1, controller)).sort().enumerate(); 31 } 32 if (!e2.isSorted()) { 33 e2 = (new NodeSetExtent(e2, controller)).sort().enumerate(); 34 } 35 36 if (e1.hasMoreElements()) { 37 nextNode1 = e1.nextElement(); 38 } 39 if (e2.hasMoreElements()) { 40 nextNode2 = e2.nextElement(); 41 } 42 } 43 44 public boolean hasMoreElements() { 45 return nextNode1!=null || nextNode2!=null; 46 } 47 48 public NodeInfo nextElement() throws XPathException { 49 50 52 if (nextNode1 != null && nextNode2 != null) { 53 int c = controller.compare(nextNode1, nextNode2); 54 if (c<0) { 55 NodeInfo next = nextNode1; 56 if (e1.hasMoreElements()) { 57 nextNode1 = e1.nextElement(); 58 } else { 59 nextNode1 = null; 60 } 61 return next; 62 63 } else if (c>0) { 64 NodeInfo next = nextNode2; 65 if (e2.hasMoreElements()) { 66 nextNode2 = e2.nextElement(); 67 } else { 68 nextNode2 = null; 69 } 70 return next; 71 72 } else { 73 NodeInfo next = nextNode2; 74 if (e2.hasMoreElements()) { 75 nextNode2 = e2.nextElement(); 76 } else { 77 nextNode2 = null; 78 } 79 if (e1.hasMoreElements()) { 80 nextNode1 = e1.nextElement(); 81 } else { 82 nextNode1 = null; 83 } 84 return next; 85 } 86 } 87 88 90 if (nextNode1!=null) { 91 NodeInfo next = nextNode1; 92 if (e1.hasMoreElements()) { 93 nextNode1 = e1.nextElement(); 94 } else { 95 nextNode1 = null; 96 } 97 return next; 98 } 99 if (nextNode2!=null) { 100 NodeInfo next = nextNode2; 101 if (e2.hasMoreElements()) { 102 nextNode2 = e2.nextElement(); 103 } else { 104 nextNode2 = null; 105 } 106 return next; 107 } 108 return null; 109 } 110 111 public boolean isSorted() { 112 return true; 113 } 114 115 public boolean isReverseSorted() { 116 return false; 117 } 118 119 public boolean isPeer() { 120 return false; 121 } 122 123 } 124 125 126 127 | Popular Tags |