KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.expr;
2 import com.icl.saxon.Context;
3 import com.icl.saxon.Controller;
4 import com.icl.saxon.om.*;
5
6
7 import java.util.Hashtable JavaDoc;
8
9 /**
10 * An enumeration returning the distinct nodes from a supplied nodeset
11 */

12
13
14 public class DistinctEnumeration implements NodeEnumeration {
15
16     private NodeEnumeration p1;
17     private NodeEnumeration e1;
18     private Hashtable JavaDoc lookup = new Hashtable JavaDoc();
19     private Context context;
20     private Expression expression;
21     private Controller controller;
22
23     NodeInfo nextNode = null;
24
25     /**
26     * Form an enumeration of the distinct nodes in a node-set, distinguishing nodes
27     * by their string-value
28     */

29
30     public DistinctEnumeration(NodeEnumeration p1, Controller controller) throws XPathException {
31         this.p1 = p1;
32         this.context = null;
33         this.expression = null;
34         this.controller = controller;
35         e1 = p1;
36         if (!e1.isSorted()) {
37             //System.err.println("distinct - base enumeration not sorted");
38
e1 = (new NodeSetExtent(e1, controller)).sort().enumerate();
39         }
40         
41         // move to the first node in the input nodeset
42

43         if (e1.hasMoreElements()) {
44             nextNode = e1.nextElement();
45             advance();
46         }
47     }
48
49     public DistinctEnumeration(Context c, NodeEnumeration p1, Expression exp)
50     throws XPathException {
51         this.p1 = p1;
52         this.context = c.newContext();
53         this.expression = exp;
54         this.controller = c.getController();
55         e1 = p1;
56         if (!e1.isSorted()) {
57             e1 = (new NodeSetExtent(e1, controller)).sort().enumerate();
58         }
59         
60         // move to the first node in the input nodeset
61

62         if (e1.hasMoreElements()) {
63             nextNode = e1.nextElement();
64             advance();
65         }
66     }
67     
68     public boolean hasMoreElements() {
69         return nextNode!=null;
70     }
71
72     public NodeInfo nextElement() throws XPathException {
73         NodeInfo current = nextNode;
74         advance();
75         return current;
76     }
77
78     private void advance() throws XPathException {
79
80         while (nextNode != null) {
81             String JavaDoc val;
82             if (expression==null) {
83                 val = nextNode.getStringValue();
84             } else {
85                 context.setContextNode(nextNode);
86                 context.setPosition(1);
87                 context.setLast(1);
88                 val = expression.evaluateAsString(context);
89             }
90             if (lookup.get(val)==null) {
91                 lookup.put(val, nextNode);
92                 return;
93             } else {
94                 if (e1.hasMoreElements()) {
95                     nextNode = e1.nextElement();
96                 } else {
97                     nextNode = null;
98                 }
99             }
100         }
101
102     }
103
104     public boolean isSorted() {
105         return true;
106     }
107
108     public boolean isReverseSorted() {
109         return false;
110     }
111
112     public boolean isPeer() {
113         return false;
114     }
115
116
117 }
118
119 //
120
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
121
// you may not use this file except in compliance with the License. You may obtain a copy of the
122
// License at http://www.mozilla.org/MPL/
123
//
124
// Software distributed under the License is distributed on an "AS IS" basis,
125
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
126
// See the License for the specific language governing rights and limitations under the License.
127
//
128
// The Original Code is: all this file.
129
//
130
// The Initial Developer of the Original Code is
131
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
132
//
133
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
134
//
135
// Contributor(s): none.
136
//
137
Popular Tags