Search in sources :

Example 1 with MapType

use of org.exist.xquery.functions.map.MapType in project exist by eXist-db.

the class JSON method readValue.

private static Item readValue(XQueryContext context, JsonParser parser, Item parent, String handleDuplicates) throws IOException, XPathException {
    JsonToken token;
    Item next = null;
    while ((token = parser.nextValue()) != null) {
        if (token == JsonToken.END_OBJECT || token == JsonToken.END_ARRAY) {
            return parent;
        }
        switch(token) {
            case START_OBJECT:
                next = new MapType(context, null);
                readValue(context, parser, next, handleDuplicates);
                break;
            case START_ARRAY:
                next = new ArrayType(context, Sequence.EMPTY_SEQUENCE);
                readValue(context, parser, next, handleDuplicates);
                break;
            case VALUE_FALSE:
                next = BooleanValue.FALSE;
                break;
            case VALUE_TRUE:
                next = BooleanValue.TRUE;
                break;
            case VALUE_NUMBER_FLOAT:
            case VALUE_NUMBER_INT:
                // according to spec, all numbers are converted to double
                next = new StringValue(parser.getText()).convertTo(Type.DOUBLE);
                break;
            case VALUE_NULL:
                next = null;
                break;
            default:
                next = new StringValue(parser.getText());
                break;
        }
        if (parent != null) {
            switch(parent.getType()) {
                case Type.ARRAY:
                    ((ArrayType) parent).add(next == null ? Sequence.EMPTY_SEQUENCE : next.toSequence());
                    break;
                case Type.MAP:
                    final String currentName = parser.getCurrentName();
                    if (currentName == null) {
                        throw new XPathException(ErrorCodes.FOJS0001, "Invalid JSON object");
                    }
                    final StringValue name = new StringValue(currentName);
                    final MapType map = (MapType) parent;
                    if (map.contains(name)) {
                        // handle duplicate keys
                        if (handleDuplicates.equals(OPTION_DUPLICATES_REJECT)) {
                            throw new XPathException(ErrorCodes.FOJS0003, "Duplicate key: " + currentName);
                        }
                        if (handleDuplicates.equals(OPTION_DUPLICATES_USE_LAST)) {
                            map.add(name, next == null ? Sequence.EMPTY_SEQUENCE : next.toSequence());
                        }
                    } else {
                        map.add(name, next == null ? Sequence.EMPTY_SEQUENCE : next.toSequence());
                    }
                    break;
            }
        }
    }
    return next;
}
Also used : ArrayType(org.exist.xquery.functions.array.ArrayType) JsonToken(com.fasterxml.jackson.core.JsonToken) MapType(org.exist.xquery.functions.map.MapType)

Example 2 with MapType

use of org.exist.xquery.functions.map.MapType in project exist by eXist-db.

the class Facets method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final String dimension = args[1].getStringValue();
    int count = Integer.MAX_VALUE;
    if (getArgumentCount() == 3 && args[2].hasOne()) {
        count = ((IntegerValue) args[2].itemAt(0)).getInt();
    }
    String[] paths = null;
    if (getArgumentCount() == 4 && !args[3].isEmpty()) {
        paths = new String[args[3].getItemCount()];
        int j = 0;
        for (SequenceIterator i = args[3].unorderedIterator(); i.hasNext(); j++) {
            paths[j] = i.nextItem().getStringValue();
        }
    }
    // Find all lucene queries referenced from the input sequence and remember
    // the first match for each. Every query will have its own facets attached,
    // so we have to merge them below.
    final Map<Query, LuceneMatch> luceneQueries = new IdentityHashMap<>();
    for (final SequenceIterator i = args[0].unorderedIterator(); i.hasNext(); ) {
        final NodeValue nv = (NodeValue) i.nextItem();
        if (nv.getImplementationType() == NodeValue.PERSISTENT_NODE) {
            final NodeProxy proxy = (NodeProxy) nv;
            Match match = proxy.getMatches();
            while (match != null) {
                if (match.getIndexId().equals(LuceneIndex.ID)) {
                    final LuceneMatch luceneMatch = (LuceneMatch) match;
                    luceneQueries.putIfAbsent(luceneMatch.getQuery(), luceneMatch);
                }
                match = match.getNextMatch();
            }
        }
    }
    // Iterate the found queries/matches and collect facets for each
    final IMap<AtomicValue, Sequence> map = newLinearMap(null);
    for (LuceneMatch match : luceneQueries.values()) {
        try {
            addFacetsToMap(map, dimension, count, paths, match);
        } catch (IOException e) {
            throw new XPathException(this, LuceneModule.EXXQDYFT0002, e.getMessage());
        }
    }
    return new MapType(context, map.forked(), Type.STRING);
}
Also used : Query(org.apache.lucene.search.Query) IdentityHashMap(java.util.IdentityHashMap) IOException(java.io.IOException) NodeProxy(org.exist.dom.persistent.NodeProxy) MapType(org.exist.xquery.functions.map.MapType) Match(org.exist.dom.persistent.Match) LuceneMatch(org.exist.indexing.lucene.LuceneMatch) LuceneMatch(org.exist.indexing.lucene.LuceneMatch)

