}
private final void processText(MemTreeBuilder builder, NodeProxy proxy, Sequence result,
FunctionReference callback, Sequence extraArgs)
throws DOMException, XPathException {
final TextImpl text = (TextImpl) proxy.getNode();
final Match match = proxy.getMatches();
int nodeNr;
if (match == null) {
nodeNr = builder.characters(text.getXMLString());
result.add(builder.getDocument().getNode(nodeNr));
} else {
List<Match.Offset> offsets = null;
Match next = match;
while (next != null) {
if (next.getNodeId().equals(text.getNodeId())) {
if (offsets == null)
{offsets = new ArrayList<Match.Offset>();}
final int freq = next.getFrequency();
for (int i = 0; i < freq; i++) {
offsets.add(next.getOffset(i));
}
}
next = next.getNextMatch();
}
if (offsets != null) {
FastQSort.sort(offsets, 0, offsets.size() - 1);
final XMLString str = text.getXMLString();
int pos = 0;
for (final Match.Offset offset : offsets) {
if (offset.getOffset() > pos) {
nodeNr = builder.characters(str.substring(pos, offset.getOffset() - pos));
result.add(builder.getDocument().getNode(nodeNr));
}
final Sequence params[] = {
new StringValue(str.substring(offset.getOffset(), offset.getLength())),
proxy,
extraArgs
};
result.addAll(callback.evalFunction(null, null, params));
pos = offset.getOffset() + offset.getLength();
}
if (pos < str.length()) {
nodeNr = builder.characters(str.substring(pos, str.length() - pos));
result.add(builder.getDocument().getNode(nodeNr));
}
} else {
nodeNr = builder.characters(text.getXMLString());
result.add(builder.getDocument().getNode(nodeNr));
}
}
}