Search in sources :

Example 1 with Expression

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

the class FunSubstring method eval.

public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    // start profiler
    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());
        }
    }
    // get arguments
    final Expression argSourceString = getArgument(0);
    final Expression argStartingLoc = getArgument(1);
    Expression argLength = null;
    // get the context sequence
    if (contextItem != null) {
        contextSequence = contextItem.toSequence();
    }
    Sequence result;
    final Sequence seqSourceString = argSourceString.eval(contextSequence);
    // If the value of $sourceString is the empty sequence return EMPTY_STRING, there must be a string to operate on!
    if (seqSourceString.isEmpty()) {
        result = StringValue.EMPTY_STRING;
    } else {
        // get the string to substring
        final String sourceString = seqSourceString.getStringValue();
        // check for a valid start position for the substring
        final NumericValue startingLoc = ((NumericValue) (argStartingLoc.eval(contextSequence).itemAt(0).convertTo(Type.NUMBER))).round();
        if (!validStartPosition(startingLoc, sourceString.length())) {
            // invalid start position
            result = StringValue.EMPTY_STRING;
        } else {
            // are there 2 or 3 arguments to this function?
            if (getArgumentCount() > 2) {
                argLength = getArgument(2);
                final NumericValue length = ((NumericValue) (argLength.eval(contextSequence).itemAt(0).convertTo(Type.NUMBER))).round();
                // Relocate length to position according to spec:
                // fn:round($startingLoc) <=
                // $p
                // < fn:round($startingLoc) + fn:round($length)
                NumericValue endingLoc;
                if (!length.isInfinite()) {
                    endingLoc = (NumericValue) new IntegerValue(startingLoc.getInt() + length.getInt());
                } else {
                    endingLoc = length;
                }
                // check for a valid end position for the substring
                if (!validEndPosition(endingLoc, startingLoc)) {
                    // invalid length
                    result = StringValue.EMPTY_STRING;
                } else {
                    if (endingLoc.getInt() > sourceString.length() || endingLoc.isInfinite()) {
                        // fallback to fn:substring(string, start)
                        result = substring(sourceString, startingLoc);
                    } else {
                        // three arguments fn:substring(string, start, end)
                        result = substring(sourceString, startingLoc, endingLoc);
                    }
                }
            } else {
                // No length argument
                // two arguments fn:substring(string, start)
                result = substring(sourceString, startingLoc);
            }
        }
    }
    // end profiler
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Expression(org.exist.xquery.Expression) IntegerValue(org.exist.xquery.value.IntegerValue) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) NumericValue(org.exist.xquery.value.NumericValue)

Example 2 with Expression

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

the class FunReplace method setArguments.

@Override
public void setArguments(List<Expression> arguments) {
    steps.clear();
    Expression arg = arguments.get(0);
    arg = new DynamicCardinalityCheck(context, Cardinality.ZERO_OR_ONE, arg, new Error(Error.FUNC_PARAM_CARDINALITY, "1", getSignature()));
    if (!Type.subTypeOf(arg.returnsType(), Type.ATOMIC)) {
        arg = new Atomize(context, arg);
    }
    steps.add(arg);
    arg = arguments.get(1);
    arg = new DynamicCardinalityCheck(context, Cardinality.EXACTLY_ONE, arg, new Error(Error.FUNC_PARAM_CARDINALITY, "2", getSignature()));
    if (!Type.subTypeOf(arg.returnsType(), Type.ATOMIC)) {
        arg = new Atomize(context, arg);
    }
    steps.add(arg);
    arg = arguments.get(2);
    arg = new DynamicCardinalityCheck(context, Cardinality.EXACTLY_ONE, arg, new Error(Error.FUNC_PARAM_CARDINALITY, "3", getSignature()));
    if (!Type.subTypeOf(arg.returnsType(), Type.ATOMIC)) {
        arg = new Atomize(context, arg);
    }
    steps.add(arg);
    if (arguments.size() == 4) {
        arg = arguments.get(3);
        arg = new DynamicCardinalityCheck(context, Cardinality.EXACTLY_ONE, arg, new Error(Error.FUNC_PARAM_CARDINALITY, "4", getSignature()));
        if (!Type.subTypeOf(arg.returnsType(), Type.ATOMIC)) {
            arg = new Atomize(context, arg);
        }
        steps.add(arg);
    }
}
Also used : Expression(org.exist.xquery.Expression) RegularExpression(net.sf.saxon.regex.RegularExpression) Atomize(org.exist.xquery.Atomize) DynamicCardinalityCheck(org.exist.xquery.DynamicCardinalityCheck) Error(org.exist.xquery.util.Error)

Example 3 with Expression

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

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

the class OrderedValueSequenceTest method mockOrderedValueSequence.

private static OrderedValueSequence mockOrderedValueSequence(final int size) throws XPathException {
    final Expression mockSortExpr = createMock(Expression.class);
    expect(mockSortExpr.eval(null)).andReturn(Sequence.EMPTY_SEQUENCE).anyTimes();
    replay(mockSortExpr);
    final OrderedValueSequence orderedValueSequence = new OrderedValueSequence(new OrderSpec[] { new OrderSpec(null, mockSortExpr) }, size);
    for (int i = 0; i < size; i++) {
        final Item item = createMock(Item.class);
        expect(item.getType()).andReturn(Type.ANY_TYPE);
        replay(item);
        orderedValueSequence.add(item);
    }
    return orderedValueSequence;
}
Also used : OrderSpec(org.exist.xquery.OrderSpec) Expression(org.exist.xquery.Expression)

Example 5 with Expression

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

the class StackGet method stackToString.

private StringBuilder stackToString(int index) {
    StringBuilder result = new StringBuilder();
    if (stacks == null || stacks.size() == 0)
        return result;
    Expression expr = stacks.get(index);
    if (expr == null)
        return result;
    int level = stacks.size() - index - 1;
    result.append("<stack level=\"");
    result.append(String.valueOf(level));
    result.append("\" lineno=\"");
    result.append(expr.getLine());
    result.append("\" type=\"file\" filename=\"");
    result.append(getFileuri(expr.getSource()));
    result.append("\" ");
    // +
    // "where=\"\" " +
    result.append("cmdbegin=\"");
    result.append(expr.getLine());
    result.append(":");
    result.append(expr.getColumn());
    result.append("\"  />");
    // "cmdend=\""+(expr.getLine())+":"+(expr.getColumn()+1)+"\"/>";
    return result;
}
Also used : Expression(org.exist.xquery.Expression)

Aggregations

Expression (org.exist.xquery.Expression)12 Sequence (org.exist.xquery.value.Sequence)7 XPathException (org.exist.xquery.XPathException)3 SequenceIterator (org.exist.xquery.value.SequenceIterator)3 Collator (com.ibm.icu.text.Collator)2 NodeProxy (org.exist.dom.persistent.NodeProxy)2 NodeSet (org.exist.dom.persistent.NodeSet)2 DynamicCardinalityCheck (org.exist.xquery.DynamicCardinalityCheck)2 Error (org.exist.xquery.util.Error)2 NodeValue (org.exist.xquery.value.NodeValue)2 StringValue (org.exist.xquery.value.StringValue)2 ValueSequence (org.exist.xquery.value.ValueSequence)2 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1