Search in sources :

Example 1 with Chunk

use of difflib.Chunk in project Gargoyle by callakrsos.

the class DiffPathText method diffPathTest.

@Test
public void diffPathTest() 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();
        List<?> lines = original.getLines();
        lines.stream().forEach(System.out::println);
    });
// 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) Chunk(difflib.Chunk) File(java.io.File) Patch(difflib.Patch) URL(java.net.URL) Test(org.junit.Test)

Example 2 with Chunk

use of difflib.Chunk in project Gargoyle by callakrsos.

the class DiffPathText method patchTest.

@Test
public void patchTest() 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());
    List<String> tmpOrigin = new LinkedList<String>();
    test1List.forEach(str -> {
        tmpOrigin.add(str);
    });
    List<String> tmpRevice = new LinkedList<String>();
    test2List.forEach(str -> {
        tmpRevice.add(str);
    });
    Patch diff = DiffUtils.diff(test1List, test2List);
    System.out.println(diff.getDeltas());
    List<Delta> deltas = diff.getDeltas();
    Collections.reverse(deltas);
    System.out.println("before");
    System.out.println(tmpOrigin);
    // 공백 작업pos
    deltas.forEach(d -> {
        Chunk original = d.getOriginal();
        Chunk revised = d.getRevised();
        int position = original.getPosition();
        switch(d.getType()) {
            case INSERT:
                {
                    List<String> lines = (List<String>) original.getLines();
                    List<String> lines2 = (List<String>) revised.getLines();
                    tmpOrigin.addAll(position, lines2);
                    tmpRevice.addAll(position, lines);
                }
                break;
        }
    });
    System.out.println("origin");
    System.out.println(tmpOrigin);
    System.out.println("revice");
    System.out.println(tmpRevice);
// tmpOrigin.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) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 3 with Chunk

use of difflib.Chunk in project Gargoyle by callakrsos.

the class HtmlBaseDiffAppController method extractedWrapperedChunk.

/**
	 * chunk객체를 UI로 표현하기 위해 wrapping처리함.
	 *
	 * @param deltaType
	 * @param deltas
	 * @param readLines
	 * @return
	 */
private List<ChunkWrapper> extractedWrapperedChunk(final DeltaType deltaType, final List<Delta> deltas, final List<String> readLines) {
    int size = readLines.size();
    ArrayList<ChunkWrapper> collect = deltas.stream().map(delta -> {
        Chunk chunk = null;
        if (DeltaType.ORIGINAL == deltaType) {
            chunk = delta.getOriginal();
        } else {
            chunk = delta.getRevised();
        }
        int position = chunk.getPosition();
        @SuppressWarnings("unchecked") List<String> lines = (List<String>) chunk.getLines();
        TYPE type = delta.getType();
        ChunkWrapper chunkWrapper = new ChunkWrapper();
        chunkWrapper.setType(type);
        chunkWrapper.setLines(lines);
        chunkWrapper.setChunk(chunk);
        chunkWrapper.setPosition(position);
        chunkWrapper.setDelta(delta);
        return chunkWrapper;
    }).collect(() -> {
        ArrayList<ChunkWrapper> newChunk = new ArrayList<>(size);
        for (int i = 0; i < readLines.size(); i++) {
            ChunkWrapper chunkWrapper = new ChunkWrapper();
            chunkWrapper.setStr(readLines.get(i));
            chunkWrapper.setPosition(i);
            newChunk.add(chunkWrapper);
        }
        return newChunk;
    }, (collection, item) -> {
        int position = item.getPosition();
        List<String> lines = item.getLines();
        TYPE type = item.getType();
        int tmpPosition = position;
        for (String str : lines) {
            ChunkWrapper chunkWrapper = new ChunkWrapper();
            chunkWrapper.setType(type);
            chunkWrapper.setLines(lines);
            chunkWrapper.setPosition(position);
            chunkWrapper.setChunk(item.getChunk());
            chunkWrapper.setStr(str);
            chunkWrapper.setDelta(item.getDelta());
            collection.set(tmpPosition++, chunkWrapper);
        }
    }, (collection1, collection2) -> collection1.addAll(collection2));
    return collect;
}
Also used : Button(javafx.scene.control.Button) TextArea(javafx.scene.control.TextArea) SimpleStringProperty(javafx.beans.property.SimpleStringProperty) ListCell(javafx.scene.control.ListCell) CompareResult(com.kyj.fx.voeditor.visual.diff.CompareResult) LoggerFactory(org.slf4j.LoggerFactory) ChunkWrapper(com.kyj.fx.voeditor.visual.diff.ChunkWrapper) ArrayList(java.util.ArrayList) TableColumn(javafx.scene.control.TableColumn) Insets(javafx.geometry.Insets) Chunk(difflib.Chunk) BackgroundFill(javafx.scene.layout.BackgroundFill) SimpleIntegerProperty(javafx.beans.property.SimpleIntegerProperty) TableView(javafx.scene.control.TableView) Callback(javafx.util.Callback) Tooltip(javafx.scene.control.Tooltip) GridPane(javafx.scene.layout.GridPane) HBox(javafx.scene.layout.HBox) Color(javafx.scene.paint.Color) WebView(javafx.scene.web.WebView) ObjectProperty(javafx.beans.property.ObjectProperty) Label(javafx.scene.control.Label) Logger(org.slf4j.Logger) IOException(java.io.IOException) DiffComparable(com.kyj.fx.voeditor.visual.diff.DiffComparable) Background(javafx.scene.layout.Background) CellDataFeatures(javafx.scene.control.TableColumn.CellDataFeatures) FXML(javafx.fxml.FXML) TextBaseComparator(com.kyj.fx.voeditor.visual.diff.TextBaseComparator) List(java.util.List) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) AnchorPane(javafx.scene.layout.AnchorPane) ImageView(javafx.scene.image.ImageView) ValueUtil(kyj.Fx.dao.wizard.core.util.ValueUtil) ObservableValue(javafx.beans.value.ObservableValue) TYPE(difflib.Delta.TYPE) BorderPane(javafx.scene.layout.BorderPane) StringProperty(javafx.beans.property.StringProperty) Delta(difflib.Delta) ArrayList(java.util.ArrayList) ChunkWrapper(com.kyj.fx.voeditor.visual.diff.ChunkWrapper) ArrayList(java.util.ArrayList) List(java.util.List) Chunk(difflib.Chunk) TYPE(difflib.Delta.TYPE)

