public void process(XPathContext context) throws XPathException {
// This rather tortuous code is designed to ensure that we don't evaluate the
// separator argument unless there are at least two items in the sequence.
SequenceReceiver out = context.getReceiver();
if (out instanceof ComplexContentOutputter) {
// Optimization is only safe if evaluated as part of a complex content constructor
// Start and end with an empty string to force space separation from any previous or following outputs
out.append(StringValue.EMPTY_STRING, 0);
SequenceIterator iter = argument[0].iterate(context);
Item it = iter.next();
if (it == null) {
return;
}
CharSequence first = it.getStringValueCS();
out.characters(first);
it = iter.next();
if (it == null) {
out.append(StringValue.EMPTY_STRING, 0);
return;
}
// Type checking ensures that the separator is not an empty sequence
if (argument.length == 1) {
out.characters(it.getStringValueCS());
while (true) {
it = iter.next();
if (it == null) {
break;
}
out.characters(it.getStringValueCS());
}
} else {
CharSequence sep = argument[1].evaluateItem(context).getStringValueCS();
out.characters(sep);
out.characters(it.getStringValueCS());
while (true) {
it = iter.next();
if (it == null) {
break;
}
out.characters(sep);
out.characters(it.getStringValueCS());
}
}
out.append(StringValue.EMPTY_STRING, 0);
} else {
out.append(evaluateItem(context), 0);
}
}