Basically, i have done my program so that it will display differences in strings and display the whole line. I want to highlight (in a colour) the differences in the line.
Example:
Original at line 5
<rect x="60.01" width="855.38" id="rect_1" y="-244.35" height="641.13" style="stroke-width: 1; stroke: rgb(0, 0, 0); fill: none; "/>
Edited at line 5
<rect x="298.43" width="340.00" y="131.12" height="380.00" id="rect_1" style="stroke-width: 1; stroke: rgb(0, 0, 0); fill: rgb(255, 102, 0); "/>
In this example, the width is different from the ‘original’ from the ‘edited’ version. I would like to be able to highlight that difference and any other difference.
My code so far:
Patch patch = DiffUtils.diff(centralFile, remoteFile);
StringBuffer resultsBuff = new StringBuffer(remoteFileData.length);
for (Delta delta : patch.getDeltas())
{
resultsBuff.append("Original at line " + delta.getOriginal().getPosition() + "\n");
for (Object line : delta.getOriginal().getLines())
{
resultsBuff.append(" " + line + "\n");
}
resultsBuff.append("Edited at line " + delta.getRevised().getPosition() + "\n");
for (Object line : delta.getRevised().getLines())
{
resultsBuff.append(" " + line + "\n");
}
resultsBuff.append("\n");
}
return resultsBuff.toString();
}
That will display two whole lines like the example before (the original and the edited version) I want to be able to highlight the changes that have actually been made, is there any way to do this in Java?
Any particular reason why you are trying to rewrite common tools like WinDiff and Beyond Compare?
In case you really need it:
the main difficulty is finding where the differences stop
<rect x="60.01" width="855.38" id="rect_1" y="-244.35" height="641.13" style="stroke-width: 1; stroke: rgb(0, 0, 0); fill: none; "/><rect x="298.43" width="340.00" y="131.12" height="380.00" id="rect_1" style="stroke-width: 1; stroke: rgb(0, 0, 0); fill: rgb(255, 102, 0); "/>What would you consider to be the difference?
1) changed value of “x” and “width” are easy – they come in the same sequence
2) what do you think happened to “id” and it’s value? how do you want to highlight it?
Assuming that you always know which is the “original”
I suggest you scan the “original” string, tokenise it, match each token separately and remove matched ones. When you exhausted all your “original” tokens only then you start highlighting difference on remainder of “original” and “edited”.