Class IniFile


  • public final class IniFile
    extends java.lang.Object
    An INI file.

    Represents an INI file together with the ability to parse it from a CharSource.

    The INI file format used here is deliberately simple. There are two elements - key-value pairs and sections.

    The basic element is a key-value pair. The key is separated from the value using the '=' symbol. The string ' = ' is searched for before '=' to allow an equals sign to be present in the key, which implies that this string cannot be in either the key or the value. Duplicate keys are allowed. For example 'key = value'. The equals sign and value may be omitted, in which case the value is an empty string.

    All properties are grouped into named sections. The section name occurs on a line by itself surrounded by square brackets. Duplicate section names are not allowed. For example '[section]'.

    Keys, values and section names are trimmed. Blank lines are ignored. Whole line comments begin with hash '#' or semicolon ';'. No escape format is available. Lookup is case sensitive.

    This example explains the format:

      # line comment
      [foo]
      key = value
     
      [bar]
      key = value
      month = January
     

    The aim of this class is to parse the basic format. Interpolation of variables is not supported.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      com.google.common.collect.ImmutableMap<java.lang.String,​PropertySet> asMap()
      Returns the INI file as a map.
      boolean contains​(java.lang.String name)
      Checks if this INI file contains the specified section.
      boolean equals​(java.lang.Object obj)
      Checks if this INI file equals another.
      int hashCode()
      Returns a suitable hash code for the INI file.
      static IniFile of​(com.google.common.io.CharSource source)
      Parses the specified source as an INI file.
      static IniFile of​(java.util.Map<java.lang.String,​PropertySet> sectionMap)
      Obtains an instance, specifying the map of section to properties.
      PropertySet section​(java.lang.String name)
      Gets a single section of this INI file.
      com.google.common.collect.ImmutableSet<java.lang.String> sections()
      Returns the set of sections of this INI file.
      java.lang.String toString()
      Returns a string describing the INI file.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • of

        public static IniFile of​(com.google.common.io.CharSource source)
        Parses the specified source as an INI file.

        This parses the specified character source expecting an INI file format. The resulting instance can be queried for each section in the file.

        INI files sometimes contain a Unicode Byte Order Mark. Callers are responsible for handling this, such as by using UnicodeBom.

        Parameters:
        source - the INI file resource
        Returns:
        the INI file
        Throws:
        java.io.UncheckedIOException - if an IO exception occurs
        java.lang.IllegalArgumentException - if the file cannot be parsed
      • of

        public static IniFile of​(java.util.Map<java.lang.String,​PropertySet> sectionMap)
        Obtains an instance, specifying the map of section to properties.
        Parameters:
        sectionMap - the map of sections
        Returns:
        the INI file
      • sections

        public com.google.common.collect.ImmutableSet<java.lang.String> sections()
        Returns the set of sections of this INI file.
        Returns:
        the set of sections
      • asMap

        public com.google.common.collect.ImmutableMap<java.lang.String,​PropertySet> asMap()
        Returns the INI file as a map.

        The iteration order of the map matches that of the original file.

        Returns:
        the INI file sections
      • contains

        public boolean contains​(java.lang.String name)
        Checks if this INI file contains the specified section.
        Parameters:
        name - the section name
        Returns:
        true if the section exists
      • section

        public PropertySet section​(java.lang.String name)
        Gets a single section of this INI file.

        This returns the section associated with the specified name. If the section does not exist an exception is thrown.

        Parameters:
        name - the section name
        Returns:
        the INI file section
        Throws:
        java.lang.IllegalArgumentException - if the section does not exist
      • equals

        public boolean equals​(java.lang.Object obj)
        Checks if this INI file equals another.

        The comparison checks the content.

        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the other file, null returns false
        Returns:
        true if equal
      • hashCode

        public int hashCode()
        Returns a suitable hash code for the INI file.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code
      • toString

        public java.lang.String toString()
        Returns a string describing the INI file.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the descriptive string