Search in sources :

Example 1 with TypedValue

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

the class ResourceFunctionExecutorImpl method convertToExistSequence.

private org.exist.xquery.value.Sequence convertToExistSequence(final XQueryContext xqueryContext, final TypedArgumentValue argument, final int fnParameterType) throws RestXqServiceException, XPathException {
    final org.exist.xquery.value.Sequence sequence = new ValueSequence();
    for (final TypedValue value : (Sequence<Object>) argument.getTypedValue()) {
        final org.exquery.xquery.Type destinationType = TypeAdapter.toExQueryType(fnParameterType);
        final Class destinationClass;
        switch(fnParameterType) {
            case Type.ITEM:
                destinationClass = Item.class;
                break;
            case Type.DOCUMENT:
                // TODO test this
                destinationClass = DocumentImpl.class;
                break;
            case Type.STRING:
                destinationClass = StringValue.class;
                break;
            case Type.INT:
            case Type.INTEGER:
                destinationClass = IntegerValue.class;
                break;
            case Type.FLOAT:
                destinationClass = FloatValue.class;
                break;
            case Type.DOUBLE:
                destinationClass = DoubleValue.class;
                break;
            case Type.DECIMAL:
                destinationClass = DecimalValue.class;
                break;
            case Type.DATE:
                destinationClass = DateValue.class;
                break;
            case Type.DATE_TIME:
                destinationClass = DateTimeValue.class;
                break;
            case Type.TIME:
                destinationClass = TimeValue.class;
                break;
            case Type.QNAME:
                destinationClass = QNameValue.class;
                break;
            case Type.ANY_URI:
                destinationClass = AnyURIValue.class;
                break;
            case Type.BOOLEAN:
                destinationClass = BooleanValue.class;
                break;
            default:
                destinationClass = Item.class;
        }
        final TypedValue<? extends Item> val = convertToType(xqueryContext, argument.getArgumentName(), value, destinationType, destinationClass);
        sequence.add(val.getValue());
    }
    return sequence;
}
Also used : org.exist.xquery(org.exist.xquery) ValueSequence(org.exist.xquery.value.ValueSequence) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exquery.xquery.Sequence) TypedValue(org.exquery.xquery.TypedValue)

Example 2 with TypedValue

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

the class ResourceFunctionExecutorImpl method convertToType.

// TODO this needs to be abstracted into EXQuery library / or not, see the TODOs below
private <X> TypedValue<X> convertToType(final XQueryContext xqueryContext, final String argumentName, final TypedValue typedValue, final org.exquery.xquery.Type destinationType, final Class<X> underlyingDestinationClass) throws RestXqServiceException {
    // TODO consider changing Types that can be used as <T> to TypedValue to a set of interfaces for XDM types that
    // require absolute minimal implementation, and we provide some default or abstract implementations if possible
    final Item convertedValue;
    try {
        final int existDestinationType = TypeAdapter.toExistType(destinationType);
        final Item value;
        // Consider a factory or java.util.ServiceLoader pattern
        if (typedValue instanceof org.exquery.xdm.type.StringTypedValue) {
            value = new StringValue(((org.exquery.xdm.type.StringTypedValue) typedValue).getValue());
        } else if (typedValue instanceof org.exquery.xdm.type.Base64BinaryTypedValue) {
            value = BinaryValueFromInputStream.getInstance(xqueryContext, new Base64BinaryValueType(), ((org.exquery.xdm.type.Base64BinaryTypedValue) typedValue).getValue());
        } else {
            value = (Item) typedValue.getValue();
        }
        if (existDestinationType == value.getType()) {
            convertedValue = value;
        } else if (value instanceof AtomicValue) {
            convertedValue = value.convertTo(existDestinationType);
        } else {
            LOG.warn("Could not convert parameter '{}' from '{}' to '{}'.", argumentName, typedValue.getType().name(), destinationType.name());
            convertedValue = value;
        }
    } catch (final XPathException xpe) {
        // TODO define an ErrorCode
        throw new RestXqServiceException("TODO need to implement error code for problem with parameter conversion!: " + xpe.getMessage(), xpe);
    }
    return new TypedValue<X>() {

        @Override
        public org.exquery.xquery.Type getType() {
            // return destinationType;
            return TypeAdapter.toExQueryType(convertedValue.getType());
        }

        @Override
        public X getValue() {
            return (X) convertedValue;
        }
    };
}
Also used : RestXqServiceException(org.exquery.restxq.RestXqServiceException) org.exist.xquery(org.exist.xquery) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) AtomicValue(org.exist.xquery.value.AtomicValue) Item(org.exist.xquery.value.Item) StringValue(org.exist.xquery.value.StringValue) TypedValue(org.exquery.xquery.TypedValue)

Aggregations

org.exist.xquery (org.exist.xquery)2 TypedValue (org.exquery.xquery.TypedValue)2 AtomicValue (org.exist.xquery.value.AtomicValue)1 Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)1 Item (org.exist.xquery.value.Item)1 StringValue (org.exist.xquery.value.StringValue)1 ValueSequence (org.exist.xquery.value.ValueSequence)1 RestXqServiceException (org.exquery.restxq.RestXqServiceException)1 Sequence (org.exquery.xquery.Sequence)1