mvn install:install-file -DgroupId=com.google.difflib -DartifactId=diff-match-patch -Dversion=1.0.0 -Dpackaging=jar -Dfile=E:\eclipse~\diff\target\diff.jar
<dependency>
<groupId>com.google.difflib</groupId>
<artifactId>diff-match-patch</artifactId>
<version>1.0.0</version>
</dependency>
package com.ruoyi;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Stream;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.common.primitives.Booleans;
import name.fraser.neil.plaintext.diff_match_patch;
import name.fraser.neil.plaintext.diff_match_patch.Diff;
import name.fraser.neil.plaintext.diff_match_patch.Patch;
public class DiffMatchPatch {
@Autowired
@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException {
diff_match_patch dmp = new diff_match_patch();
String r1 = "我";
String r2 = "我是中";
LinkedList<diff_match_patch.Diff> diff = dmp.diff_main(r1, r2);
//清理差异符号空格
dmp.diff_cleanupSemantic(diff);
String html = dmp.diff_prettyHtml(diff);
System.out.println(html);
//001
LinkedList<Patch> patch = dmp.patch_make(r1, r2);//r2和r1比较
StringBuilder result = new StringBuilder();
patch.forEach(item->{
LinkedList<Diff> diffs = item.diffs;
diffs.forEach(it->{
System.out.println(it.operation+"\t"+it.text);
switch (it.operation) {
case INSERT:
result.append("<font style='your_font_style_here'>").append(it.text).append("</font>");
break;
case DELETE:
result.append("<font style='your_font_style_here'>").append(it.text).append("</font>");
break;
case EQUAL:
result.append(it.text);
break;
}
});
});
System.out.println(result.toString());
// patch_apply将差异内容应用到原始文本中假设你已经有了一个Patch对象列表
Object[] apply = dmp.patch_apply(patch, r1);
for (Object obj : apply) {
if (obj instanceof boolean[]) {
//boolean[] t=(boolean[]) obj;
//System.out.println(t[0]);
//System.out.println(ArrayUtils.toString(obj));
}else {
System.out.println(obj);
}
}
}
}
~~只能针对文本HTML不支持保留结构DiffMatchPatch001 ~~
public class DiffMatchPatch001 {
public static void main(String[] args) {
String html1 = "<html><body><br><b>ta</b><div id=\"gu\" class=\"tu\">测试1</div></body></html>";//html().replaceAll("\\s+", " ").trim(); // 先去除所有空格
String html2 = "<html><body><div id=\"gu\" class=\"tu\">测试2</div></body></html>";//html().trem()
// Parse HTML to DOM
Document doc1 = Jsoup.parse(html1);
Document doc2 = Jsoup.parse(html2);
//Elements elements1 = doc1.body().children();
//Elements elements2 = doc2.body().children();
// Convert DOM to string
String html1Str = doc1.html();
String html2Str = doc2.html();
// Compare using JsDiff
diff_match_patch dmp = new diff_match_patch();
LinkedList<diff_match_patch.Diff> diffs = dmp.diff_main(html1Str, html2Str);
//LinkedList<diff_match_patch.Diff> diffs = dmp.diff_main(elements1.text(), elements2.text());
dmp.diff_cleanupSemantic(diffs);
// Mark differences with colors
StringBuilder coloredDiffs = new StringBuilder();
for (diff_match_patch.Diff diff : diffs) {
switch (diff.operation) {
case EQUAL:
coloredDiffs.append(diff.text);
break;
case INSERT:
coloredDiffs.append("<span style='color:green'>" + diff.text + "</span>");
break;
case DELETE:
coloredDiffs.append("<span style='text-decoration: line-through;'>" + diff.text + "</span>");
break;
}
}
System.out.println(coloredDiffs.toString());
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/200903.html