Search in sources :

Example 16 with IntegerValue

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

the class FunOnFunctions method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    try {
        if (isCalledAs("function-lookup")) {
            final QName fname = ((QNameValue) args[0].itemAt(0)).getQName();
            final int arity = ((IntegerValue) args[1].itemAt(0)).getInt();
            FunctionCall call;
            try {
                call = NamedFunctionReference.lookupFunction(this, context, fname, arity);
            } catch (final XPathException e) {
                if (e.getErrorCode() == ErrorCodes.XPST0017) {
                    // return empty sequence for all "function not found" related errors
                    return Sequence.EMPTY_SEQUENCE;
                }
                throw e;
            }
            return call == null ? Sequence.EMPTY_SEQUENCE : new FunctionReference(call);
        } else if (isCalledAs("function-name")) {
            final FunctionReference ref = (FunctionReference) args[0].itemAt(0);
            final QName qname = ref.getSignature().getName();
            if (qname == null || qname == InlineFunction.INLINE_FUNCTION_QNAME) {
                return Sequence.EMPTY_SEQUENCE;
            } else {
                return new QNameValue(context, qname);
            }
        } else {
            // isCalledAs("function-arity")
            final FunctionReference ref = (FunctionReference) args[0].itemAt(0);
            return new IntegerValue(ref.getSignature().getArgumentCount());
        }
    } catch (final Exception e) {
        if (e instanceof XPathException) {
            throw (XPathException) e;
        } else {
            throw new XPathException(this, ErrorCodes.XPST0017, e.getMessage());
        }
    }
}
Also used : QName(org.exist.dom.QName) QNameValue(org.exist.xquery.value.QNameValue) IntegerValue(org.exist.xquery.value.IntegerValue) FunctionReference(org.exist.xquery.value.FunctionReference)

Example 17 with IntegerValue

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

the class FunPosition 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 inSequence = context.getContextSequence();
    if (inSequence == null) {
        inSequence = contextSequence;
    }
    if (inSequence == null) {
        throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
    }
    Sequence result;
    if (inSequence.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        result = new IntegerValue(context.getContextPosition() + 1);
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) Sequence(org.exist.xquery.value.Sequence)

Example 18 with IntegerValue

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

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

Example 19 with IntegerValue

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

the class BaseConversionFunctions method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (isCalledAs(qnIntToOctal.getLocalPart())) {
        final int i = args[0].toJavaObject(Integer.class);
        final String octal = i == 0 ? "0" : "0" + Integer.toOctalString(i);
        return new StringValue(octal);
    } else if (isCalledAs(qnOctalToInt.getLocalPart())) {
        final String octal = args[0].toString();
        return new IntegerValue(Integer.parseInt(octal, 8));
    } else {
        throw new XPathException("Unknown function call: " + getSignature());
    }
}
Also used : XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) StringValue(org.exist.xquery.value.StringValue)

Example 20 with IntegerValue

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

the class BaseConverter 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[] args, Sequence contextSequence) throws XPathException {
    int intValue;
    String stringValue;
    final String number = args[0].itemAt(0).getStringValue();
    final int intBase = ((IntegerValue) args[1].itemAt(0)).getInt();
    if (isCalledAs("base-to-integer")) {
        intValue = Integer.parseInt(number, intBase);
        return new IntegerValue(intValue);
    } else {
        switch(Base.getBase(intBase)) {
            case BINARY:
                stringValue = Integer.toBinaryString(Integer.parseInt(number));
                break;
            case OCTAL:
                stringValue = Integer.toOctalString(Integer.parseInt(number));
                break;
            // break;
            case HEXADECIMAL:
                stringValue = Integer.toHexString(Integer.parseInt(number));
                break;
            default:
                {
                    logger.error("Unhandled base for conversion target in integer-to-base().");
                    throw new XPathException("Unhandled base for conversion target in integer-to-base().");
                }
        }
        return new StringValue(stringValue);
    }
}
Also used : XPathException(org.exist.xquery.XPathException) IntegerValue(org.exist.xquery.value.IntegerValue) StringValue(org.exist.xquery.value.StringValue)

Aggregations

IntegerValue (org.exist.xquery.value.IntegerValue)63 Sequence (org.exist.xquery.value.Sequence)33 XPathException (org.exist.xquery.XPathException)32 BrokerPool (org.exist.storage.BrokerPool)11 DBBroker (org.exist.storage.DBBroker)11 Test (org.junit.Test)11 Txn (org.exist.storage.txn.Txn)8 MessagingException (jakarta.mail.MessagingException)7 StringInputSource (org.exist.util.StringInputSource)7 NamingException (javax.naming.NamingException)6 DirContext (javax.naming.directory.DirContext)6 QName (org.exist.dom.QName)6 Source (org.exist.source.Source)6 StringSource (org.exist.source.StringSource)6 InputStream (java.io.InputStream)5 Map (java.util.Map)5 StringValue (org.exist.xquery.value.StringValue)5 ValueSequence (org.exist.xquery.value.ValueSequence)5 Folder (jakarta.mail.Folder)4 Image (java.awt.Image)4