From 9fe363712684a87e68ea16d7908691f69fb9ae51 Mon Sep 17 00:00:00 2001 From: "moandji.ezana" Date: Tue, 26 Feb 2013 21:20:00 +0200 Subject: [PATCH] Moved parsing from constructor to parse() methods --- README.md | 5 +- src/main/java/com/moandjiezana/toml/Toml.java | 23 +++++--- .../com/moandjiezana/toml/TomlParser.java | 2 +- .../moandjiezana/toml/TomlDefaultsTest.java | 14 ++--- .../java/com/moandjiezana/toml/TomlTest.java | 56 ++++++++++++------- 5 files changed, 62 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 8abcf41..1754615 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ toml4j is a [TOML](https://github.com/mojombo/toml) parser that uses the [Parboi ## Usage ````java -Toml toml = new Toml(getTomlFile()); // throws an Exception if the TOML is incorrect +Toml toml = new Toml().parse(getTomlFile()); // throws an Exception if the TOML is incorrect String title = toml.getString("title"); // if a key doesn't exist, returns null Boolean enabled = toml.getBoolean("database.enabled"); // gets the key enabled from the key group database @@ -17,7 +17,8 @@ Toml servers = toml.getKeyGroup("servers"); // returns a new Toml instance conta The constructor can be given a set of default values that will be used if necessary. ````java -Toml toml = new Toml("a = 1", new Toml("a = 2\nb = 3"); +Toml defaults = new Toml().parse("a = 2\nb = 3"); +Toml toml = new Toml(defaults).parse("a = 1"); Long a = toml.getLong("a"); // returns 1, not 2 Long b = toml.getLong("b"); // returns 3 diff --git a/src/main/java/com/moandjiezana/toml/Toml.java b/src/main/java/com/moandjiezana/toml/Toml.java index 9170ac0..a6451f8 100644 --- a/src/main/java/com/moandjiezana/toml/Toml.java +++ b/src/main/java/com/moandjiezana/toml/Toml.java @@ -19,18 +19,26 @@ import org.parboiled.support.ParsingResult; */ public class Toml { - private final Map values; + private Map values = new HashMap(); private final Toml defaults; - public Toml(File file) throws FileNotFoundException { - this(new Scanner(file).useDelimiter("\\Z").next()); + public Toml() { + this((Toml) null); } - public Toml(String tomlString) { - this(tomlString, null); + public Toml(Toml defaults) { + this.defaults = defaults; } - public Toml(String tomlString, Toml defaults) { + public Toml parse(File file) { + try { + return parse(new Scanner(file).useDelimiter("\\Z").next()); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } + } + + public Toml parse(String tomlString) { TomlParser parser = Parboiled.createParser(TomlParser.class); ParsingResult result = new RecoveringParseRunner(parser.Toml()).run(tomlString); // ParsingResult parsingResult = new ReportingParseRunner(parser.Toml()).run(tomlString); @@ -42,7 +50,8 @@ public class Toml { } this.values = results.values; - this.defaults = defaults; + + return this; } public String getString(String key) { diff --git a/src/main/java/com/moandjiezana/toml/TomlParser.java b/src/main/java/com/moandjiezana/toml/TomlParser.java index 710d5bb..f2190f7 100644 --- a/src/main/java/com/moandjiezana/toml/TomlParser.java +++ b/src/main/java/com/moandjiezana/toml/TomlParser.java @@ -12,7 +12,7 @@ import org.parboiled.annotations.BuildParseTree; import org.parboiled.annotations.SuppressNode; @BuildParseTree -public class TomlParser extends BaseParser { +class TomlParser extends BaseParser { static class Results { public Map values = new HashMap(); diff --git a/src/test/java/com/moandjiezana/toml/TomlDefaultsTest.java b/src/test/java/com/moandjiezana/toml/TomlDefaultsTest.java index 0f93591..d8cc165 100644 --- a/src/test/java/com/moandjiezana/toml/TomlDefaultsTest.java +++ b/src/test/java/com/moandjiezana/toml/TomlDefaultsTest.java @@ -13,47 +13,47 @@ public class TomlDefaultsTest { @Before public void before() { - defaultToml = new Toml("a = \"a\"\n[group]\na=\"a\""); + defaultToml = new Toml().parse("a = \"a\"\n[group]\na=\"a\""); } @Test public void should_fall_back_to_default_value() { - Toml toml = new Toml("", defaultToml); + Toml toml = new Toml(defaultToml); assertEquals("a", toml.getString("a")); } @Test public void should_use_value_when_present_in_values_and_defaults() { - Toml toml = new Toml("a = \"b\"", defaultToml); + Toml toml = new Toml(defaultToml).parse("a = \"b\""); assertEquals("b", toml.getString("a")); } @Test public void should_return_null_when_no_defaults_for_key() throws Exception { - Toml toml = new Toml("", defaultToml); + Toml toml = new Toml(defaultToml).parse(""); assertNull(toml.getString("b")); } @Test public void should_fall_back_to_default_with_multi_key() throws Exception { - Toml toml = new Toml("", defaultToml); + Toml toml = new Toml(defaultToml).parse(""); assertEquals("a", toml.getString("group.a")); } @Test public void should_fall_back_to_key_group() throws Exception { - Toml toml = new Toml("", defaultToml); + Toml toml = new Toml(defaultToml).parse(""); assertEquals("a", toml.getKeyGroup("group").getString("a")); } @Test public void should_fall_back_to_key_within_key_group() throws Exception { - Toml toml = new Toml("[group]\nb=1", defaultToml); + Toml toml = new Toml(defaultToml).parse("[group]\nb=1"); assertEquals(1, toml.getKeyGroup("group").getLong("b").intValue()); assertEquals("a", toml.getKeyGroup("group").getString("a")); diff --git a/src/test/java/com/moandjiezana/toml/TomlTest.java b/src/test/java/com/moandjiezana/toml/TomlTest.java index f209296..1bcf132 100644 --- a/src/test/java/com/moandjiezana/toml/TomlTest.java +++ b/src/test/java/com/moandjiezana/toml/TomlTest.java @@ -16,28 +16,28 @@ public class TomlTest { @Test public void should_get_string() throws Exception { - Toml toml = new Toml("a = \"a\""); + Toml toml = new Toml().parse("a = \"a\""); assertEquals("a", toml.getString("a")); } @Test public void should_get_number() throws Exception { - Toml toml = new Toml("b = 1001"); + Toml toml = new Toml().parse("b = 1001"); assertEquals(1001, toml.getLong("b").intValue()); } @Test public void should_get_list() throws Exception { - Toml toml = new Toml("list = [\"a\", \"b\", \"c\"]"); + Toml toml = new Toml().parse("list = [\"a\", \"b\", \"c\"]"); assertEquals(asList("a", "b", "c"), toml.getList("list", String.class)); } @Test public void should_get_boolean() throws Exception { - Toml toml = new Toml("bool_false = false\nbool_true = true"); + Toml toml = new Toml().parse("bool_false = false\nbool_true = true"); assertFalse(toml.getBoolean("bool_false")); assertTrue(toml.getBoolean("bool_true")); @@ -45,7 +45,7 @@ public class TomlTest { @Test public void should_get_date() throws Exception { - Toml toml = new Toml("a_date = 2011-11-10T13:12:00Z"); + Toml toml = new Toml().parse("a_date = 2011-11-10T13:12:00Z"); Calendar calendar = Calendar.getInstance(); calendar.set(2011, Calendar.NOVEMBER, 10, 13, 12, 00); @@ -57,14 +57,14 @@ public class TomlTest { @Test public void should_get_double() throws Exception { - Toml toml = new Toml("double = 5.25"); + Toml toml = new Toml().parse("double = 5.25"); assertEquals(5.25D, toml.getDouble("double").doubleValue(), 0.0); } @Test public void should_get_key_group() throws Exception { - Toml toml = new Toml("[group]\nkey = \"value\""); + Toml toml = new Toml().parse("[group]\nkey = \"value\""); Toml group = toml.getKeyGroup("group"); @@ -73,84 +73,98 @@ public class TomlTest { @Test public void should_get_value_for_multi_key() throws Exception { - Toml toml = new Toml("[group]\nkey = \"value\""); + Toml toml = new Toml().parse("[group]\nkey = \"value\""); assertEquals("value", toml.getString("group.key")); } @Test - public void should_get_value_for_multi_key_with_no_parent_keygroup() throws Exception { - Toml toml = new Toml("[group.sub]\nkey = \"value\""); + public void should_get_value_for_multi_key_with_no_parent_key_group() throws Exception { + Toml toml = new Toml().parse("[group.sub]\nkey = \"value\""); assertEquals("value", toml.getString("group.sub.key")); } + @Test + public void should_get_key_group_for_multi_key() throws Exception { + Toml toml = new Toml().parse("[group]\nother=1\n[group.sub]\nkey = \"value\""); + + assertEquals("value", toml.getKeyGroup("group.sub").getString("key")); + } + + @Test + public void should_get_key_group_for_multi_key_with_no_parent_key_group() throws Exception { + Toml toml = new Toml().parse("[group.sub]\nkey = \"value\""); + + assertEquals("value", toml.getKeyGroup("group.sub").getString("key")); + } + @Test public void should_return_null_if_no_value_for_key() throws Exception { - Toml toml = new Toml(""); + Toml toml = new Toml().parse(""); assertNull(toml.getString("a")); } @Test public void should_return_null_when_no_value_for_multi_key() throws Exception { - Toml toml = new Toml(""); + Toml toml = new Toml().parse(""); assertNull(toml.getString("group.key")); } @Test public void should_load_from_file() throws Exception { - Toml toml = new Toml(new File(getClass().getResource("should_load_from_file.toml").getFile())); + Toml toml = new Toml().parse(new File(getClass().getResource("should_load_from_file.toml").getFile())); assertEquals("value", toml.getString("key")); } @Test public void should_support_numbers_in_key_names() throws Exception { - Toml toml = new Toml("a1 = 1"); + Toml toml = new Toml().parse("a1 = 1"); assertEquals(1, toml.getLong("a1").intValue()); } @Test public void should_support_numbers_in_key_group_names() throws Exception { - Toml toml = new Toml("[group1]\na = 1"); + Toml toml = new Toml().parse("[group1]\na = 1"); assertEquals(1, toml.getLong("group1.a").intValue()); } @Test public void should_support_underscores_in_key_names() throws Exception { - Toml toml = new Toml("a_a = 1"); + Toml toml = new Toml().parse("a_a = 1"); assertEquals(1, toml.getLong("a_a").intValue()); } @Test public void should_support_underscores_in_key_group_names() throws Exception { - Toml toml = new Toml("[group_a]\na = 1"); + Toml toml = new Toml().parse("[group_a]\na = 1"); assertEquals(1, toml.getLong("group_a.a").intValue()); } @Test(expected = IllegalStateException.class) public void should_fail_when_dot_in_key_name() throws Exception { - new Toml("a.a = 1"); + new Toml().parse("a.a = 1"); } @Test(expected = IllegalStateException.class) public void should_fail_on_invalid_date() throws Exception { - new Toml("d = 2012-13-01T15:00:00Z"); + new Toml().parse("d = 2012-13-01T15:00:00Z"); } @Test(expected = IllegalStateException.class) public void should_fail_when_key_is_overwritten_by_key_group() { - new Toml("[fruit]\ntype=\"apple\"\n[fruit.type]\napple=\"yes\""); + new Toml().parse("[fruit]\ntype=\"apple\"\n[fruit.type]\napple=\"yes\""); } @Test(expected = IllegalStateException.class) public void should_fail_when_key_is_overwritten_by_another_key() { - new Toml("[fruit]\ntype=\"apple\"\ntype=\"orange\""); + new Toml().parse("[fruit]\ntype=\"apple\"\ntype=\"orange\""); } }