diff --git a/java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java b/java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java index 06a61073..e926560d 100644 --- a/java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java +++ b/java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java @@ -19,11 +19,15 @@ import com.github.difflib.patch.Chunk; import com.github.difflib.patch.AbstractDelta; import com.github.difflib.patch.Patch; + import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; /** * @@ -313,4 +317,151 @@ private static List getDeltaText(AbstractDelta delta) { private UnifiedDiffUtils() { } + + /** + * Compare the differences between two files and return to the original file and diff format + * + * (This method compares the original file with the comparison file to obtain a diff, and inserts the diff into the corresponding position of the original file. + * You can see all the differences and unmodified places from the original file. + * Also, this will be very easy and useful for making side-by-side comparison display applications, + * for example, if you use diff2html (https://github.com/rtfpessoa/diff2html#usage) + * Wait for tools to display your differences on html pages, you only need to insert the return value into your js code) + * + * @param original Original file content + * @param revised revised file content + * + */ + public static List generateOriginalAndDiff(List original, List revised) { + return generateOriginalAndDiff(original, revised, null, null); + } + + + /** + * Compare the differences between two files and return to the original file and diff format + * + * (This method compares the original file with the comparison file to obtain a diff, and inserts the diff into the corresponding position of the original file. + * You can see all the differences and unmodified places from the original file. + * Also, this will be very easy and useful for making side-by-side comparison display applications, + * for example, if you use diff2html (https://github.com/rtfpessoa/diff2html#usage) + * Wait for tools to display your differences on html pages, you only need to insert the return value into your js code) + * + * @param original Original file content + * @param revised revised file content + * @param originalFileName Original file name + * @param revisedFileName revised file name + */ + public static List generateOriginalAndDiff(List original, List revised, String originalFileName, String revisedFileName) { + String originalFileNameTemp = originalFileName; + String revisedFileNameTemp = originalFileName; + if (originalFileNameTemp == null) { + originalFileNameTemp = "original"; + } + if (revisedFileNameTemp == null) { + revisedFileNameTemp = "revised"; + } + Patch patch = DiffUtils.diff(original, revised); + List unifiedDiff = generateUnifiedDiff(originalFileNameTemp, revisedFileNameTemp, original, patch, 0); + if (unifiedDiff.isEmpty()) { + unifiedDiff.add("--- " + originalFileNameTemp); + unifiedDiff.add("+++ " + revisedFileNameTemp); + unifiedDiff.add("@@ -0,0 +0,0 @@"); + } else if (unifiedDiff.size() >= 3 && !unifiedDiff.get(2).contains("@@ -1,")) { + unifiedDiff.set(1, unifiedDiff.get(1)); + unifiedDiff.add(2, "@@ -0,0 +0,0 @@"); + } + List originalWithPrefix = original.stream().map(v -> " " + v).collect(Collectors.toList()); + return insertOrig(originalWithPrefix, unifiedDiff); + } + + //Insert the diff format to the original file + private static List insertOrig(List original, List unifiedDiff) { + List result = new ArrayList<>(); + List> diffList = new ArrayList<>(); + List diff = new ArrayList<>(); + for (int i = 0; i < unifiedDiff.size(); i++) { + String u = unifiedDiff.get(i); + if (u.startsWith("@@") && !"@@ -0,0 +0,0 @@".equals(u) && !u.contains("@@ -1,")) { + List twoList = new ArrayList<>(); + twoList.addAll(diff); + diffList.add(twoList); + diff.clear(); + diff.add(u); + continue; + } + if (i == unifiedDiff.size() - 1) { + diff.add(u); + List twoList = new ArrayList<>(); + twoList.addAll(diff); + diffList.add(twoList); + diff.clear(); + break; + } + diff.add(u); + } + insertOrig(diffList, result, original); + return result; + } + + //Insert the diff format to the original file + private static void insertOrig(List> diffList, List result, List original) { + for (int i = 0; i < diffList.size(); i++) { + List diff = diffList.get(i); + List nexDiff = i == diffList.size() - 1 ? null : diffList.get(i + 1); + String simb = i == 0 ? diff.get(2) : diff.get(0); + String nexSimb = nexDiff == null ? null : nexDiff.get(0); + insert(result, diff); + Map map = getRowMap(simb); + if (null != nexSimb) { + Map nexMap = getRowMap(nexSimb); + int start = 0; + if (map.get("orgRow") != 0) { + start = map.get("orgRow") + map.get("orgDel") - 1; + } + int end = nexMap.get("revRow") - 2; + insert(result, getOrigList(original, start, end)); + } + if (simb.contains("@@ -1,") && null == nexSimb && map.get("orgDel") != original.size()) { + insert(result, getOrigList(original, 0, original.size() - 1)); + } else if (null == nexSimb && (map.get("orgRow") + map.get("orgDel") - 1) < original.size()) { + int start = map.get("orgRow") + map.get("orgDel") - 1; + start = start == -1 ? 0 : start; + insert(result, getOrigList(original, start, original.size() - 1)); + } + } + } + + //Insert the unchanged content in the source file into result + private static void insert(List result, List noChangeContent) { + for (String ins : noChangeContent) { + result.add(ins); + } + } + + //Parse the line containing @@ to get the modified line number to delete or add a few lines + private static Map getRowMap(String str) { + Map map = new HashMap<>(); + if (str.startsWith("@@")) { + String[] sp = str.split(" "); + String org = sp[1]; + String[] orgSp = org.split(","); + map.put("orgRow", Integer.valueOf(orgSp[0].substring(1))); + map.put("orgDel", Integer.valueOf(orgSp[1])); + String[] revSp = org.split(","); + map.put("revRow", Integer.valueOf(revSp[0].substring(1))); + map.put("revAdd", Integer.valueOf(revSp[1])); + } + return map; + } + + //Get the specified part of the line from the original file + private static List getOrigList(List originalWithPrefix, int start, int end) { + List list = new ArrayList<>(); + if (originalWithPrefix.size() >= 1 && start <= end && end < originalWithPrefix.size()) { + int startTemp = start; + for (; startTemp <= end; startTemp++) { + list.add(originalWithPrefix.get(startTemp)); + } + } + return list; + } } diff --git a/java-diff-utils/src/test/java/com/github/difflib/examples/OriginalAndDiffTest.java b/java-diff-utils/src/test/java/com/github/difflib/examples/OriginalAndDiffTest.java new file mode 100644 index 00000000..7f2d3b29 --- /dev/null +++ b/java-diff-utils/src/test/java/com/github/difflib/examples/OriginalAndDiffTest.java @@ -0,0 +1,44 @@ +package com.github.difflib.examples; + +import com.github.difflib.TestConstants; +import com.github.difflib.UnifiedDiffUtils; +import org.junit.jupiter.api.Test; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import static java.util.stream.Collectors.joining; +import static org.junit.jupiter.api.Assertions.fail; + +public class OriginalAndDiffTest { + + @Test + public void testGenerateOriginalAndDiff() { + List origLines = null; + List revLines = null; + try { + origLines = fileToLines(TestConstants.MOCK_FOLDER + "original.txt"); + revLines = fileToLines(TestConstants.MOCK_FOLDER + "revised.txt"); + } catch (IOException e) { + fail(e.getMessage()); + } + + List originalAndDiff = UnifiedDiffUtils.generateOriginalAndDiff(origLines, revLines); + System.out.println(originalAndDiff.stream().collect(joining("\n"))); + } + + public static List fileToLines(String filename) throws FileNotFoundException, IOException { + List lines = new ArrayList<>(); + String line = ""; + try (BufferedReader in = new BufferedReader(new FileReader(filename))) { + while ((line = in.readLine()) != null) { + lines.add(line); + } + } + return lines; + } +}