Search in sources :

Example 11 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class DatabaseResources method executeQuery.

public Sequence executeQuery(String queryPath, Map<String, String> params, Subject user) {
    final String namespace = params.get(TARGETNAMESPACE);
    final String publicId = params.get(PUBLICID);
    final String catalogPath = params.get(CATALOG);
    final String collection = params.get(COLLECTION);
    if (logger.isDebugEnabled()) {
        logger.debug("collection={} namespace={} publicId={} catalogPath={}", collection, namespace, publicId, catalogPath);
    }
    Sequence result = null;
    try (final DBBroker broker = brokerPool.get(Optional.ofNullable(user))) {
        final XQuery xquery = brokerPool.getXQueryService();
        final XQueryContext context = new XQueryContext(brokerPool);
        if (collection != null) {
            context.declareVariable(COLLECTION, collection);
        }
        if (namespace != null) {
            context.declareVariable(TARGETNAMESPACE, namespace);
        }
        if (publicId != null) {
            context.declareVariable(PUBLICID, publicId);
        }
        if (catalogPath != null) {
            context.declareVariable(CATALOG, catalogPath);
        }
        CompiledXQuery compiled = xquery.compile(context, new ClassLoaderSource(queryPath));
        result = xquery.execute(broker, compiled, null);
    } catch (final EXistException | XPathException | IOException | PermissionDeniedException ex) {
        logger.error("Problem executing xquery", ex);
        result = null;
    }
    return result;
}
Also used : ClassLoaderSource(org.exist.source.ClassLoaderSource) DBBroker(org.exist.storage.DBBroker) XPathException(org.exist.xquery.XPathException) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) PermissionDeniedException(org.exist.security.PermissionDeniedException) Sequence(org.exist.xquery.value.Sequence) EXistException(org.exist.EXistException) IOException(java.io.IOException)

Example 12 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class XUpdateProcessor method processQuery.

private Sequence processQuery(String select) throws SAXException {
    XQueryContext context = null;
    try {
        context = new XQueryContext(broker.getBrokerPool());
        context.setStaticallyKnownDocuments(documentSet);
        Map.Entry<String, String> namespaceEntry;
        for (Map.Entry<String, String> stringStringEntry : namespaces.entrySet()) {
            namespaceEntry = stringStringEntry;
            context.declareNamespace(namespaceEntry.getKey(), namespaceEntry.getValue());
        }
        Map.Entry<String, Object> entry;
        for (Map.Entry<String, Object> stringObjectEntry : variables.entrySet()) {
            entry = stringObjectEntry;
            context.declareVariable(entry.getKey(), entry.getValue());
        }
        // TODO(pkaminsk2): why replicate XQuery.compile here?
        final XQueryLexer lexer = new XQueryLexer(context, new StringReader(select));
        final XQueryParser parser = new XQueryParser(lexer);
        final XQueryTreeParser treeParser = new XQueryTreeParser(context);
        parser.xpath();
        if (parser.foundErrors()) {
            throw new SAXException(parser.getErrorMessage());
        }
        final AST ast = parser.getAST();
        if (LOG.isDebugEnabled()) {
            LOG.debug("generated AST: {}", ast.toStringTree());
        }
        final PathExpr expr = new PathExpr(context);
        treeParser.xpath(ast, expr);
        if (treeParser.foundErrors()) {
            throw new SAXException(treeParser.getErrorMessage());
        }
        expr.analyze(new AnalyzeContextInfo());
        final Sequence seq = expr.eval(null, null);
        return seq;
    } catch (final RecognitionException | TokenStreamException e) {
        LOG.warn("error while creating variable", e);
        throw new SAXException(e);
    } catch (final XPathException e) {
        throw new SAXException(e);
    } finally {
        if (context != null) {
            context.reset(false);
        }
    }
}
Also used : AST(antlr.collections.AST) XPathException(org.exist.xquery.XPathException) XQueryContext(org.exist.xquery.XQueryContext) XQueryParser(org.exist.xquery.parser.XQueryParser) XQueryLexer(org.exist.xquery.parser.XQueryLexer) Sequence(org.exist.xquery.value.Sequence) AnalyzeContextInfo(org.exist.xquery.AnalyzeContextInfo) XQueryTreeParser(org.exist.xquery.parser.XQueryTreeParser) SAXException(org.xml.sax.SAXException) TokenStreamException(antlr.TokenStreamException) StringReader(java.io.StringReader) PathExpr(org.exist.xquery.PathExpr) RecognitionException(antlr.RecognitionException)

Example 13 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class TestDataGenerator method generate.

public Path[] generate(final DBBroker broker, final Collection collection, final String xqueryContent) throws SAXException {
    try {
        final DocumentSet docs = collection.allDocs(broker, new DefaultDocumentSet(), true);
        final XQuery service = broker.getBrokerPool().getXQueryService();
        final XQueryContext context = new XQueryContext(broker.getBrokerPool());
        context.declareVariable("filename", "");
        context.declareVariable("count", "0");
        context.setStaticallyKnownDocuments(docs);
        final String query = IMPORT + xqueryContent;
        final CompiledXQuery compiled = service.compile(context, query);
        for (int i = 0; i < count; i++) {
            generatedFiles[i] = Files.createTempFile(prefix, ".xml");
            context.declareVariable("filename", generatedFiles[i].getFileName().toString());
            context.declareVariable("count", new Integer(i));
            final Sequence results = service.execute(broker, compiled, Sequence.EMPTY_SEQUENCE);
            final Serializer serializer = broker.borrowSerializer();
            try (final Writer out = Files.newBufferedWriter(generatedFiles[i], StandardCharsets.UTF_8)) {
                final SAXSerializer sax = new SAXSerializer(out, outputProps);
                serializer.setSAXHandlers(sax, sax);
                for (final SequenceIterator iter = results.iterate(); iter.hasNext(); ) {
                    final Item item = iter.nextItem();
                    if (!Type.subTypeOf(item.getType(), Type.NODE)) {
                        continue;
                    }
                    serializer.toSAX((NodeValue) item);
                }
            } finally {
                broker.returnSerializer(serializer);
            }
        }
    } catch (final XPathException | PermissionDeniedException | LockException | IOException e) {
        LOG.error(e.getMessage(), e);
        throw new SAXException(e.getMessage(), e);
    }
    return generatedFiles;
}
Also used : DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) XPathException(org.exist.xquery.XPathException) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) SAXException(org.xml.sax.SAXException) LockException(org.exist.util.LockException) PermissionDeniedException(org.exist.security.PermissionDeniedException) DefaultDocumentSet(org.exist.dom.persistent.DefaultDocumentSet) DocumentSet(org.exist.dom.persistent.DocumentSet) SAXSerializer(org.exist.util.serializer.SAXSerializer) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer)

Example 14 with XQueryContext

use of org.exist.xquery.XQueryContext 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 15 with XQueryContext

use of org.exist.xquery.XQueryContext in project exist by eXist-db.

the class Util method compileQuery.

public static CompiledXQuery compileQuery(final DBBroker broker, final XQuery xqueryService, final XQueryPool xqueryPool, final Source query) throws PermissionDeniedException, XPathException, IOException {
    CompiledXQuery compiled = xqueryPool.borrowCompiledXQuery(broker, query);
    XQueryContext context;
    if (compiled == null) {
        context = new XQueryContext(broker.getBrokerPool());
    } else {
        context = compiled.getContext();
        context.prepareForReuse();
    }
    if (compiled == null) {
        compiled = xqueryService.compile(context, query);
    } else {
        compiled.getContext().updateContext(context);
        context.getWatchDog().reset();
    }
    return compiled;
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext)

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