Search in sources :

Example 1 with XQueryParser

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

the class Compile method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // get the query expression
    final String expr = args[0].getStringValue();
    if (expr.trim().isEmpty()) {
        return new EmptySequence();
    }
    context.pushNamespaceContext();
    logger.debug("eval: {}", expr);
    // TODO(pkaminsk2): why replicate XQuery.compile here?
    String error = null;
    ErrorCodes.ErrorCode code = null;
    int line = -1;
    int column = -1;
    final XQueryContext pContext = new XQueryContext(context.getBroker().getBrokerPool());
    if (getArgumentCount() == 2 && args[1].hasOne()) {
        pContext.setModuleLoadPath(args[1].getStringValue());
    }
    final XQueryLexer lexer = new XQueryLexer(pContext, new StringReader(expr));
    final XQueryParser parser = new XQueryParser(lexer);
    // shares the context of the outer expression
    final XQueryTreeParser astParser = new XQueryTreeParser(pContext);
    try {
        parser.xpath();
        if (parser.foundErrors()) {
            logger.debug(parser.getErrorMessage());
            throw new XPathException(this, "error found while executing expression: " + parser.getErrorMessage());
        }
        final AST ast = parser.getAST();
        final PathExpr path = new PathExpr(pContext);
        astParser.xpath(ast, path);
        if (astParser.foundErrors()) {
            throw astParser.getLastException();
        }
        path.analyze(new AnalyzeContextInfo());
    } catch (final RecognitionException | TokenStreamException e) {
        error = e.toString();
    } catch (final XPathException e) {
        line = e.getLine();
        column = e.getColumn();
        code = e.getCode();
        error = e.getDetailMessage();
    } catch (final Exception e) {
        error = e.getMessage();
    } finally {
        context.popNamespaceContext();
        pContext.reset(false);
    }
    if (isCalledAs("compile")) {
        return error == null ? Sequence.EMPTY_SEQUENCE : new StringValue(error);
    } else {
        return response(pContext, error, code, line, column);
    }
}
Also used : EmptySequence(org.exist.xquery.value.EmptySequence) 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) AnalyzeContextInfo(org.exist.xquery.AnalyzeContextInfo) RecognitionException(antlr.RecognitionException) TokenStreamException(antlr.TokenStreamException) XPathException(org.exist.xquery.XPathException) ErrorCode(org.exist.xquery.ErrorCodes.ErrorCode) XQueryTreeParser(org.exist.xquery.parser.XQueryTreeParser) TokenStreamException(antlr.TokenStreamException) ErrorCodes(org.exist.xquery.ErrorCodes) StringReader(java.io.StringReader) StringValue(org.exist.xquery.value.StringValue) PathExpr(org.exist.xquery.PathExpr) RecognitionException(antlr.RecognitionException)

Example 2 with XQueryParser

use of org.exist.xquery.parser.XQueryParser 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 3 with XQueryParser

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

the class LexerTest method query.

@Test
public void query() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException, RecognitionException, XPathException, TokenStreamException {
    String query = "//p[. = '\u4ED6\u4E3A\u8FD9\u9879\u5DE5\u7A0B\u6295" + "\u5165\u4E86\u5341\u4E09\u5E74\u65F6\u95F4\u3002']";
    // get a BrokerPool for access to the database engine
    BrokerPool pool = BrokerPool.getInstance();
    final TransactionManager transact = pool.getTransactionManager();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        try (final Txn transaction = transact.beginTransaction()) {
            // parse the xml source
            Collection collection = broker.getOrCreateCollection(transaction, TestConstants.TEST_COLLECTION_URI);
            broker.saveCollection(transaction, collection);
            broker.storeDocument(transaction, XmldbURI.create("test.xml"), new StringInputSource(xml), MimeType.XML_TYPE, collection);
            // TODO : unlock the collection here ?
            transact.commit(transaction);
        }
        // parse the query into the internal syntax tree
        XQueryContext context = new XQueryContext(broker.getBrokerPool());
        XQueryLexer lexer = new XQueryLexer(context, new StringReader(query));
        XQueryParser xparser = new XQueryParser(lexer);
        XQueryTreeParser treeParser = new XQueryTreeParser(context);
        xparser.xpath();
        if (xparser.foundErrors()) {
            fail(xparser.getErrorMessage());
            return;
        }
        AST ast = xparser.getAST();
        PathExpr expr = new PathExpr(context);
        treeParser.xpath(ast, expr);
        if (treeParser.foundErrors()) {
            fail(treeParser.getErrorMessage());
            return;
        }
        expr.analyze(new AnalyzeContextInfo());
        // execute the query
        Sequence result = expr.eval(null, null);
        // check results
        int count = result.getItemCount();
    }
}
Also used : AST(antlr.collections.AST) XQueryParser(org.exist.xquery.parser.XQueryParser) Txn(org.exist.storage.txn.Txn) XQueryLexer(org.exist.xquery.parser.XQueryLexer) Sequence(org.exist.xquery.value.Sequence) XQueryTreeParser(org.exist.xquery.parser.XQueryTreeParser) DBBroker(org.exist.storage.DBBroker) StringInputSource(org.exist.util.StringInputSource) TransactionManager(org.exist.storage.txn.TransactionManager) StringReader(java.io.StringReader) Collection(org.exist.collections.Collection) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 4 with XQueryParser

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

