Unlike XML, cfg-2 does not support any digester-rule. It only supports parsing a configuration file into a fly.dream.cfg.Config object. Digestions left to the programmers.
# Comment
string1 "string value"
map1
{
string1 "string value"
map1
{
...
}
list1
[
...
]
...
}
# A list can either be a map list, a string list or a list list.
# Elements of a list must be anonymous, that they're not key specified.
mapList1
[
{
...
}
{
...
}
...
]
stringList1
[
"string1"
"string2"
...
]
listList1
[
[
...
]
[
...
]
...
]
- String - Surrounded by quotes (e.g. "...")
- Map - Surrounded by braces (e.g. {...})
- List - Surrounded by brackets (e.g. [...])
- The root of a configuration must be a map without braces.
- Each entry of a map must be a key-value pair.
- Elements of a list must be same type.
- String must be surrounded by quotes.
Its instances can be used to load configuration data from the specified file into a fly.dream.cfg.Config object.
Constructor | Description |
---|---|
Loader(Path path) | Constructs a loader with the specified path of file. |
Type | Method | Description |
---|---|---|
Config | load() | Returns the fly.dream.cfg.Config object parsed from the specified file. |
Represents the root element of the configuration, and it's an anonymous map.
Type | Method | Description |
---|---|---|
boolean | contains(String key) | Returns true if it contains an entry with the specified key. |
Item | get(String key) | Returns a fly.dream.cfg.Item object to which the specified key is mapped, or null if it contains no mapping for the key. |
Type | Method | Description |
---|---|---|
ItemType | getType() | Returns the type of this item. |
String | getString() | Returns the string value of this item if its type is STRING, or throws a runtime exception. |
Map<String, Item> | getMap() | Returns the map value of this item if its type is MAP, or throws a runtime exception. |
List<Item> | getList() | Returns the list value of this item if its type is LIST, or throws a runtime exception. |
Includes following types:
- STRING
- MAP
- LIST
-
Install it as 3rd party JARs to your maven repository.
-
Add it to your project via pom.xml.
<dependency>
<groupId>fly.dream</groupId>
<artifactId>cfg-2</artifactId>
<version>1</version>
</dependency>
- Java code
Path file = ...
try
{
Loader loader = new Loader(file);
Config config = loader.load();
...
}
catch (IOException | ConfigException e)
{
...
}
Shows how to write code (see the constructor of Configuration.java below) to digest the fly.dream.cfg.Config instance loaded from the server.cfg file below.
tmp.dir "custom-temp-dir"
hosts
{
localhost
{
app.base "web-apps"
}
}
connectors
[
{
port "8080"
upgrade.insecure.request "1"
upgrade.port "8443"
}
{
port "8443"
ssl "on"
ssl.context.protocol "TLS"
keystore.file ".keystore"
passphrase "Change it"
keystore.type "JKS"
keystore.algorithm "PKIX"
truststore.type "JKS"
truststore.algorithm "PKIX"
compression "on"
}
]
import ...
class Configuration
{
private final String tmpDir;
private final Map<String, Host> hosts;
private final List<Connector> connectors;
// Digestion can go in constructor
// Exception can be substituted to more proper subclasses
Configuration(Config config) throws Exception
{
this.tmpDir = getTmpDir(config);
this.hosts = getHosts(config);
this.connectors = getConnectors(config);
}
private String getTmpDir(Config config) throws Exception
{
String key = "tmp.dir";
if (config.contains(key))
{
Item item = config.get(key);
if (item.getType() != STRING)
{
throw new Exception("Type of '" + key + "' must be STRING");
}
else
{
return item.getString();
}
}
else
{
return System.getProperty("user.dir") + File.separator + "temp";
}
}
private Map<String, Host> getHosts(Config config) throws Exception
{
String key = "hosts";
if (!config.contains(key))
{
throw new Exception("'" + key + "' is required");
}
else
{
Item item = config.get(key);
if (item.getType() != MAP)
{
throw new Exception("Type of '" + key + "' must be MAP");
}
else
{
Map<String, Host> hosts = new HashMap<>(1);
for (Map.Entry<String, Item> entry : item.getMap().entrySet())
{
hosts.put(entry.getKey(), new Host(entry.getValue()));
}
return hosts;
}
}
}
private List<Connector> getConnectors(Config config) throws Exception
{
String key = "connectors";
if (!config.contains(key))
{
throw new Exception("'" + key + "' is required");
}
else
{
Item item = config.get(key);
if (item.getType() != LIST)
{
throw new Exception("Type of '" + key + "' must be LIST");
}
else
{
List<Connector> connectors = new ArrayList<>(2);
for (Item i : item.getList())
{
connectors.add(new Connector(i));
}
return connectors;
}
}
}
// Getters are omitted
}
Apache 2.0 License. See LICENSE file.