Skip to content

Commit

Permalink
Merge pull request #4931 from evolvedbinary/hotfix/eb-2061-transform-…
Browse files Browse the repository at this point in the history
…xmldburi-resolution

Fix the resolution of XmldbURIs from within fn:transform
  • Loading branch information
adamretter committed May 30, 2023
2 parents bbc03d4 + 00aa699 commit 958b396
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public final class SaxonConfiguration {
private SaxonConfiguration(final net.sf.saxon.Configuration configuration) {
this.configuration = configuration;
this.processor = new Processor(configuration);
//TODO (AP) This is a better place to configure URI/Resource resolution for Saxon within eXist
//At present the configuration for Saxon to resolve xmldb:exist: URIs is restricted to fn:transform
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,41 +507,13 @@ private Source resolveStylesheetLocation(final String stylesheetLocation) throws

final URI uri = URI.create(stylesheetLocation);
if (uri.isAbsolute()) {
return resolvePossibleStylesheetLocation(stylesheetLocation);
return URIResolution.resolveDocument(stylesheetLocation, context, fnTransform);
} else {
final AnyURIValue resolved = resolveURI(new AnyURIValue(stylesheetLocation), context.getBaseURI());
return resolvePossibleStylesheetLocation(resolved.getStringValue());
return URIResolution.resolveDocument(resolved.getStringValue(), context, fnTransform);
}
}

/**
* Resolve an absolute stylesheet location
*
* @param location of the stylesheet
* @return the resolved stylesheet as a source
* @throws XPathException if the item does not exist, or is not a document
*/
private Source resolvePossibleStylesheetLocation(final String location) throws XPathException {

final Sequence document;
try {
document = DocUtils.getDocument(context, location);
} catch (final PermissionDeniedException e) {
throw new XPathException(fnTransform, ErrorCodes.FODC0002,
"Can not access '" + location + "'" + e.getMessage());
}
if (document != null && document.hasOne() && Type.subTypeOf(document.getItemType(), Type.NODE)) {
if (document instanceof NodeProxy proxy) {
return new DOMSource(proxy.getNode());
}
else if (document.itemAt(0) instanceof Node node) {
return new DOMSource(node);
}
}
throw new XPathException(fnTransform, ErrorCodes.FODC0002,
"Location '"+ location + "' returns an item which is not a document node");
}

/**
* URI resolution, the core should be the same as for fn:resolve-uri
* @param relative URI to resolve
Expand All @@ -550,19 +522,11 @@ else if (document.itemAt(0) instanceof Node node) {
* @throws XPathException if resolution is not possible
*/
private AnyURIValue resolveURI(final AnyURIValue relative, final AnyURIValue base) throws XPathException {
final URI relativeURI;
final URI baseURI;
try {
relativeURI = new URI(relative.getStringValue());
baseURI = new URI(base.getStringValue() );
return URIResolution.resolveURI(relative, base);
} catch (final URISyntaxException e) {
throw new XPathException(fnTransform, ErrorCodes.FORG0009, "unable to resolve a relative URI against a base URI in fn:transform(): " + e.getMessage(), null, e);
}
if (relativeURI.isAbsolute()) {
return relative;
} else {
return new AnyURIValue(baseURI.resolve(relativeURI));
}
}

private XSLTVersion getXsltVersion(final Source xsltStylesheet) throws XPathException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,22 +214,24 @@ private XsltExecutable compileExecutable(final Options options) throws XPathExce
xsltCompiler.setParameter(new net.sf.saxon.s9api.QName(qKey.getPrefix(), qKey.getLocalPart()), value);
}

