mirror of
https://github.com/plexusorg/toml4j.git
synced 2024-12-28 19:24:15 +00:00
Moved parsing from constructor to parse() methods
This commit is contained in:
parent
4ad8a683cc
commit
9fe3637126
5 changed files with 62 additions and 38 deletions
|
@ -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
|
||||
|
|
|
@ -19,18 +19,26 @@ import org.parboiled.support.ParsingResult;
|
|||
*/
|
||||
public class Toml {
|
||||
|
||||
private final Map<String, Object> values;
|
||||
private Map<String, Object> values = new HashMap<String, Object>();
|
||||
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<Object> result = new RecoveringParseRunner<Object>(parser.Toml()).run(tomlString);
|
||||
// ParsingResult<Object> parsingResult = new ReportingParseRunner<Object>(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) {
|
||||
|
|
|
@ -12,7 +12,7 @@ import org.parboiled.annotations.BuildParseTree;
|
|||
import org.parboiled.annotations.SuppressNode;
|
||||
|
||||
@BuildParseTree
|
||||
public class TomlParser extends BaseParser<Object> {
|
||||
class TomlParser extends BaseParser<Object> {
|
||||
|
||||
static class Results {
|
||||
public Map<String, Object> values = new HashMap<String, Object>();
|
||||
|
|
|
@ -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"));
|
||||
|
|
|
@ -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\"");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue