principalResult = result;
if (principalResultURI == null) {
principalResultURI = result.getSystemId();
}
XPathContextMajor initialContext = newXPathContext();
initialContext.setOriginatingConstructType(Location.CONTROLLER);
if (startNode != null) {
initialContextItem = startNode;
contextForGlobalVariables = startNode.getRoot();
if (startNode.getConfiguration()==null) {
// must be a non-standard document implementation
throw new TransformerException("The supplied source document must be associated with a Configuration");
}
if (!startNode.getConfiguration().isCompatible(preparedStylesheet.getConfiguration())) {
throw new XPathException(
"Source document and stylesheet must use the same or compatible Configurations",
SaxonErrorCode.SXXP0004);
}
SequenceIterator currentIter = SingletonIterator.makeIterator(startNode);
if (initialTemplate != null) {
currentIter.next();
}
initialContext.setCurrentIterator(currentIter);
}
initializeController();
// In tracing/debugging mode, evaluate all the global variables first
if (traceListener != null) {
preEvaluateGlobals(initialContext);
}
result = openResult(result, initialContext);
// Process the source document by applying template rules to the initial context node
if (initialTemplate == null) {
initialContextItem = startNode;
Mode mode = getRuleManager().getMode(initialMode, false);
if (mode == null || (initialMode != null && mode.isEmpty())) {
throw new XPathException("Requested initial mode " +
(initialMode == null ? "" : initialMode.getDisplayName()) +
" does not exist", "XTDE0045");
}
if (mode.isStreamable()) {
throw new XPathException("Requested initial mode " +
(initialMode == null ? "" : initialMode.getDisplayName()) +
" is streamable: must supply a StreamSource or SAXSource");
}
TailCall tc = ApplyTemplates.applyTemplates(
initialContext.getCurrentIterator(),
mode,
null, null, initialContext, 0);
while (tc != null) {
tc = tc.processLeavingTail();
}
} else {
Template t = initialTemplate;
XPathContextMajor c2 = initialContext.newContext();
initialContext.setOriginatingConstructType(Location.CONTROLLER);
c2.openStackFrame(t.getStackFrameMap());
c2.setLocalParameters(new ParameterSet());
c2.setTunnelParameters(new ParameterSet());
TailCall tc = t.expand(c2);
while (tc != null) {
tc = tc.processLeavingTail();
}