Search in sources :

Example 16 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class LocalXPathQueryService method execute.

private ResourceSet execute(final LocalXmldbFunction<Source> sourceOp) throws XMLDBException {
    return withDb((broker, transaction) -> {
        final long start = System.currentTimeMillis();
        final Source source = sourceOp.apply(broker, transaction);
        final XmldbURI[] docs = new XmldbURI[] { XmldbURI.create(collection.getName(broker, transaction)) };
        final XQuery xquery = brokerPool.getXQueryService();
        final XQueryPool pool = brokerPool.getXQueryPool();
        XQueryContext context;
        CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
        if (compiled == null) {
            context = new XQueryContext(broker.getBrokerPool());
        } else {
            context = compiled.getContext();
            context.prepareForReuse();
        }
        context.setStaticallyKnownDocuments(docs);
        if (variableDecls.containsKey(Debuggee.PREFIX + ":session")) {
            context.declareVariable(Debuggee.SESSION, variableDecls.get(Debuggee.PREFIX + ":session"));
            variableDecls.remove(Debuggee.PREFIX + ":session");
        }
        setupContext(source, context);
        if (compiled == null) {
            compiled = xquery.compile(context, source);
        }
        try {
            final Sequence result = xquery.execute(broker, compiled, null, properties);
            if (LOG.isDebugEnabled()) {
                LOG.debug("query took {} ms.", System.currentTimeMillis() - start);
            }
            final Properties resourceSetProperties = new Properties(properties);
            resourceSetProperties.setProperty(EXistOutputKeys.XDM_SERIALIZATION, "yes");
            return result != null ? new LocalResourceSet(user, brokerPool, collection, resourceSetProperties, result, null) : null;
        } finally {
            compiled.getContext().runCleanupTasks();
            pool.returnCompiledXQuery(source, compiled);
        }
    });
}
Also used : XQueryPool(org.exist.storage.XQueryPool) 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) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) FileSource(org.exist.source.FileSource)

Example 17 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class FunUnparsedText method getSource.

private Source getSource(final String uriParam) throws XPathException {
    try {
        final URI uri = new URI(uriParam);
        if (uri.getFragment() != null) {
            throw new XPathException(this, ErrorCodes.FOUT1170, "href argument may not contain fragment identifier");
        }
        final Source source = SourceFactory.getSource(context.getBroker(), "", uri.toASCIIString(), false);
        if (source == null) {
            throw new XPathException(this, ErrorCodes.FOUT1170, "Could not find source for: " + uriParam);
        }
        if (source instanceof FileSource && !context.getBroker().getCurrentSubject().hasDbaRole()) {
            throw new PermissionDeniedException("non-dba user not allowed to read from file system");
        }
        return source;
    } catch (final IOException | PermissionDeniedException | URISyntaxException e) {
        throw new XPathException(this, ErrorCodes.FOUT1170, e.getMessage());
    }
}
Also used : FileSource(org.exist.source.FileSource) PermissionDeniedException(org.exist.security.PermissionDeniedException) URISyntaxException(java.net.URISyntaxException) XmldbURI(org.exist.xmldb.XmldbURI) URI(java.net.URI) FileSource(org.exist.source.FileSource) Source(org.exist.source.Source)

Example 18 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class JSON method parseResource.

private Sequence parseResource(Sequence href, String handleDuplicates, JsonFactory factory) throws XPathException {
    if (href.isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    try {
        String url = href.getStringValue();
        if (url.indexOf(':') == Constants.STRING_NOT_FOUND) {
            url = XmldbURI.EMBEDDED_SERVER_URI_PREFIX + url;
        }
        final Source source = SourceFactory.getSource(context.getBroker(), "", url, false);
        if (source == null) {
            throw new XPathException(this, ErrorCodes.FOUT1170, "failed to load json doc from URI " + url);
        }
        try (final InputStream is = source.getInputStream();
            final JsonParser parser = factory.createParser(is)) {
            final Item result = readValue(context, parser, handleDuplicates);
            return result == null ? Sequence.EMPTY_SEQUENCE : result.toSequence();
        }
    } catch (IOException | PermissionDeniedException e) {
        throw new XPathException(this, ErrorCodes.FOUT1170, e.getMessage());
    }
}
Also used : InputStream(java.io.InputStream) PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException) Source(org.exist.source.Source) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 19 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class Conditional method process.