the class XQuery method compile.

/**
 * Compiles an XQuery from a Source.
 *
 * @param context the XQuery context
 * @param reader the reader to use for obtaining theXQuery to compile
 * @param xpointer true if the query is part of an XPointer, false otherwise
 *
 * @return the compiled XQuery
 *
 * @throws XPathException if an error occurs during compilation
 * @throws PermissionDeniedException if the caller is not permitted to compile the XQuery
 */
private CompiledXQuery compile(final XQueryContext context, final Reader reader, final boolean xpointer) throws XPathException, PermissionDeniedException {
    // check read permission
    if (context.getSource() instanceof DBSource) {
        ((DBSource) context.getSource()).validate(Permission.READ);
    }
    // TODO: move XQueryContext.getUserFromHttpSession() here, have to check if servlet.jar is in the classpath
    // before compiling/executing that code though to avoid a dependency on servlet.jar - reflection? - deliriumsky
    // how about - if(XQuery.class.getResource("servlet.jar") != null) do load my class with dependency and call method?
    /*
    	 	<|wolf77|> I think last time I checked, I already had problems with the call to
    	 	<|wolf77|> HTTPUtils.addLastModifiedHeader( result, context );
			<|wolf77|> in line 184 of XQuery.java, because it introduces another dependency on HTTP.
    	 */
    final long start = System.currentTimeMillis();
    final XQueryLexer lexer = new XQueryLexer(context, reader);
    final XQueryParser parser = new XQueryParser(lexer);
    final XQueryTreeParser treeParser = new XQueryTreeParser(context);
    try {
        if (xpointer) {
            parser.xpointer();
        } else {
            parser.xpath();
        }
        if (parser.foundErrors()) {
            LOG.debug(parser.getErrorMessage());
            throw new StaticXQueryException(parser.getErrorMessage());
        }
        final AST ast = parser.getAST();
        if (ast == null) {
            throw new XPathException("Unknown XQuery parser error: the parser returned an empty syntax tree.");
        }
        // LOG.debug("Generated AST: " + ast.toStringTree());
        final PathExpr expr = new PathExpr(context);
        if (xpointer) {
            treeParser.xpointer(ast, expr);
        } else {
            treeParser.xpath(ast, expr);
        }
        if (treeParser.foundErrors()) {
            // AST treeAst = treeParser.getAST();
            throw new StaticXQueryException(ast.getLine(), ast.getColumn(), treeParser.getErrorMessage(), treeParser.getLastException());
        }
        context.getRootContext().resolveForwardReferences();
        context.analyzeAndOptimizeIfModulesChanged(expr);
        // dumping huge queries to the log
        if (LOG.isDebugEnabled()) {
            if (context.getExpressionCount() < 150) {
                LOG.debug("Query diagnostics:\n{}", ExpressionDumper.dump(expr));
            } else {
                LOG.debug("Query diagnostics:\n" + "[skipped: more than 150 expressions]");
            }
        }
        if (LOG.isDebugEnabled()) {
            final NumberFormat nf = NumberFormat.getNumberInstance();
            LOG.debug("Compilation took {} ms", nf.format(System.currentTimeMillis() - start));
        }
        return expr;
    } catch (final RecognitionException e) {
        LOG.debug("Error compiling query: {}", e.getMessage(), e);
        String msg = e.getMessage();
        if (msg.endsWith(", found 'null'")) {
            msg = msg.substring(0, msg.length() - ", found 'null'".length());
        }
        throw new StaticXQueryException(e.getLine(), e.getColumn(), msg);
    } catch (final TokenStreamException e) {
        final String es = e.toString();
        if (es.matches("^line \\d+:\\d+:.+")) {
            LOG.debug("Error compiling query: {}", e.getMessage(), e);
            final int line = Integer.parseInt(es.substring(5, es.indexOf(':')));
            final String tmpColumn = es.substring(es.indexOf(':') + 1);
            final int column = Integer.parseInt(tmpColumn.substring(0, tmpColumn.indexOf(':')));
            throw new StaticXQueryException(line, column, e.getMessage(), e);
        } else {
            LOG.debug("Error compiling query: {}", e.getMessage(), e);
            throw new StaticXQueryException(e.getMessage(), e);
        }
    }
}
Also used : AST(antlr.collections.AST) XQueryParser(org.exist.xquery.parser.XQueryParser) XQueryLexer(org.exist.xquery.parser.XQueryLexer) XQueryTreeParser(org.exist.xquery.parser.XQueryTreeParser) TokenStreamException(antlr.TokenStreamException) DBSource(org.exist.source.DBSource) RecognitionException(antlr.RecognitionException) NumberFormat(java.text.NumberFormat)