Example 3 with MapType

use of org.exist.xquery.functions.map.MapType in project exist by eXist-db.

the class ExtTestErrorFunction method eval.

@Override
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
    final Sequence arg1 = getCurrentArguments()[0];
    final String name = arg1.itemAt(0).getStringValue();
    final Sequence arg2 = getCurrentArguments().length == 2 ? getCurrentArguments()[1] : null;
    final MapType error = arg2 != null ? (MapType) arg2.itemAt(0) : null;
    final Description description = Description.createTestDescription(suiteName, name, new Annotation[0]);
    // notify JUnit
    try {
        final XPathException errorReason = errorMapAsXPathException(error);
        notifier.fireTestFailure(new Failure(description, errorReason));
    } catch (final XPathException e) {
        // signal internal failure
        notifier.fireTestFailure(new Failure(description, e));
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : Description(org.junit.runner.Description) XPathException(org.exist.xquery.XPathException) MapType(org.exist.xquery.functions.map.MapType) Failure(org.junit.runner.notification.Failure)

Example 4 with MapType

use of org.exist.xquery.functions.map.MapType in project exist by eXist-db.

the class ExtTestAssumptionFailedFunction method eval.

@Override
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
    final Sequence arg1 = getCurrentArguments()[0];
    final String name = arg1.itemAt(0).getStringValue();
    final Sequence arg2 = getCurrentArguments().length == 2 ? getCurrentArguments()[1] : null;
    final MapType assumption = arg2 != null ? (MapType) arg2.itemAt(0) : null;
    final Description description = Description.createTestDescription(suiteName, name, new Annotation[0]);
    // notify JUnit
    try {
        final AssumptionViolatedException assumptionFailureReason = assumptionMapAsAssumptionViolationException(assumption);
        // NOTE: We remove the StackTrace, because it is not useful to have a Java Stack Trace pointing into the XML XQuery Test Suite code
        assumptionFailureReason.setStackTrace(new StackTraceElement[0]);
        notifier.fireTestAssumptionFailed(new Failure(description, assumptionFailureReason));
    } catch (final XPathException e) {
        // signal internal failure
        notifier.fireTestFailure(new Failure(description, e));
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : Description(org.junit.runner.Description) AssumptionViolatedException(org.junit.AssumptionViolatedException) XPathException(org.exist.xquery.XPathException) Sequence(org.exist.xquery.value.Sequence) MapType(org.exist.xquery.functions.map.MapType) Failure(org.junit.runner.notification.Failure)

Example 5 with MapType

use of org.exist.xquery.functions.map.MapType in project exist by eXist-db.

the class ExtTestFailureFunction method eval.

@Override
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
    final Sequence arg1 = getCurrentArguments()[0];
    final String name = arg1.itemAt(0).getStringValue();
    final Sequence arg2 = getCurrentArguments()[1];
    final MapType expected = (MapType) arg2.itemAt(0);
    final Sequence arg3 = getCurrentArguments()[2];
    final MapType actual = (MapType) arg3.itemAt(0);
    final Description description = Description.createTestDescription(suiteName, name, new Annotation[0]);
    // notify JUnit
    try {
        final AssertionError failureReason = new ComparisonFailure("", expectedToString(expected), actualToString(actual));
        // NOTE: We remove the StackTrace, because it is not useful to have a Java Stack Trace pointing into the XML XQuery Test Suite code
        failureReason.setStackTrace(new StackTraceElement[0]);
        notifier.fireTestFailure(new Failure(description, failureReason));
    } catch (final XPathException | SAXException | IOException | IllegalStateException e) {
        // signal internal failure
        notifier.fireTestFailure(new Failure(description, e));
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : Description(org.junit.runner.Description) XPathException(org.exist.xquery.XPathException) ComparisonFailure(org.junit.ComparisonFailure) Sequence(org.exist.xquery.value.Sequence) IOException(java.io.IOException) MapType(org.exist.xquery.functions.map.MapType) Failure(org.junit.runner.notification.Failure) ComparisonFailure(org.junit.ComparisonFailure) SAXException(org.xml.sax.SAXException)

Aggregations

MapType (org.exist.xquery.functions.map.MapType)9 XPathException (org.exist.xquery.XPathException)3 Description (org.junit.runner.Description)3 Failure (org.junit.runner.notification.Failure)3 IOException (java.io.IOException)2 AbstractMapType (org.exist.xquery.functions.map.AbstractMapType)2 Sequence (org.exist.xquery.value.Sequence)2 ConsumerE (com.evolvedbinary.j8fu.function.ConsumerE)1 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonToken (com.fasterxml.jackson.core.JsonToken)1 IEntry (io.lacuna.bifurcan.IEntry)1 IMap (io.lacuna.bifurcan.IMap)1 Map (io.lacuna.bifurcan.Map)1 Maps (io.lacuna.bifurcan.Maps)1 StringWriter (java.io.StringWriter)1 java.util (java.util)1 IdentityHashMap (java.util.IdentityHashMap)1 Query (org.apache.lucene.search.Query)1 QName (org.exist.dom.QName)1 Match (org.exist.dom.persistent.Match)1