KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
10 * An enumeration representing a nodeset that is teh difference of two other NodeSets.
11 * There is an "except" operator in XPath 2.0 to create such an expression.
12 */

13
14
15 public class DifferenceEnumeration implements SequenceIterator {
16
17
18     private SequenceIterator p1;
19     private SequenceIterator p2;
20
21     private NodeInfo nextNode1 = null;
22     private NodeInfo nextNode2 = null;
23     private NodeOrderComparer comparer;
24
25     //private NodeInfo nextNode = null;
26
private NodeInfo current = null;
27     private int position = 0;
28
29     /**
30     * Form an enumeration of the difference of two nodesets, that is, the nodes
31     * that are in p1 and that are not in p2.
32     * @param p1 the first operand
33     * @param p2 the second operand
34     * @param comparer the comparer
35     */

36
37     public DifferenceEnumeration(SequenceIterator p1, SequenceIterator p2,
38                                  NodeOrderComparer comparer) throws XPathException {
39         this.p1 = p1;
40         this.p2 = p2;
41         this.comparer = comparer;
42
43         // move to the first node in each input nodeset
44

45         nextNode1 = next(p1);
46         nextNode2 = next(p2);
47     }
48
49     /**
50     * Get the next item from one of the input sequences,
51     * checking that it is a node.
52     */

53
54     private NodeInfo next(SequenceIterator iter) throws XPathException {
55         return (NodeInfo)iter.next();
56         // rely on type-checking to prevent a ClassCastException
57
}
58
59     public Item next() throws XPathException {
60         // main merge loop: if the node in p1 has a lower key value that that in p2, return it;
61
// if they are equal, advance both nodesets; if p1 is higher, advance p2.
62

63         while (true) {
64
65             if (nextNode1 == null) {
66                 current = null;
67                 position = -1;
68                 return null;
69             }
70
71             if (nextNode2 == null) {
72                 // second node-set is exhausted; return the next node from the first node-set
73
return deliver();
74             }
75
76             int c = comparer.compare(nextNode1, nextNode2);
77             if (c<0) { // p1 is lower
78
return deliver();
79
80             } else if (c>0) { // p1 is higher
81
nextNode2 = next(p2);
82                 if (nextNode2 == null) {
83                     return deliver();
84                 }
85
86             } else { // keys are equal
87
nextNode2 = next(p2);
88                 nextNode1 = next(p1);
89             }
90         }
91     }
92
93     /**
94      * Deliver the next node from the first node-set, advancing the iterator to
95      * look-ahead for the next item, and setting the current and position variables.
96      * @return the next node from the first node-set
97      * @throws XPathException
98      */

99     private NodeInfo deliver() throws XPathException {
100         current = nextNode1;
101         nextNode1 = next(p1);
102         position++;
103         return current;
104     }
105
106     public Item current() {
107         return current;
108     }
109
110     public int position() {
111         return position;
112     }
113
114     public SequenceIterator getAnother() throws XPathException {
115         return new DifferenceEnumeration(p1.getAnother(), p2.getAnother(), comparer);
116     }
117
118     /**
119      * Get properties of this iterator, as a bit-significant integer.
120      *
121      * @return the properties of this iterator. This will be some combination of
122      * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
123      * and {@link LOOKAHEAD}. It is always
124      * acceptable to return the value zero, indicating that there are no known special properties.
125      * It is acceptable for the properties of the iterator to change depending on its state.
126      */

127
128     public int getProperties() {
129         return 0;
130     }
131 }
132
133 //
134
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
135
// you may not use this file except in compliance with the License. You may obtain a copy of the
136
// License at http://www.mozilla.org/MPL/
137
//
138
// Software distributed under the License is distributed on an "AS IS" basis,
139
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
140
// See the License for the specific language governing rights and limitations under the License.
141
//
142
// The Original Code is: all this file.
143
//
144
// The Initial Developer of the Original Code is Michael H. Kay.
145
//
146
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
147
//
148
// Contributor(s): none.
149
//
150
Popular Tags