Skip to content

Commit a79be27

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 b53857c commit a79be27

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-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, (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, (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: 23 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;
@@ -588,9 +589,28 @@ public Optional<ExistRepository> getRepository() {
588589

589590
// use the resolved file or return null
590591
if (resolved != null) {
591-
// build a module object from the file
592-
final Source src = new FileSource(resolved, false);
593-
return compileOrBorrowModule(prefix, namespace, "", src);
592+
593+
String location = "";
594+
595+
try {
596+
597+
// see if the src exists in the database and if so, use that instead
598+
Source src = repo.get().resolveStoredXQueryModuleFromDb(getBroker(), resolved);
599+
if (src != null) {
600+
// 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!
601+
location = Paths.get(XmldbURI.create(moduleLoadPath).getCollectionPath()).relativize(Paths.get(((DBSource)src).getDocumentPath().getCollectionPath())).toString();
602+
} else {
603+
// else, fallback to the one from the filesystem
604+
src = new FileSource(resolved, false);
605+
}
606+
607+
// build a module object from the source
608+
final ExternalModule module = compileOrBorrowModule(prefix, namespace, location, src);
609+
return module;
610+
611+
} catch (final PermissionDeniedException e) {
612+
throw new XPathException(e.getMessage(), e);
613+
}
594614
}
595615
}
596616

exist-parent/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@
394394
<dependency>
395395
<groupId>org.expath.packaging</groupId>
396396
<artifactId>pkg-java</artifactId>
397-
<version>2.0.0</version>
397+
<version>2.0.1</version>
398398
</dependency>
399399

400400
<dependency>

0 commit comments

Comments
 (0)