KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3 import com.icl.saxon.om.*;
4
5
6
7 /**
8 * A node set expression that will always return zero or one nodes
9 */

10
11 public abstract class SingletonExpression extends NodeSetExpression {
12
13     /**
14     * Determine, in the case of an expression whose data type is Value.NODESET,
15     * whether all the nodes in the node-set are guaranteed to come from the same
16     * document as the context node. Used for optimization.
17     */

18     
19     public boolean isContextDocumentNodeSet() {
20         return true;
21     }
22
23     /**
24     * Get the single node to which this expression refers
25     */

26
27     public abstract NodeInfo getNode(Context context) throws XPathException;
28
29     /**
30     * Return the first node selected by this Expression when evaluated in the current context
31     * @param context The context for the evaluation
32     * @return the NodeInfo of the first node in document order, or null if the node-set
33     * is empty.
34     */

35
36     public NodeInfo selectFirst(Context context) throws XPathException {
37         return getNode(context);
38     }
39
40     /**
41     * Evaluate the expression in a given context to return a Node enumeration
42     * @param context the evaluation context
43     * @param sort Indicates result must be in document order
44     */

45
46     public NodeEnumeration enumerate(Context context, boolean sort) throws XPathException {
47         return new SingletonEnumeration(getNode(context));
48     }
49
50     /**
51     * Evaluate an expression as a NodeSet.
52     * @param context The context in which the expression is to be evaluated
53     * @return the value of the expression, evaluated in the current context
54     */

55
56     public NodeSetValue evaluateAsNodeSet(Context context) throws XPathException {
57         return new SingletonNodeSet(getNode(context));
58     }
59
60     /**
61     * Evaluate as a string. Returns the string value of the node if it exists
62     * @param context The context in which the expression is to be evaluated
63     * @return true if there are any nodes selected by the NodeSetExpression
64     */

65
66     public String JavaDoc evaluateAsString(Context context) throws XPathException {
67         NodeInfo node = getNode(context);
68         if (node==null) return "";
69         return node.getStringValue();
70     }
71
72     /**
73     * Evaluate as a boolean. Returns true if there are any nodes
74     * selected by the NodeSetExpression
75     * @param context The context in which the expression is to be evaluated
76     * @return true if there are any nodes selected by the NodeSetExpression
77     */

78
79     public boolean evaluateAsBoolean(Context context) throws XPathException {
80         return getNode(context) != null;
81     }
82
83 }
84
85
86
87 //
88
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
89
// you may not use this file except in compliance with the License. You may obtain a copy of the
90
// License at http://www.mozilla.org/MPL/
91
//
92
// Software distributed under the License is distributed on an "AS IS" basis,
93
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
94
// See the License for the specific language governing rights and limitations under the License.
95
//
96
// The Original Code is: all this file.
97
//
98
// The Initial Developer of the Original Code is
99
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
100
//
101
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
102
//
103
// Contributor(s): none.
104
//
105
Popular Tags