use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class EmbeddedBinariesTest method executeXQuery.
@Override
protected QueryResultAccessor<Sequence, IOException> executeXQuery(final String query) throws Exception {
final Source source = new StringSource(query);
final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
final XQueryPool pool = brokerPool.getXQueryPool();
final XQuery xquery = brokerPool.getXQueryService();
try (final DBBroker broker = brokerPool.get(Optional.of(brokerPool.getSecurityManager().getSystemSubject()))) {
final CompiledXQuery existingCompiled = pool.borrowCompiledXQuery(broker, source);
final XQueryContext context;
final CompiledXQuery compiled;
if (existingCompiled == null) {
context = new XQueryContext(brokerPool);
compiled = xquery.compile(context, source);
} else {
context = existingCompiled.getContext();
context.prepareForReuse();
compiled = existingCompiled;
}
final Sequence results = xquery.execute(broker, compiled, null);
return consumer2E -> {
try {
// context.runCleanupTasks(); //TODO(AR) shows the ordering issue with binary values (see comment below)
consumer2E.accept(results);
} finally {
// TODO(AR) performing #runCleanupTasks causes the stream to be closed, so if we do so before we are finished with the results, serialization fails.
context.runCleanupTasks();
pool.returnCompiledXQuery(source, compiled);
}
};
}
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class EXIUtils method getInputStream.
protected static InputStream getInputStream(Item item, XQueryContext context) throws XPathException, MalformedURLException, IOException {
switch(item.getType()) {
case Type.ANY_URI:
LOG.debug("Streaming xs:anyURI");
// anyURI provided
String url = item.getStringValue();
// Fix URL
if (url.startsWith("/")) {
url = "xmldb:exist://" + url;
}
return new URL(url).openStream();
case Type.ELEMENT:
case Type.DOCUMENT:
LOG.debug("Streaming element or document node");
/*
if (item instanceof NodeProxy) {
NodeProxy np = (NodeProxy) item;
String url = "xmldb:exist://" + np.getDocument().getBaseURI();
LOG.debug("Document detected, adding URL " + url);
streamSource.setSystemId(url);
}
*/
// Node provided
final ConsumerE<ConsumerE<Serializer, IOException>, IOException> withSerializerFn = fn -> {
final Serializer serializer = context.getBroker().borrowSerializer();
try {
fn.accept(serializer);
} finally {
context.getBroker().returnSerializer(serializer);
}
};
NodeValue node = (NodeValue) item;
return new NodeInputStream(context.getBroker().getBrokerPool(), withSerializerFn, node);
default:
LOG.error("Wrong item type {}", Type.getTypeName(item.getType()));
throw new XPathException("wrong item type " + Type.getTypeName(item.getType()));
}
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class UserXQueryJob method executeXQuery.
private void executeXQuery(final BrokerPool pool, final DBBroker broker, final Source source, final Properties params) throws PermissionDeniedException, XPathException, JobExecutionException {
XQueryPool xqPool = null;
CompiledXQuery compiled = null;
XQueryContext context = null;
try {
// execute the xquery
final XQuery xquery = pool.getXQueryService();
xqPool = pool.getXQueryPool();
// try and get a pre-compiled query from the pool
compiled = xqPool.borrowCompiledXQuery(broker, source);
if (compiled == null) {
context = new XQueryContext(pool);
} else {
context = compiled.getContext();
context.prepareForReuse();
}
if (source instanceof DBSource) {
final XmldbURI collectionUri = ((DBSource) source).getDocumentPath().removeLastSegment();
context.setModuleLoadPath(XmldbURI.EMBEDDED_SERVER_URI.append(collectionUri.getCollectionPath()).toString());
context.setStaticallyKnownDocuments(new XmldbURI[] { collectionUri });
}
if (compiled == null) {
try {
compiled = xquery.compile(context, source);
} catch (final IOException e) {
abort("Failed to read query from " + xqueryResource);
}
}
// declare any parameters as external variables
if (params != null) {
String bindingPrefix = params.getProperty("bindingPrefix");
if (bindingPrefix == null) {
bindingPrefix = "local";
}
for (final Entry param : params.entrySet()) {
final String key = (String) param.getKey();
final String value = (String) param.getValue();
context.declareVariable(bindingPrefix + ":" + key, new StringValue(value));
}
}
xquery.execute(broker, compiled, null);
} finally {
if (context != null) {
context.runCleanupTasks();
}
// return the compiled query to the pool
if (xqPool != null && source != null && compiled != null) {
xqPool.returnCompiledXQuery(source, compiled);
}
}
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class DocumentBuilderReceiverTest method use_namespace_prefix_from_context.
@Test
public void use_namespace_prefix_from_context() throws SAXException {
// if a namespace is mapped in the current context, use its prefix and overwrite
// local prefix
final String title = "title";
final String titleQName = "atom:title";
XQueryContext mockContext = EasyMock.createMock(XQueryContext.class);
expect(mockContext.getDatabase()).andReturn(null);
expect(mockContext.getSharedNamePool()).andReturn(new NamePool());
// namespace mapping in context
expect(mockContext.getPrefixForURI(ATOM_NS)).andReturn("a");
replay(mockContext);
MemTreeBuilder builder = new MemTreeBuilder(mockContext);
DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
builder.startDocument();
receiver.startElement(ATOM_NS, title, titleQName, null);
receiver.endElement(ATOM_NS, title, titleQName);
builder.endDocument();
verify(mockContext);
Document doc = builder.getDocument();
Node entryNode = doc.getFirstChild();
assertEquals("Explicit namespace prefix should be preserved", "a:title", entryNode.getNodeName());
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class DocumentBuilderReceiverTest method when_prefix_is_known_in_context_dont_use_if_namespace_equals_default_namespace.
// private static String ATOM_PREFIX = "atom";
@Test
public void when_prefix_is_known_in_context_dont_use_if_namespace_equals_default_namespace() throws SAXException {
final String entry_name = "entry";
final String id_name = "id";
XQueryContext mockContext = EasyMock.createMock(XQueryContext.class);
expect(mockContext.getDatabase()).andReturn(null);
expect(mockContext.getSharedNamePool()).andReturn(new NamePool());
// expect(mockContext.getPrefixForURI(ATOM_NS)).andReturn(ATOM_PREFIX).times(2);
replay(mockContext);
MemTreeBuilder builder = new MemTreeBuilder(mockContext);
DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
builder.startDocument();
receiver.startPrefixMapping("", ATOM_NS);
receiver.startElement(ATOM_NS, entry_name, entry_name, null);
receiver.startElement(ATOM_NS, id_name, id_name, null);
receiver.endElement(ATOM_NS, id_name, id_name);
receiver.endElement(ATOM_NS, entry_name, entry_name);
builder.endDocument();
verify(mockContext);
Document doc = builder.getDocument();
Node entryNode = doc.getFirstChild();
assertEquals(entry_name, entryNode.getNodeName());
Node idNode = entryNode.getFirstChild();
assertEquals(id_name, idNode.getNodeName());
}
Aggregations