Search in sources :

Example 31 with Item

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

the class AttributeConstructor method evalEnclosedExpr.

private void evalEnclosedExpr(Sequence seq, StringBuilder buf) throws XPathException {
    Item item;
    AtomicValue atomic;
    for (final SequenceIterator i = Atomize.atomize(seq).iterate(); i.hasNext(); ) {
        item = i.nextItem();
        buf.append(item.getStringValue());
        if (i.hasNext()) {
            buf.append(' ');
        }
    }
}
Also used : Item(org.exist.xquery.value.Item) SequenceIterator(org.exist.xquery.value.SequenceIterator) AtomicValue(org.exist.xquery.value.AtomicValue)

Example 32 with Item

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

the class DynamicFunctionCall method eval.

@Override
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    context.proceed(this);
    final Sequence funcSeq = functionExpr.eval(contextSequence, contextItem);
    if (funcSeq.getCardinality() != Cardinality.EXACTLY_ONE) {
        throw new XPathException(this, ErrorCodes.XPTY0004, "Expected exactly one item for the function to be called, got " + funcSeq.getItemCount() + ". Expression: " + ExpressionDumper.dump(functionExpr));
    }
    final Item item0 = funcSeq.itemAt(0);
    if (!Type.subTypeOf(item0.getType(), Type.FUNCTION_REFERENCE)) {
        throw new XPathException(this, ErrorCodes.XPTY0004, "Type error: expected function, got " + Type.getTypeName(item0.getType()));
    }
    final FunctionReference ref = (FunctionReference) item0;
    // if the call is a partial application, create a new function
    if (isPartial) {
        try {
            final FunctionCall call = ref.getCall();
            call.setArguments(arguments);
            final PartialFunctionApplication partialApp = new PartialFunctionApplication(context, call);
            partialApp.analyze(new AnalyzeContextInfo(cachedContextInfo));
            return partialApp.eval(contextSequence, contextItem);
        } catch (final XPathException e) {
            e.setLocation(line, column, getSource());
            throw e;
        }
    } else {
        ref.setArguments(arguments);
        // need to create a new AnalyzeContextInfo to avoid memory leak
        // cachedContextInfo will stay in memory
        ref.analyze(new AnalyzeContextInfo(cachedContextInfo));
        // Evaluate the function
        try {
            return ref.eval(contextSequence);
        } catch (XPathException e) {
            if (e.getLine() <= 0) {
                e.setLocation(getLine(), getColumn(), getSource());
            }
            throw e;
        } finally {
            ref.close();
        }
    }
}
Also used : Item(org.exist.xquery.value.Item) FunctionReference(org.exist.xquery.value.FunctionReference) Sequence(org.exist.xquery.value.Sequence)

Example 33 with Item

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

the class DynamicPIConstructor method eval.

