Class CLIUtil

java.lang.Object
de.dlr.proseo.ui.cli.CLIUtil

public class CLIUtil extends Object
Utility methods for command interpretation
Author:
Dr. Thomas Bassler
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Helper class to return username and password from a method
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    JSON file format
    static final String
    XML file format
    static final String
    YAML file format
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static Instant
    Parse a date and time string in the format "yyyy-MM-dd'T'HH:mm:ss.SSSSSS[zZX]", whereby all of the following variants are allowed: yyyy-MM-dd yyyy-MM-ddTHH:mm yyyy-MM-ddTHH:mm:ss yyyy-MM-ddTHH:mm:ss.S[SSSSS] any of the above plus a time zone in general, RFC 822 or ISO 8601 format Missing parts are set to zero, a missing time zone is set to UTC.
    static <T> T
    parseObjectFile(File objectFile, String fileFormat, Class<T> clazz)
    Read the description for an object of the given type from a file in Json, XML or Yaml format
    static void
    printObject(PrintStream out, Object object, String fileFormat)
    Print the given object to the given output stream according to the requested file format; if the object is a list or set of size 1, then the single element of the collection is printed, not the list/set itself
    readIdentFile(String filePathString)
    Read the user credentials from a file consisting of one or two lines, the first line containing the username (without mission prefix) and the second line the password.
    static void
    setAttribute(Object restObject, String attributeParameter)
    Set an object attribute from an "attribute=value" parameter using reflection.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • CLIUtil

      public CLIUtil()
  • Method Details

    • parseObjectFile

      public static <T> T parseObjectFile(File objectFile, String fileFormat, Class<T> clazz) throws IllegalArgumentException, IOException
      Read the description for an object of the given type from a file in Json, XML or Yaml format
      Type Parameters:
      T - the type parameter
      Parameters:
      objectFile - the file to read
      fileFormat - the file format (one of { JSON, XML, YAML })
      clazz - the class object for type T
      Returns:
      a deserialized object of type T
      Throws:
      IllegalArgumentException - if the file format is not valid or the object file does not conform to the file format or the object file cannot be deserialized into an object of type T
      IOException - if an error occurs while reading the object file
    • printObject

      public static void printObject(PrintStream out, Object object, String fileFormat) throws IllegalArgumentException, IOException
      Print the given object to the given output stream according to the requested file format; if the object is a list or set of size 1, then the single element of the collection is printed, not the list/set itself
      Parameters:
      out - the output stream to print to
      object - the object to print
      fileFormat - the file format requested (one of JSON, XML, YAML)
      Throws:
      IllegalArgumentException - if the file format is not one of the above, or if a formatting error occurs during printing
      IOException - if an I/O error occurs during printing
    • setAttribute

      public static void setAttribute(Object restObject, String attributeParameter) throws IllegalArgumentException, ClassCastException
      Set an object attribute from an "attribute=value" parameter using reflection. Supported attribute types are String, Date, Long and List<String>.
      Parameters:
      restObject - the object to set the attribute in
      attributeParameter - a string of the form "attribute=value", where "value" may be a comma-separated string list
      Throws:
      IllegalArgumentException - if the given attribute name does not exist or is not accessible in the given object
      ClassCastException - if the attribute type is not supported (i. e. not listed above)
    • parseDateTime

      public static Instant parseDateTime(String dateTime) throws DateTimeException
      Parse a date and time string in the format "yyyy-MM-dd'T'HH:mm:ss.SSSSSS[zZX]", whereby all of the following variants are allowed:
      • yyyy-MM-dd
      • yyyy-MM-ddTHH:mm
      • yyyy-MM-ddTHH:mm:ss
      • yyyy-MM-ddTHH:mm:ss.S[SSSSS]
      • any of the above plus a time zone in general, RFC 822 or ISO 8601 format
      Missing parts are set to zero, a missing time zone is set to UTC.
      Parameters:
      dateTime - the date and time string to parse
      Returns:
      the parsed point in time
      Throws:
      DateTimeException - if the given string cannot be parsed according to the format given above
    • readIdentFile

      public static CLIUtil.Credentials readIdentFile(String filePathString) throws SecurityException, FileNotFoundException, IOException
      Read the user credentials from a file consisting of one or two lines, the first line containing the username (without mission prefix) and the second line the password. The file will only be read, if it is only readable by the current system user (as far as warranted by the operating system).
      Parameters:
      filePathString - path to the file containing the credentials
      Returns:
      a Credentials object with username and password set from the file
      Throws:
      SecurityException - if the file denoted by the file path does not meet the security criteria
      FileNotFoundException - if the file denoted by the file path does not exist
      IOException - if the file is not readable