Package jodd.props
Class Props
- java.lang.Object
-
- jodd.props.Props
-
- All Implemented Interfaces:
java.lang.Cloneable
public class Props extends java.lang.Object implements java.lang.CloneableSuper properties: fast, configurable, supports (ini) sections, profiles.Basic parsing rules:
- By default, props files are UTF8 encoded.
- Leading and trailing spaces will be trimmed from section names and property names.
- Leading and/or trailing spaces may be trimmed from property values.
- You can use either equal sign (=) or colon (:) to assign property values
- Comments begin with either a semicolon (;), or a sharp sign (#) and extend to the end of line. It doesn't have to be the first character.
- A backslash (\) escapes the next character (e.g., \# is a literal #, \\ is a literal \).
- If the last character of a line is backslash (\), the value is continued on the next line with new line character included.
- \\uXXXX is encoded as character
- \t, \r and \f are encoded as characters
Sections rules:
- Section names are enclosed between [ and ].
- Properties following a section header belong to that section. Section name is added as a prefix to section properties.
- Section ends with empty section definition [] or with new section start
Profiles rules:
- Profile names are enclosed between < and > in property key.
- Each property key may contain zero, one or more profile definitions.
Macro rules:
- Profile values may contain references to other properties using ${ and }
- Inner references are supported
- References are resolved first in the profile context and then in the base props context.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.String[]activeProfilesprotected java.lang.StringactiveProfilesPropprotected PropsDatadataprotected booleaninitializedprotected PropsParserparser
-
Constructor Summary
Constructors Modifier Constructor Description Props()Creates new props.protectedProps(PropsParser parser)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddInnerMap(java.lang.String prefix, java.util.Map<?,?> map)Adds child map to the props on given prefix.voidaddInnerMap(java.lang.String prefix, java.util.Map<?,?> map, java.lang.String profile)Adds child map to the props on given prefix.protected Propsclone()Clones props by creating new instance and copying current configuration.intcountTotalProperties()Counts the total number of properties, including all profiles.static Propscreate()Statis ctor.PropsEntriesentries()voidextractProps(java.util.Map target)Extracts props belonging to active profiles.voidextractProps(java.util.Map target, java.lang.String... profiles)Extract props of given profiles.voidextractSubProps(java.util.Map target, java.lang.String... wildcardPatterns)Extracts subset of properties that matches given wildcards.voidextractSubProps(java.util.Map target, java.lang.String[] profiles, java.lang.String[] wildcardPatterns)Extracts subset of properties that matches given wildcards.java.lang.String[]getActiveProfiles()Returns active profiles ornullif none defined.java.lang.String[]getAllProfiles()Returns all profiles names.java.lang.StringgetBaseValue(java.lang.String key)Returnsstringvalue of base property.java.lang.BooleangetBooleanValue(java.lang.String key)Returns boolean value of given property ornullif property not found.java.lang.BooleangetBooleanValue(java.lang.String key, java.lang.Boolean defaultValue)Returns boolean value or default one if property not defined.java.lang.BooleangetBooleanValue(java.lang.String key, java.lang.Boolean defaultValue, java.lang.String... profiles)java.lang.BooleangetBooleanValue(java.lang.String key, java.lang.String... profiles)java.lang.DoublegetDoubleValue(java.lang.String key)Returns double value of given property ornullif property not found.java.lang.DoublegetDoubleValue(java.lang.String key, java.lang.Double defaultValue)Returns double value or default one if property not defined.java.lang.DoublegetDoubleValue(java.lang.String key, java.lang.Double defaultValue, java.lang.String... profiles)java.lang.DoublegetDoubleValue(java.lang.String key, java.lang.String... profiles)java.lang.IntegergetIntegerValue(java.lang.String key)Returns integer value of given property ornullif property not found.java.lang.IntegergetIntegerValue(java.lang.String key, java.lang.Integer defaultValue)Returns integer value or default one if property not defined.java.lang.IntegergetIntegerValue(java.lang.String key, java.lang.Integer defaultValue, java.lang.String... profiles)java.lang.IntegergetIntegerValue(java.lang.String key, java.lang.String... profiles)java.lang.LonggetLongValue(java.lang.String key)Returns long value of given property ornullif property not found.java.lang.LonggetLongValue(java.lang.String key, java.lang.Long defaultValue)Returns long value or default one if property not defined.java.lang.LonggetLongValue(java.lang.String key, java.lang.Long defaultValue, java.lang.String... profiles)java.lang.LonggetLongValue(java.lang.String key, java.lang.String... profiles)java.lang.String[]getProfilesFor(java.lang.String propKeyNameWildcard)Returns all the profiles that define certain prop's key name.java.lang.StringgetValue(java.lang.String key)Returns value of property, using active profiles, ornullif property not found.java.lang.StringgetValue(java.lang.String key, java.lang.String... profiles)Returnsstringvalue of given profiles.java.lang.StringgetValueOrDefault(java.lang.String key, java.lang.String defaultValue)Returns value of property, using active profiles or default value if not found.protected voidinitialize()Initializes props.java.util.Map<java.lang.String,java.lang.Object>innerMap(java.lang.String prefix)Returns inner map from the props with given prefix.java.util.Iterator<PropsEntry>iterator()Returns iterator for active profiles.Propsload(java.io.File file)Loads props from the file.Propsload(java.io.File file, java.lang.String encoding)Loads properties from the file in provided encoding.Propsload(java.io.InputStream in)Loads properties from input stream.Propsload(java.io.InputStream in, java.lang.String encoding)Loads properties from input stream and provided encoding.Propsload(java.lang.String data)Loads props from the string.Propsload(java.util.Map<?,?> p)Loads base properties from the provided java properties.Propsload(java.util.Map<?,?> map, java.lang.String prefix)Loads base properties from java Map using provided prefix.PropsloadEnvironment(java.lang.String prefix)Loads environment properties with given prefix.PropsloadSystemProperties(java.lang.String prefix)Loads system properties with given prefix.protected voidparse(java.lang.String data)Parses input string and loads provided properties map.protected voidresolveActiveProfiles()Resolves active profiles from special property.PropssetActiveProfiles(java.lang.String... activeProfiles)Sets new active profiles and overrides existing ones.PropssetAppendDuplicateProps(boolean appendDuplicateProps)Appends duplicate props.PropssetEscapeNewLineValue(java.lang.String escapeNewLineValue)Specifies the new line string when EOL is escaped.PropssetIgnoreMissingMacros(boolean ignoreMissingMacros)Ignore missing macros by replacing them with an empty string.PropssetIgnorePrefixWhitespacesOnNewLine(boolean ignorePrefixWhitespacesOnNewLine)Defines if the prefix whitespaces should be ignored when value is split into the lines.PropssetMultilineValues(boolean multilineValues)Enables multiline values.PropssetSkipEmptyProps(boolean skipEmptyProps)Skips empty properties as they don't exist.voidsetValue(java.lang.String key, java.lang.String value)Sets default value.voidsetValue(java.lang.String key, java.lang.String value, java.lang.String profile)Sets value on some profile.PropssetValueTrimLeft(boolean valueTrimLeft)Specifies should the values be trimmed from the left.PropssetValueTrimRight(boolean valueTrimRight)Specifies should the values be trimmed from the right.
-
-
-
Field Detail
-
parser
protected final PropsParser parser
-
data
protected final PropsData data
-
activeProfilesProp
protected java.lang.String activeProfilesProp
-
activeProfiles
protected java.lang.String[] activeProfiles
-
initialized
protected volatile boolean initialized
-
-
Constructor Detail
-
Props
public Props()
Creates new props.
-
Props
protected Props(PropsParser parser)
-
-
Method Detail
-
create
public static Props create()
Statis ctor.
-
clone
protected Props clone()
Clones props by creating new instance and copying current configuration.- Overrides:
clonein classjava.lang.Object
-
getActiveProfiles
public java.lang.String[] getActiveProfiles()
Returns active profiles ornullif none defined.
-
setActiveProfiles
public Props setActiveProfiles(java.lang.String... activeProfiles)
Sets new active profiles and overrides existing ones. By settingnull, no active profile will be set.Note that if some props are loaded after this method call, they might override active profiles by using special property for active profiles (
@profiles).
-
setEscapeNewLineValue
public Props setEscapeNewLineValue(java.lang.String escapeNewLineValue)
Specifies the new line string when EOL is escaped. Default value is an empty string.
-
setValueTrimLeft
public Props setValueTrimLeft(boolean valueTrimLeft)
Specifies should the values be trimmed from the left. Default istrue.
-
setValueTrimRight
public Props setValueTrimRight(boolean valueTrimRight)
Specifies should the values be trimmed from the right. Default istrue.
-
setIgnorePrefixWhitespacesOnNewLine
public Props setIgnorePrefixWhitespacesOnNewLine(boolean ignorePrefixWhitespacesOnNewLine)
Defines if the prefix whitespaces should be ignored when value is split into the lines.
-
setSkipEmptyProps
public Props setSkipEmptyProps(boolean skipEmptyProps)
Skips empty properties as they don't exist.
-
setAppendDuplicateProps
public Props setAppendDuplicateProps(boolean appendDuplicateProps)
Appends duplicate props.
-
setIgnoreMissingMacros
public Props setIgnoreMissingMacros(boolean ignoreMissingMacros)
Ignore missing macros by replacing them with an empty string.
-
setMultilineValues
public Props setMultilineValues(boolean multilineValues)
Enables multiline values.
-
parse
protected void parse(java.lang.String data)
Parses input string and loads provided properties map.
-
load
public Props load(java.lang.String data)
Loads props from the string.
-
load
public Props load(java.io.File file) throws java.io.IOException
Loads props from the file. Assumes UTF8 encoding unless the file ends with '.properties', than it uses ISO 8859-1.- Throws:
java.io.IOException
-
load
public Props load(java.io.File file, java.lang.String encoding) throws java.io.IOException
Loads properties from the file in provided encoding.- Throws:
java.io.IOException
-
load
public Props load(java.io.InputStream in) throws java.io.IOException
Loads properties from input stream. Stream is not closed at the end.- Throws:
java.io.IOException
-
load
public Props load(java.io.InputStream in, java.lang.String encoding) throws java.io.IOException
Loads properties from input stream and provided encoding. Stream is not closed at the end.- Throws:
java.io.IOException
-
load
public Props load(java.util.Map<?,?> p)
Loads base properties from the provided java properties. Null values are ignored.
-
load
public Props load(java.util.Map<?,?> map, java.lang.String prefix)
Loads base properties from java Map using provided prefix. Null values are ignored.
-
loadSystemProperties
public Props loadSystemProperties(java.lang.String prefix)
Loads system properties with given prefix. If prefix isnullit will not be ignored.
-
loadEnvironment
public Props loadEnvironment(java.lang.String prefix)
Loads environment properties with given prefix. If prefix isnullit will not be used.
-
countTotalProperties
public int countTotalProperties()
Counts the total number of properties, including all profiles. This operation performs calculation each time and it might be more time consuming then expected.
-
getBaseValue
public java.lang.String getBaseValue(java.lang.String key)
Returnsstringvalue of base property. Returnsnullif property doesn't exist.
-
getValue
public java.lang.String getValue(java.lang.String key)
Returns value of property, using active profiles, ornullif property not found.
-
getValueOrDefault
public java.lang.String getValueOrDefault(java.lang.String key, java.lang.String defaultValue)Returns value of property, using active profiles or default value if not found.
-
getIntegerValue
public java.lang.Integer getIntegerValue(java.lang.String key)
Returns integer value of given property ornullif property not found.
-
getIntegerValue
public java.lang.Integer getIntegerValue(java.lang.String key, java.lang.Integer defaultValue)Returns integer value or default one if property not defined.
-
getLongValue
public java.lang.Long getLongValue(java.lang.String key)
Returns long value of given property ornullif property not found.
-
getLongValue
public java.lang.Long getLongValue(java.lang.String key, java.lang.Long defaultValue)Returns long value or default one if property not defined.
-
getDoubleValue
public java.lang.Double getDoubleValue(java.lang.String key)
Returns double value of given property ornullif property not found.
-
getDoubleValue
public java.lang.Double getDoubleValue(java.lang.String key, java.lang.Double defaultValue)Returns double value or default one if property not defined.
-
getBooleanValue
public java.lang.Boolean getBooleanValue(java.lang.String key)
Returns boolean value of given property ornullif property not found.
-
getBooleanValue
public java.lang.Boolean getBooleanValue(java.lang.String key, java.lang.Boolean defaultValue)Returns boolean value or default one if property not defined.
-
getValue
public java.lang.String getValue(java.lang.String key, java.lang.String... profiles)Returnsstringvalue of given profiles. If key is not found under listed profiles, base properties will be searched. Returnsnullif property doesn't exist.
-
getIntegerValue
public java.lang.Integer getIntegerValue(java.lang.String key, java.lang.String... profiles)
-
getIntegerValue
public java.lang.Integer getIntegerValue(java.lang.String key, java.lang.Integer defaultValue, java.lang.String... profiles)
-
getLongValue
public java.lang.Long getLongValue(java.lang.String key, java.lang.String... profiles)
-
getLongValue
public java.lang.Long getLongValue(java.lang.String key, java.lang.Long defaultValue, java.lang.String... profiles)
-
getDoubleValue
public java.lang.Double getDoubleValue(java.lang.String key, java.lang.String... profiles)
-
getDoubleValue
public java.lang.Double getDoubleValue(java.lang.String key, java.lang.Double defaultValue, java.lang.String... profiles)
-
getBooleanValue
public java.lang.Boolean getBooleanValue(java.lang.String key, java.lang.String... profiles)
-
getBooleanValue
public java.lang.Boolean getBooleanValue(java.lang.String key, java.lang.Boolean defaultValue, java.lang.String... profiles)
-
setValue
public void setValue(java.lang.String key, java.lang.String value)Sets default value.
-
setValue
public void setValue(java.lang.String key, java.lang.String value, java.lang.String profile)Sets value on some profile.
-
extractProps
public void extractProps(java.util.Map target)
Extracts props belonging to active profiles.
-
extractProps
public void extractProps(java.util.Map target, java.lang.String... profiles)Extract props of given profiles.
-
extractSubProps
public void extractSubProps(java.util.Map target, java.lang.String... wildcardPatterns)Extracts subset of properties that matches given wildcards.
-
extractSubProps
public void extractSubProps(java.util.Map target, java.lang.String[] profiles, java.lang.String[] wildcardPatterns)Extracts subset of properties that matches given wildcards.
-
innerMap
public java.util.Map<java.lang.String,java.lang.Object> innerMap(java.lang.String prefix)
Returns inner map from the props with given prefix. Keys in returned map will not have the prefix.
-
addInnerMap
public void addInnerMap(java.lang.String prefix, java.util.Map<?,?> map)Adds child map to the props on given prefix.
-
addInnerMap
public void addInnerMap(java.lang.String prefix, java.util.Map<?,?> map, java.lang.String profile)Adds child map to the props on given prefix.
-
initialize
protected void initialize()
Initializes props. By default it only resolves active profiles.
-
resolveActiveProfiles
protected void resolveActiveProfiles()
Resolves active profiles from special property. This property can be only a base property! If default active property is not defined, nothing happens. Otherwise, it will replace currently active profiles.
-
getAllProfiles
public java.lang.String[] getAllProfiles()
Returns all profiles names.
-
getProfilesFor
public java.lang.String[] getProfilesFor(java.lang.String propKeyNameWildcard)
Returns all the profiles that define certain prop's key name. Key name is given as a wildcard, or it can be matched fully.
-
entries
public PropsEntries entries()
-
iterator
public java.util.Iterator<PropsEntry> iterator()
Returns iterator for active profiles.
-
-