Class ParseConfig

  • All Implemented Interfaces:
    Serializable, Cloneable, Map<String,​Object>

    public final class ParseConfig
    extends Hashtable<String,​Object>
    This class is used to obtain configuration parameters, from a configuration file or from Java System Properties. If this is an applet, SecurityException is caught safely.
    Author:
    Qin Shi, Vladimir Menkov //@date 1999-2018
    See Also:
    Serialized Form
    • Constructor Detail

      • ParseConfig

        public ParseConfig()
        Creates an empty hashtable. That can be used simply as a convenient interface for accessing Java system options.
      • ParseConfig

        public ParseConfig​(Reader in)
                    throws IOException
        Creates a hashtable that contains the parsed data obtained from an open reader (which may, for example, be associated with an open file), and then closes the reader.

        The configuration file syntax:

        • Lines (or "tails" of lines) beginning with a '#' are comments, and are ignored
        • Blank lines are ignored
        • A line of the form
                  name  value
           
          or
                  name = value
           
          assigns a value to the named variable. The equal sign is optional. There can be a semicolon at the end of the line, but it's optional. The value may be a number (with no quotes), or a string surrounded by double quotes. If the string consists only of alphanumeric characters, with possible '/' and ':' chars, then quotes are optional too.

        A ParseConfig structure is created by reading a specified configuration file. The values of parameters stored in the table can be accessed by using accessor methods, such as getOption or getOptionDouble.

        This method throws various exceptions, so that the caller method could produce a meaningful error report.

        Parameters:
        in - A Reader (a file reader, etc.)
        Throws:
        IOException
    • Method Detail

      • getOption

        public String getOption​(String aName,
                                String aDefault)
        Gets the requested value from the hash table or from the Java system property aName. The Java system property, if given, overrides the value from the hash table.
      • getOptionDouble

        public double getOptionDouble​(String aName,
                                      double aDefault)
        Gets the requested double value from the hash table or from the Java system property aName.
      • getOption

        public int getOption​(String aName,
                             int aDefault)
        Gets the requested integer value from the hash table or from the Java system property aName.
      • getOptionLong

        public long getOptionLong​(String aName,
                                  long aDefault)
      • getOption

        public boolean getOption​(String aName,
                                 boolean aDefault)
        Gets the requested integer value from the hash table or from the Java system property aName.
      • getOptionDate

        public Date getOptionDate​(String aName,
                                  String aDefault)
                           throws ParseException
        Gets a date parameter, in the format YYYY-MM-DD
        Parameters:
        aDefault - Default date, in format 'YYYY-MM-DD'
        Throws:
        ParseException
      • getOptionEnum

        public <T extends Enum<T>> T getOptionEnum​(Class<T> retType,
                                                   String aName,
                                                   T defVal)
        Returns the value of the specified parameter if it can be interpreted as a value of the specified enumerated type. If no parameter with the specified value has been supplied, or if its value cannot be interpreted as a value of the desired type, the supplied default value is returned
        Parameters:
        defVal - The default value to be returned. May be null.
      • getOptionEnumArray

        public <T extends Enum<T>> T[] getOptionEnumArray​(Class<T> retType,
                                                          String aName,
                                                          T[] defVal)
        Reads a comma-separated array of enums. E.g. -Dcolors=RED,GREEN,BLUE
      • containsKey

        public boolean containsKey​(String aName)
      • getParameter

        public String getParameter​(String aName)
                            throws IOException
        Gets the requested value from the hash table. If the value is not found, IOException is thrown.
        Throws:
        IOException
      • getLong

        public long getLong​(String name,
                            long defVal)
      • getInt

        public int getInt​(String name,
                          int defVal)
      • getDouble

        public double getDouble​(String name,
                                double defVal)
      • getBoolean

        public boolean getBoolean​(String name,
                                  boolean defVal)
      • getEnum

        public <T extends Enum<T>> T getEnum​(Class<T> retType,
                                             String name,
                                             T defVal)
      • enrichFromArgv

        public String[] enrichFromArgv​(String[] argv)
        Scans the argv array, identifying all elements of the form X=Y. For each such element, adds the (key,value) pair (X,Y) to this ParseConfig table.
        Returns:
        An array that contains all other elements from argv (those not of the form X=Y).