KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > expr > IntersectionEnumeration


1 package net.sf.saxon.expr;
2 import net.sf.saxon.om.Item;
3 import net.sf.saxon.om.NodeInfo;
4 import net.sf.saxon.om.SequenceIterator;
5 import net.sf.saxon.sort.NodeOrderComparer;
6 import net.sf.saxon.trans.XPathException;
7
8 /**
9 * An enumeration representing a nodeset that is an intersection of two other NodeSets.
10 * This implements the XPath 2.0 operator "intersect".
11 */

12
13
14 public class IntersectionEnumeration implements SequenceIterator {
15
16     private SequenceIterator e1;
17     private SequenceIterator e2;
18     private NodeInfo nextNode1 = null;
19     private NodeInfo nextNode2 = null;
20     private NodeOrderComparer comparer;
21
22     private NodeInfo current = null;
23     private int position = 0;
24
25     /**
26     * Form an enumeration of the intersection of the nodes in two nodesets
27     * @param p1 the first operand: must be in document order
28     * @param p2 the second operand: must be in document order
29     * @param comparer Comparer to be used for putting nodes in document order
30     */

31
32     public IntersectionEnumeration(SequenceIterator p1, SequenceIterator p2,
33                                     NodeOrderComparer comparer ) throws XPathException {
34         this.e1 = p1;
35         this.e2 = p2;
36         this.comparer = comparer;
37
38         // move to the first node in each input nodeset
39

40         nextNode1 = next(e1);
41         nextNode2 = next(e2);
42     }
43
44     /**
45     * Get the next item from one of the input sequences,
46     * checking that it is a node.
47     */

48
49     private NodeInfo next(SequenceIterator iter) throws XPathException {
50         return (NodeInfo)iter.next();
51         // rely on type-checking to prevent a ClassCastException
52
}
53
54     public Item next() throws XPathException {
55         // main merge loop: iterate whichever set has the lower value, returning when a pair
56
// is found that match.
57

58         if (nextNode1 == null || nextNode2 == null) {
59             current = null;
60             position = -1;
61             return null;
62         }
63
64         while (nextNode1 != null && nextNode2 != null) {
65             int c = comparer.compare(nextNode1, nextNode2);
66             if (c<0) {
67                 nextNode1 = next(e1);
68             } else if (c>0) {
69                 nextNode2 = next(e2);
70             } else { // keys are equal
71
current = nextNode2; // which is the same as nextNode1
72
nextNode2 = next(e2);
73                 nextNode1 = next(e1);
74                 position++;
75                 return current;
76             }
77         }
78         return null;
79     }
80
81     public Item current() {
82         return current;
83     }
84
85     public int position() {
86         return position;
87     }
88
89     public SequenceIterator getAnother() throws XPathException {
90         return new IntersectionEnumeration(e1.getAnother(), e2.getAnother(), comparer);
91     }
92
93     /**
94      * Get properties of this iterator, as a bit-significant integer.
95      *
96      * @return the properties of this iterator. This will be some combination of
97      * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
98      * and {@link LOOKAHEAD}. It is always
99      * acceptable to return the value zero, indicating that there are no known special properties.
100      * It is acceptable for the properties of the iterator to change depending on its state.
101      */

102
103     public int getProperties() {
104         return 0;
105     }
106 }
107
108 //
109
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
110
// you may not use this file except in compliance with the License. You may obtain a copy of the
111
// License at http://www.mozilla.org/MPL/
112
//
113
// Software distributed under the License is distributed on an "AS IS" basis,
114
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
115
// See the License for the specific language governing rights and limitations under the License.
116
//
117
// The Original Code is: all this file.
118
//
119
// The Initial Developer of the Original Code is Michael H. Kay.
120
//
121
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
122
//
123
// Contributor(s): none.
124
//
125
Popular Tags