I am working on a Java application which is for note-taking. Now, whenever the user edits the text in the note, I want to find the difference between the oldText and the newText so I can add it to history of that note.
For this I am dividing each paragraph into multiple strings by splitting them at dot. Then I am comparing the sentences in that String-list using diff-match-patch.
As of now it works fine for add, edit in the text, but as soon as I delete a sentence, it gives a problem.
Situation is
old text : sentence1, sentence2, sentence3, sentence4
new Text : sentence1, sentence3, sentence4.
But because of that, the comparator sees that sentence2 is replaced by sentence3, sentence3 by sentence4 and so-on and so-forth.
This is not the desired behaviour, but I don't know how to remedy the situation. I will post my code, kindly let me know how I can just get differences between them properly.
GroupNoteHistory is the object in which I am saving the oldText and the newText changes only. I hope my code is understandable.
// Below is List of oldText and newText splitted at dot.
List<String> oldTextList = Arrays.asList(mnotes1.getMnotetext().split("(\\.|\\n)"));
List<String> newTextList = Arrays.asList(mnotes.getMnotetext().split("(\\.|\\n)"));
// Calculating the size of loop.
int counter = Math.max(oldTextList.size(), newTextList.size());
String oldString;
String newString;
for (int current = 0; current < counter; current++) {
oldString = "";
newString = "";
if (oldTextList.size() <= current) {
oldString = "";
newString = newTextList.get(current);
} else if (newTextList.size() <= current) {
oldString = oldTextList.get(current);
newString = "";
} else {
// isLineDifferent comes from diff_match_patch
if (isLineDifferent(oldTextList.get(current), newTextList.get(current))) {
noEdit = true;
groupNoteHistory.setWhatHasChanged("textchange");
oldString += oldTextList.get(current);
newString += newTextList.get(current);
}
}
if (oldString != null && newString != null) {
if (!(groupNoteHistory.getNewNoteText() == null)) {
if (!(newString.isEmpty())) {
groupNoteHistory.setNewNoteText(groupNoteHistory.getNewNoteText() + " " + newString);
}
} else {
groupNoteHistory.setNewNoteText(newString);
}
if (!(groupNoteHistory.getOldText() == null)) {
if (!(oldString.isEmpty())) {
groupNoteHistory.setOldText(groupNoteHistory.getOldText() + " " + oldString);
}
} else {
groupNoteHistory.setOldText(oldString);
}
}
Kindly let me know what I can do. Thanks a lot. :-)