public Expression compile(Executable exec) throws TransformerConfigurationException {
Block block = new Block();
compileChildren(exec, block, true);
Expression exp = null;
try {
exp = block.simplify(getStaticContext());
} catch (XPathException e) {
compileError(e);
}
try {
if (requiredType != null) {
RoleLocator role =
new RoleLocator(RoleLocator.TEMPLATE_RESULT, diagnosticId, 0);
exp = TypeChecker.staticTypeCheck(exp, requiredType, false, role, getStaticContext());
}
} catch (XPathException err) {
compileError(err);
}
compiledTemplate.init ( getObjectFingerprint(),
getPrecedence(),
getMinImportPrecedence());
if (getConfiguration().getTraceListener() != null) {
TraceWrapper trace = new TraceInstruction(exp, this);
trace.setLocationId(allocateLocationId(getSystemId(), getLineNumber()));
trace.setParentExpression(compiledTemplate);
exp = trace;
}
compiledTemplate.setBody(exp);
compiledTemplate.setStackFrameMap(stackFrameMap);
compiledTemplate.setExecutable(getExecutable());
compiledTemplate.setSystemId(getSystemId());
compiledTemplate.setLineNumber(getLineNumber());
ItemType contextItemType = Type.ITEM_TYPE;
if (getObjectFingerprint() == -1) {
// the template can't be called by name, so the context item must match the match pattern
contextItemType = match.getNodeTest();
}
try {
Expression exp2 = exp.analyze(staticContext, contextItemType);
if (exp != exp2) {
compiledTemplate.setBody(exp2);
}
} catch (XPathException e) {
compileError(e);