params[0] = new StringValue(str.substring(firstMatch.getOffset(), firstMatch.getOffset() + firstMatch.getLength()));
// if the callback function accepts 4 arguments, the last argument should contain additional
// information on the match:
if (callback.getSignature().getArgumentCount() == 4) {
params[3] = new ValueSequence();
params[3].add(new IntegerValue(nextOffset - 1));
params[3].add(new IntegerValue(firstMatch.getOffset()));
params[3].add(new IntegerValue(firstMatch.getLength()));
}
// now execute the callback func.
final Sequence callbackResult = callback.evalFunction(null, null, params);
// iterate through the result of the callback
for (final SequenceIterator iter = callbackResult.iterate(); iter.hasNext(); ) {
final Item next = iter.nextItem();
if (Type.subTypeOf(next.getType(), Type.NODE)) {
nodeNr = builder.getDocument().getLastNode();
try {
next.copyTo(context.getBroker(), receiver);
result.add(builder.getDocument().getNode(++nodeNr));
lastNodeNr = nodeNr;
} catch (final SAXException e) {
throw new XPathException(this, "Internal error while copying nodes: " + e.getMessage(), e);
}
}
}
currentWidth += firstMatch.getLength();
pos += firstMatch.getLength();
} else
{width = str.length();}
// output the rest of the text and matches
Match.Offset offset;
for (int i = nextOffset; i < offsets.size() && currentWidth < width; i++) {
offset = offsets.get(i);
if (offset.getOffset() > pos) {
int len = offset.getOffset() - pos;
if (currentWidth + len > width)
{len = width - currentWidth;}
nodeNr = builder.characters(str.substring(pos, pos + len));
if (lastNodeNr != nodeNr)
{result.add(builder.getDocument().getNode(nodeNr));}
currentWidth += len;
pos += len;
}
if (currentWidth + offset.getLength() < width) {
// put the matching term into argument 0 of the callback function
params[0] = new StringValue(str.substring(offset.getOffset(), offset.getOffset() + offset.getLength()));
// if the callback function accepts 4 arguments, the last argument should contain additional
// information on the match:
if (callback.getSignature().getArgumentCount() == 4) {
params[3] = new ValueSequence();
params[3].add(new IntegerValue(i));
params[3].add(new IntegerValue(offset.getOffset()));
params[3].add(new IntegerValue(offset.getLength()));
}
// execute the callback function
final Sequence callbackResult = callback.evalFunction(null, null, params);
for (final SequenceIterator iter = callbackResult.iterate(); iter.hasNext(); ) {
final Item next = iter.nextItem();
if (Type.subTypeOf(next.getType(), Type.NODE)) {
nodeNr = builder.getDocument().getLastNode();
try {
next.copyTo(context.getBroker(), receiver);
result.add(builder.getDocument().getNode(++nodeNr));
lastNodeNr = nodeNr;
} catch (final SAXException e) {
throw new XPathException(this, "Internal error while copying nodes: " + e.getMessage(), e);
}
}
}
currentWidth += offset.getLength();
pos += offset.getLength();
} else
{break;}
}
// print the final text chunk if more space is available
if (currentWidth < width && pos < str.length()) {
boolean truncated = false;
int len = str.length() - pos;
if (len > width - currentWidth) {
truncated = true;
len = width - currentWidth;
}
nodeNr = builder.characters(str.substring(pos, pos + len));
if (lastNodeNr != nodeNr)
{result.add(builder.getDocument().getNode(nodeNr));}
lastNodeNr = nodeNr;
currentWidth += len;
if (truncated) {
nodeNr = builder.characters(" ...");
if (lastNodeNr != nodeNr)
{result.add(builder.getDocument().getNode(nodeNr));}
lastNodeNr = nodeNr;
}
}
}
// if the user specified a result callback function, call it now
if (resultCallback != null) {
final Sequence params[] = new Sequence[3];
params[0] = result;
params[1] = new IntegerValue(currentWidth);
params[2] = extraArgs;
return resultCallback.evalFunction(null, null, params);
} else
{return result;}
}