Added Toml#parse(Reader) and Toml#parse(InputStream)

This commit is contained in:
moandji.ezana 2014-04-08 16:37:02 +02:00
parent 86257278b7
commit b0a8de8b19
3 changed files with 73 additions and 11 deletions

View file

@ -29,7 +29,7 @@ MyClass myClass = toml.to(MyClass.class);
## Usage
A `com.moandjiezana.toml.Toml` instance is populated by calling one of `parse(File)` or `parse(String)`.
A `com.moandjiezana.toml.Toml` instance is populated by calling one of `parse(File)`, `parse(InputStream)`, `parse(Reader)` or `parse(String)`.
````java
Toml toml = new Toml().parse("a=1");

View file

@ -1,14 +1,19 @@
package com.moandjiezana.toml;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import org.parboiled.Parboiled;
import org.parboiled.parserunners.RecoveringParseRunner;
@ -53,27 +58,60 @@ public class Toml {
}
/**
* Populates the current Toml instance with values from tomlString.
* Populates the current Toml instance with values from file.
*
* @param file
* @return this instance
* @throws IllegalStateException If file contains invalid TOML
*/
public Toml parse(File file) {
Scanner scanner = null;
try {
scanner = new Scanner(file);
return parse(scanner.useDelimiter("\\Z").next());
return parse(new FileReader(file));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} finally {
if (scanner != null) {
scanner.close();
}
}
}
/**
* Populates the current Toml instance with values from inputStream.
*
* @param inputStream
* @return this instance
* @throws IllegalStateException If file contains invalid TOML
*/
public Toml parse(InputStream inputStream) {
return parse(new InputStreamReader(inputStream));
}
/**
* Populates the current Toml instance with values from reader.
*
* @param reader
* @return this instance
* @throws IllegalStateException If file contains invalid TOML
*/
public Toml parse(Reader reader) {
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(reader);
StringBuilder w = new StringBuilder();
String line = bufferedReader.readLine();
while (line != null) {
w.append(line).append('\n');
line = bufferedReader.readLine();
}
parse(w.toString());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
bufferedReader.close();
} catch (IOException e) {}
}
return this;
}
/**
* Populates the current Toml instance with values from tomlString.
*

View file

@ -0,0 +1,24 @@
package com.moandjiezana.toml;
import static org.junit.Assert.assertEquals;
import java.io.StringReader;
import org.junit.Test;
public class TomlParseTest {
@Test
public void should_parse_input_stream() throws Exception {
Toml toml = new Toml().parse(getClass().getResourceAsStream("should_load_from_file.toml"));
assertEquals("value", toml.getString("key"));
}
@Test
public void should_parse_reader() throws Exception {
Toml toml = new Toml().parse(new StringReader("key=1"));
assertEquals(1, toml.getLong("key").intValue());
}
}