KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3 import com.icl.saxon.om.*;
4 import org.w3c.dom.Node JavaDoc;
5 import org.w3c.dom.NodeList JavaDoc;
6
7 /**
8 * A node-set value containing zero or one nodes
9 */

10
11 public class SingletonNodeSet extends NodeSetValue implements NodeList JavaDoc {
12
13     protected NodeInfo node = null;
14     protected boolean generalUseAllowed = true;
15     
16     /**
17     * Allow general use as a node-set. This is required to lift the 1.0
18     * restrictions on use of result tree fragments
19     */

20     
21     public void allowGeneralUse() {
22         generalUseAllowed = true;
23     }
24     
25     /**
26     * Determine if general use as a node-set is allowed
27     */

28     
29     public boolean isGeneralUseAllowed() {
30         return generalUseAllowed;
31     }
32
33     /**
34     * Create an empty node-set
35     */

36
37     public SingletonNodeSet() {
38         node = null;
39     }
40
41     /**
42     * Create a node-set containing one node
43     */

44
45     public SingletonNodeSet(NodeInfo node) {
46         this.node = node;
47     }
48
49     /**
50     * Simplify the expression
51     */

52
53     public Expression simplify() {
54         if (node==null) {
55             return new EmptyNodeSet();
56         } else {
57             return this;
58         }
59     }
60
61     /**
62     * Evaluate the Node Set. This guarantees to return the result in sorted order.
63     * @param context The context for evaluation (not used)
64     */

65
66     public Value evaluate(Context context) {
67         return this;
68     }
69     
70     /**
71     * Evaluate an expression as a NodeSet.
72     * @param context The context in which the expression is to be evaluated
73     * @return the value of the expression, evaluated in the current context
74     */

75
76     public NodeSetValue evaluateAsNodeSet(Context context) {
77         return this;
78     }
79
80     /**
81     * Set a flag to indicate whether the nodes are sorted. Used when the creator of the
82     * node-set knows that they are already in document order.
83     * @param isSorted true if the caller wishes to assert that the nodes are in document order
84     * and do not need to be further sorted
85     */

86
87     public void setSorted(boolean isSorted) {}
88     
89     /**
90     * Test whether the value is known to be sorted
91     * @return true if the value is known to be sorted in document order, false if it is not
92     * known whether it is sorted.
93     */

94
95     public boolean isSorted() {
96         return true;
97     }
98     
99     /**
100     * Convert to string value
101     * @return the value of the first node in the node-set if there
102     * is one, otherwise an empty string
103     */

104
105     public String JavaDoc asString() {
106         if (node==null) {
107             return "";
108         } else {
109             return node.getStringValue();
110         }
111     }
112
113     /**
114     * Evaluate as a boolean.
115     * @return true if the node set is not empty
116     */

117
118     public boolean asBoolean() {
119         return node!=null;
120     }
121
122     /**
123     * Count the nodes in the node-set. Note this will sort the node set if necessary, to
124     * make sure there are no duplicates.
125     */

126
127     public int getCount() {
128         return (node==null ? 0 : 1);
129     }
130  
131     /**
132     * Sort the nodes into document order.
133     * This does nothing if the nodes are already known to be sorted; to force a sort,
134     * call setSorted(false)
135     * @return the same NodeSetValue, after sorting. (Historic)
136     */

137
138     public NodeSetValue sort() {
139         return this;
140     }
141     
142     /**
143     * Get the first node in the nodeset (in document order)
144     * @return the first node
145     */

146
147     public NodeInfo getFirst() {
148         return node;
149     }
150         
151
152     /**
153     * Test whether a nodeset "equals" another Value
154     */

155
156     public boolean equals(Value other) throws XPathException {
157
158         if (node==null) {
159             if (other instanceof BooleanValue) {
160                 return !other.asBoolean();
161             } else {
162                 return false;
163             }
164         }
165
166         if (other instanceof StringValue ||
167                 other instanceof FragmentValue ||
168                 other instanceof TextFragmentValue ||
169                 other instanceof ObjectValue) {
170             return node.getStringValue().equals(other.asString());
171                 
172         } else if (other instanceof NodeSetValue) {
173
174             // see if there is a node in A with the same string value as a node in B
175

176             try {
177                 String JavaDoc value = node.getStringValue();
178                 NodeEnumeration e2 = ((NodeSetValue)other).enumerate();
179                 while (e2.hasMoreElements()) {
180                     if (e2.nextElement().getStringValue().equals(value)) return true;
181                 }
182                 return false;
183             } catch (XPathException err) {
184                 throw new InternalSaxonError(err.getMessage());
185             }
186
187         } else if (other instanceof NumericValue) {
188                  return Value.stringToNumber(node.getStringValue())==other.asNumber();
189
190         } else if (other instanceof BooleanValue) {
191                  return other.asBoolean();
192                 
193         } else {
194                 throw new InternalSaxonError("Unknown data type in a relational expression");
195         }
196     }
197
198     /**
199     * Test whether a nodeset "not-equals" another Value
200     */

201
202     public boolean notEquals(Value other) throws XPathException {
203                 
204         if (node==null) {
205             if (other instanceof BooleanValue) {
206                 return other.asBoolean();
207             } else {
208                 return false;
209             }
210         }
211
212         if (other instanceof StringValue ||
213                 other instanceof FragmentValue ||
214                 other instanceof TextFragmentValue ||
215                 other instanceof ObjectValue) {
216             return !node.getStringValue().equals(other.asString());
217                                 
218         } else if (other instanceof NodeSetValue) {
219
220             try {
221                 String JavaDoc value = node.getStringValue();
222                 
223                 NodeEnumeration e2 = ((NodeSetValue)other).enumerate();
224                 while (e2.hasMoreElements()) {
225                     if (!e2.nextElement().getStringValue().equals(value)) return true;
226                 }
227                 return false;
228             } catch (XPathException err) {
229                 throw new InternalSaxonError(err.getMessage());
230             }
231
232         } else if (other instanceof NumericValue) {
233              return Value.stringToNumber(node.getStringValue())!=other.asNumber();
234                 
235         } else if (other instanceof BooleanValue) {
236              return !other.asBoolean();
237                 
238         } else {
239              throw new InternalSaxonError("Unknown data type in a relational expression");
240
241         }
242     }
243
244     /**
245     * Return an enumeration of this nodeset value.
246     */

247
248     public NodeEnumeration enumerate() throws XPathException {
249         return new SingletonEnumeration(node);
250     }
251
252     // implement DOM NodeList
253

254     /**
255     * return the number of nodes in the list (DOM method)
256     */

257
258     public int getLength() {
259         return getCount();
260     }
261
262     /**
263     * Return the n'th item in the list (DOM method)
264     */

265
266     public Node JavaDoc item(int index) {
267         if (index==0 && (node instanceof Node JavaDoc)) {
268             return (Node JavaDoc)node;
269         } else {
270             return null;
271         }
272     }
273
274 }
275
276 //
277
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
278
// you may not use this file except in compliance with the License. You may obtain a copy of the
279
// License at http://www.mozilla.org/MPL/
280
//
281
// Software distributed under the License is distributed on an "AS IS" basis,
282
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
283
// See the License for the specific language governing rights and limitations under the License.
284
//
285
// The Original Code is: all this file.
286
//
287
// The Initial Developer of the Original Code is
288
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
289
//
290
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
291
//
292
// Contributor(s): none.
293
//
294

295
Popular Tags