Search in sources :

Example 11 with Sequence

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

the class UserDefinedFunction method eval.

/* (non-Javadoc)
	 * @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
	 */
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    // context.expressionStart(this);
    context.stackEnter(this);
    // make sure reset state is called after query has finished
    hasBeenReset = false;
    // Save the local variable stack
    final LocalVariable mark = context.markLocalVariables(true);
    if (closureVariables != null) {
        context.restoreStack(closureVariables);
    }
    Sequence result = null;
    try {
        QName varName;
        LocalVariable var;
        int j = 0;
        for (int i = 0; i < parameters.size(); i++, j++) {
            varName = parameters.get(i);
            var = new LocalVariable(varName);
            var.setValue(currentArguments[j]);
            if (contextDocs != null) {
                var.setContextDocs(contextDocs[i]);
            }
            context.declareVariableBinding(var);
            Cardinality actualCardinality;
            if (currentArguments[j].isEmpty()) {
                actualCardinality = Cardinality.EMPTY_SEQUENCE;
            } else if (currentArguments[j].hasMany()) {
                actualCardinality = Cardinality._MANY;
            } else {
                actualCardinality = Cardinality.EXACTLY_ONE;
            }
            if (!getSignature().getArgumentTypes()[j].getCardinality().isSuperCardinalityOrEqualOf(actualCardinality)) {
                throw new XPathException(this, ErrorCodes.XPTY0004, "Invalid cardinality for parameter $" + varName + ". Expected " + getSignature().getArgumentTypes()[j].getCardinality().getHumanDescription() + ", got " + currentArguments[j].getItemCount());
            }
        }
        result = body.eval(null, null);
        return result;
    } finally {
        // restore the local variable stack
        context.popLocalVariables(mark, result);
        context.stackLeave(this);
    // context.expressionEnd(this);
    }
}
Also used : QName(org.exist.dom.QName) Sequence(org.exist.xquery.value.Sequence)

Example 12 with Sequence

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

the class ValueComparison method nodeSetCompare.

protected Sequence nodeSetCompare(NodeSet nodes, Sequence contextSequence) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().message(this, Profiler.OPTIMIZATION_FLAGS, "OPTIMIZATION CHOICE", "nodeSetCompare");
    }
    final NodeSet result = new ExtArrayNodeSet();
    final Collator collator = getCollator(contextSequence);
    if (contextSequence != null && !contextSequence.isEmpty()) {
        for (final NodeProxy current : nodes) {
            ContextItem context = current.getContext();
            if (context == null) {
                throw new XPathException(this, ErrorCodes.XPDY0002, "Context is missing for node set comparison");
            }
            do {
                final AtomicValue lv = current.atomize();
                final Sequence rs = getRight().eval(context.getNode().toSequence());
                if (!rs.hasOne()) {
                    throw new XPathException(this, ErrorCodes.XPTY0004, "Type error: sequence with less or more than one item is not allowed here");
                }
                if (compareAtomic(collator, lv, rs.itemAt(0).atomize(), StringTruncationOperator.NONE, relation)) {
                    result.add(current);
                }
            } while ((context = context.getNextDirect()) != null);
        }
    } else {
        final Sequence rs = getRight().eval(null);
        if (!rs.hasOne()) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "Type error: sequence with less or more than one item is not allowed here");
        }
        final AtomicValue rv = rs.itemAt(0).atomize();
        for (final NodeProxy current : nodes) {
            final AtomicValue lv = current.atomize();
            if (compareAtomic(collator, lv, rv, StringTruncationOperator.NONE, Comparison.EQ)) {
                result.add(current);
            }
        }
    }
    return result;
}
Also used : ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) NodeSet(org.exist.dom.persistent.NodeSet) ContextItem(org.exist.dom.persistent.ContextItem) ExtArrayNodeSet(org.exist.dom.persistent.ExtArrayNodeSet) AtomicValue(org.exist.xquery.value.AtomicValue) Sequence(org.exist.xquery.value.Sequence) NodeProxy(org.exist.dom.persistent.NodeProxy) Collator(com.ibm.icu.text.Collator)

Example 13 with Sequence

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

the class VariableDeclaration method eval.

