use of org.exist.xquery.value.SequenceIterator in project exist by eXist-db.
the class FunIndexOf method eval.
/* (non-Javadoc)
* @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
*/
public Sequence eval(Sequence[] args, Sequence contextSequence) 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);
}
}
Sequence result;
if (args[0].isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
} else {
final AtomicValue srch = args[1].itemAt(0).atomize();
Collator collator;
if (getSignature().getArgumentCount() == 3) {
final String collation = args[2].getStringValue();
collator = context.getCollator(collation);
} else {
collator = context.getDefaultCollator();
}
result = new ValueSequence();
int j = 1;
for (final SequenceIterator i = args[0].iterate(); i.hasNext(); j++) {
final AtomicValue next = i.nextItem().atomize();
try {
if (ValueComparison.compareAtomic(collator, next, srch, StringTruncationOperator.NONE, Comparison.EQ)) {
result.add(new IntegerValue(j));
}
} catch (final XPathException e) {
// Ignore me : values can not be compared
}
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.SequenceIterator in project exist by eXist-db.
the class FunAvg 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());
}
}
Sequence result;
final Sequence inner = getArgument(0).eval(contextSequence, contextItem);
if (inner.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else {
final SequenceIterator iter = inner.iterate();
Item item = iter.nextItem();
AtomicValue value = item.atomize();
// Any values of type xdt:untypedAtomic are cast to xs:double
if (value.getType() == Type.UNTYPED_ATOMIC) {
value = value.convertTo(Type.DOUBLE);
}
if (!(value instanceof ComputableValue)) {
throw new XPathException(this, ErrorCodes.FORG0006, Type.getTypeName(value.getType()) + "(" + value + ") " + "can not be an operand in a sum", value);
}
// Set the first value
ComputableValue sum = (ComputableValue) value;
while (iter.hasNext()) {
item = iter.nextItem();
value = item.atomize();
// Any value of type xdt:untypedAtomic are cast to xs:double
if (value.getType() == Type.UNTYPED_ATOMIC) {
value = value.convertTo(Type.DOUBLE);
}
if (!(value instanceof ComputableValue)) {
throw new XPathException(this, ErrorCodes.FORG0006, "" + Type.getTypeName(value.getType()) + "(" + value + ") can not be an operand in a sum", value);
}
if (Type.subTypeOfUnion(value.getType(), Type.NUMBER)) {
if (((NumericValue) value).isInfinite()) {
gotInfinity = true;
}
if (((NumericValue) value).isNaN()) {
sum = DoubleValue.NaN;
break;
}
}
try {
sum = (ComputableValue) sum.promote(value);
// Aggregate next values
sum = sum.plus((ComputableValue) value);
} catch (final XPathException e) {
throw new XPathException(this, ErrorCodes.FORG0006, e.getMessage());
}
}
result = sum.div(new IntegerValue(inner.getItemCount()));
}
if (!gotInfinity) {
if (Type.subTypeOfUnion(result.getItemType(), Type.NUMBER) && ((NumericValue) result).isInfinite()) {
// Throw an overflow exception here since we get an infinity
// whereas is hasn't been provided by the sequence
// TODO ? -pb
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.SequenceIterator in project exist by eXist-db.
the class FunDistinctValues method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.xquery.StaticContext, 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());
}
}
final Sequence seq = getArgument(0).eval(contextSequence, contextItem);
final Collator collator = getCollator(contextSequence, contextItem, 2);
final TreeSet<AtomicValue> set = new TreeSet<>(new ValueComparator(collator));
final ValueSequence result = new ValueSequence();
Item item;
AtomicValue value;
boolean hasAlreadyNaN = false;
for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
item = i.nextItem();
value = item.atomize();
if (!set.contains(value)) {
if (Type.subTypeOfUnion(value.getType(), Type.NUMBER)) {
if (((NumericValue) value).isNaN()) {
// contains multiple NaN values a single NaN is returned.
if (!hasAlreadyNaN) {
set.add(value);
result.add(value);
hasAlreadyNaN = true;
}
} else {
set.add(value);
result.add(value);
}
} else {
set.add(value);
result.add(value);
}
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.SequenceIterator in project exist by eXist-db.
the class FunIdRef method getIdRef.
private void getIdRef(Sequence result, Sequence seq, String id) throws XPathException {
final Set<org.exist.dom.memtree.DocumentImpl> visitedDocs = new TreeSet<>();
for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
final org.exist.dom.memtree.NodeImpl v = (org.exist.dom.memtree.NodeImpl) i.nextItem();
final org.exist.dom.memtree.DocumentImpl doc = v.getOwnerDocument();
if (!visitedDocs.contains(doc)) {
final org.exist.dom.memtree.NodeImpl node = doc.selectByIdref(id);
if (node != null) {
result.add(node);
}
visitedDocs.add(doc);
}
}
}
use of org.exist.xquery.value.SequenceIterator in project exist by eXist-db.
the class NodeProxyTest method iterate_loop.
@Test
public void iterate_loop() {
final NodeProxy mockNodeProxy = new NodeProxy(null, null, Node.ELEMENT_NODE, -1);
final SequenceIterator it = mockNodeProxy.iterate();
int count = 0;
while (it.hasNext()) {
it.nextItem();
count++;
}
assertEquals(1, count);
}
Aggregations