me.grison.jtoml.impl
Class Toml

java.lang.Object
  extended by me.grison.jtoml.impl.Toml
All Implemented Interfaces:
Getter, Parser

public class Toml
extends Object
implements Parser, Getter

Toml parsing class front-end. Toml toml = Toml.parse("pi = 3.141592653589793"); Double pi = toml.getDouble("pi");

Author:
$Author: Alexandre Grison$

Field Summary
protected  Map<String,Object> context
          The instance context map holding key/values parsed from a TOML String or File
protected  Matcher keyPathMatcher
          A matcher to retrieve the path to a key.
protected  TomlParser tomlParser
          Current instance parser: default to `Toml.defaultParser` if none specified
 
Constructor Summary
Toml()
          Default constructor.
Toml(TomlParser tomlParser)
          Constructor using a specific TOML parser.
 
Method Summary
 Map<String,Object> findContext(Map<String,Object> context, String key)
          Find the correct level context.
 Object get(String key)
          Get the object.
<T> T
getAs(String key, Class<T> clazz)
          Get a new instance of the given Class filled with the value that can be found in the current context at the given key.
 Boolean getBoolean(String key)
          Get a boolean.
 Calendar getDate(String key)
          Get the date (as Calendar)
 Double getDouble(String key)
          Get the double value.
 List<Object> getList(String key)
          Get the list.
 Long getLong(String key)
          Get the Long value.
 Map<String,Object> getMap(String key)
          Get a Map.
 String getString(String key)
          Get the string.
 TomlParser getTomlParser()
          Gets the parser currently used to parse TOML.
static Toml parse(File file)
          Creates a TOML instance loaded with the given file.
static Toml parse(File file, TomlParser tomlParser)
          Creates a TOML instance loaded with the given file and using the given TOML parser.
static Toml parse(String tomlString)
          Creates a TOML instance loaded with the given String.
static Toml parse(String tomlString, TomlParser tomlParser)
          Creates a TOML instance loaded with the given file and using the given TOML parser.
 Toml parseFile(File file)
          Parse the given File as TOML.
 Toml parseString(String string)
          Parse the given String as TOML.
 String serialize()
          Serializes the current instance context to a TOML String.
static String serialize(Object object)
          Serializes the given Object to a TOML String.
 String serialize(String rootKey)
          Serializes the current instance context to a TOML String.
static String serialize(String rootKey, Object object)
          Serializes the given Object to a TOML String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

context

protected Map<String,Object> context
The instance context map holding key/values parsed from a TOML String or File


keyPathMatcher

protected final Matcher keyPathMatcher
A matcher to retrieve the path to a key.


tomlParser

protected TomlParser tomlParser
Current instance parser: default to `Toml.defaultParser` if none specified

Constructor Detail

Toml

public Toml()
Default constructor.


Toml

public Toml(TomlParser tomlParser)
Constructor using a specific TOML parser.

Parameters:
tomlParser - the specific TOML parser to be used.
Method Detail

parse

public static Toml parse(String tomlString)
Creates a TOML instance loaded with the given String.

Parameters:
tomlString - the TOML String to load.
Returns:
a TOM object instance

parse

public static Toml parse(String tomlString,
                         TomlParser tomlParser)
Creates a TOML instance loaded with the given file and using the given TOML parser.

Parameters:
tomlString - the TOML String to load.
tomlParser - the TOML parser to use
Returns:
a TOML object instance
Throws:
IOException

parse

public static Toml parse(File file)
                  throws IOException
Creates a TOML instance loaded with the given file.

Parameters:
file - the TOML file to load
Returns:
a TOML object instance
Throws:
IOException

parse

public static Toml parse(File file,
                         TomlParser tomlParser)
                  throws IOException
Creates a TOML instance loaded with the given file and using the given TOML parser.

Parameters:
file - the TOML file to load
tomlParser - the TOML parser to use
Returns:
a TOML object instance
Throws:
IOException

parseString

public Toml parseString(String string)
Description copied from interface: Parser
Parse the given String as TOML.

Specified by:
parseString in interface Parser
Parameters:
string - the string to be parsed.

parseFile

public Toml parseFile(File file)
               throws FileNotFoundException
Description copied from interface: Parser
Parse the given File as TOML.

Specified by:
parseFile in interface Parser
Parameters:
file - the file to be parsed.
Throws:
FileNotFoundException

getTomlParser

public TomlParser getTomlParser()
Gets the parser currently used to parse TOML.

Returns:
the current TOML parser.

findContext

public Map<String,Object> findContext(Map<String,Object> context,
                                      String key)
Find the correct level context. findContext({"foo": {"bar": "hello"}}, "foo.bar") -> "hello"

Parameters:
context - the context
key - the key
Returns:
the context

get

public Object get(String key)
Description copied from interface: Getter
Get the object.

Specified by:
get in interface Getter
Parameters:
key - the key where the object is located
Returns:
the object located at the given key.

getString

public String getString(String key)
Description copied from interface: Getter
Get the string.

Specified by:
getString in interface Getter
Parameters:
key - the key where the object is located
Returns:
the string located at the given key.

getLong

public Long getLong(String key)
Description copied from interface: Getter
Get the Long value.

Specified by:
getLong in interface Getter
Parameters:
key - the key where the object is located
Returns:
the long located at the given key.

getDouble

public Double getDouble(String key)
Description copied from interface: Getter
Get the double value.

Specified by:
getDouble in interface Getter
Parameters:
key - the key where the object is located
Returns:
the float located at the given key.

getDate

public Calendar getDate(String key)
Description copied from interface: Getter
Get the date (as Calendar)

Specified by:
getDate in interface Getter
Parameters:
key - the key where the object is located
Returns:
the date located at the given key.

getList

public List<Object> getList(String key)
Description copied from interface: Getter
Get the list.

Specified by:
getList in interface Getter
Parameters:
key - the key where the object is located
Returns:
the list located at the given key.

getMap

public Map<String,Object> getMap(String key)
Description copied from interface: Getter
Get a Map.

Specified by:
getMap in interface Getter
Parameters:
key - the key where the object is located
Returns:
the map located at the given key.

getBoolean

public Boolean getBoolean(String key)
Description copied from interface: Getter
Get a boolean.

Specified by:
getBoolean in interface Getter
Parameters:
key - the key where the object is located
Returns:
the boolean located at the given key.

getAs

public <T> T getAs(String key,
                   Class<T> clazz)
Get a new instance of the given Class filled with the value that can be found in the current context at the given key.

Specified by:
getAs in interface Getter
Type Parameters:
T - the resulting object type
Parameters:
key - the key where the value is located
clazz - the class of the resulting object
Returns:
the value whose key is the given parameter

serialize

public String serialize()
Serializes the current instance context to a TOML String.

Returns:
the TOML String representing the current instance context.

serialize

public String serialize(String rootKey)
Serializes the current instance context to a TOML String.

Parameters:
rootKey - the root key.
Returns:
the TOML String representing the current instance context.

serialize

public static String serialize(Object object)
Serializes the given Object to a TOML String.

Parameters:
object - the Object to be serialized
Returns:
the TOML String representing the given Object.

serialize

public static String serialize(String rootKey,
                               Object object)
Serializes the given Object to a TOML String.

Parameters:
rootKey - the root key (can be empty or null)
object - the Object to be serialized
Returns:
the TOML String representing the given Object.


Copyright © 2013. All Rights Reserved.