Class YamlFile

java.lang.Object
de.iip_ecosphere.platform.support.yaml.YamlFile

public class YamlFile extends Object
Low-level YAML file support, specific functions for oktoflow.
Author:
Holger Eichelberger, SSE
  • Constructor Details

    • YamlFile

      protected YamlFile()
      Prevents external instantiation.
  • Method Details

    • read

      public static Object read(InputStream in) throws IOException
      Reads a YAML file into a generic object-based structure. Does not close in.
      Parameters:
      in - the input stream to read from
      Returns:
      the object structure
      Throws:
      IOException - if reading from in fails
    • asString

      public static String asString(Object data, String dflt)
      Turns an object into a string.
      Parameters:
      data - the data
      dflt - the default value if data is null
      Returns:
      the string value, possibly dflt
    • asMap

      public static Map<Object,Object> asMap(Object data)
      Converts a plain object, e.g., returned from read(InputStream) to a map.
      Parameters:
      data - the data
      Returns:
      the map, possibly empty if not convertible
    • asList

      public static List<Object> asList(Object data)
      Converts a plain object, e.g., returned from read(InputStream) to a list.
      Parameters:
      data - the data
      Returns:
      the list, possibly empty if not convertible
    • getField

      public static Object getField(Object data, String... field)
      Reads a field from a plain object, e.g., returned from read(InputStream).
      Parameters:
      data - the data
      field - the field to read, may be a sequence of nested fields
      Returns:
      the field, possibly null if not found
    • getFieldAsMap

      public static Map<Object,Object> getFieldAsMap(Object data, String... field)
      Reads a field as a map from a plain object, e.g., returned from read(InputStream).
      Parameters:
      data - the data
      field - the field to read
      Returns:
      the field as map, may be a sequence of nested fields, possibly empty if not found
      See Also:
    • getFieldAsList

      public static List<Object> getFieldAsList(Object data, String field)
      Reads a field as a map from a plain object, e.g., returned from read(InputStream).
      Parameters:
      data - the data
      field - the field to read
      Returns:
      the field as map, possibly empty if not found
      See Also:
    • getFieldAsString

      public static String getFieldAsString(Object data, String field, String dflt)
      Reads a field as a string value from a plain object, e.g., returned from read(InputStream).
      Parameters:
      data - the data
      field - the field to read
      dflt - what to return if there is no field/no convertible value
      Returns:
      the field as string value, possibly dflt if not found
      See Also:
    • fixListSafe

      public 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. This method fixes the instances if the root cause cannot be determined. It also handles list is null and logs potential exceptions.
      Type Parameters:
      T - the expected type of objects
      Parameters:
      list - the list
      cls - the class denoting the expected type
      Returns:
      list eventually with modified entries
    • fixList

      public static <T> List<T> fixList(List<T> list, Class<T> cls) throws ExecutionException
      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 list
      cls - the class denoting the expected type
      Returns:
      list eventually with modified entries
      Throws:
      ExecutionException - if creation of objects fails
    • createInstance

      private static <T> T createInstance(Class<T> cls) throws ExecutionException
      Creates an instance of class cls and 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

      public static Field findField(Class<?> cls, String name)
      Finds a field recursively in cls.
      Parameters:
      cls - the class to start searching with
      name - the field name
      Returns:
      the field or null if there is none
    • getMap

      public static Map<String,Object> getMap(Map<String,Object> yaml, String... path)
      Returns a map from a YAML structure given as map object.
      Parameters:
      yaml - the YAML structure
      path - the key-name path into the YAML structure
      Returns:
      the found YAML sub-structure or null if not found
    • overwrite

      public static <T> T overwrite(T obj, Class<T> cls, Map<String,Object> data)
      Overwrites values in obj of type cls with data from data, but only if fields map into cls. Uses getters and setters of obj.
      Type Parameters:
      T - the type of obj
      Parameters:
      obj - the object for which fields shall be overwritten
      cls - the class type of obj
      data - the data to overwrite, keys shall map to fields, otherwise ignored
      Returns:
      a new instance for obj containing the updated fields
    • map

      private static void map(String key, Object value, Map<String,Object> target)
      Maps key with value value to target. Performs a nested, recursive mapping if value and the value of key in target are of type Map (assuming String-Object maps).
      Parameters:
      key - the key to map
      value - the value associated to key
      target - the target map to override