net.sf.saxon.value
Class Closure

java.lang.Object
  |
  +--net.sf.saxon.value.Value
        |
        +--net.sf.saxon.value.SequenceValue
              |
              +--net.sf.saxon.value.Closure
All Implemented Interfaces:
Expression, java.io.Serializable

public final class Closure
extends SequenceValue

A Closure represents a value that has not yet been evaluated: the value is represented by an expression, together with saved values of all the context variables that the expression depends on.

The Closure maintains a reservoir containing those items in the value that have already been read. When a new iterator is requested to read the value, this iterator first examines and returns any items already placed in the reservoir by previous users of the Closure. When the reservoir is exhausted, it then uses an underlying Input Iterator to read further values of the underlying expression. If the value is not read to completion (for example, if the first user did exists($expr), then the Input Iterator is left positioned where this user abandoned it. The next user will read any values left in the reservoir by the first user, and then pick up iterating the base expression where the first user left off. Eventually, all the values of the expression will find their way into the reservoir, and future users simply iterate over the reservoir contents. Alternatively, of course, the values may be left unread.

Delayed evaluation is used only for expressions with a static type that allows more than one item, so the evaluateItem() method will not normally be used, but it is supported for completeness.

The expression may depend on local variables and on the context item; these values are held in the saved XPathContext object that is kept as part of the Closure, and they will always be read from that object. The expression may also depend on global variables; these are unchanging, so they can be read from the Bindery in the normal way. Expressions that depend on other contextual information, for example the values of position(), last(), current(), current-group(), should not be evaluated using this mechanism: they should always be evaluated eagerly. This means that the Closure does not need to keep a copy of these context variables.

See Also:
Serialized Form

Nested Class Summary
 class Closure.ProgressiveIterator
          A ProgressiveIterator starts by reading any items already held in the reservoir; when the reservoir is exhausted, it reads further items from the inputIterator, copying them into the reservoir as they are read.
 
Method Summary
 java.lang.Object convertToJava(java.lang.Class target)
          Convert to Java object (for passing to external functions)
 void display(int level, NamePool pool)
          Diagnostic display of the expression
 Item evaluateItem(XPathContext context)
          Evaluate as a singleton.
 int getCardinality()
          Get the cardinality
 ItemType getItemType()
          Get the item type
 int getSpecialProperties()
          Get the static properties of this expression (other than its type).
 Item itemAt(int n)
          Get the n'th item in the sequence (starting from 0).
 SequenceIterator iterate(XPathContext context)
          Evaluate the expression in a given context to return an iterator over a sequence
static Value make(Expression expression, XPathContext context)
          Construct a Closure by supplying the expression and the set of context variables.
 
Methods inherited from class net.sf.saxon.value.SequenceValue
conversionPreference, effectiveBooleanValue, evaluateAsString, nodeToJavaPreference
 
Methods inherited from class net.sf.saxon.value.Value
analyze, asItem, asValue, collapseWhitespace, getDependencies, getSubExpressions, inverse, normalizeWhitespace, promote, simplify, stringToInteger, stringToNumber, trimWhitespace
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

make

public static Value make(Expression expression,
                         XPathContext context)
                  throws XPathException
Construct a Closure by supplying the expression and the set of context variables.

XPathException

getItemType

public ItemType getItemType()
Get the item type

Specified by:
getItemType in interface Expression
Overrides:
getItemType in class SequenceValue
Returns:
AnyItemType (not known)

getCardinality

public int getCardinality()
Get the cardinality

Specified by:
getCardinality in interface Expression
Overrides:
getCardinality in class SequenceValue
Returns:
one of the values Cardinality.ONE_OR_MORE, Cardinality.ZERO_OR_MORE, Cardinality.EXACTLY_ONE, Cardinality.ZERO_OR_ONE, Cardinality.EMPTY. This default implementation returns ZERO_OR_MORE (which effectively gives no information).

getSpecialProperties

public int getSpecialProperties()
Get the static properties of this expression (other than its type). The result is bit-signficant. These properties are used for optimizations. In general, if property bit is set, it is true, but if it is unset, the value is unknown.

Specified by:
getSpecialProperties in interface Expression
Overrides:
getSpecialProperties in class Value
Returns:
a set of flags indicating static properties of this expression

evaluateItem

public Item evaluateItem(XPathContext context)
                  throws XPathException
Evaluate as a singleton. We don't use a Closure for singleton expressions, so this method shouldn't be called, but we implement it anyway for safety.

Specified by:
evaluateItem in interface Expression
Overrides:
evaluateItem in class SequenceValue
Parameters:
context - The context in which the expression is to be evaluated
Returns:
the node or atomic value that results from evaluating the expression; or null to indicate that the result is an empty sequence
Throws:
XPathException - if any dynamic error occurs evaluating the expression

iterate

public SequenceIterator iterate(XPathContext context)
                         throws XPathException
Evaluate the expression in a given context to return an iterator over a sequence

Parameters:
context - the evaluation context. This is ignored; we use the context saved as part of the Closure instead.
Returns:
a SequenceIterator that can be used to iterate over the result of the expression
Throws:
XPathException - if any dynamic error occurs evaluating the expression

itemAt

public Item itemAt(int n)
            throws XPathException
Get the n'th item in the sequence (starting from 0). This is defined for all SequenceValues, but its real benefits come for a SequenceValue stored extensionally

Overrides:
itemAt in class SequenceValue
XPathException

convertToJava

public java.lang.Object convertToJava(java.lang.Class target)
                               throws XPathException
Convert to Java object (for passing to external functions)

Overrides:
convertToJava in class SequenceValue
Parameters:
target - The class required by the external function
Returns:
an object of the target class
XPathException

display

public void display(int level,
                    NamePool pool)
Description copied from class: SequenceValue
Diagnostic display of the expression

Specified by:
display in interface Expression
Overrides:
display in class SequenceValue
Parameters:
level - indentation level for this expression