diff-match-patch文本

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());
    }
}
  • 13
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有知识的山巅

文章对你有用,学到了知识。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值