Search in sources :

Example 6 with Item

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

the class TransformTest method keys.

/**
 * {@see https://github.com/eXist-db/exist/issues/1506}
 */
@Test
public void keys() throws EXistException, PermissionDeniedException, XPathException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final XQuery xquery = pool.getXQueryService();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final Sequence sequence = xquery.execute(broker, LIST_OPS_XQUERY, null);
        assertNotNull(sequence);
        assertEquals(1, sequence.getItemCount());
        final Item item = sequence.itemAt(0);
        assertTrue(item instanceof Element);
        final Element dsn_flat = ((Element) item);
        assertEquals("DSN_FLAT", dsn_flat.getNodeName());
        final NodeList nodeList = dsn_flat.getElementsByTagName("listOpsEntry");
        assertEquals(4, nodeList.getLength());
        assertEquals("id: IRCANTEC doEntiteAff: false doGenerateB20: true ", nodeList.item(0).getTextContent());
        assertEquals("id: CIBTP doEntiteAff: true doGenerateB20: true ", nodeList.item(1).getTextContent());
        assertEquals("id: AGIRC-ARRCO doEntiteAff: true doGenerateB20: false ", nodeList.item(2).getTextContent());
        assertEquals("id: CTIP-FFSA-FNMF doEntiteAff: true doGenerateB20: true ", nodeList.item(3).getTextContent());
    }
}
Also used : Item(org.exist.xquery.value.Item) DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 7 with Item

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

the class TransformTest method xslDocument.

@Ignore("https://github.com/eXist-db/exist/issues/2096")
@Test
public void xslDocument() throws EXistException, PermissionDeniedException, XPathException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final XQuery xquery = pool.getXQueryService();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final Sequence sequence = xquery.execute(broker, DOCUMENT_XSLT_QUERY, null);
        assertNotNull(sequence);
        assertTrue(sequence.hasOne());
        final Item item = sequence.itemAt(0);
        assertEquals(Type.DOCUMENT, item.getType());
        final Source expected = Input.fromString("<elem1/>").build();
        final Source actual = Input.fromDocument(sequence.itemAt(0).toJavaObject(Document.class)).build();
        final Diff diff = DiffBuilder.compare(actual).withTest(expected).checkForSimilar().build();
        assertFalse(diff.toString(), diff.hasDifferences());
    }
}
Also used : Item(org.exist.xquery.value.Item) DBBroker(org.exist.storage.DBBroker) Diff(org.xmlunit.diff.Diff) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) StringInputSource(org.exist.util.StringInputSource) Source(javax.xml.transform.Source)

Example 8 with Item

use of org.exist.xquery.value.Item 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)

Example 9 with Item

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

the class SendRequestFunction method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    Sequence bodies = Sequence.EMPTY_SEQUENCE;
    String href = null;
    NodeValue request = null;
    switch(getArgumentCount()) {
        case 3:
            bodies = args[2];
        case 2:
            {
                Item i = args[1].itemAt(0);
                if (i != null) {
                    href = i.getStringValue();
                }
            }
        case 1:
            request = (NodeValue) args[0].itemAt(0);
            break;
        default:
            return Sequence.EMPTY_SEQUENCE;
    }
    return sendRequest(request, href, bodies);
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) Item(org.exist.xquery.value.Item) EXistSequence(org.expath.tools.model.exist.EXistSequence) Sequence(org.exist.xquery.value.Sequence)

Example 10 with Item

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

the class FunMin 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());
        }
    }
    boolean computableProcessing = false;
    Sequence result;
    final Sequence arg = getArgument(0).eval(contextSequence, contextItem);
    if (arg.isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        // TODO : test if a range index is defined *iff* it is compatible with the collator
        final Collator collator = getCollator(contextSequence, contextItem, 2);
        final SequenceIterator iter = arg.unorderedIterator();
        AtomicValue min = null;
        while (iter.hasNext()) {
            final Item item = iter.nextItem();
            if (item instanceof QNameValue) {
                throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(item.getType()), arg);
            }
            AtomicValue value = item.atomize();
            // Duration values must either all be xs:yearMonthDuration values or must all be xs:dayTimeDuration values.
            if (Type.subTypeOf(value.getType(), Type.DURATION)) {
                value = ((DurationValue) value).wrap();
                if (value.getType() == Type.YEAR_MONTH_DURATION) {
                    if (min != null && min.getType() != Type.YEAR_MONTH_DURATION) {
                        throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) + " and " + Type.getTypeName(value.getType()), value);
                    }
                } else if (value.getType() == Type.DAY_TIME_DURATION) {
                    if (min != null && min.getType() != Type.DAY_TIME_DURATION) {
                        throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) + " and " + Type.getTypeName(value.getType()), value);
                    }
                } else {
                    throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(value.getType()), value);
                }
            // Any value of type xdt:untypedAtomic is cast to xs:double
            } else if (value.getType() == Type.UNTYPED_ATOMIC) {
                value = value.convertTo(Type.DOUBLE);
            }
            if (min == null) {
                min = value;
            } else {
                if (Type.getCommonSuperType(min.getType(), value.getType()) == Type.ATOMIC) {
                    throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) + " and " + Type.getTypeName(value.getType()), value);
                }
                // Any value of type xdt:untypedAtomic is cast to xs:double
                if (value.getType() == Type.ATOMIC) {
                    value = value.convertTo(Type.DOUBLE);
                }
                // Numeric tests
                if (Type.subTypeOfUnion(value.getType(), Type.NUMBER)) {
                    // Don't mix comparisons
                    if (!Type.subTypeOfUnion(min.getType(), Type.NUMBER)) {
                        throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) + " and " + Type.getTypeName(value.getType()), min);
                    }
                    if (((NumericValue) value).isNaN()) {
                        // Type NaN correctly
                        value = value.promote(min);
                        if (value.getType() == Type.FLOAT) {
                            min = FloatValue.NaN;
                        } else {
                            min = DoubleValue.NaN;
                        }
                        // although result will be NaN, we need to continue on order to type correctly
                        continue;
                    }
                    min = min.promote(value);
                }
                // Ugly test
                if (value instanceof ComputableValue) {
                    if (!(min instanceof ComputableValue)) {
                        throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) + " and " + Type.getTypeName(value.getType()), min);
                    }
                    // Type value correctly
                    value = value.promote(min);
                    min = min.min(collator, value);
                    computableProcessing = true;
                } else {
                    if (computableProcessing) {
                        throw new XPathException(this, ErrorCodes.FORG0006, "Cannot compare " + Type.getTypeName(min.getType()) + " and " + Type.getTypeName(value.getType()), value);
                    }
                    if (Collations.compare(collator, value.getStringValue(), min.getStringValue()) < 0) {
                        min = value;
                    }
                }
            }
        }
        result = min;
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : Item(org.exist.xquery.value.Item) ComputableValue(org.exist.xquery.value.ComputableValue) SequenceIterator(org.exist.xquery.value.SequenceIterator) XPathException(org.exist.xquery.XPathException) QNameValue(org.exist.xquery.value.QNameValue) AtomicValue(org.exist.xquery.value.AtomicValue) Sequence(org.exist.xquery.value.Sequence) NumericValue(org.exist.xquery.value.NumericValue) Collator(com.ibm.icu.text.Collator)

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