@Override
public long process(Txn transaction) throws PermissionDeniedException, LockException, EXistException, XPathException, TriggerException {
    LOG.debug("Processing xupdate:if ...");
    final XQuery xquery = broker.getBrokerPool().getXQueryService();
    final XQueryPool pool = broker.getBrokerPool().getXQueryPool();
    final Source source = new StringSource(selectStmt);
    CompiledXQuery compiled = pool.borrowCompiledXQuery(broker, source);
    XQueryContext context;
    if (compiled == null) {
        context = new XQueryContext(broker.getBrokerPool());
    } else {
        context = compiled.getContext();
        context.prepareForReuse();
    }
    // context.setBackwardsCompatibility(true);
    context.setStaticallyKnownDocuments(docs);
    declareNamespaces(context);
    declareVariables(context);
    if (compiled == null)
        try {
            compiled = xquery.compile(context, source);
        } catch (final IOException e) {
            throw new EXistException("An exception occurred while compiling the query: " + e.getMessage());
        }
    Sequence seq = null;
    try {
        seq = xquery.execute(broker, compiled, null);
    } finally {
        context.runCleanupTasks();
        pool.returnCompiledXQuery(source, compiled);
    }
    if (seq.effectiveBooleanValue()) {
        long mods = 0;
        for (final Modification modification : modifications) {
            mods += modification.process(transaction);
            broker.flush();
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("{} modifications processed.", mods);
        }
        return mods;
    } else {
        return 0;
    }
}
Also used : XQueryPool(org.exist.storage.XQueryPool) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) StringSource(org.exist.source.StringSource) IOException(java.io.IOException) EXistException(org.exist.EXistException) Sequence(org.exist.xquery.value.Sequence) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source)

Example 20 with Source

use of org.exist.source.Source in project exist by eXist-db.

the class InternalModuleTest method reusedModuleVariables.

/**
 * Similar to {@link #moduleVariables()} but
 * re-executes the query to ensure on subsequent
 * invocations, reusing the cached query (and query
 * context) do not cause problems.
 */
@Test
public void reusedModuleVariables() throws XMLDBException {
    final Source querySource = new StringSource(getModuleVariableQuery("org.exist.xquery.InternalModuleTest$TestModuleWithVariables"));
    final EXistXQueryService queryService = (EXistXQueryService) existServer.getRoot().getService("XQueryService", "1.0");
    moduleVariablesQuery(queryService, querySource, COUNTER.get());
    moduleVariablesQuery(queryService, querySource, COUNTER.get());
    moduleVariablesQuery(queryService, querySource, COUNTER.get());
}
Also used : EXistXQueryService(org.exist.xmldb.EXistXQueryService) StringSource(org.exist.source.StringSource) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) Test(org.junit.Test)

Aggregations

Source (org.exist.source.Source)83 StringSource (org.exist.source.StringSource)62 Sequence (org.exist.xquery.value.Sequence)43 BrokerPool (org.exist.storage.BrokerPool)42 DBBroker (org.exist.storage.DBBroker)42 Txn (org.exist.storage.txn.Txn)40 Test (org.junit.Test)35 StringInputSource (org.exist.util.StringInputSource)32 DBSource (org.exist.source.DBSource)23 IOException (java.io.IOException)19 PermissionDeniedException (org.exist.security.PermissionDeniedException)19 InputSource (org.xml.sax.InputSource)15 EXistException (org.exist.EXistException)12 XmldbURI (org.exist.xmldb.XmldbURI)11 XQueryPool (org.exist.storage.XQueryPool)9 XQueryContext (org.exist.xquery.XQueryContext)9 FileSource (org.exist.source.FileSource)8 CompiledXQuery (org.exist.xquery.CompiledXQuery)8 XQuery (org.exist.xquery.XQuery)8 Collection (org.exist.collections.Collection)7