Skip to content

Commit a21457e

Browse files
committed
[bugfix] Make sure to correctly setup the location so that transient dependencies of EXPath Pkg XQuerys that import other EXPath Pkg XQuerys compile correctly
1 parent 0cd3dd2 commit a21457e

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

exist-core/src/main/java/org/exist/repo/ExistRepository.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@
2323

2424
import org.apache.logging.log4j.LogManager;
2525
import org.apache.logging.log4j.Logger;
26+
import org.exist.dom.persistent.BinaryDocument;
27+
import org.exist.security.PermissionDeniedException;
28+
import org.exist.source.DBSource;
2629
import org.exist.storage.BrokerPool;
2730
import org.exist.storage.BrokerPoolService;
2831
import org.exist.storage.BrokerPoolServiceException;
32+
import org.exist.storage.DBBroker;
2933
import org.exist.storage.NativeBroker;
3034
import org.exist.util.Configuration;
3135
import org.exist.util.FileUtils;
36+
import org.exist.xmldb.XmldbURI;
3237
import org.exist.xquery.ErrorCodes;
3338
import org.exist.xquery.Expression;
3439
import org.exist.xquery.Module;
@@ -41,7 +46,9 @@
4146
import org.expath.pkg.repo.PackageException;
4247
import org.expath.pkg.repo.Repository;
4348
import org.expath.pkg.repo.URISpace;
49+
import org.w3c.dom.Document;
4450

51+
import javax.annotation.Nullable;
4552
import javax.xml.transform.Source;
4653
import javax.xml.transform.stream.StreamSource;
4754
import java.io.IOException;
@@ -273,6 +280,38 @@ public Path resolveXQueryModule(final String namespace) throws XPathException {
273280
return null;
274281
}
275282

283+
/**
284+
* Attempt to lookup an XQuery from the filesystem in the database.
285+
*
286+
* @param broker the database broker
287+
* @param xqueryPath the path to the xquery within the EXPath filesystem repo.
288+
*
289+
* @return the database source for the xquery, or null.
290+
*/
291+
public @Nullable org.exist.source.Source resolveStoredXQueryModuleFromDb(final DBBroker broker, final Path xqueryPath) throws PermissionDeniedException {
292+
if (!xqueryPath.startsWith(expathDir)) {
293+
return null;
294+
}
295+
296+
final String relXQueryPath = expathDir.relativize(xqueryPath).toString();
297+
298+
// 1. attempt to locate it within a library
299+
XmldbURI xqueryDbPath = XmldbURI.create("xmldb:exist:///db/system/repo/" + relXQueryPath);
300+
@Nullable Document doc = broker.getXMLResource(xqueryDbPath);
301+
if (doc != null && doc instanceof BinaryDocument) {
302+
return new DBSource(broker.getBrokerPool(), (BinaryDocument) doc, false);
303+
}
304+
305+
// 2. attempt to locate it within an app
306+
xqueryDbPath = XmldbURI.create("xmldb:exist:///db/apps/" + relXQueryPath);
307+
doc = broker.getXMLResource(xqueryDbPath);
308+
if (doc != null && doc instanceof BinaryDocument) {
309+
return new DBSource(broker.getBrokerPool(), (BinaryDocument) doc, false);
310+
}
311+
312+
return null;
313+
}
314+
276315
public Source resolveXSLTModule(final String namespace) throws PackageException {
277316
for (final Packages pp : myParent.listPackages()) {
278317
final Package pkg = pp.latest();

exist-core/src/main/java/org/exist/xquery/XQueryContext.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.net.URISyntaxException;
3131
import java.nio.charset.Charset;
3232
import java.nio.file.Path;
33+
import java.nio.file.Paths;
3334
import java.util.*;
3435
import java.util.concurrent.CopyOnWriteArrayList;
3536
import java.util.concurrent.atomic.AtomicReference;
@@ -575,9 +576,32 @@ public Optional<ExistRepository> getRepository() {
575576
}
576577

577578
// use the resolved file or return null
578-
// build a module object from the file
579-
final Source src = new FileSource(resolved, false);
580-
return compileOrBorrowModule(prefix, namespace, "", src);
579+
if (resolved != null) {
580+
581+
String location = "";
582+
583+
try {
584+
585+
// see if the src exists in the database and if so, use that instead
586+
Source src = repo.get().resolveStoredXQueryModuleFromDb(getBroker(), resolved);
587+
if (src != null) {
588+
// NOTE(AR) set the location of the module to import relative to this module's load path - so that transient imports of the imported module will resolve correctly!
589+
location = Paths.get(XmldbURI.create(moduleLoadPath).getCollectionPath()).relativize(Paths.get(((DBSource)src).getDocumentPath().getCollectionPath())).toString();
590+
} else {
591+
// else, fallback to the one from the filesystem
592+
src = new FileSource(resolved, false);
593+
}
594+
595+
// build a module object from the source
596+
final ExternalModule module = compileOrBorrowModule(prefix, namespace, location, src);
597+
return module;
598+
599+
} catch (final PermissionDeniedException e) {
600+
throw new XPathException(e.getMessage(), e);
601+
}
602+
}
603+
604+
return null;
581605
}
582606

583607
/**

exist-parent/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@
429429
<dependency>
430430
<groupId>org.expath.packaging</groupId>
431431
<artifactId>pkg-java</artifactId>
432-
<version>2.0.0</version>
432+
<version>2.0.1</version>
433433
</dependency>
434434

435435
<dependency>

0 commit comments

Comments
 (0)