Controller controller = c.getController();
// Resolve relative URI
String documentKey;
URIResolver resolver = controller.getURIResolver();
if (resolver == null) {
resolver = controller.getStandardURIResolver();
}
if (resolver instanceof RelativeURIResolver) {
// If this is the case, the URIResolver is responsible for absolutization as well as dereferencing
try {
documentKey = ((RelativeURIResolver)resolver).makeAbsolute(href, baseURI);
} catch (TransformerException e) {
documentKey = '/' + href;
baseURI = "";
}
} else {
// Saxon takes charge of absolutization, leaving the user URIResolver to handle dereferencing only
if (baseURI==null) { // no base URI available
try {
// the href might be an absolute URL
documentKey = (new URI(href)).toString();
} catch (URISyntaxException err) {
// it isn't; but the URI resolver might know how to cope
documentKey = '/' + href;
baseURI = "";
}
} else if (href.length() == 0) {
// common case in XSLT, which java.net.URI#resolve() does not handle correctly
documentKey = baseURI;
} else {
try {
URI uri = new URI(baseURI).resolve(href);
documentKey = uri.toString();
} catch (URISyntaxException err) {
documentKey = baseURI + "/../" + href;
} catch (IllegalArgumentException err) {
documentKey = baseURI + "/../" + href;
}
}
}
// see if the document is already loaded
DocumentInfo doc = config.getGlobalDocumentPool().find(documentKey);
if (doc != null) {
return doc;
}
doc = controller.getDocumentPool().find(documentKey);
if (doc != null) {
return getFragment(doc, fragmentId, c);
}
// check that the document was not written by this transformation
if (!controller.checkUniqueOutputDestination(documentKey)) {
XPathException err = new XPathException("Cannot read a document that was written during the same transformation: " + documentKey);
err.setXPathContext(c);
err.setErrorCode("XTRE1500");
throw err;
}
try {
// Get a Source from the URIResolver
Source source;
if (resolver instanceof RelativeURIResolver) {
try {
source = ((RelativeURIResolver)resolver).dereference(documentKey);
} catch (Exception ex) {
XPathException de = new XPathException("Exception thrown by URIResolver", ex);
if (controller.getConfiguration().isTraceExternalFunctions()) {
ex.printStackTrace();
}
de.setLocator(locator);
throw de;
}
} else {
try {
source = resolver.resolve(href, baseURI);
} catch (Exception ex) {
XPathException de = new XPathException("Exception thrown by URIResolver", ex);
if (controller.getConfiguration().isTraceExternalFunctions()) {
ex.printStackTrace();
}
de.setLocator(locator);
throw de;
}
}
// if a user URI resolver returns null, try the standard one
// (Note, the standard URI resolver never returns null)
if (source==null && !(resolver instanceof NonDelegatingURIResolver)) {
resolver = controller.getStandardURIResolver();
if (resolver instanceof RelativeURIResolver) {
source = ((RelativeURIResolver)resolver).dereference(documentKey);
} else {
source = resolver.resolve(href, baseURI);
}
}
//System.err.println("URI resolver returned " + source.getClass() + " " + source.getSystemId());
source = config.getSourceResolver().resolveSource(source, config);