Skip to content

Commit b28dbaf

Browse files
authored
Merge pull request #5577 from line-o/backport/5576
[6.x.x] registered import-uris have precedence
2 parents 3011d1f + e4f5e0a commit b28dbaf

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

exist-core/src/main/java/org/exist/xslt/XsltURIResolverHelper.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,16 @@ public class XsltURIResolverHelper {
5252
@Nullable final URIResolver defaultResolver, @Nullable final String base, final boolean avoidSelf) {
5353
final List<URIResolver> resolvers = new ArrayList<>();
5454

55+
// EXpath Pkg resolver
56+
// This resolver needs to be the first one to prevent
57+
// HTTP requests for registered package names (e.g. http://www.functx.com/functx.xsl)
58+
brokerPool.getExpathRepo().map(repo -> resolvers.add(new PkgXsltModuleURIResolver(repo)));
59+
5560
if (base != null) {
5661
// database resolver
5762
resolvers.add(new EXistURISchemeURIResolver(new EXistURIResolver(brokerPool, base)));
5863
}
5964

60-
// EXpath Pkg resolver
61-
brokerPool.getExpathRepo().map(repo -> resolvers.add(new PkgXsltModuleURIResolver(repo)));
62-
6365
// default resolver
6466
if (defaultResolver != null) {
6567
if (avoidSelf) {

exist-core/src/test/java/org/exist/xquery/functions/transform/TransformFromPkgTest.java renamed to exist-core/src/test/java/org/exist/xquery/functions/transform/ImportStylesheetTest.java

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,22 @@
3838
import static org.junit.Assert.assertNotNull;
3939

4040
/**
41+
* Test transform:transform with an imported stylesheet from various
42+
* locations
43+
*
4144
* @author <a href="mailto:[email protected]">Adam Retter</a>
45+
* @author <a href="mailto:[email protected]">Juri Leino</a>
4246
*/
43-
public class TransformFromPkgTest {
47+
public class ImportStylesheetTest {
4448

45-
private static final String moduleLocation = "/db/system/repo/functx-1.0.1/functx/functx.xsl";
46-
private static final String inputXml = "<x>bonjourno</x>";
47-
private static final String expectedOutput = "<r xmlns:functx=\"http://www.functx.com\">hello</r>";
49+
private static final String XSL_DB_LOCATION = "/db/system/repo/functx-1.0.1/functx/functx.xsl";
50+
private static final String INPUT_XML = "<in>bonjourno</in>";
51+
private static final String EXPECTED_OUTPUT = "<out>hello</out>";
4852

4953
private static Path getConfigFile() {
50-
final ClassLoader loader = TransformFromPkgTest.class.getClassLoader();
54+
final ClassLoader loader = ImportStylesheetTest.class.getClassLoader();
5155
final char separator = System.getProperty("file.separator").charAt(0);
52-
final String packagePath = TransformFromPkgTest.class.getPackage().getName().replace('.', separator);
56+
final String packagePath = ImportStylesheetTest.class.getPackage().getName().replace('.', separator);
5357

5458
try {
5559
return Paths.get(loader.getResource(packagePath + separator + "conf.xml").toURI());
@@ -60,51 +64,72 @@ private static Path getConfigFile() {
6064
}
6165

6266
private static String getQuery(final String importLocation) {
63-
final String xslt = "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"\n" +
67+
final String xslt =
68+
"<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"\n" +
6469
" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"\n" +
6570
" xmlns:functx=\"http://www.functx.com\"\n" +
66-
" exclude-result-prefixes=\"xs\"\n" +
71+
" exclude-result-prefixes=\"functx xs\"\n" +
6772
" version=\"2.0\">\n" +
68-
" \n" +
6973
" <xsl:import href=\"" + importLocation + "\"/>\n" +
70-
" \n" +
7174
" <xsl:template match=\"/\">\n" +
72-
" <r>" +
75+
" <out>\n" +
7376
" <xsl:value-of select=\"functx:replace-first(., 'bonjourno', 'hello')\"/>\n" +
74-
" </r>" +
77+
" </out>\n" +
7578
" </xsl:template>\n" +
76-
" \n" +
7779
"</xsl:stylesheet>";
7880

79-
return "transform:transform(" + inputXml + ", " + xslt + ", ())";
81+
return "transform:transform(" + INPUT_XML + ", " + xslt + ", ())";
8082
}
8183

8284
private static void assertTransformationResult(ResourceSet result) throws XMLDBException {
8385
assertNotNull(result);
8486
assertEquals(1, result.getSize());
85-
assertEquals(expectedOutput, result.getResource(0).getContent());
87+
assertEquals(EXPECTED_OUTPUT, result.getResource(0).getContent());
8688
}
8789

8890
@ClassRule
8991
public static ExistXmldbEmbeddedServer existXmldbEmbeddedServer = new ExistXmldbEmbeddedServer(true, false, true, getConfigFile());
9092

9193
@Test
92-
public void testImportNoScheme() throws XMLDBException {
93-
final String xquery = getQuery(moduleLocation);
94+
public void fromRegisteredImportUri() throws XMLDBException {
95+
final String xquery = getQuery("http://www.functx.com/functx.xsl");
96+
final ResourceSet result = existXmldbEmbeddedServer.executeQuery(xquery);
97+
assertTransformationResult(result);
98+
}
99+
100+
@Test
101+
public void fromDbLocationWithoutScheme() throws XMLDBException {
102+
final String xquery = getQuery(XSL_DB_LOCATION);
103+
final ResourceSet result = existXmldbEmbeddedServer.executeQuery(xquery);
104+
assertTransformationResult(result);
105+
}
106+
107+
@Test
108+
public void fromDbLocationWithXmldbScheme() throws XMLDBException {
109+
final String xquery = getQuery("xmldb:" + XSL_DB_LOCATION);
110+
final ResourceSet result = existXmldbEmbeddedServer.executeQuery(xquery);
111+
assertTransformationResult(result);
112+
}
113+
114+
@Test
115+
public void fromDbLocationWithXmldbSchemeDoubleSlash() throws XMLDBException {
116+
final String xquery = getQuery("xmldb://" + XSL_DB_LOCATION);
94117
final ResourceSet result = existXmldbEmbeddedServer.executeQuery(xquery);
95118
assertTransformationResult(result);
96119
}
97120

121+
/** This test fails at the moment with "unknown protocol: exist" */
98122
@Test
99-
public void testImportXmldbScheme() throws XMLDBException {
100-
final String xquery = getQuery("xmldb:" + moduleLocation);
123+
@Ignore
124+
public void fromDbLocationWithExistScheme() throws XMLDBException {
125+
final String xquery = getQuery("exist:" + XSL_DB_LOCATION);
101126
final ResourceSet result = existXmldbEmbeddedServer.executeQuery(xquery);
102127
assertTransformationResult(result);
103128
}
104129

105130
@Test
106-
public void testImportXmldbSchemeDoubleSlash() throws XMLDBException {
107-
final String xquery = getQuery("xmldb://" + moduleLocation);
131+
public void fromDbLocationWithExistSchemeDoubleSlash() throws XMLDBException {
132+
final String xquery = getQuery("exist://" + XSL_DB_LOCATION);
108133
final ResourceSet result = existXmldbEmbeddedServer.executeQuery(xquery);
109134
assertTransformationResult(result);
110135
}

0 commit comments

Comments
 (0)