Package de.dlr.proseo.ui.cli
Class CLIUtil
java.lang.Object
de.dlr.proseo.ui.cli.CLIUtil
Utility methods for command interpretation
- Author:
- Dr. Thomas Bassler
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classHelper class to return username and password from a method -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic InstantparseDateTime(String dateTime) 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> TparseObjectFile(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 formatstatic voidprintObject(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 itselfstatic CLIUtil.CredentialsreadIdentFile(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 voidsetAttribute(Object restObject, String attributeParameter) Set an object attribute from an "attribute=value" parameter using reflection.
-
Field Details
-
FILE_FORMAT_YAML
YAML file format- See Also:
-
FILE_FORMAT_JSON
JSON file format- See Also:
-
FILE_FORMAT_XML
XML file format- See Also:
-
-
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 readfileFormat- 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 TIOException- 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 toobject- the object to printfileFormat- 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 printingIOException- 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 inattributeParameter- 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 objectClassCastException- if the attribute type is not supported (i. e. not listed above)
-
parseDateTime
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
- 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 criteriaFileNotFoundException- if the file denoted by the file path does not existIOException- if the file is not readable
-