@Override
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
    }
    context.pushInScopeNamespaces(false);
    try {
        final Module myModule = findDeclaringModule(context.getRootModules(qname.getNamespaceURI()));
        context.pushDocumentContext();
        try {
            context.prologEnter(this);
            if (expression.isPresent()) {
                // normal variable declaration or external var with default value
                final Sequence seq = expression.get().eval(contextSequence, null);
                final Variable var;
                if (myModule != null) {
                    var = myModule.declareVariable(qname, seq);
                    var.setSequenceType(sequenceType);
                    var.checkType();
                } else {
                    var = new VariableImpl(qname);
                    var.setValue(seq);
                    var.setSequenceType(sequenceType);
                    var.checkType();
                    context.declareGlobalVariable(var);
                }
                if (context.getProfiler().isEnabled()) {
                    // Note : that we use seq but we return Sequence.EMPTY_SEQUENCE
                    context.getProfiler().end(this, "", seq);
                }
            } else {
                // external variable without default
                final Variable external = context.resolveGlobalVariable(qname);
                if (external == null) {
                    // is not specified, then a dynamic error is raised [err:XPDY0002]
                    throw new XPathException(ErrorCodes.XPDY0002, "no value specified for external variable " + qname);
                }
                external.setSequenceType(sequenceType);
                if (myModule != null) {
                    // declare on module
                    myModule.declareVariable(external);
                }
            }
        } finally {
            context.popDocumentContext();
        }
    } finally {
        context.popInScopeNamespaces();
    }
    return null;
}
Also used : Sequence(org.exist.xquery.value.Sequence)

Example 14 with Sequence

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

the class OpAnd method eval.

public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    Sequence result;
    if (getLength() == 0) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        if (contextItem != null) {
            contextSequence = contextItem.toSequence();
        }
        boolean doOptimize = optimize;
        if (contextSequence != null && !contextSequence.isPersistentSet()) {
            doOptimize = false;
        }
        final Expression left = getLeft();
        final Expression right = getRight();
        // setContextId(getExpressionId());
        if (doOptimize && contextSequence != null) {
            contextSequence.setSelfAsContext(getContextId());
        }
        final Sequence ls = left.eval(contextSequence, null);
        doOptimize = doOptimize && (ls.isPersistentSet() || ls.isEmpty());
        if (doOptimize) {
            if (inPredicate) {
                NodeSet lr = ls.toNodeSet();
                lr = lr.getContextNodes(getContextId());
                if (lr.isEmpty()) {
                    return NodeSet.EMPTY_SET;
                }
                final Sequence rs = right.eval(lr, null);
                final NodeSet rr = rs.toNodeSet();
                result = rr.getContextNodes(getContextId());
            } else {
                final Sequence rs = right.eval(contextSequence, null);
                final boolean rl = ls.effectiveBooleanValue();
                if (!rl) {
                    result = BooleanValue.FALSE;
                } else {
                    final boolean rr = rs.effectiveBooleanValue();
                    result = (rl && rr) ? BooleanValue.TRUE : BooleanValue.FALSE;
                }
            }
        } else {
            boolean rl = ls.effectiveBooleanValue();
            // Immediately return false if the left operand is false
            if (!rl) {
                result = BooleanValue.FALSE;
            } else {
                final Sequence rs = right.eval(contextSequence, null);
                final boolean rr = rs.effectiveBooleanValue();
                result = (rl && rr) ? BooleanValue.TRUE : BooleanValue.FALSE;
            }
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) Sequence(org.exist.xquery.value.Sequence)

Example 15 with Sequence

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

the class GrammarTooling method eval.

