KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > expr > IntersectionEnumeration


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 /**
10 * An enumeration representing a nodeset that is an intersection of two other NodeSets.
11 * There is currently no operator in XPath to create such an expression, but it is used
12 * by the extension function intersection(). The code is derived from the analagous UnionEnumeration,
13 * an inner class of UnionExpression.
14 */

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     /**
30     * Form an enumeration of the intersection of the nodes in two nodesets
31     * @param p1 the first operand
32     * @param p2 the second operand
33     * @param controller Comparer to be used for putting nodes in document order
34     */

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         // move to the first node in each input nodeset
51

52         if (e1.hasMoreElements()) {
53             nextNode1 = e1.nextElement();
54         }
55         if (e2.hasMoreElements()) {
56             nextNode2 = e2.nextElement();
57         }
58
59         // move to the first node that matches in both
60

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         // main merge loop: iterate whichever set has the lower value, returning when a pair
78
// is found that match.
79

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 { // keys are equal
101

102                 nextNode = nextNode2; // which is the same as nextNode1
103
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 //
136
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
137
// you may not use this file except in compliance with the License. You may obtain a copy of the
138
// License at http://www.mozilla.org/MPL/
139
//
140
// Software distributed under the License is distributed on an "AS IS" basis,
141
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
142
// See the License for the specific language governing rights and limitations under the License.
143
//
144
// The Original Code is: all this file.
145
//
146
// The Initial Developer of the Original Code is
147
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
148
//
149
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
150
//
151
// Contributor(s): none.
152
//
153
Popular Tags