/* (non-Javadoc)
     * @see org.exist.xquery.Expression#eval(org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
     */
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());
        }
    }
    if (newDocumentContext) {
        context.pushDocumentContext();
    }
    try {
        final MemTreeBuilder builder = context.getDocumentBuilder();
        context.proceed(this, builder);
        final Sequence nameSeq = name.eval(contextSequence, contextItem);
        // TODO : get rid of getLength()
        if (!nameSeq.hasOne()) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "The name expression should evaluate to a single value");
        }
        final Item nameItem = nameSeq.itemAt(0);
        if (!(nameItem.getType() == Type.STRING || nameItem.getType() == Type.NCNAME || nameItem.getType() == Type.UNTYPED_ATOMIC)) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "The name expression should evaluate to a " + Type.getTypeName(Type.STRING) + " or a " + Type.getTypeName(Type.NCNAME) + " or a " + Type.getTypeName(Type.UNTYPED_ATOMIC) + ". Got: " + Type.getTypeName(nameItem.getType()));
        }
        if (!XMLNames.isNCName(nameSeq.getStringValue())) {
            throw new XPathException(this, ErrorCodes.XQDY0041, nameSeq.getStringValue() + "' is not a valid processing instruction name", nameSeq);
        }
        if (nameSeq.getStringValue().equalsIgnoreCase("XML")) {
            throw new XPathException(this, ErrorCodes.XQDY0064, nameSeq.getStringValue() + "' is not a valid processing instruction name", nameSeq);
        }
        String contentString;
        final Sequence contentSeq = content.eval(contextSequence, contextItem);
        if (contentSeq.isEmpty()) {
            contentString = "";
        } else {
            final StringBuilder buf = new StringBuilder();
            for (final SequenceIterator i = Atomize.atomize(contentSeq).iterate(); i.hasNext(); ) {
                context.proceed(this, builder);
                final Item next = i.nextItem();
                if (buf.length() > 0) {
                    buf.append(' ');
                }
                buf.append(next.getStringValue());
            }
            while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) buf.deleteCharAt(0);
            contentString = buf.toString();
        }
        if (contentString.contains("?>")) {
            throw new XPathException(this, ErrorCodes.XQDY0026, contentString + "' is not a valid processing intruction content", contentSeq);
        }
        final int nodeNo = builder.processingInstruction(nameSeq.getStringValue(), contentString);
        final Sequence result = ((DocumentImpl) builder.getDocument()).getNode(nodeNo);
        if (context.getProfiler().isEnabled()) {
            context.getProfiler().end(this, "", result);
        }
        return result;
    } finally {
        if (newDocumentContext) {
            context.popDocumentContext();
        }
    }
}
Also used : Item(org.exist.xquery.value.Item) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) SequenceIterator(org.exist.xquery.value.SequenceIterator) Sequence(org.exist.xquery.value.Sequence) DocumentImpl(org.exist.dom.memtree.DocumentImpl)

Example 34 with Item

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

the class FunAbs 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 {
    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;
    final Sequence seq = getArgument(0).eval(contextSequence, contextItem);
    if (seq.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        final Item item = seq.itemAt(0);
        NumericValue value;
        if (item instanceof NumericValue) {
            value = (NumericValue) item;
        } else {
            value = (NumericValue) item.convertTo(Type.NUMBER);
        }
        result = value.abs();
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) Sequence(org.exist.xquery.value.Sequence) NumericValue(org.exist.xquery.value.NumericValue)

Example 35 with Item

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

the class FunLocalName method eval.

@Override
public Sequence eval(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);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    if (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    final Item item;
    // the context sequence
    if (getArgumentCount() > 0) {
        final Sequence seq = getArgument(0).eval(contextSequence);
        if (!seq.isEmpty()) {
            item = seq.itemAt(0);
        } else {
            item = null;
        }
    } else {
        if (contextSequence == null) {
            throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
        }
        item = contextSequence.itemAt(0);
    }
    final Sequence result;
    if (item == null) {
        result = StringValue.EMPTY_STRING;
    } else {
        if (!Type.subTypeOf(item.getType(), Type.NODE)) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "item is not a node; got '" + Type.getTypeName(item.getType()) + "'");
        }
        // TODO : how to improve performance ?
        final Node n = ((NodeValue) item).getNode();
        final String localName = n.getLocalName();
        if (localName != null) {
            result = new StringValue(localName);
        } else {
            result = StringValue.EMPTY_STRING;
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) Node(org.w3c.dom.Node) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue)

Aggregations

Item (org.exist.xquery.value.Item)88 Sequence (org.exist.xquery.value.Sequence)69 SequenceIterator (org.exist.xquery.value.SequenceIterator)36 XPathException (org.exist.xquery.XPathException)26 DBBroker (org.exist.storage.DBBroker)18 NodeValue (org.exist.xquery.value.NodeValue)17 XQuery (org.exist.xquery.XQuery)16 ValueSequence (org.exist.xquery.value.ValueSequence)16 BrokerPool (org.exist.storage.BrokerPool)14 NumericValue (org.exist.xquery.value.NumericValue)12 SAXException (org.xml.sax.SAXException)11 Properties (java.util.Properties)10 StringValue (org.exist.xquery.value.StringValue)10 Node (org.w3c.dom.Node)10 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)9 AtomicValue (org.exist.xquery.value.AtomicValue)9 Txn (org.exist.storage.txn.Txn)8 IOException (java.io.IOException)7 StringWriter (java.io.StringWriter)7 EXistException (org.exist.EXistException)7