}
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) {
XPathException e = new XPathException("There is no current template rule");
e.setXPathContext(context);
e.setErrorCode("XTDE0560");
e.setLocator(this);
throw e;
}
Template currentTemplate = (Template)currentTemplateRule.getAction();
int min = currentTemplate.getMinImportPrecedence();
int max = currentTemplate.getPrecedence()-1;
Mode mode = context.getCurrentMode();
if (mode == null) {
mode = controller.getRuleManager().getDefaultMode();
}
if (context.getCurrentIterator()==null) {
XPathException e = new XPathException("Cannot call xsl:apply-imports when there is no context item");
e.setXPathContext(context);
e.setErrorCode("XTDE0565");
e.setLocator(this);
throw e;
}
Item currentItem = context.getCurrentIterator().current();
if (!(currentItem instanceof NodeInfo)) {
XPathException e = new XPathException("Cannot call xsl:apply-imports when context item is not a node");
e.setXPathContext(context);
e.setErrorCode("XTDE0565");
e.setLocator(this);
throw e;
}
NodeInfo node = (NodeInfo)currentItem;
Rule rule = controller.getRuleManager().getTemplateRule(node, mode, min, max, context);
if (rule==null) { // use the default action for the node
ApplyTemplates.defaultAction(node, params, tunnels, context, backwardsCompatible, getLocationId());
} else {
XPathContextMajor c2 = context.newContext();