use of difflib.Chunk in project Gargoyle by callakrsos.
the class TextSameLineDiffAppController 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) {
List<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(() -> {
List<ChunkWrapper> newChunk = new ArrayList<>(readLines.size() * 3);
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());
try {
collection.set(tmpPosition++, chunkWrapper);
} catch (Exception e) {
e.printStackTrace();
}
}
}, (collection1, collection2) -> collection1.addAll(collection2));
return collect;
}
use of difflib.Chunk 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);
}
Aggregations