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 6 7 import java.util.*; 8 9 15 16 17 public class IntersectionEnumeration implements NodeEnumeration { 18 19 private NodeEnumeration p1; 20 private NodeEnumeration p2; 21 private NodeEnumeration e1; 22 private NodeEnumeration e2; 23 private NodeInfo nextNode1 = null; 24 private NodeInfo nextNode2 = null; 25 26 private NodeInfo nextNode = null; 27 private Controller controller; 28 29 35 36 public IntersectionEnumeration(NodeEnumeration p1, NodeEnumeration p2, 37 Controller controller) throws XPathException { 38 this.p1 = p1; 39 this.p2 = p2; 40 this.controller = controller; 41 e1 = p1; 42 e2 = p2; 43 if (!e1.isSorted()) { 44 e1 = (new NodeSetExtent(e1, controller)).sort().enumerate(); 45 } 46 if (!e2.isSorted()) { 47 e2 = (new NodeSetExtent(e2, controller)).sort().enumerate(); 48 } 49 50 52 if (e1.hasMoreElements()) { 53 nextNode1 = e1.nextElement(); 54 } 55 if (e2.hasMoreElements()) { 56 nextNode2 = e2.nextElement(); 57 } 58 59 61 advance(); 62 63 } 64 65 public boolean hasMoreElements() { 66 return nextNode!=null; 67 } 68 69 public NodeInfo nextElement() throws XPathException { 70 NodeInfo current = nextNode; 71 advance(); 72 return current; 73 } 74 75 private void advance() throws XPathException { 76 77 80 while (nextNode1 != null && nextNode2 !=null) { 81 int c = controller.compare(nextNode1, nextNode2); 82 if (c<0) { 83 NodeInfo next = nextNode1; 84 if (e1.hasMoreElements()) { 85 nextNode1 = e1.nextElement(); 86 } else { 87 nextNode1 = null; 88 nextNode = null; 89 } 90 91 } else if (c>0) { 92 NodeInfo next = nextNode2; 93 if (e2.hasMoreElements()) { 94 nextNode2 = e2.nextElement(); 95 } else { 96 nextNode2 = null; 97 nextNode = null; 98 } 99 100 } else { 102 nextNode = nextNode2; if (e2.hasMoreElements()) { 104 nextNode2 = e2.nextElement(); 105 } else { 106 nextNode2 = null; 107 } 108 if (e1.hasMoreElements()) { 109 nextNode1 = e1.nextElement(); 110 } else { 111 nextNode1 = null; 112 } 113 114 return; 115 } 116 } 117 nextNode = null; 118 } 119 120 public boolean isSorted() { 121 return true; 122 } 123 124 public boolean isReverseSorted() { 125 return false; 126 } 127 128 public boolean isPeer() { 129 return false; 130 } 131 132 133 } 134 135 | Popular Tags |