Search in sources :

Example 1 with Patch

use of com.sksamuel.diffpatch.DiffMatchPatch.Patch in project muikku by otavanopisto.

the class CoOpsDmpDiffAlgorithm method createUnpatch.

private LinkedList<Patch> createUnpatch(LinkedList<Patch> patches) {
    LinkedList<Patch> result = new LinkedList<>();
    for (int patchIndex = 0, patchesLength = patches.size(); patchIndex < patchesLength; patchIndex++) {
        Patch patch = patches.get(patchIndex);
        Patch unpatch = new Patch();
        unpatch.length1 = patch.length1;
        unpatch.length2 = patch.length2;
        unpatch.start1 = patch.start1;
        unpatch.start2 = patch.start2;
        for (int diffIndex = 0, diffsLength = patch.diffs.size(); diffIndex < diffsLength; diffIndex++) {
            Diff diff = patch.diffs.get(diffIndex);
            switch(diff.operation) {
                case DELETE:
                    patch.diffs.add(new Diff(Operation.INSERT, diff.text));
                    break;
                case INSERT:
                    patch.diffs.add(new Diff(Operation.DELETE, diff.text));
                    break;
                case EQUAL:
                    patch.diffs.add(new Diff(diff.operation, diff.text));
                    break;
            }
        }
        result.add(unpatch);
    }
    return result;
}
Also used : Diff(com.sksamuel.diffpatch.DiffMatchPatch.Diff) DiffMatchPatch(com.sksamuel.diffpatch.DiffMatchPatch) Patch(com.sksamuel.diffpatch.DiffMatchPatch.Patch) LinkedList(java.util.LinkedList)

Example 2 with Patch

use of com.sksamuel.diffpatch.DiffMatchPatch.Patch in project muikku by otavanopisto.

the class CoOpsDmpDiffAlgorithm method unpatch.

@Override
public String unpatch(String data, String patch) throws CoOpsConflictException {
    DiffMatchPatch diffMatchPatch = new DiffMatchPatch();
    LinkedList<Patch> patches = createUnpatch(new LinkedList<Patch>(diffMatchPatch.patch_fromText(patch)));
    Object[] patchResult = diffMatchPatch.patch_apply(patches, data);
    for (boolean applied : (boolean[]) patchResult[1]) {
        if (!applied) {
            throw new CoOpsConflictException();
        }
    }
    return (String) patchResult[0];
}
Also used : DiffMatchPatch(com.sksamuel.diffpatch.DiffMatchPatch) CoOpsConflictException(fi.foyt.coops.CoOpsConflictException) DiffMatchPatch(com.sksamuel.diffpatch.DiffMatchPatch) Patch(com.sksamuel.diffpatch.DiffMatchPatch.Patch)

Example 3 with Patch

use of com.sksamuel.diffpatch.DiffMatchPatch.Patch in project muikku by otavanopisto.

the class CoOpsDmpDiffAlgorithm method patch.

@Override
public String patch(String data, String patch) throws CoOpsConflictException {
    DiffMatchPatch diffMatchPatch = new DiffMatchPatch();
    LinkedList<Patch> patches = new LinkedList<Patch>(diffMatchPatch.patch_fromText(patch));
    Object[] patchResult = diffMatchPatch.patch_apply(patches, data);
    for (boolean applied : (boolean[]) patchResult[1]) {
        if (!applied) {
            throw new CoOpsConflictException();
        }
    }
    return (String) patchResult[0];
}
Also used : DiffMatchPatch(com.sksamuel.diffpatch.DiffMatchPatch) CoOpsConflictException(fi.foyt.coops.CoOpsConflictException) DiffMatchPatch(com.sksamuel.diffpatch.DiffMatchPatch) Patch(com.sksamuel.diffpatch.DiffMatchPatch.Patch) LinkedList(java.util.LinkedList)

Example 4 with Patch

use of com.sksamuel.diffpatch.DiffMatchPatch.Patch in project muikku by otavanopisto.

the class HtmlMaterialCleaner method createPatch.

private String createPatch(String oldHtml, String newHtml) {
    DiffMatchPatch diffMatchPatch = new DiffMatchPatch();
    LinkedList<Diff> diffs = diffMatchPatch.diff_main(oldHtml, newHtml);
    diffMatchPatch.diff_cleanupEfficiency(diffs);
    LinkedList<Patch> patch = diffMatchPatch.patch_make(diffs);
    return diffMatchPatch.patch_toText(patch);
}
Also used : Diff(com.sksamuel.diffpatch.DiffMatchPatch.Diff) DiffMatchPatch(com.sksamuel.diffpatch.DiffMatchPatch) Patch(com.sksamuel.diffpatch.DiffMatchPatch.Patch) DiffMatchPatch(com.sksamuel.diffpatch.DiffMatchPatch)

Aggregations

DiffMatchPatch (com.sksamuel.diffpatch.DiffMatchPatch)4 Patch (com.sksamuel.diffpatch.DiffMatchPatch.Patch)4 Diff (com.sksamuel.diffpatch.DiffMatchPatch.Diff)2 CoOpsConflictException (fi.foyt.coops.CoOpsConflictException)2 LinkedList (java.util.LinkedList)2