TextComparer.java 1.07 KB
Newer Older
hulei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
package com.brilliance.isc.doc.textcompare;

import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class TextComparer {
    public CompareResult compareGitText(String leftText,String rightText){
        diff_match_patch dmp = new diff_match_patch();
        CompareResult compareResult = new CompareResult();
        LinkedList<diff_match_patch.Diff> diffs = dmp.diff_main(leftText,rightText);
        dmp.diff_cleanupSemantic(diffs);
        AtomicInteger counter = new AtomicInteger();
        diffs.forEach(diff -> {
            if(!diff.operation.equals(diff_match_patch.Operation.EQUAL)){
                counter.incrementAndGet();
            }
        });
        //设置差异
        compareResult.setEditCount(counter.intValue());
        compareResult.setDiffs(diffs);
        String html = dmp.diff_prettyHtml(diffs)
                .replaceAll("&para;","")
                .replaceAll("&amp;nbsp;","&nbsp;");
        compareResult.setResultHtml(html);
        return compareResult;
    }
}