KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > exslt > Sets


1 package net.sf.saxon.exslt;
2 import net.sf.saxon.expr.*;
3 import net.sf.saxon.om.Item;
4 import net.sf.saxon.om.NodeInfo;
5 import net.sf.saxon.om.SequenceIterator;
6 import net.sf.saxon.sort.GlobalOrderComparer;
7 import net.sf.saxon.trans.DynamicError;
8 import net.sf.saxon.trans.XPathException;
9 import net.sf.saxon.value.SingletonNode;
10
11 /**
12 * This class implements extension functions in the
13 * http://exslt.org/sets namespace. <p>
14 */

15
16 public abstract class Sets {
17
18     private Sets() {}
19
20     /**
21     * Return the intersection of two node-sets
22     * @param p1 The first node-set
23     * @param p2 The second node-set
24     * @return A node-set containing all nodes that are in both p1 and p2
25     */

26
27     public static SequenceIterator intersection(SequenceIterator p1, SequenceIterator p2) throws XPathException {
28         return new IntersectionEnumeration(p1, p2, GlobalOrderComparer.getInstance());
29     }
30
31     /**
32     * Return the difference of two node-sets
33     * @param p1 The first node-set
34     * @param p2 The second node-set
35     * @return A node-set containing all nodes that are in p1 and not in p2
36     */

37
38     public static SequenceIterator difference(SequenceIterator p1, SequenceIterator p2) throws XPathException {
39         return new DifferenceEnumeration(p1, p2, GlobalOrderComparer.getInstance());
40     }
41
42     /**
43     * Determine whether two node-sets contain at least one node in common
44     * @param p1 The first node-set
45     * @param p2 The second node-set
46     * @return true if p1 and p2 contain at least one node in common (i.e. if the intersection
47     * is not empty)
48     */

49
50     public static boolean hasSameNode(SequenceIterator p1, SequenceIterator p2) throws XPathException {
51         SequenceIterator intersection =
52             new IntersectionEnumeration(p1, p2, GlobalOrderComparer.getInstance());
53         return intersection.next() != null;
54     }
55
56     /**
57     * Find all the nodes in ns1 that are before the first node in ns2.
58     * Return empty set if ns2 is empty,
59     */

60
61     public static SequenceIterator leading (
62                      XPathContext context,
63                      SequenceIterator ns1, SequenceIterator ns2) throws XPathException {
64
65         NodeInfo first = null;
66
67         // Find the first node in ns2 (in document order)
68

69         GlobalOrderComparer comparer = GlobalOrderComparer.getInstance();
70         while (true) {
71             Item item = ns2.next();
72             if (item == null) {
73                 if (first == null) {
74                     return ns1;
75                 }
76                 break;
77             }
78             if (item instanceof NodeInfo) {
79                 NodeInfo node = (NodeInfo)item;
80                 if (first==null) {
81                     first = node;
82                 } else {
83                     if (comparer.compare(node, first) < 0) {
84                         first = node;
85                     }
86                 }
87             } else {
88                 DynamicError e = new DynamicError(
89                         "Operand of leading() contains an item that is not a node");
90                 e.setXPathContext(context);
91                 throw e;
92             }
93         }
94
95         // Filter ns1 to select nodes that come before this one
96

97         Expression filter = new IdentityComparison(
98                                     new ContextItemExpression(),
99                                     Token.PRECEDES,
100                                     new SingletonNode(first));
101
102         return new FilterIterator(ns1, filter, context);
103
104     }
105
106     /**
107     * Find all the nodes in ns1 that are after the first node in ns2.
108     * Return empty set if ns2 is empty,
109     */

110
111     public static SequenceIterator trailing (
112                      XPathContext c,
113                      SequenceIterator ns1, SequenceIterator ns2) throws XPathException {
114
115         return net.sf.saxon.functions.Extensions.after(c, ns1, ns2);
116     }
117
118 }
119
120
121
122
123
124 //
125
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
126
// you may not use this file except in compliance with the License. You may obtain a copy of the
127
// License at http://www.mozilla.org/MPL/
128
//
129
// Software distributed under the License is distributed on an "AS IS" basis,
130
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
131
// See the License for the specific language governing rights and limitations under the License.
132
//
133
// The Original Code is: all this file.
134
//
135
// The Initial Developer of the Original Code is Michael H. Kay.
136
//
137
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
138
//
139
// Contributor(s): none.
140
//
141
Popular Tags