use of org.exist.xquery.value.IntegerValue in project exist by eXist-db.
the class FunOnFunctions method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
try {
if (isCalledAs("function-lookup")) {
final QName fname = ((QNameValue) args[0].itemAt(0)).getQName();
final int arity = ((IntegerValue) args[1].itemAt(0)).getInt();
FunctionCall call;
try {
call = NamedFunctionReference.lookupFunction(this, context, fname, arity);
} catch (final XPathException e) {
if (e.getErrorCode() == ErrorCodes.XPST0017) {
// return empty sequence for all "function not found" related errors
return Sequence.EMPTY_SEQUENCE;
}
throw e;
}
return call == null ? Sequence.EMPTY_SEQUENCE : new FunctionReference(call);
} else if (isCalledAs("function-name")) {
final FunctionReference ref = (FunctionReference) args[0].itemAt(0);
final QName qname = ref.getSignature().getName();
if (qname == null || qname == InlineFunction.INLINE_FUNCTION_QNAME) {
return Sequence.EMPTY_SEQUENCE;
} else {
return new QNameValue(context, qname);
}
} else {
// isCalledAs("function-arity")
final FunctionReference ref = (FunctionReference) args[0].itemAt(0);
return new IntegerValue(ref.getSignature().getArgumentCount());
}
} catch (final Exception e) {
if (e instanceof XPathException) {
throw (XPathException) e;
} else {
throw new XPathException(this, ErrorCodes.XPST0017, e.getMessage());
}
}
}
use of org.exist.xquery.value.IntegerValue in project exist by eXist-db.
the class FunPosition method eval.
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 inSequence = context.getContextSequence();
if (inSequence == null) {
inSequence = contextSequence;
}
if (inSequence == null) {
throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
}
Sequence result;
if (inSequence.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else {
result = new IntegerValue(context.getContextPosition() + 1);
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.IntegerValue in project exist by eXist-db.
the class FunRoundHalfToEven method eval.
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;
IntegerValue precision = null;
final Sequence seq = getArgument(0).eval(contextSequence, contextItem);
if (seq.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else {
if (contextItem != null) {
contextSequence = contextItem.toSequence();
}
if (getSignature().getArgumentCount() > 1) {
precision = (IntegerValue) getArgument(1).eval(contextSequence, contextItem).itemAt(0).convertTo(Type.INTEGER);
}
final Item item = seq.itemAt(0);
NumericValue value;
if (item instanceof NumericValue) {
value = (NumericValue) item;
} else {
value = (NumericValue) item.convertTo(Type.NUMBER);
}
result = value.round(precision);
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.IntegerValue in project exist by eXist-db.
the class BaseConversionFunctions method eval.
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
if (isCalledAs(qnIntToOctal.getLocalPart())) {
final int i = args[0].toJavaObject(Integer.class);
final String octal = i == 0 ? "0" : "0" + Integer.toOctalString(i);
return new StringValue(octal);
} else if (isCalledAs(qnOctalToInt.getLocalPart())) {
final String octal = args[0].toString();
return new IntegerValue(Integer.parseInt(octal, 8));
} else {
throw new XPathException("Unknown function call: " + getSignature());
}
}
use of org.exist.xquery.value.IntegerValue in project exist by eXist-db.
the class BaseConverter 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[] args, Sequence contextSequence) throws XPathException {
int intValue;
String stringValue;
final String number = args[0].itemAt(0).getStringValue();
final int intBase = ((IntegerValue) args[1].itemAt(0)).getInt();
if (isCalledAs("base-to-integer")) {
intValue = Integer.parseInt(number, intBase);
return new IntegerValue(intValue);
} else {
switch(Base.getBase(intBase)) {
case BINARY:
stringValue = Integer.toBinaryString(Integer.parseInt(number));
break;
case OCTAL:
stringValue = Integer.toOctalString(Integer.parseInt(number));
break;
// break;
case HEXADECIMAL:
stringValue = Integer.toHexString(Integer.parseInt(number));
break;
default:
{
logger.error("Unhandled base for conversion target in integer-to-base().");
throw new XPathException("Unhandled base for conversion target in integer-to-base().");
}
}
return new StringValue(stringValue);
}
}
Aggregations