Example 4 with Chunk

use of difflib.Chunk in project Gargoyle by callakrsos.

the class FileBaseDiffAppController method extractedWrapperedChunk.

/**
	 * chunk객체를 UI로 표현하기 위해 wrapping처리함.
	 *
	 * @param deltaType
	 * @param deltas
	 * @param readLines
	 * @return
	 */
private List<ChunkWrapper> extractedWrapperedChunk(final DeltaType deltaType, final List<Delta> deltas, final List<String> readLines) {
    int size = readLines.size();
    ArrayList<ChunkWrapper> collect = deltas.stream().map(delta -> {
        Chunk chunk = null;
        if (DeltaType.ORIGINAL == deltaType) {
            chunk = delta.getOriginal();
        } else {
            chunk = delta.getRevised();
        }
        int position = chunk.getPosition();
        @SuppressWarnings("unchecked") List<String> lines = (List<String>) chunk.getLines();
        TYPE type = delta.getType();
        ChunkWrapper chunkWrapper = new ChunkWrapper();
        chunkWrapper.setType(type);
        chunkWrapper.setLines(lines);
        chunkWrapper.setChunk(chunk);
        chunkWrapper.setPosition(position);
        chunkWrapper.setDelta(delta);
        return chunkWrapper;
    }).collect(() -> {
        ArrayList<ChunkWrapper> newChunk = new ArrayList<>(size);
        for (int i = 0; i < readLines.size(); i++) {
            ChunkWrapper chunkWrapper = new ChunkWrapper();
            chunkWrapper.setStr(readLines.get(i));
            chunkWrapper.setPosition(i);
            newChunk.add(chunkWrapper);
        }
        return newChunk;
    }, (collection, item) -> {
        int position = item.getPosition();
        List<String> lines = item.getLines();
        TYPE type = item.getType();
        int tmpPosition = position;
        for (String str : lines) {
            ChunkWrapper chunkWrapper = new ChunkWrapper();
            chunkWrapper.setType(type);
            chunkWrapper.setLines(lines);
            chunkWrapper.setPosition(position);
            chunkWrapper.setChunk(item.getChunk());
            chunkWrapper.setStr(str);
            chunkWrapper.setDelta(item.getDelta());
            collection.set(tmpPosition++, chunkWrapper);
        }
    }, (collection1, collection2) -> collection1.addAll(collection2));
    return collect;
}
Also used : ListView(javafx.scene.control.ListView) CompareResult(com.kyj.fx.voeditor.visual.diff.CompareResult) ListCell(javafx.scene.control.ListCell) LoggerFactory(org.slf4j.LoggerFactory) ChunkWrapper(com.kyj.fx.voeditor.visual.diff.ChunkWrapper) SnapshotParameters(javafx.scene.SnapshotParameters) ArrayList(java.util.ArrayList) FileBaseComparator(com.kyj.fx.voeditor.visual.diff.FileBaseComparator) Insets(javafx.geometry.Insets) Chunk(difflib.Chunk) BackgroundFill(javafx.scene.layout.BackgroundFill) Tooltip(javafx.scene.control.Tooltip) GridPane(javafx.scene.layout.GridPane) HBox(javafx.scene.layout.HBox) Color(javafx.scene.paint.Color) ObjectProperty(javafx.beans.property.ObjectProperty) Logger(org.slf4j.Logger) Label(javafx.scene.control.Label) FXMLController(com.kyj.fx.voeditor.visual.framework.annotation.FXMLController) WritableImage(javafx.scene.image.WritableImage) IOException(java.io.IOException) DiffComparable(com.kyj.fx.voeditor.visual.diff.DiffComparable) ScrollEvent(javafx.scene.input.ScrollEvent) Background(javafx.scene.layout.Background) File(java.io.File) Platform(javafx.application.Platform) FXML(javafx.fxml.FXML) List(java.util.List) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) AnchorPane(javafx.scene.layout.AnchorPane) ImageView(javafx.scene.image.ImageView) ValueUtil(kyj.Fx.dao.wizard.core.util.ValueUtil) TYPE(difflib.Delta.TYPE) BorderPane(javafx.scene.layout.BorderPane) Delta(difflib.Delta) ArrayList(java.util.ArrayList) ChunkWrapper(com.kyj.fx.voeditor.visual.diff.ChunkWrapper) ArrayList(java.util.ArrayList) List(java.util.List) Chunk(difflib.Chunk) TYPE(difflib.Delta.TYPE)

