// javadoc inherited
protected Object functionConcat(EvalContext context) {
if (getArgumentCount() < 2) {
// the concat function must be invoked with 2 or more arguments
throw new JXPathException(
"concat function must have at least two arguments. " +
"Actual argument count is " + getArgumentCount());
}
// use a string buffer to perform the concatenation
StringBuffer buffer = new StringBuffer();
Expression[] operands = getArguments();
Sequence sequence;
try {
for (int i = 0; i < operands.length; i++) {
// evaluate the operand and convert it to a sequence
sequence = JXPathExpression.asValue(
factory,
operands[i].computeValue(context)).getSequence();
int length = sequence.getLength();
// 1) If sequence is empty then we append the empty string.
// 2) If sequence has one item we convert the item to a string
// and append to the buffer
// 3) If sequence contains more that one item we raise an error
if (length == 1) {
buffer.append(
sequence.getItem(1).stringValue().asJavaString());
} else if (length > 1) {
throw new JXPathException(
"fn:concat cannot accept a sequence operand " +
"with more than one item");
}
}
} catch (ExpressionException e) {
// Tunnel the exception out in a JXPath runtime exception
throw new JXPathException(
"Illegal argument to fn:concat(): " + e);
}
// return the concatenated arguments as a StringValue
return factory.createStringValue(buffer.toString());
}