Search in sources :

Example 1 with AnalyzeContextInfo

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

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

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

the class CallFunction method eval.

/* (non-Javadoc)
     * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
     */
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    final Sequence arg0 = getArgument(0).eval(contextSequence, contextItem);
    if (arg0.getCardinality() != Cardinality.EXACTLY_ONE) {
        throw new XPathException(this, "Expected exactly one item for first argument");
    }
    final Item item0 = arg0.itemAt(0);
    if (item0.getType() != Type.FUNCTION_REFERENCE) {
        throw new XPathException(this, "Type error: expected function, got " + Type.getTypeName(item0.getType()));
    }
    try (final FunctionReference ref = (FunctionReference) item0) {
        // pass the remaining parameters to the function call
        final List<Expression> params = new ArrayList<>(getArgumentCount() - 1);
        for (int i = 1; i < getArgumentCount(); i++) {
            params.add(getArgument(i));
        }
        ref.setArguments(params);
        ref.analyze(new AnalyzeContextInfo(this, 0));
        // Evaluate the function
        return ref.eval(contextSequence);
    }
}
Also used : Item(org.exist.xquery.value.Item) XPathException(org.exist.xquery.XPathException) Expression(org.exist.xquery.Expression) ArrayList(java.util.ArrayList) FunctionReference(org.exist.xquery.value.FunctionReference) Sequence(org.exist.xquery.value.Sequence) AnalyzeContextInfo(org.exist.xquery.AnalyzeContextInfo)

Example 4 with AnalyzeContextInfo

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

the class FunUnordered method analyze.

public void analyze(AnalyzeContextInfo contextInfo) throws XPathException {
    final AnalyzeContextInfo newContextInfo = new AnalyzeContextInfo(contextInfo);
    newContextInfo.setParent(this);
    newContextInfo.addFlag(UNORDERED);
    super.analyze(newContextInfo);
}
Also used : AnalyzeContextInfo(org.exist.xquery.AnalyzeContextInfo)

Aggregations

AnalyzeContextInfo (org.exist.xquery.AnalyzeContextInfo)4 XPathException (org.exist.xquery.XPathException)3 RecognitionException (antlr.RecognitionException)2 TokenStreamException (antlr.TokenStreamException)2 AST (antlr.collections.AST)2 StringReader (java.io.StringReader)2 PathExpr (org.exist.xquery.PathExpr)2 XQueryContext (org.exist.xquery.XQueryContext)2 XQueryLexer (org.exist.xquery.parser.XQueryLexer)2 XQueryParser (org.exist.xquery.parser.XQueryParser)2 XQueryTreeParser (org.exist.xquery.parser.XQueryTreeParser)2 Sequence (org.exist.xquery.value.Sequence)2 ArrayList (java.util.ArrayList)1 ErrorCodes (org.exist.xquery.ErrorCodes)1 ErrorCode (org.exist.xquery.ErrorCodes.ErrorCode)1 Expression (org.exist.xquery.Expression)1 EmptySequence (org.exist.xquery.value.EmptySequence)1 FunctionReference (org.exist.xquery.value.FunctionReference)1 Item (org.exist.xquery.value.Item)1 StringValue (org.exist.xquery.value.StringValue)1