Class YamlConfig
This class will load the contents of a YAML file into a correspondingly nested
set of Map
and List
objects. Methods in this class
can be used to walk through the nodes of the data structure, testing the
values of the nodes, and retrieving branch and leaf values.
Example
This example will consider a YAML file with a name of myConfig.yaml containing the following contents:
Key1:
- list value 1
- list value 2
Key2:
Key2a: 3
Key2b: 4
This YAML file contains a top level dictionary with keys of Key1
and
Key2
. Under the branch for Key1
is a list containing two
entries. Under the branch for Key2
is another dictionary containing
two keys.
We can use this YamlConfig class to open and parse this file, loading the contents in to appropriate data structures:
YamlConfig config = new YamlConfig("myConfig.yaml");
Now that the file is loaded we can use method to retrieve values.
This YamlConfig
class contains many convenience features for
testing conditions and safely extracting values (for example
getDouble
, getString
,
isLeaf
, isMap
and others).
There are also methods to retrieve the raw Map
,
List
or Object
values (getMap
,
getList
and get
respectively).
There are two types of methods that can be used to extract branches.
-
The methods
getMap
andgetList
return references to the rawMap
andList
objects. Values within these containers can be extracted using the standardCollection
interface. -
The
getBranch
method returns aYamlConfig
object that is rooted at the specified element. This object provides all of the conveience methods, but operating on a different branch of the tree.
For example, to access the String "list value 1" that is the first list
element under the key Key1
we could use the getString
method:
String s = config.getString("Key1", 0);
We could do the same operation in several steps:
YamlConfig branch1 = config.getBranch("Key1");
String s = branch1.getString(0);
Rather than returning a branch that is wrapped with the convenience
methods, we can get the raw container and process it using the
Collection
interface methods:
for (Object s : config.getList("Key1"))
System.err.println("list contains "+s);
Scripting with BeanShell
This class can be used within the BeanShell scripting environment with one caveat and a few simplifications.- BeanShell version 2.0b5 which is currently being used in Patchworks does not support the varargs style of variable argument passing. When a variable number of arguments are passed to a constructor or method they must be boxed in to an array.
- BeanShell has a relaxed syntax and does not required variable type declarations.
s = config.getString(new Object[] {"Key1", 0});
branch1 = config.getBranch(new Object[] {"Key1"});
s = branch1.getString(new Object[] {0});
for (Object s : config.getList(new Object[] {"Key1"}))
System.err.println("list contains "+s);
-
Constructor Summary
ConstructorsConstructorDescriptionYamlConfig(String configFile)
Initialize this class with the name of a yaml fileYamlConfig(List config)
Initialize this class with a List objectYamlConfig(Map config)
Initialize this class with a Map object -
Method Summary
Modifier and TypeMethodDescriptionboolean
containsKey(Object key)
Check if the key exists in the map, or the index number is valid for the list.Retrieve an object from the configurationstatic Object
Retrieve an entry from the YAML file based on a list of keys.boolean
getBoolean(Object... keys)
Retrieve a boolean from the configurationdouble
Retrieve a double from the configurationint
Retrieve an integer from the configurationRetrieve a List object from the configurationRetrieve a Map object from the configurationgetMatcher(Object... keys)
Retrieve aMatcher
object from the configuration.Matcher[]
getMatchers(Object... keys)
Retrieve an array ofMatcher
objects from the configuration.Retrieve a String from the configurationboolean
Is this branch of the config a leaf?boolean
Is this object at these keys a List?boolean
Is this object at these keys a Map?int
size()
-
Constructor Details
-
YamlConfig
Initialize this class with the name of a yaml file -
YamlConfig
Initialize this class with a Map object -
YamlConfig
Initialize this class with a List object
-
-
Method Details
-
isList
Is this object at these keys a List? -
isMap
Is this object at these keys a Map? -
isLeaf
Is this branch of the config a leaf? -
containsKey
Check if the key exists in the map, or the index number is valid for the list. -
get
Retrieve an object from the configuration- Parameters:
keys
- an array of keys in to the configuration
-
getString
Retrieve a String from the configuration- Parameters:
keys
- an array of keys in to the configuration
-
getInt
Retrieve an integer from the configuration- Parameters:
keys
- an array of keys in to the configuration
-
getDouble
Retrieve a double from the configuration- Parameters:
keys
- an array of keys in to the configuration
-
getBoolean
Retrieve a boolean from the configuration- Parameters:
keys
- an array of keys in to the configuration
-
getList
Retrieve a List object from the configuration- Parameters:
keys
- an array of keys in to the configuration
-
getMap
Retrieve a Map object from the configuration- Parameters:
keys
- an array of keys in to the configuration
-
getMatcher
Retrieve aMatcher
object from the configuration. This method will extract a String value from the location in the tree specified by the keys argument, compile this value as a regular expression pattern and retrieve aMatcher
object on this pattern.- Parameters:
keys
- an array of keys in to the configuration
-
getMatchers
Retrieve an array ofMatcher
objects from the configuration. This method will retrieve a list from the location in the configuration as specified by the keys. Each of the list items will be retrieved as a String and compiled as a regular expression pattern. An array ofMatcher
objects will be returned, one for each element in the list.- Parameters:
keys
- an array of keys in to the configuration
-
getBranch
- Parameters:
keys
- an array of keys in to the configuration
-
size
public int size() -
get
Retrieve an entry from the YAML file based on a list of keys. The method will search through a hierarchical set of dictionaries and lists to find the requested entry.The found object will be tested against the specified class and an error will be thrown if the type does not match.
- Parameters:
config
- a dictionary or list from the yaml configclz
- the expected class of the entry. If the class does not match then an error will be thrown. To skip type checking specify Object.classkeys
- the chain of keys to look for in the dictionary
-