KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.expr;
2 import com.icl.saxon.Context;
3 import com.icl.saxon.Controller;
4 import com.icl.saxon.om.NodeInfo;
5 import com.icl.saxon.om.NodeEnumeration;
6 import com.icl.saxon.output.Outputter;
7
8 import javax.xml.transform.TransformerException;
9
10 /**
11 * A NodeSetExpression is any expression denoting a set of nodes. <BR>
12 * This is an abstract class, the methods are defaults which may be overridden in subclasses
13 */

14
15
16 public abstract class NodeSetExpression extends Expression {
17
18     /**
19     * Return a node enumeration. All NodeSetExpressions must implement this method:
20     * the evaluate() function is defined in terms of it. (But note that some expressions
21     * that return node-sets are not NodeSetExpressions: for example functions such as
22     * key(), id(), and document() are not, and neither are variable references).
23     * @param context The evaluation context
24     * @param sorted True if the nodes must be returned in document order
25     */

26
27     public abstract NodeEnumeration enumerate(Context context, boolean sorted) throws XPathException;
28
29     /**
30     * Evaluate this node-set. This doesn't actually retrieve all the nodes: it returns a wrapper
31     * around a node-set expression in which all context dependencies have been eliminated.
32     */

33
34     public Value evaluate(Context context) throws XPathException {
35         
36         // lazy evaluation:
37
// we eliminate all context dependencies, and save the resulting expression.
38

39         Expression exp = reduce(Context.ALL_DEPENDENCIES, context);
40         if (exp instanceof NodeSetValue) {
41             return (Value)exp;
42         } else if (exp instanceof NodeSetExpression) {
43             return new NodeSetIntent((NodeSetExpression)exp, context.getController());
44         } else {
45             Value value = exp.evaluate(context);
46             if (value instanceof NodeSetValue) {
47                 return value;
48             } else {
49                 throw new XPathException("Value must be a node-set: it is a " + exp.getClass());
50             }
51         }
52     }
53
54     /**
55     * Return the first node selected by this Expression when evaluated
56     * in the current context
57     * @param context The context for the evaluation
58     * @return the NodeInfo of the first node in document order, or null if the node-set
59     * is empty.
60     */

61
62     public NodeInfo selectFirst(Context context) throws XPathException {
63         NodeEnumeration enum = enumerate(context, false);
64         if (enum.isSorted()) {
65             if (enum.hasMoreElements()) {
66                 return enum.nextElement();
67             } else {
68                 return null;
69             }
70         } else {
71             // avoid doing a sort:
72
// just scan for the node that's first in document order
73
Controller controller = context.getController();
74             NodeInfo first = null;
75             while (enum.hasMoreElements()) {
76                 NodeInfo next = enum.nextElement();
77                 if (first==null || controller.compare(next, first)<0) {
78                     first = next;
79                 }
80             }
81             return first;
82         }
83     }
84
85     /**
86     * Evaluate as a string. Returns the string value of the first node
87     * selected by the NodeSetExpression
88     * @param context The context in which the expression is to be evaluated
89     * @return the value of the NodeSetExpression, evaluated in the current context
90     */

91
92     public String evaluateAsString(Context context) throws XPathException {
93         NodeInfo e = selectFirst(context);
94         if (e==null) return "";
95         return e.getStringValue();
96     }
97
98     /**
99     * Evaluate an expression as a String and write the result to the
100     * specified outputter.<br>
101     * @param context The context in which the expression is to be evaluated
102     * @return the value of the expression, evaluated in the current context
103     */

104
105     public void outputStringValue(Outputter out, Context context) throws TransformerException {
106         NodeInfo first = selectFirst(context);
107         if (first!=null) {
108             first.copyStringValue(out);
109         }
110     }
111
112     /**
113     * Evaluate as a boolean. Returns true if there are any nodes
114     * selected by the NodeSetExpression
115     * @param context The context in which the expression is to be evaluated
116     * @return true if there are any nodes selected by the NodeSetExpression
117     */

118
119     public boolean evaluateAsBoolean(Context context) throws XPathException {
120         return enumerate(context, false).hasMoreElements();
121     }
122
123     /**
124     * Evaluate an expression as a NodeSet.
125     * @param context The context in which the expression is to be evaluated
126     * @return the value of the expression, evaluated in the current context
127     */

128
129     public NodeSetValue evaluateAsNodeSet(Context context) throws XPathException {
130         return (NodeSetValue)this.evaluate(context);
131     }
132
133     /**
134     * Determine the data type of the exprssion, if possible
135     * @return Value.NODESET
136     */

137
138     public int getDataType() {
139         return Value.NODESET;
140     }
141
142 }
143
144 //
145
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
146
// you may not use this file except in compliance with the License. You may obtain a copy of the
147
// License at http://www.mozilla.org/MPL/
148
//
149
// Software distributed under the License is distributed on an "AS IS" basis,
150
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
151
// See the License for the specific language governing rights and limitations under the License.
152
//
153
// The Original Code is: all this file.
154
//
155
// The Initial Developer of the Original Code is
156
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
157
//
158
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
159
//
160
// Contributor(s): none.
161
//
162
Popular Tags