Class YamlFile
java.lang.Object
de.iip_ecosphere.platform.support.yaml.YamlFile
Low-level YAML file support, specific functions for oktoflow.
- Author:
- Holger Eichelberger, SSE
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionConverts a plain object, e.g., returned fromread(InputStream)to a list.Converts a plain object, e.g., returned fromread(InputStream)to a map.static StringTurns an object into a string.private static <T> TcreateInstance(Class<T> cls) Creates an instance of classclsand wraps all exceptions.static FieldFinds a field recursively incls.static <T> List<T> Somehow, Snakeyaml does not take up a generic type in a list and delivers a list of hashmaps instead of a list of objects of that type.static <T> List<T> fixListSafe(List<T> list, Class<T> cls) Somehow, Snakeyaml does not take up a generic type in a list and delivers a list of hashmaps instead of a list of objects of that type.static ObjectReads a field from a plain object, e.g., returned fromread(InputStream).getFieldAsList(Object data, String field) Reads a field as a map from a plain object, e.g., returned fromread(InputStream).getFieldAsMap(Object data, String... field) Reads a field as a map from a plain object, e.g., returned fromread(InputStream).static StringgetFieldAsString(Object data, String field, String dflt) Reads a field as a string value from a plain object, e.g., returned fromread(InputStream).Returns a map from a YAML structure given as map object.private static voidMapskeywith valuevalueto target.static <T> TOverwrites values inobjof typeclswith data fromdata, but only if fields map intocls.static Objectread(InputStream in) Reads a YAML file into a generic object-based structure.
-
Constructor Details
-
YamlFile
protected YamlFile()Prevents external instantiation.
-
-
Method Details
-
read
Reads a YAML file into a generic object-based structure. Does not closein.- Parameters:
in- the input stream to read from- Returns:
- the object structure
- Throws:
IOException- if reading frominfails
-
asString
Turns an object into a string.- Parameters:
data- the datadflt- the default value ifdatais null- Returns:
- the string value, possibly
dflt
-
asMap
Converts a plain object, e.g., returned fromread(InputStream)to a map.- Parameters:
data- the data- Returns:
- the map, possibly empty if not convertible
-
asList
Converts a plain object, e.g., returned fromread(InputStream)to a list.- Parameters:
data- the data- Returns:
- the list, possibly empty if not convertible
-
getField
Reads a field from a plain object, e.g., returned fromread(InputStream).- Parameters:
data- the datafield- the field to read, may be a sequence of nested fields- Returns:
- the field, possibly null if not found
-
getFieldAsMap
Reads a field as a map from a plain object, e.g., returned fromread(InputStream).- Parameters:
data- the datafield- the field to read- Returns:
- the field as map, may be a sequence of nested fields, possibly empty if not found
- See Also:
-
getFieldAsList
Reads a field as a map from a plain object, e.g., returned fromread(InputStream).- Parameters:
data- the datafield- the field to read- Returns:
- the field as map, possibly empty if not found
- See Also:
-
getFieldAsString
Reads a field as a string value from a plain object, e.g., returned fromread(InputStream).- Parameters:
data- the datafield- the field to readdflt- what to return if there is no field/no convertible value- Returns:
- the field as string value, possibly
dfltif not found - See Also:
-
fixListSafe
Somehow, Snakeyaml does not take up a generic type in a list and delivers a list of hashmaps instead of a list of objects of that type. This method fixes the instances if the root cause cannot be determined. It also handleslistis null and logs potential exceptions.- Type Parameters:
T- the expected type of objects- Parameters:
list- the listcls- the class denoting the expected type- Returns:
listeventually with modified entries
-
fixList
Somehow, Snakeyaml does not take up a generic type in a list and delivers a list of hashmaps instead of a list of objects of that type. This method fixes the instances if the root cause cannot be determined.- Type Parameters:
T- the expected type of objects- Parameters:
list- the listcls- the class denoting the expected type- Returns:
listeventually with modified entries- Throws:
ExecutionException- if creation of objects fails
-
createInstance
Creates an instance of classclsand wraps all exceptions.- Type Parameters:
T- the type of the instance- Parameters:
cls- the class stating the type- Returns:
- the instance
- Throws:
ExecutionException- if the creation fails, e.g., no public no-arg constructor
-
findField
Finds a field recursively incls.- Parameters:
cls- the class to start searching withname- the field name- Returns:
- the field or null if there is none
-
getMap
Returns a map from a YAML structure given as map object.- Parameters:
yaml- the YAML structurepath- the key-name path into the YAML structure- Returns:
- the found YAML sub-structure or null if not found
-
overwrite
Overwrites values inobjof typeclswith data fromdata, but only if fields map intocls. Uses getters and setters ofobj.- Type Parameters:
T- the type ofobj- Parameters:
obj- the object for which fields shall be overwrittencls- the class type ofobjdata- the data to overwrite, keys shall map to fields, otherwise ignored- Returns:
- a new instance for
objcontaining the updated fields
-
map
Mapskeywith valuevalueto target. Performs a nested, recursive mapping ifvalueand the value ofkeyintargetare of typeMap(assuming String-Object maps).- Parameters:
key- the key to mapvalue- the value associated tokeytarget- the target map to override
-