return found;
}
public TailCall processLeavingTail(XPathContext context) throws XPathException {
final Controller controller = context.getController();
final Configuration config = controller.getConfiguration();
final NamePool namePool = config.getNamePool();
XPathContext c2 = context.newMinorContext();
c2.setOrigin(this);
Result result;
OutputURIResolver resolver = null;
if (href == null) {
result = controller.getPrincipalResult();
} else {
try {
String base;
if (resolveAgainstStaticBase) {
base = baseURI;
} else {
base = controller.getCookedBaseOutputURI();
}
resolver = controller.getOutputURIResolver();
String hrefValue = EscapeURI.iriToUri(href.evaluateAsString(context)).toString();
try {
result = resolver.resolve(hrefValue, base);
} catch (Exception err) {
throw new XPathException("Exception thrown by OutputURIResolver", err);
}
if (result == null) {
resolver = StandardOutputResolver.getInstance();
result = resolver.resolve(hrefValue, base);
}
} catch (TransformerException e) {
throw XPathException.makeXPathException(e);
}
}
if (controller.getDocumentPool().find(result.getSystemId()) != null) {
XPathException err = new XPathException("Cannot write to a URI that has already been read: " +
result.getSystemId());
err.setXPathContext(context);
err.setErrorCode("XTRE1500");
throw err;
}
if (!controller.checkUniqueOutputDestination(result.getSystemId())) {
XPathException err = new XPathException("Cannot write more than one result document to the same URI: " +
result.getSystemId());
err.setXPathContext(context);
err.setErrorCode("XTDE1490");
throw err;
} else {
controller.addUnavailableOutputDestination(result.getSystemId());
controller.setThereHasBeenAnExplicitResultDocument();
}
boolean timing = controller.getConfiguration().isTiming();
if (timing) {
String dest = result.getSystemId();
if (dest == null) {
if (result instanceof StreamResult) {
dest = "anonymous output stream";
} else if (result instanceof SAXResult) {
dest = "SAX2 ContentHandler";
} else if (result instanceof DOMResult) {
dest = "DOM tree";
} else {
dest = result.getClass().getName();
}
}
System.err.println("Writing to " + dest);
}
Properties computedGlobalProps = globalProperties;
if (formatExpression != null) {
// format was an AVT and now needs to be computed
CharSequence format = formatExpression.evaluateAsString(context);
String[] parts;
try {
parts = controller.getConfiguration().getNameChecker().getQNameParts(format);
} catch (QNameException e) {
XPathException err = new XPathException("The requested output format " + Err.wrap(format) + " is not a valid QName");
err.setErrorCode("XTDE1460");
err.setXPathContext(context);
throw err;
}
String uri = nsResolver.getURIForPrefix(parts[0], false);
if (uri == null) {
XPathException err = new XPathException("The namespace prefix in the format name " + format + " is undeclared");
err.setErrorCode("XTDE1460");
err.setXPathContext(context);
throw err;
}
StructuredQName qName = new StructuredQName(parts[0], uri, parts[1]);
computedGlobalProps = getExecutable().getOutputProperties(qName);
if (computedGlobalProps == null) {
XPathException err = new XPathException("There is no xsl:output format named " + format);
err.setErrorCode("XTDE1460");
err.setXPathContext(context);
throw err;
}
}
// Now combine the properties specified on xsl:result-document with those specified on xsl:output
Properties computedLocalProps = new Properties(computedGlobalProps);
// First handle the properties with fixed values on xsl:result-document
final NameChecker checker = config.getNameChecker();
for (Iterator citer=localProperties.keySet().iterator(); citer.hasNext();) {
String key = (String)citer.next();
String[] parts = NamePool.parseClarkName(key);
try {
setSerializationProperty(computedLocalProps, parts[0], parts[1],