Skip to content

Commit

Permalink
TreeGenerator use ReaderConfigurator to prepare input reader
Browse files Browse the repository at this point in the history
  • Loading branch information
morandat committed Mar 28, 2019
1 parent 7774ff4 commit 4615333
Show file tree
Hide file tree
Showing 21 changed files with 97 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public static class TreeData {
public void load() {
try {
String otherPath = refPath.replace("_v0_", "_v1_");
src = TreeIoUtils.fromXml().generateFromFile(refPath).getRoot();
dst = TreeIoUtils.fromXml().generateFromFile(otherPath).getRoot();
src = TreeIoUtils.fromXml().generateFrom().file(refPath).getRoot();
dst = TreeIoUtils.fromXml().generateFrom().file(otherPath).getRoot();
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public static void collectActions() throws Exception {
for (Path path : outputPaths) {
Path otherPath = Paths.get(path.toString().replace("_v0_","_v1_"));
Path outputPath = Paths.get(path.toString().replace("_v0_","_actions_"));
TreeContext src = TreeIoUtils.fromXml().generateFromFile(path.toString());
TreeContext dst = TreeIoUtils.fromXml().generateFromFile(otherPath.toString());
TreeContext src = TreeIoUtils.fromXml().generateFrom().file(path.toString());
TreeContext dst = TreeIoUtils.fromXml().generateFrom().file(otherPath.toString());
CompositeMatchers.ClassicGumtree matcher = new CompositeMatchers.ClassicGumtree(
src.getRoot(), dst.getRoot(), new MappingStore());
matcher.match();
Expand All @@ -97,8 +97,8 @@ public static void checkActions() throws Exception {
StringBuilder b = new StringBuilder();
for (Path path : outputPaths) {
Path otherPath = Paths.get(path.toString().replace("_v0_","_v1_"));
TreeContext src = TreeIoUtils.fromXml().generateFromFile(path.toString());
TreeContext dst = TreeIoUtils.fromXml().generateFromFile(otherPath.toString());
TreeContext src = TreeIoUtils.fromXml().generateFrom().file(path.toString());
TreeContext dst = TreeIoUtils.fromXml().generateFrom().file(otherPath.toString());
CompositeMatchers.ClassicGumtree matcher = new CompositeMatchers.ClassicGumtree(
src.getRoot(), dst.getRoot(), new MappingStore());
matcher.match();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ public static void processFilePair(String src, String dst) {
private static TreeContext getTreeContext(String file) {
try {
if (file.endsWith(".java"))
return new JdtTreeGenerator().generateFromFile(file);
return new JdtTreeGenerator().generateFrom().file(file);
else if (file.endsWith(".rb"))
return new RubyTreeGenerator().generateFromFile(file);
return new RubyTreeGenerator().generateFrom().file(file);
else if (file.endsWith(".js"))
return new RhinoTreeGenerator().generateFromFile(file);
return new RhinoTreeGenerator().generateFrom().file(file);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/com/github/gumtreediff/gen/Generators.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public TreeContext getTree(String file) throws UnsupportedOperationException, IO
TreeGenerator p = get(file);
if (p == null)
throw new UnsupportedOperationException("No generator found for file: " + file);
return p.generateFromFile(file);
return p.generateFrom().file(file);
}

public TreeContext getTree(String generator, String file) throws UnsupportedOperationException, IOException {
for (Entry e : entries)
if (e.id.equals(generator))
return e.instantiate(null).generateFromFile(file);
return e.instantiate(null).generateFrom().file(file);
throw new UnsupportedOperationException("No generator \"" + generator + "\" found.");
}

Expand Down
55 changes: 44 additions & 11 deletions core/src/main/java/com/github/gumtreediff/gen/TreeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,65 @@
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@IndexSubclasses
public abstract class TreeGenerator {

protected abstract TreeContext generate(Reader r) throws IOException;

public TreeContext generateFromReader(Reader r) throws IOException {
protected TreeContext generateTree(Reader r) throws IOException {
TreeContext ctx = generate(r);
ctx.validate();
return ctx;
}

public TreeContext generateFromFile(String path) throws IOException {
return generateFromReader(Files.newBufferedReader(Paths.get(path), Charset.forName("UTF-8")));
public ReaderConfigurator generateFrom() {
return new ReaderConfigurator();
}

public TreeContext generateFromFile(File file) throws IOException {
return generateFromReader(Files.newBufferedReader(file.toPath(), Charset.forName("UTF-8")));
}
public class ReaderConfigurator {

public TreeContext generateFromStream(InputStream stream) throws IOException {
return generateFromReader(new InputStreamReader(stream, "UTF-8"));
}
private String charsetName = "UTF-8";
private Charset charset;

private Charset charset() {
return (charset != null) ? charset : Charset.forName(charsetName);
}

public ReaderConfigurator charset(Charset charset) {
this.charset = charset;
return this;
}

public ReaderConfigurator charset(String name) {
charsetName = name;
return this;
}

public TreeContext file(Path path) throws IOException {
return reader(Files.newBufferedReader(path, charset()));
}

public TreeContext file(String path) throws IOException {
return file(Paths.get(path));
}

public TreeContext file(File file) throws IOException {
return file(file.toPath());
}

public TreeContext reader(Reader stream) throws IOException {
return generateTree(stream);
}

public TreeContext stream(InputStream stream) throws IOException {
return reader(new InputStreamReader(stream, charset()));
}

public TreeContext generateFromString(String content) throws IOException {
return generateFromReader(new StringReader(content));
public TreeContext string(String content) throws IOException {
return reader(new StringReader(content));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void testSerializeTree() throws Exception {
ByteArrayOutputStream bos = new ByteArrayOutputStream();

TreeIoUtils.toXml(tc).writeTo(bos);
TreeContext tca = TreeIoUtils.fromXml().generateFromString(bos.toString());
TreeContext tca = TreeIoUtils.fromXml().generateFrom().string(bos.toString());
ITree ca = tca.getRoot();

assertTrue(a.isIsomorphicTo(ca));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static ITree getDummyBig() {

public static TreeContext load(String name) {
try {
return TreeIoUtils.fromXml().generateFromStream(System.class.getResourceAsStream(name));
return TreeIoUtils.fromXml().generateFrom().stream(System.class.getResourceAsStream(name));
} catch (IOException e) {
throw new RuntimeException(String.format("Unable to load test ressorce: %s", name), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public class TestJsonParsing {

@Test
public void testJsonParsing() throws Exception {
TreeContext tc = new AntlrJsonTreeGenerator().generateFromReader(
new InputStreamReader(getClass().getResourceAsStream("/sample.json"), "UTF-8"));
TreeContext tc = new AntlrJsonTreeGenerator().generateFrom().charset("UTF-8").
stream(getClass().getResourceAsStream("/sample.json"));
ITree tree = tc.getRoot();

assertEquals(ARRAY, tree.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static Collection provideStringAndExpectedLength() {

@Test
public void testSimpleParse() throws IOException {
ITree t = new RTreeGenerator().generateFromString(input).getRoot();
ITree t = new RTreeGenerator().generateFrom().string(input).getRoot();
assertEquals(expectedRootSymbol, t.getType());
assertEquals(expectedSize, t.getSize());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class CTreeGenerator extends ExternalProcessTreeGenerator {
@Override
public TreeContext generate(Reader r) throws IOException {
String output = readStandardOutput(r);
return TreeIoUtils.fromXml(CTreeGenerator.defaultUnserializers).generateFromString(output);
return TreeIoUtils.fromXml(CTreeGenerator.defaultUnserializers).generateFrom().string(output);
}

protected String[] getCommandLine(String file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class TestCGenerator {
@Test
public void testSimpleSyntax() throws IOException {
String input = "int main() { printf(\"Hello world!\"); return 0; }";
ITree t = new CTreeGenerator().generateFromString(input).getRoot();
ITree t = new CTreeGenerator().generateFrom().string(input).getRoot();
Assert.assertEquals(18, t.getSize());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package com.github.gumtreediff.gen.css;

import com.github.gumtreediff.gen.SyntaxException;
import com.github.gumtreediff.io.TreeIoUtils;
import com.github.gumtreediff.tree.ITree;
import com.github.gumtreediff.tree.TreeContext;
import org.junit.Test;
Expand All @@ -40,14 +39,14 @@ public void testSimple() throws Exception {
+ "ul li {\n"
+ "\tbackground-color: black;\n"
+ "}");
TreeContext ctx = new CssTreeGenerator().generateFromReader(r);
TreeContext ctx = new CssTreeGenerator().generateFrom().reader(r);
ITree tree = ctx.getRoot();
assertEquals(10, tree.getSize());
}

@Test(expected = SyntaxException.class)
public void badSyntax() throws IOException {
String input = ".foo \"toto {\nfont-size: 11pt;\n}";
TreeContext ct = new CssTreeGenerator().generateFromString(input);
TreeContext ct = new CssTreeGenerator().generateFrom().string(input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ public static Collection provideStringAndExpectedLength() {

@Test
public void testSimpleSyntax() throws IOException {
ITree tree = new JavaParserGenerator().generateFromString(input).getRoot();
ITree tree = new JavaParserGenerator().generateFrom().string(input).getRoot();
assertEquals(expectedRootSymbol, tree.getType());
assertEquals(expectedSize, tree.getSize());
}

@Test(expected = SyntaxException.class)
public void badSyntax() throws IOException {
String input = "public clas Foo {}";
TreeContext ct = new JavaParserGenerator().generateFromString(input);
TreeContext ct = new JavaParserGenerator().generateFrom().string(input);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public void shouldHaveDifferentTree() throws IOException {
String left = "public interface Main { }";
String right = "public class Main { }";

ITree leftTree = new JdtTreeGenerator().generateFromString(left).getRoot();
ITree rightTree = new JdtTreeGenerator().generateFromString(right).getRoot();
ITree leftTree = new JdtTreeGenerator().generateFrom().string(left).getRoot();
ITree rightTree = new JdtTreeGenerator().generateFrom().string(right).getRoot();
Matcher m = Matchers.getInstance().getMatcher(leftTree, rightTree);
m.match();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class TestJdtGenerator {
@Test
public void testSimpleSyntax() throws IOException {
String input = "public class Foo { public int foo; }";
ITree tree = new JdtTreeGenerator().generateFromString(input).getRoot();
ITree tree = new JdtTreeGenerator().generateFrom().string(input).getRoot();
assertEquals(COMPILATION_UNIT, tree.getType());
assertEquals(10, tree.getSize());
}
Expand All @@ -48,7 +48,7 @@ public void testSimpleSyntax() throws IOException {
public void testJava5Syntax() throws IOException {
String input = "public class Foo<A> { public List<A> foo; public void foo() "
+ "{ for (A f : foo) { System.out.println(f); } } }";
ITree tree = new JdtTreeGenerator().generateFromString(input).getRoot();
ITree tree = new JdtTreeGenerator().generateFrom().string(input).getRoot();
assertEquals(COMPILATION_UNIT, tree.getType());
assertEquals(35, tree.getSize());
}
Expand All @@ -60,78 +60,78 @@ public void testMethodInvocation() throws IOException {
+ " a(b);\n"
+ " }\n"
+ "}\n";
TreeContext ctx = new JdtTreeGenerator().generateFromString(input);
TreeContext ctx = new JdtTreeGenerator().generateFrom().string(input);
String o1 = TreeIoUtils.toLisp(ctx).toString();

input = "class Main {\n"
+ " public static void foo() {\n"
+ " a.b();\n"
+ " }\n"
+ "}";
ctx = new JdtTreeGenerator().generateFromString(input);
ctx = new JdtTreeGenerator().generateFrom().string(input);
String o2 = TreeIoUtils.toLisp(ctx).toString();
assertNotEquals(o1, o2);
}

@Test
public void testJava8Syntax() throws IOException {
String input = "public class Foo { public void foo(){ new ArrayList<Object>().stream().forEach(a -> {}); } }";
ITree tree = new JdtTreeGenerator().generateFromString(input).getRoot();
ITree tree = new JdtTreeGenerator().generateFrom().string(input).getRoot();
assertEquals(COMPILATION_UNIT, tree.getType());
assertEquals(28, tree.getSize());
}

@Test(expected = SyntaxException.class)
public void badSyntax() throws IOException {
String input = "public clas Foo {}";
TreeContext ct = new JdtTreeGenerator().generateFromString(input);
TreeContext ct = new JdtTreeGenerator().generateFrom().string(input);
}

@Test
public void testTypeDefinition() throws IOException {
String input1 = "public class Foo {}";
String input2 = "public interface Foo {}";
TreeContext ct1 = new JdtTreeGenerator().generateFromString(input1);
TreeContext ct2 = new JdtTreeGenerator().generateFromString(input2);
TreeContext ct1 = new JdtTreeGenerator().generateFrom().string(input1);
TreeContext ct2 = new JdtTreeGenerator().generateFrom().string(input2);
assertTrue(!ct1.getRoot().toStaticHashString().equals(ct2.getRoot().toStaticHashString()));
}

@Test
public void testInfixOperator() throws IOException {
String input = "class Foo { int i = 3 + 3}";
TreeContext ct = new JdtTreeGenerator().generateFromString(input);
TreeContext ct = new JdtTreeGenerator().generateFrom().string(input);
System.out.println(ct.getRoot().toPrettyTreeString(ct));
}

@Test
public void testAssignment() throws IOException {
String input = "class Foo { void foo() { s.foo = 12; } }";
TreeContext ct = new JdtTreeGenerator().generateFromString(input);
TreeContext ct = new JdtTreeGenerator().generateFrom().string(input);
System.out.println(ct.getRoot().toPrettyTreeString(ct));
}

@Test
public void testPrefixExpression() throws IOException {
String input = "class Foo { void foo() { ++s.i; } }";
TreeContext ct = new JdtTreeGenerator().generateFromString(input);
TreeContext ct = new JdtTreeGenerator().generateFrom().string(input);
System.out.println(ct.getRoot().toPrettyTreeString(ct));
}

@Test
public void testPostfixExpression() throws IOException {
String input = "class Foo { void foo() { s.i++; } }";
TreeContext ct = new JdtTreeGenerator().generateFromString(input);
TreeContext ct = new JdtTreeGenerator().generateFrom().string(input);
System.out.println(ct.getRoot().toPrettyTreeString(ct));
}

@Test
public void testArrayCreation() throws IOException {
String input1 = "class Foo { void foo() { int[][] t = new int[12][]; } }";
TreeContext ct1 = new JdtTreeGenerator().generateFromString(input1);
TreeContext ct1 = new JdtTreeGenerator().generateFrom().string(input1);
System.out.println(ct1.getRoot().toPrettyTreeString(ct1));

String input2 = "class Foo { void foo() { int[][] t = new int[][12]; } }";
TreeContext ct2 = new JdtTreeGenerator().generateFromString(input2);
TreeContext ct2 = new JdtTreeGenerator().generateFrom().string(input2);
System.out.println(ct2.getRoot().toPrettyTreeString(ct2));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,28 @@ public class TestJsGenerator {
@Test
public void testStatement() throws IOException {
String input = "console.log(\"Hello world!\");";
ITree tree = new RhinoTreeGenerator().generateFromString(input).getRoot();
ITree tree = new RhinoTreeGenerator().generateFrom().string(input).getRoot();
assertEquals(7, tree.getSize());
}

@Test
public void testComment() throws IOException {
String input = "console.log(\"Hello world!\"); /* with comment */";
ITree tree = new RhinoTreeGenerator().generateFromString(input).getRoot();
ITree tree = new RhinoTreeGenerator().generateFrom().string(input).getRoot();
assertEquals(8, tree.getSize());
}

@Test
public void testComplexFile() throws IOException {
Reader r = new InputStreamReader(getClass().getResourceAsStream("/sample.js"), "UTF-8");
ITree tree = new RhinoTreeGenerator().generateFromReader(r).getRoot();
ITree tree = new RhinoTreeGenerator().generateFrom().charset("UTF-8").
stream(getClass().getResourceAsStream("/sample.js")).getRoot();
assertEquals(402, tree.getSize());
}

@Test(expected = SyntaxException.class)
public void badSyntax() throws IOException {
String input = "function foo((bar) {}";
TreeContext ct = new RhinoTreeGenerator().generateFromString(input);
TreeContext ct = new RhinoTreeGenerator().generateFrom().string(input);
}

}
Loading

0 comments on commit 4615333

Please sign in to comment.