use of org.exist.dom.persistent.BinaryDocument in project exist by eXist-db.
the class SourceFactoryTest method getSourceFromXmldbEmbedded_noContext.
@Test
public void getSourceFromXmldbEmbedded_noContext() throws IOException, PermissionDeniedException {
final String contextPath = null;
final String location = "xmldb:exist://embedded-eXist-server/db/library.xqm";
final DBBroker mockBroker = createMock(DBBroker.class);
final LockedDocument mockLockedDoc = createMock(LockedDocument.class);
final BinaryDocument mockBinDoc = createMock(BinaryDocument.class);
expect(mockBroker.getXMLResource(anyObject(), anyObject())).andReturn(mockLockedDoc);
expect(mockLockedDoc.getDocument()).andReturn(mockBinDoc);
expect(mockBinDoc.getResourceType()).andReturn(BinaryDocument.BINARY_FILE);
expect(mockBinDoc.getURI()).andReturn(XmldbURI.create(location)).times(2);
expect(mockBinDoc.getLastModified()).andReturn(123456789l);
/*expect*/
mockLockedDoc.close();
replay(mockBroker, mockLockedDoc, mockBinDoc);
final Source libSource = SourceFactory.getSource(mockBroker, contextPath, location, false);
assertTrue(libSource instanceof DBSource);
assertEquals(XmldbURI.create(location), ((DBSource) libSource).getDocumentPath());
verify(mockBroker, mockLockedDoc, mockBinDoc);
}
use of org.exist.dom.persistent.BinaryDocument in project exist by eXist-db.
the class SourceFactoryTest method getSourceFromDb.
@Test
public void getSourceFromDb() throws IOException, PermissionDeniedException {
final String contextPath = "/db";
final String location = "library.xqm";
final DBBroker mockBroker = createMock(DBBroker.class);
final LockedDocument mockLockedDoc = createMock(LockedDocument.class);
final BinaryDocument mockBinDoc = createMock(BinaryDocument.class);
expect(mockBroker.getXMLResource(anyObject(), anyObject())).andReturn(mockLockedDoc);
expect(mockLockedDoc.getDocument()).andReturn(mockBinDoc);
expect(mockBinDoc.getResourceType()).andReturn(BinaryDocument.BINARY_FILE);
expect(mockBinDoc.getURI()).andReturn(XmldbURI.create(contextPath).append(location)).times(2);
expect(mockBinDoc.getLastModified()).andReturn(123456789l);
/*expect*/
mockLockedDoc.close();
replay(mockBroker, mockLockedDoc, mockBinDoc);
final Source libSource = SourceFactory.getSource(mockBroker, contextPath, location, false);
assertTrue(libSource instanceof DBSource);
assertEquals(XmldbURI.create(contextPath).append(location), ((DBSource) libSource).getDocumentPath());
verify(mockBroker, mockLockedDoc, mockBinDoc);
}
use of org.exist.dom.persistent.BinaryDocument in project exist by eXist-db.
the class SourceFactoryTest method getSourceFromXmldbEmbedded.
@Test
public void getSourceFromXmldbEmbedded() throws IOException, PermissionDeniedException {
final String contextPath = "xmldb:exist://embedded-eXist-server/db";
final String location = "library.xqm";
final DBBroker mockBroker = createMock(DBBroker.class);
final LockedDocument mockLockedDoc = createMock(LockedDocument.class);
final BinaryDocument mockBinDoc = createMock(BinaryDocument.class);
expect(mockBroker.getXMLResource(anyObject(), anyObject())).andReturn(mockLockedDoc);
expect(mockLockedDoc.getDocument()).andReturn(mockBinDoc);
expect(mockBinDoc.getResourceType()).andReturn(BinaryDocument.BINARY_FILE);
expect(mockBinDoc.getURI()).andReturn(XmldbURI.create(contextPath).append(location)).times(2);
expect(mockBinDoc.getLastModified()).andReturn(123456789l);
/*expect*/
mockLockedDoc.close();
replay(mockBroker, mockLockedDoc, mockBinDoc);
final Source libSource = SourceFactory.getSource(mockBroker, contextPath, location, false);
assertTrue(libSource instanceof DBSource);
assertEquals(XmldbURI.create(contextPath).append(location), ((DBSource) libSource).getDocumentPath());
verify(mockBroker, mockLockedDoc, mockBinDoc);
}
use of org.exist.dom.persistent.BinaryDocument in project exist by eXist-db.
the class SourceFactoryTest method getSourceFromDb_noContext.
@Test
public void getSourceFromDb_noContext() throws IOException, PermissionDeniedException {
final String contextPath = null;
final String location = "/db/library.xqm";
final DBBroker mockBroker = createMock(DBBroker.class);
final LockedDocument mockLockedDoc = createMock(LockedDocument.class);
final BinaryDocument mockBinDoc = createMock(BinaryDocument.class);
expect(mockBroker.getXMLResource(anyObject(), anyObject())).andReturn(mockLockedDoc);
expect(mockLockedDoc.getDocument()).andReturn(mockBinDoc);
expect(mockBinDoc.getResourceType()).andReturn(BinaryDocument.BINARY_FILE);
expect(mockBinDoc.getURI()).andReturn(XmldbURI.create(location)).times(2);
expect(mockBinDoc.getLastModified()).andReturn(123456789l);
/*expect*/
mockLockedDoc.close();
replay(mockBroker, mockLockedDoc, mockBinDoc);
final Source libSource = SourceFactory.getSource(mockBroker, contextPath, location, false);
assertTrue(libSource instanceof DBSource);
assertEquals(XmldbURI.create(location), ((DBSource) libSource).getDocumentPath());
verify(mockBroker, mockLockedDoc, mockBinDoc);
}
use of org.exist.dom.persistent.BinaryDocument in project exist by eXist-db.
the class Scan method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
Source source = null;
String name;
if (getArgumentCount() == 2) {
byte[] data;
try {
data = binaryValueToByteArray((BinaryValue) args[0].itemAt(0));
} catch (IOException ioe) {
throw new XPathException(ioe.getMessage(), ioe);
}
name = args[1].getStringValue();
source = new BinarySource(data, true);
} else {
String uri = args[0].getStringValue();
if (uri.startsWith(XmldbURI.XMLDB_URI_PREFIX)) {
try {
XmldbURI resourceURI = XmldbURI.xmldbUriFor(uri);
try (final Collection collection = context.getBroker().openCollection(resourceURI.removeLastSegment(), LockMode.READ_LOCK)) {
if (collection == null) {
LOG.warn("collection not found: {}", resourceURI.getCollectionPath());
return Sequence.EMPTY_SEQUENCE;
}
try (final LockedDocument lockedDoc = collection.getDocumentWithLock(context.getBroker(), resourceURI.lastSegment(), LockMode.READ_LOCK)) {
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
collection.close();
final DocumentImpl doc = lockedDoc == null ? null : lockedDoc.getDocument();
if (doc == null) {
return Sequence.EMPTY_SEQUENCE;
}
if (doc.getResourceType() != DocumentImpl.BINARY_FILE || !doc.getMimeType().equals("application/xquery")) {
throw new XPathException(this, "XQuery resource: " + uri + " is not an XQuery or " + "declares a wrong mime-type");
}
source = new DBSource(context.getBroker(), (BinaryDocument) doc, false);
name = doc.getFileURI().toString();
}
} catch (LockException e) {
throw new XPathException(this, "internal lock error: " + e.getMessage());
} catch (PermissionDeniedException pde) {
throw new XPathException(this, pde.getMessage(), pde);
}
} catch (URISyntaxException e) {
throw new XPathException(this, "invalid module uri: " + uri + ": " + e.getMessage(), e);
}
} else {
// first check if the URI points to a registered module
String location = context.getModuleLocation(uri);
if (location != null)
uri = location;
try {
source = SourceFactory.getSource(context.getBroker(), context.getModuleLoadPath(), uri, false);
if (source == null) {
throw new XPathException(this, "failed to read module " + uri);
}
name = extractName(uri);
} catch (IOException e) {
throw new XPathException(this, "failed to read module " + uri, e);
} catch (PermissionDeniedException e) {
throw new XPathException(this, "permission denied to read module " + uri, e);
}
}
}
try {
XQDocHelper helper = new XQDocHelper();
String xml = helper.scan(source, name);
NodeValue root = ModuleUtils.stringToXML(context, xml);
if (root == null)
return Sequence.EMPTY_SEQUENCE;
return normalize((NodeValue) ((Document) root).getDocumentElement());
} catch (XQDocException | SAXException e) {
throw new XPathException(this, "error while scanning module: " + e.getMessage(), e);
} catch (IOException e) {
throw new XPathException(this, "IO error while scanning module: " + e.getMessage(), e);
}
}
Aggregations