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;
}
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);
}
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;
}
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;
}
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;
}
Aggregations