KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > expr > LazyExpression


1 package net.sf.saxon.expr;
2
3 import net.sf.saxon.om.NamePool;
4 import net.sf.saxon.om.Item;
5 import net.sf.saxon.om.SequenceIterator;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.value.Value;
8
9 /**
10  * A LazyExpression is an expression that forces lazy evaluation: it must not be evaluated eagerly,
11  * because a failure must not be reported unless the value is actually referenced. This is used
12  * for an expression that has been moved out of a loop. If the loop iterates zero times, the expression
13  * will not be evaluated, and in particular, it will not cause a dynamic error.
14  */

15
16 public class LazyExpression extends UnaryExpression {
17
18     private LazyExpression(Expression operand) {
19         super(operand);
20     }
21
22     public static Expression makeLazyExpression(Expression operand) {
23         if (operand instanceof LazyExpression || operand instanceof Value) {
24             return operand;
25         } else {
26             return new LazyExpression(operand);
27         }
28     }
29
30     /**
31      * Evaluate an expression as a single item. This always returns either a single Item or
32      * null (denoting the empty sequence). No conversion is done. This method should not be
33      * used unless the static type of the expression is a subtype of "item" or "item?": that is,
34      * it should not be called if the expression may return a sequence. There is no guarantee that
35      * this condition will be detected.
36      *
37      * @param context The context in which the expression is to be evaluated
38      * @return the node or atomic value that results from evaluating the
39      * expression; or null to indicate that the result is an empty
40      * sequence
41      * @throws net.sf.saxon.trans.XPathException
42      * if any dynamic error occurs evaluating the
43      * expression
44      */

45
46     public Item evaluateItem(XPathContext context) throws XPathException {
47         return operand.evaluateItem(context);
48     }
49
50     /**
51      * Return an Iterator to iterate over the values of a sequence. The value of every
52      * expression can be regarded as a sequence, so this method is supported for all
53      * expressions. This default implementation handles iteration for expressions that
54      * return singleton values: for non-singleton expressions, the subclass must
55      * provide its own implementation.
56      *
57      * @param context supplies the context for evaluation
58      * @return a SequenceIterator that can be used to iterate over the result
59      * of the expression
60      * @throws net.sf.saxon.trans.XPathException
61      * if any dynamic error occurs evaluating the
62      * expression
63      */

64
65     public SequenceIterator iterate(XPathContext context) throws XPathException {
66         return operand.iterate(context);
67     }
68
69     /**
70      * Process the instruction, without returning any tail calls
71      *
72      * @param context The dynamic context, giving access to the current node,
73      * the current variables, etc.
74      */

75
76     public void process(XPathContext context) throws XPathException {
77         operand.process(context);
78     }
79
80     protected String JavaDoc displayOperator(NamePool pool) {
81         return "lazy";
82     }
83
84
85 }
86
87
88
89 //
90
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
91
// you may not use this file except in compliance with the License. You may obtain a copy of the
92
// License at http://www.mozilla.org/MPL/
93
//
94
// Software distributed under the License is distributed on an "AS IS" basis,
95
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
96
// See the License for the specific language governing rights and limitations under the License.
97
//
98
// The Original Code is: all this file.
99
//
100
// The Initial Developer of the Original Code is Michael H. Kay
101
//
102
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
103
//
104
// Contributor(s): none.
105
//
106

107
Popular Tags