Skip to content

Commit

Permalink
[bugfix] Make sure to correctly setup the location so that transient …
Browse files Browse the repository at this point in the history
…dependencies of EXPath Pkg XQuerys that import other EXPath Pkg XQuerys compile correctly
  • Loading branch information
adamretter committed Aug 19, 2023
1 parent b53857c commit a79be27
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
39 changes: 39 additions & 0 deletions exist-core/src/main/java/org/exist/repo/ExistRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.exist.dom.persistent.BinaryDocument;
import org.exist.security.PermissionDeniedException;
import org.exist.source.DBSource;
import org.exist.storage.BrokerPool;
import org.exist.storage.BrokerPoolService;
import org.exist.storage.BrokerPoolServiceException;
import org.exist.storage.DBBroker;
import org.exist.storage.NativeBroker;
import org.exist.util.Configuration;
import org.exist.util.FileUtils;
import org.exist.xmldb.XmldbURI;
import org.exist.xquery.ErrorCodes;
import org.exist.xquery.Expression;
import org.exist.xquery.Module;
Expand All @@ -41,7 +46,9 @@
import org.expath.pkg.repo.PackageException;
import org.expath.pkg.repo.Repository;
import org.expath.pkg.repo.URISpace;
import org.w3c.dom.Document;

import javax.annotation.Nullable;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
Expand Down Expand Up @@ -273,6 +280,38 @@ public Path resolveXQueryModule(final String namespace) throws XPathException {
return null;
}

/**
* Attempt to lookup an XQuery from the filesystem in the database.
*
* @param broker the database broker
* @param xqueryPath the path to the xquery within the EXPath filesystem repo.
*
* @return the database source for the xquery, or null.
*/
public @Nullable org.exist.source.Source resolveStoredXQueryModuleFromDb(final DBBroker broker, final Path xqueryPath) throws PermissionDeniedException {
if (!xqueryPath.startsWith(expathDir)) {
return null;
}

final String relXQueryPath = expathDir.relativize(xqueryPath).toString();

// 1. attempt to locate it within a library
XmldbURI xqueryDbPath = XmldbURI.create("xmldb:exist:///db/system/repo/" + relXQueryPath);
@Nullable Document doc = broker.getXMLResource(xqueryDbPath);
if (doc != null && doc instanceof BinaryDocument) {
return new DBSource(broker, (BinaryDocument) doc, false);
}

// 2. attempt to locate it within an app
xqueryDbPath = XmldbURI.create("xmldb:exist:///db/apps/" + relXQueryPath);
doc = broker.getXMLResource(xqueryDbPath);
if (doc != null && doc instanceof BinaryDocument) {
return new DBSource(broker, (BinaryDocument) doc, false);
}

return null;
}

public Source resolveXSLTModule(final String namespace) throws PackageException {
for (final Packages pp : myParent.listPackages()) {
final Package pkg = pp.latest();
Expand Down
26 changes: 23 additions & 3 deletions exist-core/src/main/java/org/exist/xquery/XQueryContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -588,9 +589,28 @@ public Optional<ExistRepository> getRepository() {

// use the resolved file or return null
if (resolved != null) {
// build a module object from the file
final Source src = new FileSource(resolved, false);
return compileOrBorrowModule(prefix, namespace, "", src);

String location = "";

try {

// see if the src exists in the database and if so, use that instead
Source src = repo.get().resolveStoredXQueryModuleFromDb(getBroker(), resolved);
if (src != null) {
// 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!
location = Paths.get(XmldbURI.create(moduleLoadPath).getCollectionPath()).relativize(Paths.get(((DBSource)src).getDocumentPath().getCollectionPath())).toString();
} else {
// else, fallback to the one from the filesystem
src = new FileSource(resolved, false);
}

// build a module object from the source
final ExternalModule module = compileOrBorrowModule(prefix, namespace, location, src);
return module;

} catch (final PermissionDeniedException e) {
throw new XPathException(e.getMessage(), e);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion exist-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@
<dependency>
<groupId>org.expath.packaging</groupId>
<artifactId>pkg-java</artifactId>
<version>2.0.0</version>
<version>2.0.1</version>
</dependency>

<dependency>
Expand Down

0 comments on commit a79be27

Please sign in to comment.