Example 5 with Chunk

use of difflib.Chunk in project disconf by knightliao.

the class FileComparatorTestCase method shouldGetInsertsBetweenFiles.

@Test
public void shouldGetInsertsBetweenFiles() {
    final FileComparator comparator = new FileComparator(original, revised);
    try {
        final List<Chunk> insertsFromOriginal = comparator.getInsertsFromOriginal();
        assertEquals(1, insertsFromOriginal.size());
        final Chunk firstInsert = insertsFromOriginal.get(0);
        final int firstLineOfFirstInsert = firstInsert.getPosition() + 1;
        final int firstInsertSize = firstInsert.size();
        assertEquals(7, firstLineOfFirstInsert);
        assertEquals(1, firstInsertSize);
        final String firstInsertText = firstInsert.getLines().get(0).toString();
        assertEquals("new line 6.1", firstInsertText);
    } catch (IOException ioe) {
        fail("Error running test shouldGetInsertsBetweenFiles " + ioe.toString());
    }
}
Also used : IOException(java.io.IOException) Chunk(difflib.Chunk) Test(org.junit.Test)

Aggregations

Chunk (difflib.Chunk)17 Delta (difflib.Delta)13 IOException (java.io.IOException)11 ChunkWrapper (com.kyj.fx.voeditor.visual.diff.ChunkWrapper)7 CompareResult (com.kyj.fx.voeditor.visual.diff.CompareResult)7 TYPE (difflib.Delta.TYPE)7 ArrayList (java.util.ArrayList)7 List (java.util.List)7 FXML (javafx.fxml.FXML)7 ImageView (javafx.scene.image.ImageView)7 SimpleIntegerProperty (javafx.beans.property.SimpleIntegerProperty)6 SimpleStringProperty (javafx.beans.property.SimpleStringProperty)6 StringProperty (javafx.beans.property.StringProperty)6 ObservableValue (javafx.beans.value.ObservableValue)6 File (java.io.File)5 CellDataFeatures (javafx.scene.control.TableColumn.CellDataFeatures)5 DiffComparable (com.kyj.fx.voeditor.visual.diff.DiffComparable)4 Patch (difflib.Patch)4 ObjectProperty (javafx.beans.property.ObjectProperty)4 SimpleObjectProperty (javafx.beans.property.SimpleObjectProperty)4