From 401ac6e89fe36fbf4ba756ae0e0733af43977ea6 Mon Sep 17 00:00:00 2001 From: Juri Leino Date: Tue, 10 Dec 2024 14:55:33 +0100 Subject: [PATCH] [bugfix] registered import-uris have precedence When importing stylesheets the URIs are resolved in order - registered import-uris in package repo - XMLDB-locations (relative, absolute and with or without scheme) - any other location is treated as an exteranal source The test class and its cases were renamed to reflect their purpose. Some inline comments were added to help describing the intent. --- .../org/exist/xslt/XsltURIResolverHelper.java | 8 ++- ...PkgTest.java => ImportStylesheetTest.java} | 67 +++++++++++++------ 2 files changed, 51 insertions(+), 24 deletions(-) rename exist-core/src/test/java/org/exist/xquery/functions/transform/{TransformFromPkgTest.java => ImportStylesheetTest.java} (57%) diff --git a/exist-core/src/main/java/org/exist/xslt/XsltURIResolverHelper.java b/exist-core/src/main/java/org/exist/xslt/XsltURIResolverHelper.java index 8b7b8089784..12421b32e91 100644 --- a/exist-core/src/main/java/org/exist/xslt/XsltURIResolverHelper.java +++ b/exist-core/src/main/java/org/exist/xslt/XsltURIResolverHelper.java @@ -52,14 +52,16 @@ public class XsltURIResolverHelper { @Nullable final URIResolver defaultResolver, @Nullable final String base, final boolean avoidSelf) { final List resolvers = new ArrayList<>(); + // EXpath Pkg resolver + // This resolver needs to be the first one to prevent + // HTTP requests for registered package names (e.g. http://www.functx.com/functx.xsl) + brokerPool.getExpathRepo().map(repo -> resolvers.add(new PkgXsltModuleURIResolver(repo))); + if (base != null) { // database resolver resolvers.add(new EXistURISchemeURIResolver(new EXistURIResolver(brokerPool, base))); } - // EXpath Pkg resolver - brokerPool.getExpathRepo().map(repo -> resolvers.add(new PkgXsltModuleURIResolver(repo))); - // default resolver if (defaultResolver != null) { if (avoidSelf) { diff --git a/exist-core/src/test/java/org/exist/xquery/functions/transform/TransformFromPkgTest.java b/exist-core/src/test/java/org/exist/xquery/functions/transform/ImportStylesheetTest.java similarity index 57% rename from exist-core/src/test/java/org/exist/xquery/functions/transform/TransformFromPkgTest.java rename to exist-core/src/test/java/org/exist/xquery/functions/transform/ImportStylesheetTest.java index dcdc57e4e61..17f1168615f 100644 --- a/exist-core/src/test/java/org/exist/xquery/functions/transform/TransformFromPkgTest.java +++ b/exist-core/src/test/java/org/exist/xquery/functions/transform/ImportStylesheetTest.java @@ -35,18 +35,22 @@ import static org.junit.Assert.assertNotNull; /** + * Test transform:transform with an imported stylesheet from various + * locations + * * @author Adam Retter + * @author Juri Leino */ -public class TransformFromPkgTest { +public class ImportStylesheetTest { - private static final String moduleLocation = "/db/system/repo/functx-1.0.1/functx/functx.xsl"; - private static final String inputXml = "bonjourno"; - private static final String expectedOutput = "hello"; + private static final String XSL_DB_LOCATION = "/db/system/repo/functx-1.0.1/functx/functx.xsl"; + private static final String INPUT_XML = "bonjourno"; + private static final String EXPECTED_OUTPUT = "hello"; private static Path getConfigFile() { - final ClassLoader loader = TransformFromPkgTest.class.getClassLoader(); + final ClassLoader loader = ImportStylesheetTest.class.getClassLoader(); final char separator = System.getProperty("file.separator").charAt(0); - final String packagePath = TransformFromPkgTest.class.getPackage().getName().replace('.', separator); + final String packagePath = ImportStylesheetTest.class.getPackage().getName().replace('.', separator); try { return Paths.get(loader.getResource(packagePath + separator + "conf.xml").toURI()); @@ -57,51 +61,72 @@ private static Path getConfigFile() { } private static String getQuery(final String importLocation) { - final String xslt = "\n" + - " \n" + " \n" + - " \n" + " \n" + - " " + + " \n" + " \n" + - " " + + " \n" + " \n" + - " \n" + ""; - return "transform:transform(" + inputXml + ", " + xslt + ", ())"; + return "transform:transform(" + INPUT_XML + ", " + xslt + ", ())"; } private static void assertTransformationResult(ResourceSet result) throws XMLDBException { assertNotNull(result); assertEquals(1, result.getSize()); - assertEquals(expectedOutput, result.getResource(0).getContent()); + assertEquals(EXPECTED_OUTPUT, result.getResource(0).getContent()); } @ClassRule public static ExistXmldbEmbeddedServer existXmldbEmbeddedServer = new ExistXmldbEmbeddedServer(true, false, true, getConfigFile()); @Test - public void testImportNoScheme() throws XMLDBException { - final String xquery = getQuery(moduleLocation); + public void fromRegisteredImportUri() throws XMLDBException { + final String xquery = getQuery("http://www.functx.com/functx.xsl"); + final ResourceSet result = existXmldbEmbeddedServer.executeQuery(xquery); + assertTransformationResult(result); + } + + @Test + public void fromDbLocationWithoutScheme() throws XMLDBException { + final String xquery = getQuery(XSL_DB_LOCATION); + final ResourceSet result = existXmldbEmbeddedServer.executeQuery(xquery); + assertTransformationResult(result); + } + + @Test + public void fromDbLocationWithXmldbScheme() throws XMLDBException { + final String xquery = getQuery("xmldb:" + XSL_DB_LOCATION); + final ResourceSet result = existXmldbEmbeddedServer.executeQuery(xquery); + assertTransformationResult(result); + } + + @Test + public void fromDbLocationWithXmldbSchemeDoubleSlash() throws XMLDBException { + final String xquery = getQuery("xmldb://" + XSL_DB_LOCATION); final ResourceSet result = existXmldbEmbeddedServer.executeQuery(xquery); assertTransformationResult(result); } + /** This test fails at the moment with "unknown protocol: exist" */ @Test - public void testImportXmldbScheme() throws XMLDBException { - final String xquery = getQuery("xmldb:" + moduleLocation); + @Ignore + public void fromDbLocationWithExistScheme() throws XMLDBException { + final String xquery = getQuery("exist:" + XSL_DB_LOCATION); final ResourceSet result = existXmldbEmbeddedServer.executeQuery(xquery); assertTransformationResult(result); } @Test - public void testImportXmldbSchemeDoubleSlash() throws XMLDBException { - final String xquery = getQuery("xmldb://" + moduleLocation); + public void fromDbLocationWithExistSchemeDoubleSlash() throws XMLDBException { + final String xquery = getQuery("exist://" + XSL_DB_LOCATION); final ResourceSet result = existXmldbEmbeddedServer.executeQuery(xquery); assertTransformationResult(result); }