- All Implemented Interfaces:
AutoCloseable
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
orproperty
- The value will be resolved from the previously read properties of this readersys
orsystem
- The value will be resolved from the system propertiesenv
orenvironment
- The value will be resolved from the system environment
<key_part>.?{<target_type>?<variable_key>?<default_value>}.<key_part><separator><value>
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Pattern
The pattern to check if a key is a compacted key.private final PropertyConfig
The configuration for this reader.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
ConstructorsConstructorDescriptionPropertyReader
(@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 TypeMethodDescriptionvoid
close()
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
getWhitespaceAlignmentCount
(@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.private boolean
isAdvancedKey
(@NotNull String key) Checks if the given key is either a compacted or variable key.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.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.resolveAdvancedKeys
(@NotNull String key) 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.
-
Field Details
-
COMPACTED_KEY_PATTERN
The pattern to check if a key is a compacted key. -
VARIABLE_KEY_PATTERN
The pattern to check if a key is a variable key. -
properties
A cache for all properties that have been read. -
config
The configuration for this reader. -
reader
The internal io reader to read the properties.
-
-
Constructor Details
-
PropertyReader
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 forconfig
- 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 fromscope
- The scope which is expected- Returns:
- The content of the reader without the scope
-
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
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 nullPropertySyntaxException
- If the line is not parsable
-
getValuePart
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 parserawValue
- 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
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
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 fromalignment
- The alignment count to removeisKey
- 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 parsevalue
- 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 nullPropertySyntaxException
- 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 resolvecompacted
- The compacted key part to resolve- Returns:
- The resolved keys
- Throws:
NullPointerException
- If the key or compacted key part is nullPropertySyntaxException
- 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 target type must be one of the following:
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.
<empty>
,prop
orproperty
- The value will be resolved from the previously read properties of this readersys
orsystem
- The value will be resolved from the system propertiesenv
orenvironment
- The value will be resolved from the system environment
- Parameters:
key
- The key to resolvevariable
- The variable key part to resolve- Returns:
- The resolved key
- Throws:
NullPointerException
- If the key or variable key part is nullPropertySyntaxException
- 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
orproperty
, the value will be resolved from the previously read properties of this reader.
If the target type issys
orsystem
, the value will be resolved from the system properties.
If the target type isenv
orenvironment
, the value will be resolved from the system environment.
- Parameters:
key
- The key to resolve, used for error messagesvariable
- The variable key part to resolve- Returns:
- The resolved value
- Throws:
NullPointerException
- If the key or variable key part is nullPropertySyntaxException
- 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 resolvevariable
- The variable key part to resolveparts
- 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 nullPropertySyntaxException
- 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 extendkeyParts
- The new key parts to append- Returns:
- The extended resolved keys
- Throws:
NullPointerException
- If the resolved keys are null
-
close
- Specified by:
close
in interfaceAutoCloseable
- Throws:
IOException
-