Class PropertyReader

java.lang.Object
net.luis.utils.io.data.property.PropertyReader
All Implemented Interfaces:
AutoCloseable

public class PropertyReader extends Object implements AutoCloseable
Represents a reader for properties.
This reader reads the properties from a defined input and returns them as a properties object.
The base format of the properties is a key-value pair separated by a separator:

 <key><separator><value>
 
The line as a whole can contain multiple separators, but the first one will be used to split the key and value.

Compacted keys

A compacted key is a key that contains multiple keys separated by a pipe.
The compacted part of a key is enclosed in square brackets [, ] and the keys are separated by |.

 <key_part>.[<compacted_key_part>|<compacted_key_part>|...].<key_part><separator><value>
 
All compacted keys will be resolved to multiple keys with the same value.
A compacted key part can contain a variable key part that will be resolved to a value.

Variable keys

A variable key is a key that contains a variable part that will be resolved to a value.
The variable part of a key is enclosed in curly brackets {, } and the parts are separated by ?.

The first part is the target type, the second part is the target key and the third part is the default value.
The default value is optional and will be used if the resolved value is null.
The target type can be one of the following:

  • <empty>, prop or property - The value will be resolved from the previously read properties of this reader
  • sys or system - The value will be resolved from the system properties
  • env or environment - The value will be resolved from the system environment

 <key_part>.?{<target_type>?<variable_key>?<default_value>}.<key_part><separator><value>
 
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final Pattern
    The pattern to check if a key is a compacted key.
    private final PropertyConfig
    The configuration for this reader.
    private final Map<String,String>
    A cache for all properties that have been read.
    private final BufferedReader
    The internal io reader to read the properties.
    private static final Pattern
    The pattern to check if a key is a variable key.
  • Constructor Summary

    Constructors
    Constructor
    Description
    PropertyReader(@NotNull InputProvider input)
    Constructs a new property reader with the given input and the default configuration.
    PropertyReader(@NotNull InputProvider input, @NotNull PropertyConfig config)
    Constructs a new property reader with the given input and configuration.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    private @NotNull List<String>
    extendResolvedKeys(@NotNull List<String> resolvedKeys, String @Nullable ... keyParts)
    Extends the given list of resolved keys with the given new key parts.
    The new key parts will be appended to each resolved key:
    private @NotNull String
    getTargetKey(@NotNull String key, @NotNull String variable, String @NotNull [] parts)
    Validates and returns the target key of the given variable key part.
    The target key must not be empty, contain nested variable parts or compacted parts.
    The target key will be stripped of whitespaces.
    private @NotNull String
    getValuePart(String @NotNull [] valueParts)
    Returns the value part of the given parts.
    If the line contains multiple separators, the value part will be concatenated with the separator.
    private int
    Returns the alignment count of the given key.
    The alignment count is the count of whitespaces at the end of the key.
    private boolean
    isAdvancedKey(@NotNull String key)
    Checks if the given key is either a compacted or variable key.
    private @NotNull @Unmodifiable List<Property>
    parseLine(@NotNull String rawLine)
    Parses the given line and returns the properties that have been read.
    If the line is empty or a comment, an empty list will be returned.
    In the case of an error, the error action of the configuration will be executed.
    private @NotNull @Unmodifiable List<Property>
    parseProperty(@NotNull String rawKey, @NotNull String rawValue)
    Parses the given key and value and returns the properties that have been read.
    If advanced parsing is enabled, the key can be a compacted or variable key.
    private @NotNull Property
    parsePropertySimple(@NotNull String key, @NotNull String value)
    Parses the given key and value and returns a property.
    The key and value will be checked against the configuration.
    @NotNull Properties
    Reads the properties from the input and returns them as a properties object.
    private static @NotNull String
    readScopeExclude(@NotNull ScopedStringReader reader, @NotNull StringScope scope)
    Reads the content of the give scoped string reader and returns it without the scope.
    private @NotNull String
    removeAlignment(@NotNull String str, int alignment, boolean isKey)
    Removes the alignment from the given string.
    If the alignment is 0, the string will be returned as is.
    If the alignment is greater than 0, the alignment will be removed from the string.
    private @NotNull @Unmodifiable List<String>
    Validates and resolves the given advanced key and returns the resolved keys.
    The key can be a compacted or variable key.
    private String @NotNull []
    resolveCompactedKeyPart(@NotNull String key, @NotNull String compacted)
    Validates and resolves the given compacted key part against the expected format.
    The compacted key part must contain at least one value, a pipe separates multiple values.
    The values must not be blank, contain whitespaces or be nested.
    private @NotNull String
    resolveVariableKeyPart(@NotNull String key, @NotNull String variable)
    Validates and resolves the given variable key part against the expected format.
    The variable key part must contain at least one question mark but at most two.
    The first part is the target type, the second part is the target key and the third part is the optional default value.
    private @NotNull String
    resolveVariableValue(@NotNull String key, @NotNull String variable)
    Resolves the actual value of the given variable key part.

    Methods inherited from class java.lang.Object

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

    • COMPACTED_KEY_PATTERN

      private static final Pattern COMPACTED_KEY_PATTERN
      The pattern to check if a key is a compacted key.
    • VARIABLE_KEY_PATTERN

      private static final Pattern VARIABLE_KEY_PATTERN
      The pattern to check if a key is a variable key.
    • properties

      private final Map<String,String> properties
      A cache for all properties that have been read.
    • config

      private final PropertyConfig config
      The configuration for this reader.
    • reader

      private final BufferedReader reader
      The internal io reader to read the properties.
  • Constructor Details

    • PropertyReader

      public PropertyReader(@NotNull @NotNull InputProvider input)
      Constructs a new property reader with the given input and the default configuration.
      Parameters:
      input - The input to create the reader for
      Throws:
      NullPointerException - If the input is null
    • PropertyReader

      public PropertyReader(@NotNull @NotNull InputProvider input, @NotNull @NotNull PropertyConfig config)
      Constructs a new property reader with the given input and configuration.
      Parameters:
      input - The input to create the reader for
      config - The configuration for this reader
      Throws:
      NullPointerException - If the input or the configuration is null
  • Method Details

    • readScopeExclude

      @NotNull private static @NotNull String readScopeExclude(@NotNull @NotNull ScopedStringReader reader, @NotNull @NotNull StringScope scope)
      Reads the content of the give scoped string reader and returns it without the scope.
      Parameters:
      reader - The reader to read the content from
      scope - The scope which is expected
      Returns:
      The content of the reader without the scope
    • readProperties

      @NotNull public @NotNull Properties readProperties()
      Reads the properties from the input and returns them as a properties object.
      Returns:
      The properties that have been read
      Throws:
      PropertySyntaxException - If an error occurs while reading the properties
    • parseLine

      @NotNull private @NotNull @Unmodifiable List<Property> parseLine(@NotNull @NotNull String rawLine)
      Parses the given line and returns the properties that have been read.
      If the line is empty or a comment, an empty list will be returned.
      In the case of an error, the error action of the configuration will be executed.
      Parameters:
      rawLine - The line to parse
      Returns:
      The properties that have been read
      Throws:
      NullPointerException - If the line is null
      PropertySyntaxException - If the line is not parsable
    • getValuePart

      @NotNull private @NotNull String getValuePart(String @NotNull [] valueParts)
      Returns the value part of the given parts.
      If the line contains multiple separators, the value part will be concatenated with the separator.
      Parameters:
      valueParts - The parts of the line that were split at the separator
      Returns:
      The value part of the line
      Throws:
      NullPointerException - If the value parts are null
    • parseProperty

      @NotNull private @NotNull @Unmodifiable List<Property> parseProperty(@NotNull @NotNull String rawKey, @NotNull @NotNull String rawValue)
      Parses the given key and value and returns the properties that have been read.
      If advanced parsing is enabled, the key can be a compacted or variable key.
      Parameters:
      rawKey - The key to parse
      rawValue - The value to parse
      Returns:
      The properties that have been read
      Throws:
      PropertySyntaxException - If an error occurs while parsing the key or value
      See Also:
    • isAdvancedKey

      private boolean isAdvancedKey(@NotNull @NotNull String key)
      Checks if the given key is either a compacted or variable key.
      Parameters:
      key - The key to check
      Returns:
      True if the key is advanced, otherwise false
    • getWhitespaceAlignmentCount

      private int getWhitespaceAlignmentCount(@NotNull @NotNull String key)
      Returns the alignment count of the given key.
      The alignment count is the count of whitespaces at the end of the key.
      Parameters:
      key - The key to get the alignment count of
      Returns:
      The alignment count of the key
      Throws:
      NullPointerException - If the key is null
    • removeAlignment

      @NotNull private @NotNull String removeAlignment(@NotNull @NotNull String str, int alignment, boolean isKey)
      Removes the alignment from the given string.
      If the alignment is 0, the string will be returned as is.
      If the alignment is greater than 0, the alignment will be removed from the string.
      Parameters:
      str - The string to remove the alignment from
      alignment - The alignment count to remove
      isKey - True if the string is a key, otherwise false
      Returns:
      The string without the alignment
      Throws:
      NullPointerException - If the string is null
    • parsePropertySimple

      @NotNull private @NotNull Property parsePropertySimple(@NotNull @NotNull String key, @NotNull @NotNull String value)
      Parses the given key and value and returns a property.
      The key and value will be checked against the configuration.
      Parameters:
      key - The key to parse
      value - The value to parse
      Returns:
      The property that has been read
      Throws:
      PropertySyntaxException - If an error occurs while parsing the key or value
    • resolveAdvancedKeys

      @NotNull private @NotNull @Unmodifiable List<String> resolveAdvancedKeys(@NotNull @NotNull String key)
      Validates and resolves the given advanced key and returns the resolved keys.
      The key can be a compacted or variable key.
      Parameters:
      key - The key to resolve
      Returns:
      The resolved keys
      Throws:
      NullPointerException - If the key is null
      PropertySyntaxException - If the key does not match the expected format
      See Also:
    • resolveCompactedKeyPart

      private String @NotNull [] resolveCompactedKeyPart(@NotNull @NotNull String key, @NotNull @NotNull String compacted)
      Validates and resolves the given compacted key part against the expected format.
      The compacted key part must contain at least one value, a pipe separates multiple values.
      The values must not be blank, contain whitespaces or be nested.
      Parameters:
      key - The key to resolve
      compacted - The compacted key part to resolve
      Returns:
      The resolved keys
      Throws:
      NullPointerException - If the key or compacted key part is null
      PropertySyntaxException - If the compacted key part does not match the expected format
    • resolveVariableKeyPart

      @NotNull private @NotNull String resolveVariableKeyPart(@NotNull @NotNull String key, @NotNull @NotNull String variable)

      Validates and resolves the given variable key part against the expected format.
      The variable key part must contain at least one question mark but at most two.
      The first part is the target type, the second part is the target key and the third part is the optional default value.

      The target type must be one of the following:
      • <empty>, prop or property - The value will be resolved from the previously read properties of this reader
      • sys or system - The value will be resolved from the system properties
      • env or environment - The value will be resolved from the system environment
      Parameters:
      key - The key to resolve
      variable - The variable key part to resolve
      Returns:
      The resolved key
      Throws:
      NullPointerException - If the key or variable key part is null
      PropertySyntaxException - If the variable key part does not match the expected format or the value could not be resolved
      See Also:
    • resolveVariableValue

      @NotNull private @NotNull String resolveVariableValue(@NotNull @NotNull String key, @NotNull @NotNull String variable)
      Resolves the actual value of the given variable key part.

      If the target type is empty, prop or property, the value will be resolved from the previously read properties of this reader.
      If the target type is sys or system, the value will be resolved from the system properties.
      If the target type is env or environment, the value will be resolved from the system environment.

      Parameters:
      key - The key to resolve, used for error messages
      variable - The variable key part to resolve
      Returns:
      The resolved value
      Throws:
      NullPointerException - If the key or variable key part is null
      PropertySyntaxException - If the target type is not valid or the resolved value is null
    • getTargetKey

      @NotNull private @NotNull String getTargetKey(@NotNull @NotNull String key, @NotNull @NotNull String variable, String @NotNull [] parts)
      Validates and returns the target key of the given variable key part.
      The target key must not be empty, contain nested variable parts or compacted parts.
      The target key will be stripped of whitespaces.
      Parameters:
      key - The key to resolve
      variable - The variable key part to resolve
      parts - The parts of the variable key part
      Returns:
      The target key of the variable key part
      Throws:
      NullPointerException - If the key, variable key part or parts are null
      PropertySyntaxException - If the target key is not valid
    • extendResolvedKeys

      @NotNull private @NotNull List<String> extendResolvedKeys(@NotNull @NotNull List<String> resolvedKeys, String @Nullable ... keyParts)
      Extends the given list of resolved keys with the given new key parts.
      The new key parts will be appended to each resolved key:
      
       <resolved_key>.<new_key_part>
       
      Parameters:
      resolvedKeys - The resolved keys to extend
      keyParts - The new key parts to append
      Returns:
      The extended resolved keys
      Throws:
      NullPointerException - If the resolved keys are null
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException