throw new XPathException("The supplied context item does not match the required context item type");
}
// Now check it against the required type defined in the query prolog
StructuredQName varQName = executable.getInitialContextItemVariableName();
if (varQName != null) {
GlobalVariable var = executable.getGlobalVariable(varQName);
if (var == null) {
throw new IllegalStateException("Context item variable not found in executable");
}
controller.setParameter(varQName, contextItem);
try {
// do early evaluation to force the type-checking of the context item
controller.getBindery().useGlobalParameter(
varQName, var.getSlotNumber(), var.getRequiredType(), context);
} catch (XPathException err) {
err.maybeSetLocation(var);
if (!err.hasBeenReported()) {
try {
controller.getErrorListener().fatalError(err);
} catch (TransformerException e) {
// no action
}
}
throw err;
}
}
UnfailingIterator single = SingletonIterator.makeIterator(contextItem);
single.next();
context.setCurrentIterator(single);
controller.setInitialContextItem(contextItem);
} else {
// there might be a default value for the context item
StructuredQName varQName = executable.getInitialContextItemVariableName();
if (varQName != null) {
GlobalVariable var = executable.getGlobalVariable(varQName);
if (var == null) {
throw new IllegalStateException("Context item variable not found in executable");
}
try {
// do early evaluation to force the type-checking of the context item
controller.getBindery().useGlobalParameter(varQName, var.getSlotNumber(), var.getRequiredType(), context);
} catch (XPathException err) {
err.maybeSetLocation(var);
if (!err.hasBeenReported()) {
try {
controller.getErrorListener().fatalError(err);
} catch (TransformerException e) {
// no action
}
}
throw err;
}
ValueRepresentation val = var.getSelectValue(context);
contextItem = Value.asItem(val);
UnfailingIterator single = SingletonIterator.makeIterator(contextItem);
single.next();
context.setCurrentIterator(single);
controller.setInitialContextItem(contextItem);