public void compileStylesheet() throws XPathException {
try {
PreparedStylesheet pss = getPreparedStylesheet();
//Configuration config = pss.getConfiguration();
Executable exec = pss.getExecutable();
// Register template rules with the rule manager
for (int i = 0; i < topLevel.size(); i++) {
Declaration decl = topLevel.get(i);
StyleElement snode = decl.getSourceElement();
if (snode instanceof XSLTemplate) {
((XSLTemplate)snode).register(decl);
}
}
// Call compile method for each top-level object in the stylesheet
// Note, some declarations (templates) need to be compiled repeatedly if the module
// is imported repeatedly; others (variables, functions) do not
for (int i = 0; i < topLevel.size(); i++) {
Declaration decl = topLevel.get(i);
StyleElement snode = decl.getSourceElement();
if (!snode.isActionCompleted(StyleElement.ACTION_COMPILE)) {
snode.setActionCompleted(StyleElement.ACTION_COMPILE);
Expression inst = snode.compile(exec, decl);
if (inst != null) {
inst.setSourceLocator(snode);
}
}
}
// Call type-check method for each user-defined function in the stylesheet. This is no longer
// done during the optimize step, to avoid functions being inlined before they are type-checked.
// for (int i = 0; i < topLevel.size(); i++) {
// NodeInfo node = (NodeInfo) topLevel.get(i);
// if (node instanceof XSLFunction) {
// ((XSLFunction) node).typeCheckBody();
// }
// }
for (Iterator<Integer> arities = functionIndex.keySet().iterator(); arities.hasNext();) {
for (Iterator<Declaration> fi = functionIndex.get(arities.next()).values().iterator(); fi.hasNext();) {
Declaration decl = fi.next();
StyleElement node = decl.getSourceElement();
if (!node.isActionCompleted(StyleElement.ACTION_TYPECHECK)) {
node.setActionCompleted(StyleElement.ACTION_TYPECHECK);
((XSLFunction)node).typeCheckBody();
}
}
}
if (getPreparedStylesheet().getErrorCount() > 0) {
// not much point carrying on
return;
}
// Call optimize method for each top-level object in the stylesheet
// But for functions, do it only for those of highest precedence.
for (int i = 0; i < topLevel.size(); i++) {
Declaration decl = topLevel.get(i);
StyleElement node = decl.getSourceElement();
if (node instanceof StylesheetProcedure && !(node instanceof XSLFunction) &&
!node.isActionCompleted(StyleElement.ACTION_OPTIMIZE)) {
node.setActionCompleted(StyleElement.ACTION_OPTIMIZE);
((StylesheetProcedure) node).optimize(decl);
}
}
for (Iterator<Integer> arities = functionIndex.keySet().iterator(); arities.hasNext();) {
for (Iterator<Declaration> fi = functionIndex.get(arities.next()).values().iterator(); fi.hasNext();) {
Declaration decl = fi.next();
StyleElement node = decl.getSourceElement();
if (!node.isActionCompleted(StyleElement.ACTION_OPTIMIZE)) {
node.setActionCompleted(StyleElement.ACTION_OPTIMIZE);
((StylesheetProcedure) node).optimize(decl);
}
}
}
// Fix up references to the default default decimal format
if (pss.getDecimalFormatManager() != null) {
try {
pss.getDecimalFormatManager().fixupDefaultDefault();
} catch (XPathException err) {
compileError(err.getMessage(), err.getErrorCodeLocalPart());
}
}