// Take URI resolution into our own hands when there is no base
xsltCompiler.setURIResolver((href, base) -> {
try {
final URI hrefURI = URI.create(href);
if (options.resolvedStylesheetBaseURI.isEmpty() && !hrefURI.isAbsolute() && StringUtils.isEmpty(base)) {
final XPathException resolutionException = new XPathException(fnTransform,
xsltCompiler.setURIResolver(new URIResolution.CompileTimeURIResolver(context, fnTransform) {
@Override public Source resolve(final String href, final String base) throws TransformerException {
// Correct error from URI resolution when there is no base
try {
final URI hrefURI = URI.create(href);
if (options.resolvedStylesheetBaseURI.isEmpty() && !hrefURI.isAbsolute() && StringUtils.isEmpty(base)) {
final XPathException resolutionException = new XPathException(fnTransform,
ErrorCodes.XTSE0165,
"transform using a relative href, \n" +
"using option stylesheet-text, but without stylesheet-base-uri");
throw new TransformerException(resolutionException);
"using option stylesheet-text, but without stylesheet-base-uri");
throw new TransformerException(resolutionException);
}
} catch (final IllegalArgumentException e) {
throw new TransformerException(e);
}
} catch (final IllegalArgumentException e) {
throw new TransformerException(e);
// Checked the special error case, defer to eXist resolution
return super.resolve(href, base);
}
// Pass it back
return null;
});

try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
* [email protected]
* http://www.exist-db.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package org.exist.xquery.functions.fn.transform;

import org.exist.dom.persistent.NodeProxy;
import org.exist.security.PermissionDeniedException;
import org.exist.xmldb.XmldbURI;
import org.exist.xquery.ErrorCodes;
import org.exist.xquery.Expression;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.util.DocUtils;
import org.exist.xquery.value.AnyURIValue;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.Type;
import org.w3c.dom.Node;

import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.dom.DOMSource;
import java.net.URI;
import java.net.URISyntaxException;

public class URIResolution {

/**
* URI resolution, the core should be the same as for fn:resolve-uri
* @param relative URI to resolve
* @param base to resolve against
* @return resolved URI
* @throws URISyntaxException if resolution is not possible
*/
static AnyURIValue resolveURI(final AnyURIValue relative, final AnyURIValue base) throws URISyntaxException, XPathException {
var relativeURI = new URI(relative.getStringValue());
if (relativeURI.isAbsolute()) {
return relative;
}
var baseURI = new URI(base.getStringValue() );
if (!baseURI.isAbsolute()) {
return relative;
}
try {
var xBase = XmldbURI.xmldbUriFor(baseURI);
var resolved = xBase.getURI().resolve(relativeURI);
return new AnyURIValue(XmldbURI.XMLDB_URI_PREFIX + resolved);
} catch (URISyntaxException e) {
return new AnyURIValue(baseURI.resolve(relativeURI));
}
}

public static class CompileTimeURIResolver implements URIResolver {

private final XQueryContext xQueryContext;
private final Expression containingExpression;

public CompileTimeURIResolver(XQueryContext xQueryContext, Expression containingExpression) {
this.xQueryContext = xQueryContext;
this.containingExpression = containingExpression;
}

@Override
public Source resolve(final String href, final String base) throws TransformerException {

try {
final AnyURIValue baseURI = new AnyURIValue(base);
final AnyURIValue hrefURI = new AnyURIValue(href);
var resolved = resolveURI(hrefURI, baseURI);
return resolveDocument(resolved.getStringValue());
} catch (URISyntaxException e) {
throw new TransformerException(
"Failed to resolve " + href + " against " + base, e);
} catch (XPathException e) {
throw new TransformerException(
"Failed to find document as result of resolving " + href + " against " + base, e);
}
}

protected Source resolveDocument(final String location) throws XPathException {
return URIResolution.resolveDocument(location, xQueryContext, containingExpression);
}
}

/**
* Resolve an absolute document location, stylesheet or included source
*
* @param location of the stylesheet
* @return the resolved stylesheet as a source
* @throws org.exist.xquery.XPathException if the item does not exist, or is not a document
*/
static Source resolveDocument(final String location, final XQueryContext xQueryContext, Expression containingExpression) throws XPathException {

final Sequence document;
try {
document = DocUtils.getDocument(xQueryContext, location);
} catch (final PermissionDeniedException e) {
throw new XPathException(containingExpression, ErrorCodes.FODC0002,
"Can not access '" + location + "'" + e.getMessage());
}
if (document == null || document.isEmpty()) {
throw new XPathException(containingExpression, ErrorCodes.FODC0002,
"No document found at location '"+ location);
}
if (document.hasOne() && Type.subTypeOf(document.getItemType(), Type.NODE)) {
if (document instanceof NodeProxy proxy) {
return new DOMSource(proxy.getNode());
}
else if (document.itemAt(0) instanceof Node node) {
return new DOMSource(node);
}
}
throw new XPathException(containingExpression, ErrorCodes.FODC0002,
"Location '"+ location + "' returns an item which is not a document node");
}
}
Loading

0 comments on commit 958b396

Please sign in to comment.