Search in sources :

Example 11 with Patch

use of difflib.Patch in project Gargoyle by callakrsos.

the class DiffPathText method diffPath.

@Test
public void diffPath() throws IOException, URISyntaxException, PatchFailedException {
    URL test1 = ClassLoader.getSystemClassLoader().getResource("compare/Test1");
    URL test2 = ClassLoader.getSystemClassLoader().getResource("compare/Test2");
    List<String> test1List = Files.readAllLines(new File(test1.toURI()).toPath());
    List<String> test2List = Files.readAllLines(new File(test2.toURI()).toPath());
    Patch diff = DiffUtils.diff(test1List, test2List);
    List<Delta> deltas = diff.getDeltas();
    deltas.stream().forEach(d -> {
        Chunk original = d.getOriginal();
        int position = original.getPosition();
        List<String> lines = (List<String>) original.getLines();
        for (String str : lines) {
            switch(d.getType()) {
                case INSERT:
                    test1List.set(position++, "+".concat(str));
                    break;
                case DELETE:
                    test1List.set(position++, "-".concat(str));
                    break;
                case CHANGE:
                    test1List.set(position++, "*".concat(str));
                    break;
            }
        }
        Chunk revised = d.getRevised();
        position = revised.getPosition();
        lines = (List<String>) revised.getLines();
    });
    test1List.stream().forEach(System.out::println);
// System.out.println(original);
// List<?> patch = DiffUtils.patch(test1List, diff);
// patch.stream().forEach(System.out::println);
//
// List<?> unpatch = DiffUtils.unpatch(test2List, diff);
// unpatch.stream().forEach(System.out::println);
}
Also used : Delta(difflib.Delta) List(java.util.List) LinkedList(java.util.LinkedList) Chunk(difflib.Chunk) File(java.io.File) Patch(difflib.Patch) URL(java.net.URL) Test(org.junit.Test)

Aggregations

Patch (difflib.Patch)11 Delta (difflib.Delta)6 Chunk (difflib.Chunk)4 File (java.io.File)4 URL (java.net.URL)3 LinkedList (java.util.LinkedList)3 List (java.util.List)3 Test (org.junit.Test)3 Migrator (com.evolveum.midpoint.model.impl.migrator.Migrator)1 PrismContext (com.evolveum.midpoint.prism.PrismContext)1 DiffUtils (difflib.DiffUtils)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1