// order expressions for every item when it is added to the result sequence.
if (resultSequence == null) {
if (orderSpecs != null && !fastOrderBy) {
resultSequence = new OrderedValueSequence(orderSpecs, in.getItemCount());
} else {
resultSequence = new ValueSequence();
((ValueSequence)resultSequence).keepUnOrdered(unordered);
}
}
Sequence val = null;
int p = 1;
final IntegerValue atVal = new IntegerValue(1);
if(positionalVariable != null)
{at.setValue(atVal);}
//Type.EMPTY is *not* a subtype of other types ;
//the tests below would fail without this prior cardinality check
if (in.isEmpty() && sequenceType != null &&
!Cardinality.checkCardinality(sequenceType.getCardinality(),
Cardinality.EMPTY)) {
throw new XPathException(this, ErrorCodes.XPTY0004,
"Invalid cardinality for variable $" + varName +
". Expected " + Cardinality.getDescription(sequenceType.getCardinality()) +
", got " + Cardinality.getDescription(in.getCardinality()));
}
// Loop through each variable binding
p = 0;
for (final SequenceIterator i = in.iterate(); i.hasNext(); p++) {
context.proceed(this);
contextItem = i.nextItem();
context.setContextSequencePosition(p, in);
if (positionalVariable != null)
{at.setValue(new IntegerValue(p + 1));}
contextSequence = contextItem.toSequence();
// set variable value to current item
var.setValue(contextSequence);
if (sequenceType == null)
{var.checkType();} //because it makes some conversions !
val = contextSequence;
// check optional where clause
if (whereExpr != null && (!fastExec)) {
if (contextItem instanceof NodeProxy)
{((NodeProxy)contextItem).addContextNode(getExpressionId(), (NodeProxy)contextItem);}
final Sequence bool = applyWhereExpression(null);
if (contextItem instanceof NodeProxy)
{((NodeProxy)contextItem).clearContext(getExpressionId());}
// if where returned false, continue
if (!bool.effectiveBooleanValue())
{continue;}
} else {
val = contextItem.toSequence();
}
//Reset the context position
context.setContextSequencePosition(0, null);
if (groupedSequence==null) {
if (returnExpr instanceof BindingExpression) {
((BindingExpression)returnExpr).eval(null, null, resultSequence, null);
// otherwise call the return expression and add results to resultSequence
} else {
val = returnExpr.eval(null);
resultSequence.addAll(val);
}
} else {
/* bv : special processing for groupby :
if returnExpr is a Binding expression, pass the groupedSequence.
Else, add item to groupedSequence and don't evaluate here !
*/
if (returnExpr instanceof BindingExpression){
((BindingExpression)returnExpr).eval(null, null, resultSequence, groupedSequence);
} else {
final Sequence toGroupSequence = context.resolveVariable(groupedSequence.getToGroupVarName()).getValue();
groupedSequence.addAll(toGroupSequence);
}
}
// free resources
var.destroy(context, resultSequence);
}
} finally {
// restore the local variable stack
context.popLocalVariables(mark, resultSequence);
}
// bv : Special processing for groupBy : one return per group in groupedSequence
if (groupSpecs!=null) {
mark = context.markLocalVariables(false);
context.declareVariableBinding(var);
// Declare positional variable if required
LocalVariable at = null;
if (positionalVariable != null) {
at = new LocalVariable(QName.parse(context, positionalVariable, null));
at.setSequenceType(POSITIONAL_VAR_TYPE);
context.declareVariableBinding(at);
}
final IntegerValue atVal = new IntegerValue(1);
if(positionalVariable != null) {
at.setValue(atVal);
}
int p = 0;
for (final Iterator<String> it = groupedSequence.iterate(); it.hasNext(); ) {
final GroupedValueSequence currentGroup = groupedSequence.get(it.next());
context.proceed(this);
// set binding variable to current group
var.setValue(currentGroup);
var.checkType();
//set value of grouping keys for the current group
for (int i=0; i< groupKeyVar.length ; i ++) {
groupKeyVar[i].setValue(currentGroup.getGroupKey().itemAt(i).toSequence());
}
if (positionalVariable != null) {
final ValueSequence ps = new ValueSequence();
for (int i = 0; i < currentGroup.getItemCount(); i++) {
ps.add(new IntegerValue(p + i + 1));
}
at.setValue(ps);
}
//evaluate real return expression
final Sequence val = groupReturnExpr.eval(null);