Good Morning,
I just noticed, that the Custom ReplacingCallback is ment to fit to the problem i described. Somehow i overread this possibility at a first glance…
Hi Robert,
Thanks Awais for your reply,
Hi Rob,
static class ReplaceEvaluatorFindAndReplace implements IReplacingCallback {<font color="RED"><b>public</b></font> <font color="RED"><b>int</b></font> replacing<font color="BLUE"><b>(</b></font>ReplacingArgs e<font color="BLUE"><b>)</b></font> <font color="RED"><b>throws</b></font> Exception <font color="BLUE"><b>{</b></font> Node currentNode <font color="BLUE">=</font> e<font color="BLUE"><b>.</b></font>getMatchNode<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font> <font color="GREEN"><i>// The first (and may be the only) run can contain text before the match,
// in this case it is necessary to split the run.
if (e.getMatchOffset() > 0)
currentNode = SplitRun((Run) currentNode, e.getMatchOffset());<font color="GREEN"><i>// This array is used to store all nodes of the match for further removing.
ArrayList runs = new ArrayList();
<font color="GREEN"><i>// Find all runs that contain parts of the match string.
int remainingLength = e.getMatch().group(0).length();
while (
(remainingLength > 0) &&
(currentNode != null) &&
(currentNode.getText().length() <= remainingLength)) {
runs.add(currentNode);
remainingLength = remainingLength - currentNode.getText().length();<font color="GREEN"><i>// Select the next Run node.
// Have to loop because there could be other nodes such as BookmarkStart etc.
do {
currentNode = currentNode.getNextSibling();
}
while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));
}<font color="GREEN"><i>// Split the last run that contains the match if there is any text left.
if ((currentNode != null) && (remainingLength > 0)) {
SplitRun((Run) currentNode, remainingLength);
runs.add(currentNode);
}DocumentBuilder builder <font color="BLUE">=</font> <font color="RED"><b>new</b></font> DocumentBuilder<font color="BLUE"><b>(</b></font><font color="BLUE"><b>(</b></font>Document<font color="BLUE"><b>)</b></font> e<font color="BLUE"><b>.</b></font>getMatchNode<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>getDocument<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font> builder<font color="BLUE"><b>.</b></font>moveTo<font color="BLUE"><b>(</b></font><font color="BLUE"><b>(</b></font>Run<font color="BLUE"><b>)</b></font> runs<font color="BLUE"><b>.</b></font>get<font color="BLUE"><b>(</b></font>runs<font color="BLUE"><b>.</b></font>size<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font> <font color="BLUE">-</font> <font color="BROWN">1</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font> builder<font color="BLUE"><b>.</b></font>write<font color="BLUE"><b>(</b></font><font color="PURPLE">"new value"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font> <font color="GREEN"><i>//Now remove all runs in the sequence.
for (Run run : (Iterable<Run>) runs)
run.remove();<font color="RED"><b>return</b></font> ReplaceAction<font color="BLUE"><b>.</b></font>SKIP<font color="BLUE"><b>;</b></font> <font color="BLUE"><b>}</b></font>
}
private static Run SplitRun(Run run, int position) throws Exception {
Run afterRun = (Run) run.deepClone(true);
afterRun.setText(run.getText().substring(position));
run.setText(run.getText().substring(0, position));
run.getParentNode().insertAfter(afterRun, run);
return afterRun;
}