/**
 * @see org.exist.xquery.BasicFunction#eval(Sequence[], Sequence)
 */
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final GrammarPool grammarpool = (GrammarPool) config.getProperty(XMLReaderObjectFactory.GRAMMAR_POOL);
    if (isCalledAs("clear-grammar-cache")) {
        final Sequence result = new ValueSequence();
        final int before = countTotalNumberOfGrammar(grammarpool);
        LOG.debug("Clearing {} grammars", before);
        clearGrammarPool(grammarpool);
        final int after = countTotalNumberOfGrammar(grammarpool);
        LOG.debug("Remained {} grammars", after);
        final int delta = before - after;
        result.add(new IntegerValue(delta));
        return result;
    } else if (isCalledAs("show-grammar-cache")) {
        context.pushDocumentContext();
        try {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            final NodeImpl result = writeReport(grammarpool, builder);
            return result;
        } finally {
            context.popDocumentContext();
        }
    } else if (isCalledAs("pre-parse-grammar")) {
        if (args[0].isEmpty()) {
            return Sequence.EMPTY_SEQUENCE;
        }
        // Setup for XML schema support only
        final XMLGrammarPreparser parser = new XMLGrammarPreparser();
        parser.registerPreparser(TYPE_XSD, null);
        final List<Grammar> allGrammars = new ArrayList<>();
        // iterate through the argument sequence and parse url
        for (final SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
            String url = i.nextItem().getStringValue();
            // Fix database urls
            if (url.startsWith("/")) {
                url = "xmldb:exist://" + url;
            }
            LOG.debug("Parsing {}", url);
            // parse XSD grammar
            try {
                if (url.endsWith(".xsd")) {
                    final InputStream is = new URL(url).openStream();
                    final XMLInputSource xis = new XMLInputSource(null, url, url, is, null);
                    final Grammar schema = parser.preparseGrammar(TYPE_XSD, xis);
                    is.close();
                    allGrammars.add(schema);
                } else {
                    throw new XPathException(this, "Only XMLSchemas can be preparsed.");
                }
            } catch (final Exception ex) {
                LOG.debug(ex);
                throw new XPathException(this, ex);
            }
        }
        LOG.debug("Successfully parsed {} grammars.", allGrammars.size());
        // Send all XSD grammars to grammarpool
        Grammar[] grammars = new Grammar[allGrammars.size()];
        grammars = allGrammars.toArray(grammars);
        grammarpool.cacheGrammars(TYPE_XSD, grammars);
        // Construct result to end user
        final ValueSequence result = new ValueSequence();
        for (final Grammar one : grammars) {
            result.add(new StringValue(one.getGrammarDescription().getNamespace()));
        }
        return result;
    } else {
        // oh oh
        LOG.error("function not found error");
        throw new XPathException(this, "function not found");
    }
}
Also used : NodeImpl(org.exist.dom.memtree.NodeImpl) XMLInputSource(org.apache.xerces.xni.parser.XMLInputSource) XPathException(org.exist.xquery.XPathException) InputStream(java.io.InputStream) IntegerValue(org.exist.xquery.value.IntegerValue) ArrayList(java.util.ArrayList) GrammarPool(org.exist.validation.GrammarPool) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) Grammar(org.apache.xerces.xni.grammars.Grammar) URL(java.net.URL) XPathException(org.exist.xquery.XPathException) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) XMLGrammarPreparser(org.apache.xerces.parsers.XMLGrammarPreparser) SequenceIterator(org.exist.xquery.value.SequenceIterator) ValueSequence(org.exist.xquery.value.ValueSequence) StringValue(org.exist.xquery.value.StringValue)

Aggregations

Sequence (org.exist.xquery.value.Sequence)427 DBBroker (org.exist.storage.DBBroker)179 BrokerPool (org.exist.storage.BrokerPool)158 Test (org.junit.Test)114 XQuery (org.exist.xquery.XQuery)108 XPathException (org.exist.xquery.XPathException)86 Txn (org.exist.storage.txn.Txn)81 Item (org.exist.xquery.value.Item)68 ValueSequence (org.exist.xquery.value.ValueSequence)55 StringValue (org.exist.xquery.value.StringValue)49 Source (org.exist.source.Source)45 QName (org.exist.dom.QName)42 StringSource (org.exist.source.StringSource)42 SequenceIterator (org.exist.xquery.value.SequenceIterator)40 StringInputSource (org.exist.util.StringInputSource)37 CompiledXQuery (org.exist.xquery.CompiledXQuery)36 XQueryContext (org.exist.xquery.XQueryContext)35 IntegerValue (org.exist.xquery.value.IntegerValue)33 InputSource (org.xml.sax.InputSource)23 Diff (org.xmlunit.diff.Diff)21