1、核心jar包
<dependency>
<groupId>org.bitbucket.cowwoc</groupId>
<artifactId>diff-match-patch</artifactId>
<version>1.2</version>
</dependency>
2、代码实现
package com.yiscn.smartfilesys.backserver.utils.textsign;
import org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
public class TextSignUtil {
private static TextSignUtil textSignUtil = null;
private static final String OPEN = "<span style=\"color:red;\">";
private static final String CLOSE = "</span>";
private String open;
private String close;
private TextSignUtil(){
this.open = OPEN;
this.close = CLOSE;
}
private TextSignUtil(String open, String close){
this.open = open;
this.close = close;
}
public String getOpen() {
return open;
}
public void setOpen(String open) {
this.open = open;
}
public String getClose() {
return close;
}
public void setClose(String close) {
this.close = close;
}
public static TextSignUtil getInstance(){
if (textSignUtil == null || !textSignUtil.getOpen().equals(OPEN)){
synchronized (TextSignUtil.class){
textSignUtil = new TextSignUtil();
}
}
return textSignUtil;
}
public static TextSignUtil getInstance(String open, String close){
if (textSignUtil == null || !textSignUtil.getOpen().equals(open)){
synchronized (TextSignUtil.class){
textSignUtil = new TextSignUtil(open,close);
}
}
return textSignUtil;
}
public SignVO signTextDifferent(String text, String comparisonText, boolean bool){
DiffMatchPatch dmp = new DiffMatchPatch();
LinkedList<DiffMatchPatch.Diff> diff = dmp.diffMain(text, comparisonText, false);
List<DiffMatchPatch.Diff> textDiffList = diff.stream()
.filter(d -> d.operation.equals(DiffMatchPatch.Operation.EQUAL) || d.operation.equals(DiffMatchPatch.Operation.DELETE))
.collect(Collectors.toList());
List<DiffMatchPatch.Diff> comparisonDiffList = diff.stream()
.filter(d -> d.operation.equals(DiffMatchPatch.Operation.EQUAL) || d.operation.equals(DiffMatchPatch.Operation.INSERT))
.collect(Collectors.toList());
return singTextByListDiff(textDiffList,comparisonDiffList,bool);
}
private SignVO singTextByListDiff(List<DiffMatchPatch.Diff> textDiffList, List<DiffMatchPatch.Diff> comparisonDiffList, boolean bool){
if (bool){
String text = signSpanForEqual(textDiffList);
String comparisonText = signSpanForEqual(comparisonDiffList);
return new SignVO(text,comparisonText);
}else {
String text = signSpanDifferent(textDiffList);
String comparisonText = signSpanDifferent(comparisonDiffList);
return new SignVO(text,comparisonText);
}
}
private String signSpanDifferent(List<DiffMatchPatch.Diff> diffs){
StringBuilder builder = new StringBuilder();
for (DiffMatchPatch.Diff diff : diffs) {
if (!diff.operation.equals(DiffMatchPatch.Operation.EQUAL)){
builder.append(getOpen());
builder.append(diff.text);
builder.append(getClose());
}else {
builder.append(diff.text);
}
}
return builder.toString();
}
private String signSpanForEqual(List<DiffMatchPatch.Diff> diffs){
StringBuilder builder = new StringBuilder();
for (DiffMatchPatch.Diff diff : diffs) {
if (diff.operation.equals(DiffMatchPatch.Operation.EQUAL)){
builder.append(getOpen());
builder.append(diff.text);
builder.append(getClose());
}else {
builder.append(diff.text);
}
}
return builder.toString();
}
}