public Sequence eval(Sequence<? extends Item> contextSeq, ValueSequence argv, DynamicContext dynEnv)
throws XQueryException {
final Item contextItem = dynEnv.contextItem();
if(argv == null) {
if(contextItem == null) {
throw new DynamicError("err:XPDY0002", "ContextItem is not set");
} else {
// If no argument is supplied, this function returns the string value
// of the context item (.).
final String sv = contextItem.stringValue();
return XString.valueOf(sv);
}
}
// If $arg is the empty sequence, the zero-length string is returned.
if(argv.isEmpty()) {
return XString.valueOf("");
}
final Item arg = argv.getItem(0);
final IFocus<? extends Item> argItor = arg.iterator();
if(argItor.hasNext()) {
final Item firstItem = argItor.next();
final Sequence ret;
if(firstItem instanceof XQNode) {
// If $arg is a node, the function returns the string-value of the node.
final String sv = ((XQNode) firstItem).stringValue();
ret = XString.valueOf(sv);
} else if(firstItem instanceof AtomicValue) {
// If $arg is an atomic value, then the function returns the same string as
// is returned by the expression "$arg cast as xs:string".
final XString sv = ((AtomicValue) firstItem).<XString> castAs(StringType.STRING, dynEnv);
ret = sv;
} else {
argItor.closeQuietly();
throw new IllegalStateException("Illegal argument type: "
+ arg.getClass().getName());
}
if(argItor.hasNext()) {
argItor.closeQuietly();
throw new DynamicError("argument should have zero or one element.");
}
argItor.closeQuietly();
return ret;
} else {
argItor.closeQuietly();