Package jodd.props

Class Props

java.lang.Object
jodd.props.Props
All Implemented Interfaces:
Cloneable

public class Props extends Object implements Cloneable
Super 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 Details

    • DEFAULT_PROFILES_PROP

      private static final String DEFAULT_PROFILES_PROP
      See Also:
    • parser

      protected final PropsParser parser
    • data

      protected final PropsData data
    • activeProfilesProp

      protected String activeProfilesProp
    • activeProfiles

      protected String[] activeProfiles
    • initialized

      protected volatile boolean initialized
  • Constructor Details

    • Props

      public Props()
      Creates new props.
    • Props

      protected Props(PropsParser parser)
  • Method Details

    • create

      public static Props create()
      Statis ctor.
    • clone

      protected Props clone()
      Clones props by creating new instance and copying current configuration.
      Overrides:
      clone in class Object
    • getActiveProfiles

      public String[] getActiveProfiles()
      Returns active profiles or null if none defined.
    • setActiveProfiles

      public Props setActiveProfiles(String... activeProfiles)
      Sets new active profiles and overrides existing ones. By setting null, 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(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 is true.
    • setValueTrimRight

      public Props setValueTrimRight(boolean valueTrimRight)
      Specifies should the values be trimmed from the right. Default is true.
    • 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(String data)
      Parses input string and loads provided properties map.
    • load

      public Props load(String data)
      Loads props from the string.
    • load

      public Props load(File file) throws IOException
      Loads props from the file. Assumes UTF8 encoding unless the file ends with '.properties', than it uses ISO 8859-1.
      Throws:
      IOException
    • load

      public Props load(File file, String encoding) throws IOException
      Loads properties from the file in provided encoding.
      Throws:
      IOException
    • load

      public Props load(InputStream in) throws IOException
      Loads properties from input stream. Stream is not closed at the end.
      Throws:
      IOException
    • load

      public Props load(InputStream in, String encoding) throws IOException
      Loads properties from input stream and provided encoding. Stream is not closed at the end.
      Throws:
      IOException
    • load

      public Props load(Map<?,?> p)
      Loads base properties from the provided java properties. Null values are ignored.
    • load

      public Props load(Map<?,?> map, String prefix)
      Loads base properties from java Map using provided prefix. Null values are ignored.
    • loadSystemProperties

      public Props loadSystemProperties(String prefix)
      Loads system properties with given prefix. If prefix is null it will not be ignored.
    • loadEnvironment

      public Props loadEnvironment(String prefix)
      Loads environment properties with given prefix. If prefix is null it 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 String getBaseValue(String key)
      Returns string value of base property. Returns null if property doesn't exist.
    • getValue

      public String getValue(String key)
      Returns value of property, using active profiles, or null if property not found.
    • getValueOrDefault

      public String getValueOrDefault(String key, String defaultValue)
      Returns value of property, using active profiles or default value if not found.
    • getIntegerValue

      public Integer getIntegerValue(String key)
      Returns integer value of given property or null if property not found.
    • getIntegerValue

      public Integer getIntegerValue(String key, Integer defaultValue)
      Returns integer value or default one if property not defined.
    • getLongValue

      public Long getLongValue(String key)
      Returns long value of given property or null if property not found.
    • getLongValue

      public Long getLongValue(String key, Long defaultValue)
      Returns long value or default one if property not defined.
    • getDoubleValue

      public Double getDoubleValue(String key)
      Returns double value of given property or null if property not found.
    • getDoubleValue

      public Double getDoubleValue(String key, Double defaultValue)
      Returns double value or default one if property not defined.
    • getBooleanValue

      public Boolean getBooleanValue(String key)
      Returns boolean value of given property or null if property not found.
    • getBooleanValue

      public Boolean getBooleanValue(String key, Boolean defaultValue)
      Returns boolean value or default one if property not defined.
    • getValue

      public String getValue(String key, String... profiles)
      Returns string value of given profiles. If key is not found under listed profiles, base properties will be searched. Returns null if property doesn't exist.
    • getIntegerValue

      public Integer getIntegerValue(String key, String... profiles)
    • getIntegerValue

      public Integer getIntegerValue(String key, Integer defaultValue, String... profiles)
    • getLongValue

      public Long getLongValue(String key, String... profiles)
    • getLongValue

      public Long getLongValue(String key, Long defaultValue, String... profiles)
    • getDoubleValue

      public Double getDoubleValue(String key, String... profiles)
    • getDoubleValue

      public Double getDoubleValue(String key, Double defaultValue, String... profiles)
    • getBooleanValue

      public Boolean getBooleanValue(String key, String... profiles)
    • getBooleanValue

      public Boolean getBooleanValue(String key, Boolean defaultValue, String... profiles)
    • setValue

      public void setValue(String key, String value)
      Sets default value.
    • setValue

      public void setValue(String key, String value, String profile)
      Sets value on some profile.
    • extractProps

      public void extractProps(Map target)
      Extracts props belonging to active profiles.
    • extractProps

      public void extractProps(Map target, String... profiles)
      Extract props of given profiles.
    • extractSubProps

      public void extractSubProps(Map target, String... wildcardPatterns)
      Extracts subset of properties that matches given wildcards.
    • extractSubProps

      public void extractSubProps(Map target, String[] profiles, String[] wildcardPatterns)
      Extracts subset of properties that matches given wildcards.
    • innerMap

      public Map<String,Object> innerMap(String prefix)
      Returns inner map from the props with given prefix. Keys in returned map will not have the prefix.
    • addInnerMap

      public void addInnerMap(String prefix, Map<?,?> map)
      Adds child map to the props on given prefix.
    • addInnerMap

      public void addInnerMap(String prefix, Map<?,?> map, 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 String[] getAllProfiles()
      Returns all profiles names.
    • getProfilesFor

      public String[] getProfilesFor(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()
      Returns builder for entries itertor.
    • iterator

      public Iterator<PropsEntry> iterator()
      Returns iterator for active profiles.