Example 5 with XQueryParser

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

the class SortedNodeSet method addAll.

@Override
public void addAll(final NodeSet other) {
    final long start = System.currentTimeMillis();
    final MutableDocumentSet docs = new DefaultDocumentSet();
    for (final NodeProxy p : other) {
        docs.add(p.getOwnerDocument());
    }
    // TODO(pkaminsk2): why replicate XQuery.compile here?
    try (final DBBroker broker = pool.get(Optional.ofNullable(user))) {
        final XQueryContext context = new XQueryContext(pool);
        final XQueryLexer lexer = new XQueryLexer(context, new StringReader(sortExpr));
        final XQueryParser parser = new XQueryParser(lexer);
        final XQueryTreeParser treeParser = new XQueryTreeParser(context);
        parser.xpath();
        if (parser.foundErrors()) {
            // TODO : error ?
            LOG.debug(parser.getErrorMessage());
        }
        final AST ast = parser.getAST();
        LOG.debug("generated AST: {}", ast.toStringTree());
        final PathExpr expr = new PathExpr(context);
        treeParser.xpath(ast, expr);
        if (treeParser.foundErrors()) {
            LOG.debug(treeParser.getErrorMessage());
        }
        expr.analyze(new AnalyzeContextInfo());
        for (final SequenceIterator i = other.iterate(); i.hasNext(); ) {
            final NodeProxy p = (NodeProxy) i.nextItem();
            final IteratorItem item = new IteratorItem(p, expr);
            list.add(item);
        }
    } catch (final RecognitionException | TokenStreamException re) {
        // TODO : throw exception ! -pb
        LOG.debug(re);
    } catch (final EXistException | XPathException e) {
        // TODO : throw exception ! -pb
        LOG.debug("Exception during sort", e);
    }
    LOG.debug("sort-expression found {} in {}ms.", list.size(), System.currentTimeMillis() - start);
}
Also used : AST(antlr.collections.AST) XQueryParser(org.exist.xquery.parser.XQueryParser) XQueryLexer(org.exist.xquery.parser.XQueryLexer) EXistException(org.exist.EXistException) XQueryTreeParser(org.exist.xquery.parser.XQueryTreeParser) TokenStreamException(antlr.TokenStreamException) DBBroker(org.exist.storage.DBBroker) SequenceIterator(org.exist.xquery.value.SequenceIterator) StringReader(java.io.StringReader) RecognitionException(antlr.RecognitionException)

Aggregations

AST (antlr.collections.AST)5 XQueryLexer (org.exist.xquery.parser.XQueryLexer)5 XQueryParser (org.exist.xquery.parser.XQueryParser)5 XQueryTreeParser (org.exist.xquery.parser.XQueryTreeParser)5 RecognitionException (antlr.RecognitionException)4 TokenStreamException (antlr.TokenStreamException)4 StringReader (java.io.StringReader)4 DBBroker (org.exist.storage.DBBroker)2 AnalyzeContextInfo (org.exist.xquery.AnalyzeContextInfo)2 PathExpr (org.exist.xquery.PathExpr)2 XPathException (org.exist.xquery.XPathException)2 XQueryContext (org.exist.xquery.XQueryContext)2 Sequence (org.exist.xquery.value.Sequence)2 NumberFormat (java.text.NumberFormat)1 EXistException (org.exist.EXistException)1 Collection (org.exist.collections.Collection)1 DBSource (org.exist.source.DBSource)1 BrokerPool (org.exist.storage.BrokerPool)1 TransactionManager (org.exist.storage.txn.TransactionManager)1 Txn (org.exist.storage.txn.Txn)1