use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class DocTest method docAvailable_dynamicallyAvailableDocument_absoluteUri.
@Test
public void docAvailable_dynamicallyAvailableDocument_absoluteUri() throws XPathException, EXistException, PermissionDeniedException {
final BrokerPool pool = BrokerPool.getInstance();
final String doc = "<timestamp>" + System.currentTimeMillis() + "</timestamp>";
final String docUri = "http://from-dynamic-context/doc1";
final String query = "fn:doc-available('" + docUri + "')";
try (final DBBroker broker = pool.getBroker()) {
final XQueryContext context = new XQueryContext(pool);
context.addDynamicallyAvailableDocument(docUri, (broker2, transaction, uri) -> asInMemoryDocument(doc));
final XQuery xqueryService = pool.getXQueryService();
final CompiledXQuery compiled = xqueryService.compile(context, query);
final Sequence result = xqueryService.execute(broker, compiled, null);
assertFalse(result.isEmpty());
assertEquals(1, result.getItemCount());
assertTrue(result.itemAt(0).toJavaObject(Boolean.class).booleanValue());
}
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class DocumentBuilderReceiver method checkNS.
private QName checkNS(boolean isElement, final QName qname) {
if (checkNS) {
final XQueryContext context = builder.getContext();
if (qname.getPrefix() == null) {
if (!qname.hasNamespace()) {
return qname;
} else if (isElement) {
return qname;
} else {
final String prefix = generatePrefix(context, context.getInScopePrefix(qname.getNamespaceURI()));
context.declareInScopeNamespace(prefix, qname.getNamespaceURI());
return new QName(qname.getLocalPart(), qname.getNamespaceURI(), prefix);
}
}
if (qname.getPrefix().isEmpty() && qname.getNamespaceURI() == null) {
return qname;
}
final String inScopeNamespace = context.getInScopeNamespace(qname.getPrefix());
if (inScopeNamespace == null) {
context.declareInScopeNamespace(qname.getPrefix(), qname.getNamespaceURI());
} else if (!inScopeNamespace.equals(qname.getNamespaceURI())) {
final String prefix = generatePrefix(context, context.getInScopePrefix(qname.getNamespaceURI()));
context.declareInScopeNamespace(prefix, qname.getNamespaceURI());
return new QName(qname.getLocalPart(), qname.getNamespaceURI(), prefix);
}
}
return qname;
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class DocumentBuilderReceiverTest method use_given_namespace_prefix.
@Test
public void use_given_namespace_prefix() throws SAXException {
// if an explicit namespace prefix is present, it should be used
// unless a different mapping was defined in context
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());
// no namespace mapping in context
expect(mockContext.getPrefixForURI(ATOM_NS)).andReturn(null);
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", titleQName, entryNode.getNodeName());
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class FunUnparsedTextTest method unparsedText_dynamicallyAvailableDocument_absoluteUri.
@Test
public void unparsedText_dynamicallyAvailableDocument_absoluteUri() throws XPathException, EXistException, PermissionDeniedException {
final BrokerPool pool = BrokerPool.getInstance();
final String text = "hello, the time is: " + System.currentTimeMillis();
final String textUri = "http://from-dynamic-context/doc1";
final String query = "fn:unparsed-text('" + textUri + "')";
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XQueryContext context = new XQueryContext(pool);
context.addDynamicallyAvailableTextResource(textUri, UTF_8, (broker2, transaction, uri, charset) -> new InputStreamReader(new ByteArrayInputStream(text.getBytes(UTF_8)), charset));
final XQuery xqueryService = pool.getXQueryService();
final CompiledXQuery compiled = xqueryService.compile(context, query);
final Sequence result = xqueryService.execute(broker, compiled, null);
assertFalse(result.isEmpty());
assertEquals(1, result.getItemCount());
assertEquals(Type.STRING, result.itemAt(0).getType());
assertEquals(text, result.itemAt(0).getStringValue());
}
}
use of org.exist.xquery.XQueryContext in project exist by eXist-db.
the class FunUnparsedTextTest method unparsedTextAvailable_dynamicallyAvailableDocument_absoluteUri.
@Test
public void unparsedTextAvailable_dynamicallyAvailableDocument_absoluteUri() throws XPathException, EXistException, PermissionDeniedException {
final BrokerPool pool = BrokerPool.getInstance();
final String text = "hello, the time is: " + System.currentTimeMillis();
final String textUri = "http://from-dynamic-context/doc1";
final String query = "fn:unparsed-text-available('" + textUri + "')";
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
final XQueryContext context = new XQueryContext(pool);
context.addDynamicallyAvailableTextResource(textUri, UTF_8, (broker2, transaction, uri, charset) -> new InputStreamReader(new ByteArrayInputStream(text.getBytes(UTF_8)), charset));
final XQuery xqueryService = pool.getXQueryService();
final CompiledXQuery compiled = xqueryService.compile(context, query);
final Sequence result = xqueryService.execute(broker, compiled, null);
assertFalse(result.isEmpty());
assertEquals(1, result.getItemCount());
assertTrue(result.itemAt(0).toJavaObject(Boolean.class).booleanValue());
}
}
Aggregations