2013-02-26 07:57:26 +00:00
|
|
|
package com.moandjiezana.toml;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
2014-04-07 11:10:44 +00:00
|
|
|
import java.util.ArrayList;
|
2014-04-08 10:52:56 +00:00
|
|
|
import java.util.Collections;
|
2013-02-26 07:57:26 +00:00
|
|
|
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;
|
|
|
|
import org.parboiled.support.ParsingResult;
|
|
|
|
|
2014-04-07 11:10:44 +00:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
2013-02-26 07:57:26 +00:00
|
|
|
/**
|
2014-04-08 10:52:56 +00:00
|
|
|
* <p>Provides access to the keys and tables in a TOML data source.</p>
|
2013-02-26 07:57:26 +00:00
|
|
|
*
|
2014-04-08 10:52:56 +00:00
|
|
|
* <p>All getters can fall back to default values if they have been provided.
|
|
|
|
* Getters for simple values (String, Date, etc.) will return null if no matching key exists.
|
|
|
|
* {@link #getList(String, Class)}, {@link #getTable(String)} and {@link #getTables(String)} return empty values if there is no matching key.</p>
|
|
|
|
*
|
|
|
|
* <p>Example usage:</p>
|
|
|
|
* <code><pre>
|
|
|
|
* Toml toml = new Toml().parse(getTomlFile());
|
|
|
|
* String name = toml.getString("name");
|
|
|
|
* Long port = toml.getLong("server.ip"); // compound key. Is equivalent to:
|
|
|
|
* Long port2 = toml.getTable("server").getLong("ip");
|
|
|
|
* MyConfig config = toml.to(MyConfig.class);
|
|
|
|
* </pre></code>
|
2013-02-26 07:57:26 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class Toml {
|
|
|
|
|
2013-02-26 19:20:00 +00:00
|
|
|
private Map<String, Object> values = new HashMap<String, Object>();
|
2013-02-26 07:57:26 +00:00
|
|
|
private final Toml defaults;
|
|
|
|
|
2014-04-08 10:52:56 +00:00
|
|
|
/**
|
|
|
|
* Creates Toml instance with no defaults.
|
|
|
|
*/
|
2013-02-26 19:20:00 +00:00
|
|
|
public Toml() {
|
|
|
|
this((Toml) null);
|
2013-02-26 07:57:26 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 10:52:56 +00:00
|
|
|
/**
|
|
|
|
* @param defaults fallback values used when the requested key or table is not present.
|
|
|
|
*/
|
2013-02-26 19:20:00 +00:00
|
|
|
public Toml(Toml defaults) {
|
|
|
|
this.defaults = defaults;
|
|
|
|
}
|
|
|
|
|
2014-04-08 10:52:56 +00:00
|
|
|
/**
|
|
|
|
* Populates the current Toml instance with values from tomlString.
|
|
|
|
*
|
|
|
|
* @param file
|
|
|
|
* @return this instance
|
|
|
|
* @throws IllegalStateException If file contains invalid TOML
|
|
|
|
*/
|
2013-02-26 19:20:00 +00:00
|
|
|
public Toml parse(File file) {
|
2014-04-08 10:52:56 +00:00
|
|
|
Scanner scanner = null;
|
2013-02-26 19:20:00 +00:00
|
|
|
try {
|
2014-04-08 10:52:56 +00:00
|
|
|
scanner = new Scanner(file);
|
|
|
|
|
|
|
|
return parse(scanner.useDelimiter("\\Z").next());
|
2013-02-26 19:20:00 +00:00
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
throw new RuntimeException(e);
|
2014-04-08 10:52:56 +00:00
|
|
|
} finally {
|
|
|
|
if (scanner != null) {
|
|
|
|
scanner.close();
|
|
|
|
}
|
2013-02-26 19:20:00 +00:00
|
|
|
}
|
2013-02-26 07:57:26 +00:00
|
|
|
}
|
|
|
|
|
2014-04-08 10:52:56 +00:00
|
|
|
/**
|
|
|
|
* Populates the current Toml instance with values from tomlString.
|
|
|
|
*
|
|
|
|
* @param tomlString
|
|
|
|
* @return this instance
|
|
|
|
* @throws IllegalStateException If tomlString is not valid TOML
|
|
|
|
*/
|
|
|
|
public Toml parse(String tomlString) throws IllegalStateException {
|
2013-02-26 07:57:26 +00:00
|
|
|
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);
|
|
|
|
// System.out.println(ParseTreeUtils.printNodeTree(parsingResult));
|
|
|
|
|
|
|
|
TomlParser.Results results = (TomlParser.Results) result.valueStack.peek(result.valueStack.size() - 1);
|
|
|
|
if (results.errors.length() > 0) {
|
|
|
|
throw new IllegalStateException(results.errors.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
this.values = results.values;
|
2013-02-26 19:20:00 +00:00
|
|
|
|
|
|
|
return this;
|
2013-02-26 07:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getString(String key) {
|
|
|
|
return (String) get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Long getLong(String key) {
|
|
|
|
return (Long) get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public <T> List<T> getList(String key, Class<T> itemClass) {
|
2014-04-08 10:52:56 +00:00
|
|
|
List<T> list = (List<T>) get(key);
|
|
|
|
|
|
|
|
if (list == null) {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
2013-02-26 07:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Boolean getBoolean(String key) {
|
|
|
|
return (Boolean) get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Date getDate(String key) {
|
|
|
|
return (Date) get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Double getDouble(String key) {
|
|
|
|
return (Double) get(key);
|
|
|
|
}
|
|
|
|
|
2014-04-08 10:52:56 +00:00
|
|
|
/**
|
|
|
|
* If no value is found for key, an empty Toml instance is returned.
|
|
|
|
*
|
|
|
|
* @param key
|
|
|
|
*/
|
2013-02-26 07:57:26 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
2014-04-06 20:25:21 +00:00
|
|
|
public Toml getTable(String key) {
|
2013-02-26 07:57:26 +00:00
|
|
|
return new Toml((Map<String, Object>) get(key));
|
|
|
|
}
|
|
|
|
|
2014-04-08 10:52:56 +00:00
|
|
|
/**
|
|
|
|
* If no value is found for key, an empty list is returned.
|
|
|
|
* @param key
|
|
|
|
*/
|
2014-04-07 11:10:44 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
public List<Toml> getTables(String key) {
|
2014-04-08 10:52:56 +00:00
|
|
|
List<Map<String, Object>> tableArray = (List<Map<String, Object>>) get(key);
|
|
|
|
|
|
|
|
if (tableArray == null) {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
|
2014-04-07 11:10:44 +00:00
|
|
|
ArrayList<Toml> tables = new ArrayList<Toml>();
|
2014-04-08 10:52:56 +00:00
|
|
|
for (Map<String, Object> table : tableArray) {
|
2014-04-07 11:10:44 +00:00
|
|
|
tables.add(new Toml(table));
|
|
|
|
}
|
|
|
|
|
|
|
|
return tables;
|
|
|
|
}
|
|
|
|
|
2014-04-08 10:52:56 +00:00
|
|
|
/**
|
|
|
|
* <p>Populates an instance of targetClass with the values of this Toml instance.
|
|
|
|
* The target's field names must match keys or tables.
|
|
|
|
* Keys not present in targetClass will be ignored.</p>
|
|
|
|
*
|
|
|
|
* <p>Tables are recursively converted to custom classes.</p>
|
|
|
|
*
|
|
|
|
* @param targetClass
|
|
|
|
*/
|
|
|
|
public <T> T to(Class<T> targetClass) {
|
|
|
|
HashMap<String, Object> valuesCopy = new HashMap<String, Object>(values);
|
|
|
|
if (defaults != null) {
|
|
|
|
for (Map.Entry<String, Object> entry : defaults.values.entrySet()) {
|
|
|
|
if (!valuesCopy.containsKey(entry.getKey())) {
|
|
|
|
valuesCopy.put(entry.getKey(), entry.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Gson gson = new Gson();
|
|
|
|
String json = gson.toJson(valuesCopy);
|
|
|
|
return gson.fromJson(json, targetClass);
|
|
|
|
}
|
|
|
|
|
2013-02-26 07:57:26 +00:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private Object get(String key) {
|
|
|
|
String[] split = key.split("\\.");
|
|
|
|
Object current = new HashMap<String, Object>(values);
|
|
|
|
Object currentDefaults = defaults != null ? defaults.values : null;
|
|
|
|
for (String splitKey : split) {
|
|
|
|
current = ((Map<String, Object>) current).get(splitKey);
|
|
|
|
if (currentDefaults != null) {
|
|
|
|
currentDefaults = ((Map<String, Object>) currentDefaults).get(splitKey);
|
|
|
|
if (current instanceof Map && currentDefaults instanceof Map) {
|
|
|
|
for (Map.Entry<String, Object> entry : ((Map<String, Object>) currentDefaults).entrySet()) {
|
|
|
|
if (!((Map<String, Object>) current).containsKey(entry.getKey())) {
|
|
|
|
((Map<String, Object>) current).put(entry.getKey(), entry.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (current == null && currentDefaults != null) {
|
|
|
|
current = currentDefaults;
|
|
|
|
}
|
|
|
|
if (current == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Toml(Map<String, Object> values) {
|
2014-04-08 10:52:56 +00:00
|
|
|
this.values = values != null ? values : Collections.<String, Object>emptyMap();
|
2013-02-26 07:57:26 +00:00
|
|
|
this.defaults = null;
|
|
|
|
}
|
|
|
|
}
|