}
public TailCall processLeavingTail(XPathContext context) throws XPathException {
Controller controller = context.getController();
// handle parameters if any
ParameterSet params = assembleParams(context, actualParams);
ParameterSet tunnels = assembleTunnelParams(context, tunnelParams);
Rule currentTemplateRule = context.getCurrentTemplateRule();
if (currentTemplateRule==null) {
dynamicError("There is no current template rule", "XTDE0560", context);
}
int min = currentTemplateRule.getMinImportPrecedence();
int max = currentTemplateRule.getPrecedence()-1;
Mode mode = context.getCurrentMode();
if (mode == null) {
mode = controller.getRuleManager().getUnnamedMode();
}
if (context.getCurrentIterator()==null) {
dynamicError("Cannot call xsl:apply-imports when there is no context item", "XTDE0565", context);
}
Item currentItem = context.getCurrentIterator().current();
if (!(currentItem instanceof NodeInfo)) {
dynamicError("Cannot call xsl:apply-imports when context item is not a node", "XTDE0565", context);
}
NodeInfo node = (NodeInfo)currentItem;
Rule rule = controller.getRuleManager().getTemplateRule(node, mode, min, max, context);
if (rule==null) { // use the default action for the node
mode.getBuiltInRuleSet().process(node, params, tunnels, context, getSourceLocator());
} else {
XPathContextMajor c2 = context.newContext();