use of org.exist.xquery.value.AtomicValue in project exist by eXist-db.
the class AttributeConstructor method evalEnclosedExpr.
private void evalEnclosedExpr(Sequence seq, StringBuilder buf) throws XPathException {
Item item;
AtomicValue atomic;
for (final SequenceIterator i = Atomize.atomize(seq).iterate(); i.hasNext(); ) {
item = i.nextItem();
buf.append(item.getStringValue());
if (i.hasNext()) {
buf.append(' ');
}
}
}
use of org.exist.xquery.value.AtomicValue 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.AtomicValue 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.AtomicValue in project exist by eXist-db.
the class FunDeepEqual method deepEquals.
public static boolean deepEquals(Item a, Item b, Collator collator) {
try {
if (a.getType() == Type.ARRAY || b.getType() == Type.ARRAY) {
if (a.getType() != b.getType()) {
return false;
}
final ArrayType ar = (ArrayType) a;
final ArrayType br = (ArrayType) b;
if (ar.getSize() != br.getSize()) {
return false;
}
for (int i = 0; i < ar.getSize(); i++) {
if (!deepEqualsSeq(ar.get(i), br.get(i), collator)) {
return false;
}
}
return true;
}
if (a.getType() == Type.MAP || b.getType() == Type.MAP) {
if (a.getType() != b.getType()) {
return false;
}
final AbstractMapType amap = (AbstractMapType) a;
final AbstractMapType bmap = (AbstractMapType) b;
if (amap.size() != bmap.size()) {
return false;
}
for (final IEntry<AtomicValue, Sequence> aentry : amap) {
if (!bmap.contains(aentry.key())) {
return false;
}
if (!deepEqualsSeq(aentry.value(), bmap.get(aentry.key()), collator)) {
return false;
}
}
return true;
}
final boolean aAtomic = Type.subTypeOf(a.getType(), Type.ATOMIC);
final boolean bAtomic = Type.subTypeOf(b.getType(), Type.ATOMIC);
if (aAtomic || bAtomic) {
if (!aAtomic || !bAtomic) {
return false;
}
try {
final AtomicValue av = (AtomicValue) a;
final AtomicValue bv = (AtomicValue) b;
if (Type.subTypeOfUnion(av.getType(), Type.NUMBER) && Type.subTypeOfUnion(bv.getType(), Type.NUMBER)) {
// or if both values are NaN
if (((NumericValue) a).isNaN() && ((NumericValue) b).isNaN()) {
return true;
}
}
return ValueComparison.compareAtomic(collator, av, bv, StringTruncationOperator.NONE, Comparison.EQ);
} catch (final XPathException e) {
return false;
}
}
if (a.getType() != b.getType()) {
return false;
}
final NodeValue nva = (NodeValue) a;
final NodeValue nvb = (NodeValue) b;
if (nva == nvb) {
return true;
}
try {
// since the symbol table is ignored.
if (nva.getImplementationType() != NodeValue.IN_MEMORY_NODE && nva.equals(nvb)) // shortcut!
{
return true;
}
} catch (final XPathException e) {
// apparently incompatible values, do manual comparison
}
final Node na;
final Node nb;
switch(a.getType()) {
case Type.DOCUMENT:
// NodeValue.getNode() doesn't seem to work for document nodes
na = nva instanceof Node ? (Node) nva : ((NodeProxy) nva).getOwnerDocument();
nb = nvb instanceof Node ? (Node) nvb : ((NodeProxy) nvb).getOwnerDocument();
return compareContents(na, nb);
case Type.ELEMENT:
na = nva.getNode();
nb = nvb.getNode();
return compareElements(na, nb);
case Type.ATTRIBUTE:
na = nva.getNode();
nb = nvb.getNode();
return compareNames(na, nb) && safeEquals(na.getNodeValue(), nb.getNodeValue());
case Type.PROCESSING_INSTRUCTION:
case Type.NAMESPACE:
na = nva.getNode();
nb = nvb.getNode();
return safeEquals(na.getNodeName(), nb.getNodeName()) && safeEquals(nva.getStringValue(), nvb.getStringValue());
case Type.TEXT:
case Type.COMMENT:
return safeEquals(nva.getStringValue(), nvb.getStringValue());
default:
throw new RuntimeException("unexpected item type " + Type.getTypeName(a.getType()));
}
} catch (final XPathException e) {
logger.error(e.getMessage());
e.printStackTrace();
return false;
}
}
use of org.exist.xquery.value.AtomicValue 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;
}
Aggregations