Search in sources :

Example 56 with XQueryContext

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());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 57 with XQueryContext

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;
}
Also used : QName(org.exist.dom.QName) XQueryContext(org.exist.xquery.XQueryContext)

Example 58 with XQueryContext

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());
}
Also used : Node(org.w3c.dom.Node) XQueryContext(org.exist.xquery.XQueryContext) NamePool(org.exist.util.hashtable.NamePool) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 59 with XQueryContext

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());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 60 with XQueryContext

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());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Aggregations

XQueryContext (org.exist.xquery.XQueryContext)70 CompiledXQuery (org.exist.xquery.CompiledXQuery)37 Sequence (org.exist.xquery.value.Sequence)34 XQuery (org.exist.xquery.XQuery)33 DBBroker (org.exist.storage.DBBroker)24 Test (org.junit.Test)23 XPathException (org.exist.xquery.XPathException)18 BrokerPool (org.exist.storage.BrokerPool)17 IOException (java.io.IOException)11 Source (org.exist.source.Source)10 XQueryPool (org.exist.storage.XQueryPool)10 Node (org.w3c.dom.Node)10 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)8 PermissionDeniedException (org.exist.security.PermissionDeniedException)8 AnyURIValue (org.exist.xquery.value.AnyURIValue)8 URI (java.net.URI)7 StringValue (org.exist.xquery.value.StringValue)7 InputSource (org.xml.sax.InputSource)7 EXistException (org.exist.EXistException)6 QName (org.exist.dom.QName)6