Skip to content

Commit 281c073

Browse files
1506085843xutao
and
xutao
authored
A problem was found and fixed about #164 (#170)
* Add generateOriginalAndDiff method and test class * Add generateOriginalAndDiff method and test class.I split long code into short methods, changed ambiguous variable names to explicit variable names * fixes #164 * fix build issues * fix issues about (#164),detail: When deleting the first line of comparison text and adding several lines of text, the first diff of the result returned by the generateOriginalAndDiff method is inserted incorrectly * add a test for #170 --------- Co-authored-by: xutao <xutao@apexsoft.com.cn>
1 parent 1664490 commit 281c073

File tree

4 files changed

+227
-3
lines changed

4 files changed

+227
-3
lines changed

java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,11 @@ private static void insertOrig(List<List<String>> diffList, List<String> result,
420420
int end = nexMap.get("revRow") - 2;
421421
insert(result, getOrigList(original, start, end));
422422
}
423+
int start = map.get("orgRow") + map.get("orgDel") - 1;
424+
start = start == -1 ? 0 : start;
423425
if (simb.contains("@@ -1,") && null == nexSimb && map.get("orgDel") != original.size()) {
424-
insert(result, getOrigList(original, 0, original.size() - 1));
426+
insert(result, getOrigList(original, start, original.size() - 1));
425427
} else if (null == nexSimb && (map.get("orgRow") + map.get("orgDel") - 1) < original.size()) {
426-
int start = map.get("orgRow") + map.get("orgDel") - 1;
427-
start = start == -1 ? 0 : start;
428428
insert(result, getOrigList(original, start, original.size() - 1));
429429
}
430430
}

java-diff-utils/src/test/java/com/github/difflib/examples/OriginalAndDiffTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ public void testGenerateOriginalAndDiff() {
3131
System.out.println(originalAndDiff.stream().collect(joining("\n")));
3232
}
3333

