Skip to content
This repository was archived by the owner on Nov 12, 2019. It is now read-only.

Commit 6d9c7d6

Browse files
committed
implement event based parser like SAX
1 parent c4eaef4 commit 6d9c7d6

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ A fork of [java-diff-utils](https://code.google.com/p/java-diff-utils/)
77
- switch from JDK5 to JDK7
88
- add Guava to dependency
99
- let user uses other string to represent line which does not exist
10+
- implement event based parser like SAX (in difflib.event package)
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package difflib.event;
2+
3+
import difflib.Patch;
4+
5+
/**
6+
* Interface to handle list of patch about specific file.
7+
*/
8+
public interface PatchHandler<T> {
9+
void handle(String originalPath, String revisedPath, Patch<T> patch);
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package difflib.event;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileNotFoundException;
6+
import java.io.IOException;
7+
import java.util.List;
8+
9+
import javax.annotation.Nonnull;
10+
11+
import com.google.common.base.Charsets;
12+
import com.google.common.collect.Lists;
13+
import com.google.common.io.Files;
14+
15+
import difflib.DiffUtils;
16+
import difflib.Patch;
17+
18+
public class UnifiedPatchParser {
19+
20+
void parse(File unifiedPatch, @Nonnull PatchHandler<String> handler) throws FileNotFoundException, IOException {
21+
try (BufferedReader reader = Files.newReader(unifiedPatch, Charsets.UTF_8)) {
22+
String line = reader.readLine();
23+
do {
24+
List<String> patch = Lists.newArrayList();
25+
while (!line.startsWith("---")) {
26+
// in prelude: skip all lines
27+
line = reader.readLine();
28+
}
29+
assert line.startsWith("---");
30+
patch.add(line);
31+
final String originalFileName = line.substring(4);
32+
33+
line = reader.readLine();
34+
assert line.startsWith("+++");
35+
patch.add(line);
36+
final String revisedFileName = line.substring(4);
37+
38+
line = reader.readLine();
39+
do {
40+
patch.add(line);
41+
line = reader.readLine();
42+
} while (
43+
line != null &&
44+
(line.startsWith("@@ ") || line.startsWith(" ") || line.startsWith("+") || line.startsWith("-")) // TODO this implementation expects that we have comment between 2 diffs.
45+
);
46+
47+
Patch<String> parsed = DiffUtils.parseUnifiedDiff(patch);
48+
handler.handle(originalFileName, revisedFileName, parsed);
49+
} while (line != null);
50+
}
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package difflib.event;
2+
3+
import static org.hamcrest.CoreMatchers.hasItem;
4+
import static org.hamcrest.CoreMatchers.hasItems;
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.hamcrest.CoreMatchers.startsWith;
7+
import static org.junit.Assert.assertThat;
8+
9+
import java.io.File;
10+
import java.io.FileNotFoundException;
11+
import java.io.IOException;
12+
import java.util.concurrent.atomic.AtomicInteger;
13+
14+
import org.junit.Test;
15+
16+
import difflib.Delta;
17+
import difflib.Patch;
18+
19+
public class UnifiedPatchParserTest {
20+
21+
@Test
22+
public void test() throws FileNotFoundException, IOException {
23+
final AtomicInteger visited = new AtomicInteger(0);
24+
PatchHandler<String> handler = new PatchHandler<String>() {
25+
@Override
26+
public void handle(String originalPath, String revisedPath,
27+
Patch<String> patch) {
28+
visited.incrementAndGet();
29+
30+
Delta<String> delta = patch.getDeltas().get(0);
31+
assertThat(delta.getOriginal().getPosition(), is(0));
32+
if (originalPath.startsWith("test/a.txt")) {
33+
assertThat(revisedPath, startsWith("test2/a.txt"));
34+
assertThat(patch.getDeltas().size(), is(1));
35+
assertThat(delta.getOriginal().getLines(), hasItem("hello, world"));
36+
assertThat(delta.getRevised().getLines(), hasItem("hello"));
37+
} else {
38+
assertThat(originalPath, startsWith("test/b.txt"));
39+
assertThat(revisedPath, startsWith("test2/b.txt"));
40+
assertThat(patch.getDeltas().size(), is(1));
41+
assertThat(delta.getOriginal().getLines(), hasItems("hello! 1", "hello! 2"));
42+
assertThat(delta.getRevised().getLines(), hasItem("hello"));
43+
}
44+
}
45+
};
46+
new UnifiedPatchParser().parse(new File("src/test/resources/event", "2files.diff"), handler);
47+
assertThat(visited.intValue(), is(2));
48+
}
49+
}

src/test/resources/event/2files.diff

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
diff -u test/a.txt test2/a.txt
2+
--- test/a.txt 2013-12-31 19:37:09.000000000 +0800
3+
+++ test2/a.txt 2013-12-31 19:37:02.000000000 +0800
4+
@@ -1 +1 @@
5+
-hello, world
6+
+hello
7+
diff -u test/b.txt test2/b.txt
8+
--- test/b.txt 2013-12-31 19:37:20.000000000 +0800
9+
+++ test2/b.txt 2013-12-31 19:37:02.000000000 +0800
10+
@@ -1,2 +1 @@
11+
-hello! 1
12+
-hello! 2
13+
+hello

0 commit comments

Comments
 (0)