Search in sources :

Example 6 with XQueryLexer

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

the class AbstractSource method getModuleDecl.

/**
 * Check if the source is an XQuery module. If yes, return a QName containing
 * the module prefix as local name and the module namespace as namespace URI.
 *
 * @param is the input stream
 * @return QName describing the module namespace or null if the source is not
 * a module.
 */
protected static QName getModuleDecl(final InputStream is) {
    final XQueryLexer lexer = new XQueryLexer(null, new InputStreamReader(is));
    final DeclScanner scanner = new DeclScanner(lexer);
    try {
        scanner.versionDecl();
    } catch (final RecognitionException | XPathException | TokenStreamException e) {
    // Nothing to do
    }
    if (scanner.getNamespace() != null) {
        return new QName(scanner.getPrefix(), scanner.getNamespace());
    }
    return null;
}
Also used : TokenStreamException(antlr.TokenStreamException) InputStreamReader(java.io.InputStreamReader) XPathException(org.exist.xquery.XPathException) QName(org.exist.dom.QName) DeclScanner(org.exist.xquery.parser.DeclScanner) XQueryLexer(org.exist.xquery.parser.XQueryLexer) RecognitionException(antlr.RecognitionException)

Example 7 with XQueryLexer

use of org.exist.xquery.parser.XQueryLexer 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

XQueryLexer (org.exist.xquery.parser.XQueryLexer)7 RecognitionException (antlr.RecognitionException)6 TokenStreamException (antlr.TokenStreamException)6 AST (antlr.collections.AST)5 XQueryParser (org.exist.xquery.parser.XQueryParser)5 XQueryTreeParser (org.exist.xquery.parser.XQueryTreeParser)5 StringReader (java.io.StringReader)4 XPathException (org.exist.xquery.XPathException)4 InputStreamReader (java.io.InputStreamReader)2 DBBroker (org.exist.storage.DBBroker)2 AnalyzeContextInfo (org.exist.xquery.AnalyzeContextInfo)2 PathExpr (org.exist.xquery.PathExpr)2 XQueryContext (org.exist.xquery.XQueryContext)2 DeclScanner (org.exist.xquery.parser.DeclScanner)2 Sequence (org.exist.xquery.value.Sequence)2 NumberFormat (java.text.NumberFormat)1 EXistException (org.exist.EXistException)1 Collection (org.exist.collections.Collection)1 QName (org.exist.dom.QName)1 DBSource (org.exist.source.DBSource)1