34+
@Test
35+
public void testGenerateOriginalAndDiffFirstLineChange() {
36+
List<String> origLines = null;
37+
List<String> revLines = null;
38+
try {
39+
origLines = fileToLines(TestConstants.MOCK_FOLDER + "issue_170_original.txt");
40+
revLines = fileToLines(TestConstants.MOCK_FOLDER + "issue_170_revised.txt");
41+
} catch (IOException e) {
42+
fail(e.getMessage());
43+
}
44+
45+
List<String> originalAndDiff = UnifiedDiffUtils.generateOriginalAndDiff(origLines, revLines);
46+
System.out.println(originalAndDiff.stream().collect(joining("\n")));
47+
}
48+
3449
public static List<String> fileToLines(String filename) throws FileNotFoundException, IOException {
3550
List<String> lines = new ArrayList<>();
3651
String line = "";
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//According to the original text, an html will be generated by comparing the text
2+
public class generateDiffHtmlTest {
3+
/**
4+
* Here's a simple example of getting a nice html page based on the original text and the contrasted text,Read n1.txt and n2.txt of D disk, and finally generate an html file
5+
*
6+
*/
7+
@Test
8+
public static void generateOriginalAndDiffDemo(){
9+
List<String> origLines = getFileContent("D:\\n1.txt");
10+
List<String> revLines =getFileContent("D:\\n2.txt");
11+
List<String> originalAndDiff =UnifiedDiffUtils.generateOriginalAndDiff(origLines, revLines);
12+
//System.out.println(originalAndDiff.size());
13+
generateDiffHtml(originalAndDiff,"D:\\diff.html");
14+
}
15+
16+
/**
17+
* get file content
18+
* @param filePath file path
19+
*/
20+
public static List<String> getFileContent(String filePath) {
21+
//origin
22+
List<String> fileContent =null;
23+
File file = new File(filePath);
24+
try {
25+
fileContent = Files.readAllLines(file.toPath());
26+
} catch (IOException e) {
27+
e.printStackTrace();
28+
}
29+
return fileContent;
30+
}
31+
32+
/**
33+
* The html file is generated by the difference diff between the two files, and the detailed content of the file comparison can be seen by opening the html file
34+
*
35+
*/
36+
public static void generateDiffHtml(List<String> diffString, String htmlPath) {
37+
StringBuilder builder = new StringBuilder();
38+
for (String line : diffString) {
39+
builder.append(line);
40+
builder.append("\n");
41+
}
42+
String githubCss = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.1/styles/github.min.css";
43+
String diff2htmlCss = "https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css";
44+
String diff2htmlJs = "https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js";
45+
46+
String template = "<!DOCTYPE html>\n" +
47+
"<html lang=\"en-us\">\n" +
48+
" <head>\n" +
49+
" <meta charset=\"utf-8\" />\n" +
50+
" <link rel=\"stylesheet\" href=\"" + githubCss + "\" />\n" +
51+
" <link rel=\"stylesheet\" type=\"text/css\" href=\"" + diff2htmlCss + "\" />\n" +
52+
" <script type=\"text/javascript\" src=\"" + diff2htmlJs + "\"></script>\n" +
53+
" </head>\n" +
54+
" <script>\n" +
55+
" const diffString = `\n" +
56+
"temp\n" +
57+
"`;\n" +
58+
"\n" +
59+
"\n" +
60+
" document.addEventListener('DOMContentLoaded', function () {\n" +
61+
" var targetElement = document.getElementById('myDiffElement');\n" +
62+
" var configuration = {\n" +
63+
" drawFileList: true,\n" +
64+
" fileListToggle: true,\n" +
65+
" fileListStartVisible: true,\n" +
66+
" fileContentToggle: true,\n" +
67+
" matching: 'lines',\n" +
68+
" outputFormat: 'side-by-side',\n" +
69+
" synchronisedScroll: true,\n" +
70+
" highlight: true,\n" +
71+
" renderNothingWhenEmpty: true,\n" +
72+
" };\n" +
73+
" var diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration);\n" +
74+
" diff2htmlUi.draw();\n" +
75+
" diff2htmlUi.highlightCode();\n" +
76+
" });\n" +
77+
" </script>\n" +
78+
" <body>\n" +
79+
" <div id=\"myDiffElement\"></div>\n" +
80+
" </body>\n" +
81+
"</html>";
82+
template = template.replace("temp", builder.toString());
83+
FileWriter fileWriter = null;
84+
try {
85+
fileWriter = new FileWriter(htmlPath);
86+
BufferedWriter buf = new BufferedWriter(fileWriter);
87+
buf.write(template);
88+
buf.close();
89+
fileWriter.close();
90+
} catch (IOException e) {
91+
e.printStackTrace();
92+
}
93+
}
94+
95+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.github.difflib.examples;
2+
3+
import com.github.difflib.UnifiedDiffUtils;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.io.BufferedWriter;
7+
import java.io.File;
8+
import java.io.FileWriter;
9+
import java.io.IOException;
10+
import java.nio.file.Files;
11+
import java.util.List;
12+
13+
// According to the original text, an html will be generated by comparing the text.
14+
public class generateDiffHtmlTest {
15+
16+
/**
17+
* Here's a simple example of getting a nice html page based on the original text and the contrasted text,
18+
* Read n1.txt and n2.txt of D disk, and finally generate an html file
19+
*
20+
*/
21+
@Test
22+
public static void generateOriginalAndDiffDemo(){
23+
List<String> origLines = getFileContent("D:\\n1.txt");
24+
List<String> revLines = getFileContent("D:\\n2.txt");
25+
List<String> originalAndDiff = UnifiedDiffUtils.generateOriginalAndDiff(origLines, revLines);
26+
27+
//generateDiffHtml
28+
generateDiffHtml(originalAndDiff,"D:\\diff.html");
29+
}
30+
31+
/**
32+
* get file content
33+
*
34+
* @param filePath file path
35+
*/
36+
public static List<String> getFileContent(String filePath) {
37+
//原始文件
38+
List<String> fileContent = null;
39+
File file = new File(filePath);
40+
try {
41+
fileContent = Files.readAllLines(file.toPath());
42+
} catch (IOException e) {
43+
e.printStackTrace();
44+
}
45+
return fileContent;
46+
}
47+
48+
/**
49+
* The html file is generated by the difference diff between the two files,
50+
* and the detailed content of the file comparison can be seen by opening the html file
51+
*
52+
* @param diffString The comparison result obtained by calling the above diffString method
53+
* @param htmlPath Generated html path,e.g:/user/var/mbos/ent/21231/diff.html
54+
*/
55+
public static void generateDiffHtml(List<String> diffString, String htmlPath) {
56+
StringBuilder builder = new StringBuilder();
57+
for (String line : diffString) {
58+
builder.append(line);
59+
builder.append("\n");
60+
}
61+
String githubCss = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.1/styles/github.min.css";
62+
String diff2htmlCss = "https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css";
63+
String diff2htmlJs = "https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js";
64+
65+
String template = "<!DOCTYPE html>\n" +
66+
"<html lang=\"en-us\">\n" +
67+
" <head>\n" +
68+
" <meta charset=\"utf-8\" />\n" +
69+
" <link rel=\"stylesheet\" href=\"" + githubCss + "\" />\n" +
70+
" <link rel=\"stylesheet\" type=\"text/css\" href=\"" + diff2htmlCss + "\" />\n" +
71+
" <script type=\"text/javascript\" src=\"" + diff2htmlJs + "\"></script>\n" +
72+
" </head>\n" +
73+
" <script>\n" +
74+
" const diffString = `\n" +
75+
"temp\n" +
76+
"`;\n" +
77+
"\n" +
78+
"\n" +
79+
" document.addEventListener('DOMContentLoaded', function () {\n" +
80+
" var targetElement = document.getElementById('myDiffElement');\n" +
81+
" var configuration = {\n" +
82+
" drawFileList: true,\n" +
83+
" fileListToggle: true,\n" +
84+
" fileListStartVisible: true,\n" +
85+
" fileContentToggle: true,\n" +
86+
" matching: 'lines',\n" +
87+
" outputFormat: 'side-by-side',\n" +
88+
" synchronisedScroll: true,\n" +
89+
" highlight: true,\n" +
90+
" renderNothingWhenEmpty: true,\n" +
91+
" };\n" +
92+
" var diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration);\n" +
93+
" diff2htmlUi.draw();\n" +
94+
" diff2htmlUi.highlightCode();\n" +
95+
" });\n" +
96+
" </script>\n" +
97+
" <body>\n" +
98+
" <div id=\"myDiffElement\"></div>\n" +
99+
" </body>\n" +
100+
"</html>";
101+
template = template.replace("temp", builder.toString());
102+
FileWriter fileWriter = null;
103+
try {
104+
fileWriter = new FileWriter(htmlPath);
105+
BufferedWriter buf = new BufferedWriter(fileWriter);
106+
buf.write(template);
107+
buf.close();
108+
fileWriter.close();
109+
} catch (IOException e) {
110+
e.printStackTrace();
111+
}
112+
}
113+
}
114+

0 commit comments

Comments
 (0)