Search in sources :

Example 1 with EmptySequence

use of org.exist.xquery.value.EmptySequence 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)

Aggregations

RecognitionException (antlr.RecognitionException)1 TokenStreamException (antlr.TokenStreamException)1 AST (antlr.collections.AST)1 StringReader (java.io.StringReader)1 AnalyzeContextInfo (org.exist.xquery.AnalyzeContextInfo)1 ErrorCodes (org.exist.xquery.ErrorCodes)1 ErrorCode (org.exist.xquery.ErrorCodes.ErrorCode)1 PathExpr (org.exist.xquery.PathExpr)1 XPathException (org.exist.xquery.XPathException)1 XQueryContext (org.exist.xquery.XQueryContext)1 XQueryLexer (org.exist.xquery.parser.XQueryLexer)1 XQueryParser (org.exist.xquery.parser.XQueryParser)1 XQueryTreeParser (org.exist.xquery.parser.XQueryTreeParser)1 EmptySequence (org.exist.xquery.value.EmptySequence)1 StringValue (org.exist.xquery.value.StringValue)1