|
23 | 23 |
|
24 | 24 | import org.apache.logging.log4j.LogManager;
|
25 | 25 | 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; |
26 | 29 | import org.exist.storage.BrokerPool;
|
27 | 30 | import org.exist.storage.BrokerPoolService;
|
28 | 31 | import org.exist.storage.BrokerPoolServiceException;
|
| 32 | +import org.exist.storage.DBBroker; |
29 | 33 | import org.exist.storage.NativeBroker;
|
30 | 34 | import org.exist.util.Configuration;
|
31 | 35 | import org.exist.util.FileUtils;
|
| 36 | +import org.exist.xmldb.XmldbURI; |
32 | 37 | import org.exist.xquery.ErrorCodes;
|
33 | 38 | import org.exist.xquery.Expression;
|
34 | 39 | import org.exist.xquery.Module;
|
|
41 | 46 | import org.expath.pkg.repo.PackageException;
|
42 | 47 | import org.expath.pkg.repo.Repository;
|
43 | 48 | import org.expath.pkg.repo.URISpace;
|
| 49 | +import org.w3c.dom.Document; |
44 | 50 |
|
| 51 | +import javax.annotation.Nullable; |
45 | 52 | import javax.xml.transform.Source;
|
46 | 53 | import javax.xml.transform.stream.StreamSource;
|
47 | 54 | import java.io.IOException;
|
@@ -273,6 +280,38 @@ public Path resolveXQueryModule(final String namespace) throws XPathException {
|
273 | 280 | return null;
|
274 | 281 | }
|
275 | 282 |
|
| 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 | + |
276 | 315 | public Source resolveXSLTModule(final String namespace) throws PackageException {
|
277 | 316 | for (final Packages pp : myParent.listPackages()) {
|
278 | 317 | final Package pkg = pp.latest();
|
|
0 commit comments