KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > exslt > Sets


1 package com.icl.saxon.exslt;
2 import com.icl.saxon.Context;
3 import com.icl.saxon.Controller;
4 import com.icl.saxon.expr.*;
5 import com.icl.saxon.om.NodeInfo;
6 import com.icl.saxon.om.NodeEnumeration;
7 import java.util.Hashtable JavaDoc;
8 import java.util.Vector JavaDoc;
9
10 /**
11 * This class implements extension functions in the
12 * http://exslt.org/sets namespace. <p>
13 */

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

23
24     public static NodeEnumeration intersection(Context c, NodeEnumeration p1, NodeEnumeration p2) throws XPathException {
25         return new IntersectionEnumeration(p1, p2, c.getController());
26     }
27
28     /**
29     * Return the difference of two node-sets
30     * @param p1 The first node-set
31     * @param p2 The second node-set
32     * @return A node-set containing all nodes that are in p1 and not in p2
33     */

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

46
47     public static boolean hasSameNode(Context c, NodeEnumeration p1, NodeEnumeration p2) throws XPathException {
48         NodeEnumeration intersection =
49             new IntersectionEnumeration(p1, p2, c.getController());
50         return intersection.hasMoreElements();
51     }
52
53     /**
54     * Given a node-set, return a subset that includes only nodes with distinct string-values
55     */

56
57     public static NodeEnumeration distinct(Context c, NodeEnumeration in) throws XPathException {
58         return new DistinctEnumeration(in, c.getController());
59     }
60
61     /**
62     * Find all the nodes in ns1 that are before the first node in ns2.
63     * Return empty set if ns2 is empty,
64     */

65
66     public static NodeSetValue leading (
67                      Context c,
68                      NodeEnumeration ns1, NodeEnumeration ns2) throws XPathException {
69
70         Controller controller = c.getController();
71         if (!ns2.hasMoreElements()) {
72             return new NodeSetExtent(ns1, controller);
73         }
74         NodeInfo test = ns2.nextElement();
75         
76         Vector JavaDoc v = new Vector JavaDoc();
77         while (ns1.hasMoreElements()) {
78             NodeInfo node = ns1.nextElement();
79             if (controller.compare(node, test) < 0) {
80                 v.addElement(node);
81             } else {
82                 break;
83             }
84         }
85         return new NodeSetExtent(v, controller);
86
87     }
88
89     /**
90     * Find all the nodes in ns1 that are after the first node in ns2.
91     * Return empty set if ns2 is empty,
92     */

93
94     public static NodeSetValue trailing (
95                      Context c,
96                      NodeEnumeration ns1, NodeEnumeration ns2) throws XPathException {
97
98         if (!ns2.hasMoreElements()) {
99             return new EmptyNodeSet();
100         }
101         NodeInfo test = ns2.nextElement();
102         Controller controller = c.getController();
103         
104         Vector JavaDoc v = new Vector JavaDoc();
105         boolean pastLimit = false;
106         while (ns1.hasMoreElements()) {
107             NodeInfo node = ns1.nextElement();
108             if (pastLimit) {
109                 v.addElement(node);
110             } else if (controller.compare(node, test) > 0) {
111                 pastLimit = true;
112                 v.addElement(node);
113             }
114         }
115         return new NodeSetExtent(v, controller);
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
136
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
137
//
138
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
139
//
140
// Contributor(s): none.
141
//
142
Popular Tags