Class UtilProperties

java.lang.Object
org.apache.ofbiz.base.util.UtilProperties
All Implemented Interfaces:
Serializable

public final class UtilProperties extends Object implements Serializable
Generic Property Accessor with Cache - Utilities for working with properties files.

UtilProperties divides properties files into two classes: non-locale-specific - which are used for application parameters, configuration settings, etc; and locale-specific - which are used for UI labels, system messages, etc. Each class of properties files is kept in its own cache.

The locale-specific class of properties files can be in any one of three formats: the standard text-based key=value format (*.properties file), the Java XML properties format, and the OFBiz-specific XML file format (see the xmlToProperties method).

See Also:
  • Method Details

    • propertyValueEquals

      public static boolean propertyValueEquals(String resource, String name, String compareString)
      Compares the specified property to the compareString, returns true if they are the same, false otherwise
      Parameters:
      resource - The name of the resource - if the properties file is 'webevent.properties', the resource name is 'webevent'
      name - The name of the property in the properties file
      compareString - The String to compare the property value to
      Returns:
      True if the strings are the same, false otherwise
    • propertyValueEqualsIgnoreCase

      public static boolean propertyValueEqualsIgnoreCase(String resource, String name, String compareString)
      Compares Ignoring Case the specified property to the compareString, returns true if they are the same, false otherwise
      Parameters:
      resource - The name of the resource - if the properties file is 'webevent.properties', the resource name is 'webevent'
      name - The name of the property in the properties file
      compareString - The String to compare the property value to
      Returns:
      True if the strings are the same, false otherwise
    • getPropertyValue

      public static String getPropertyValue(String resource, String name, String defaultValue)
      Returns the value of the specified property name from the specified resource/properties file. If the specified property name or properties file is not found, the defaultValue is returned.
      Parameters:
      resource - The name of the resource - if the properties file is 'webevent.properties', the resource name is 'webevent'
      name - The name of the property in the properties file
      defaultValue - The value to return if the property is not found
      Returns:
      The value of the property in the properties file, or if not found then the defaultValue
    • getPropertyNumber

      public static double getPropertyNumber(String resource, String name, double defaultValue)
    • getPropertyNumber

      public static double getPropertyNumber(String resource, String name)
    • getPropertyAsBoolean

      public static Boolean getPropertyAsBoolean(String resource, String name, boolean defaultValue)
      Returns a Boolean-Object of the specified property name from the specified resource/properties file. If the specified property name or properties file is not found, the defaultValue is returned.
      Parameters:
      resource - The name of the resource - if the properties file is 'webevent.properties', the resource name is 'webevent'
      name - The name of the property in the properties file
      defaultValue - Optional: The Value to return if the property is not found or not the correct format.
      Returns:
      A Boolean-Object of the property; or if not found the defaultValue
    • getPropertyAsInteger

      public static Integer getPropertyAsInteger(String resource, String name, int defaultNumber)
      Returns an Integer-Object of the specified property name from the specified resource/properties file. If the specified property name or properties file is not found, the defaultNumber is returned.
      Parameters:
      resource - The name of the resource - if the properties file is 'webevent.properties', the resource name is 'webevent'
      name - The name of the property in the properties file
      defaultNumber - Optional: The Value to return if the property is not found.
      Returns:
      An Integer-Object of the property; or if not found the defaultNumber
    • getPropertyAsLong

      public static Long getPropertyAsLong(String resource, String name, long defaultNumber)
      Returns a Long-Object of the specified property name from the specified resource/properties file. If the specified property name or properties file is not found, the defaultNumber is returned.
      Parameters:
      resource - The name of the resource - if the properties file is 'webevent.properties', the resource name is 'webevent'
      name - The name of the property in the properties file
      defaultNumber - Optional: The Value to return if the property is not found.
      Returns:
      A Long-Object of the property; or if not found the defaultNumber
    • getPropertyAsFloat

      public static Float getPropertyAsFloat(String resource, String name, float defaultNumber)
      Returns a Float-Object of the specified property name from the specified resource/properties file. If the specified property name or properties file is not found, the defaultNumber is returned.
      Parameters:
      resource - The name of the resource - if the properties file is 'webevent.properties', the resource name is 'webevent'
      name - The name of the property in the properties file
      defaultNumber - Optional: The Value to return if the property is not found.
      Returns:
      A Long-Object of the property; or if not found the defaultNumber
    • getPropertyAsDouble

      public static Double getPropertyAsDouble(String resource, String name, double defaultNumber)
      Returns a Double-Object of the specified property name from the specified resource/properties file. If the specified property name or properties file is not found, the defaultNumber is returned.
      Parameters:
      resource - The name of the resource - if the properties file is 'webevent.properties', the resource name is 'webevent'
      name - The name of the property in the properties file
      defaultNumber - Optional: The Value to return if the property is not found.
      Returns:
      A Double-Object of the property; or if not found the defaultNumber
    • getPropertyAsBigInteger

      public static BigInteger getPropertyAsBigInteger(String resource, String name, BigInteger defaultNumber)
      Returns a BigInteger-Object of the specified property name from the specified resource/properties file. If the specified property name or properties file is not found, the defaultNumber is returned.
      Parameters:
      resource - The name of the resource - if the properties file is 'webevent.properties', the resource name is 'webevent'
      name - The name of the property in the properties file
      defaultNumber - Optional: The Value to return if the property is not found.
      Returns:
      A BigInteger-Object of the property; or if not found the defaultNumber
    • getPropertyAsBigDecimal

      public static BigDecimal getPropertyAsBigDecimal(String resource, String name, BigDecimal defaultNumber)
      Returns a BigDecimal-Object of the specified property name from the specified resource/properties file. If the specified property name or properties file is not found, the defaultNumber is returned.
      Parameters:
      resource - The name of the resource - if the properties file is 'webevent.properties', the resource name is 'webevent'
      name - The name of the property in the properties file
      defaultNumber - Optional: The Value to return if the property is not found.
      Returns:
      A BigDecimal-Object of the property; or if not found the defaultNumber
    • getPropertyValue

      public static String getPropertyValue(String resource, String name)
      Returns the value of the specified property name from the specified resource/properties file
      Parameters:
      resource - The name of the resource - can be a file, class, or URL
      name - The name of the property in the properties file
      Returns:
      The value of the property in the properties file
    • createProperties

      public static Properties createProperties(String fileName)
      Returns a new Properties instance created from fileName.

      This method is intended for low-level framework classes that need to read properties files before OFBiz has been fully initialized.

      Parameters:
      fileName - The full name of the properties file ("foo.properties")
      Returns:
      A new Properties instance created from fileName, or null if the file was not found
      Throws:
      IllegalArgumentException - if fileName is empty
      IllegalStateException - if there was a problem reading the file
    • getProperties

      public static Properties getProperties(String resource)
      Returns the specified resource/properties file
      Parameters:
      resource - The name of the resource - can be a file, class, or URL
      Returns:
      The properties file
    • getProperties

      public static Properties getProperties(URL url)
      Returns the specified resource/properties file
      Parameters:
      url - The URL to the resource
      Returns:
      The properties file
    • propertyValueEquals

      public static boolean propertyValueEquals(URL url, String name, String compareString)
      Compares the specified property to the compareString, returns true if they are the same, false otherwise
      Parameters:
      url - URL object specifying the location of the resource
      name - The name of the property in the properties file
      compareString - The String to compare the property value to
      Returns:
      True if the strings are the same, false otherwise
    • propertyValueEqualsIgnoreCase

      public static boolean propertyValueEqualsIgnoreCase(URL url, String name, String compareString)
      Compares Ignoring Case the specified property to the compareString, returns true if they are the same, false otherwise
      Parameters:
      url - URL object specifying the location of the resource
      name - The name of the property in the properties file
      compareString - The String to compare the property value to
      Returns:
      True if the strings are the same, false otherwise
    • getPropertyValue

      public static String getPropertyValue(URL url, String name, String defaultValue)
      Returns the value of the specified property name from the specified resource/properties file. If the specified property name or properties file is not found, the defaultValue is returned.
      Parameters:
      url - URL object specifying the location of the resource
      name - The name of the property in the properties file
      defaultValue - The value to return if the property is not found
      Returns:
      The value of the property in the properties file, or if not found then the defaultValue
    • getPropertyNumber

      public static double getPropertyNumber(URL url, String name, double defaultValue)
    • getPropertyNumber

      public static double getPropertyNumber(URL url, String name)
    • getPropertyValue

      public static String getPropertyValue(URL url, String name)
      Returns the value of the specified property name from the specified resource/properties file
      Parameters:
      url - URL object specifying the location of the resource
      name - The name of the property in the properties file
      Returns:
      The value of the property in the properties file
    • getSplitPropertyValue

      public static String getSplitPropertyValue(URL url, String name)
      Returns the value of a split property name from the specified resource/properties file Rather than specifying the property name the value of a name.X property is specified which will correspond to a value.X property whose value will be returned. X is a number from 1 to whatever and all values are checked until a name.X for a certain X is not found.
      Parameters:
      url - URL object specifying the location of the resource
      name - The name of the split property in the properties file
      Returns:
      The value of the split property from the properties file
    • setPropertyValueInMemory

      public static void setPropertyValueInMemory(String resource, String name, String value)
      Sets the specified value of the specified property name to the specified resource/properties in memory, does not persist it
      Parameters:
      resource - The name of the resource
      name - The name of the property in the resource
      value - The value of the property to set in memory
    • setPropertyValueInMemory

      public static void setPropertyValueInMemory(Delegator delegator, String resource, String name, String value)
      Sets the specified value of the specified property name to the specified resource/properties in memory, does not persist it
      Parameters:
      delegator - Default delegator, mostly used in tests
      resource - The name of the resource
      name - The name of the property in the resource
      value - The value of the property to set in memory
    • getMessage

      public static String getMessage(String resource, String name, Locale locale)
      Returns the value of the specified property name from the specified resource/properties file corresponding to the given locale.
      Parameters:
      resource - The name of the resource - can be a file, class, or URL
      name - The name of the property in the properties file
      locale - The locale that the given resource will correspond to
      Returns:
      The value of the property in the properties file
    • getMessage

      public static String getMessage(String resource, String name, Object[] arguments, Locale locale)
      Returns the value of the specified property name from the specified resource/properties file corresponding to the given locale and replacing argument place holders with the given arguments using the MessageFormat class
      Parameters:
      resource - The name of the resource - can be a file, class, or URL
      name - The name of the property in the properties file
      arguments - An array of Objects to insert into the message argument place holders
      locale - The locale that the given resource will correspond to
      Returns:
      The value of the property in the properties file
    • getMessage

      public static <E> String getMessage(String resource, String name, List<E> arguments, Locale locale)
      Returns the value of the specified property name from the specified resource/properties file corresponding to the given locale and replacing argument place holders with the given arguments using the MessageFormat class
      Parameters:
      resource - The name of the resource - can be a file, class, or URL
      name - The name of the property in the properties file
      arguments - A List of Objects to insert into the message argument place holders
      locale - The locale that the given resource will correspond to
      Returns:
      The value of the property in the properties file
    • getMessageList

      public static String getMessageList(String resource, String name, Locale locale, Object... arguments)
    • getMessage

      public static String getMessage(String resource, String name, Map<String,? extends Object> context, Locale locale)
      Returns the value of the specified property name from the specified resource/properties file corresponding to the given locale and replacing argument place holders with the given arguments using the FlexibleStringExpander class
      Parameters:
      resource - The name of the resource - can be a file, class, or URL
      name - The name of the property in the properties file
      context - A Map of Objects to insert into the message place holders using the ${} syntax of the FlexibleStringExpander
      locale - The locale that the given resource will correspond to
      Returns:
      The value of the property in the properties file
    • getMessageMap

      public static String getMessageMap(String resource, String name, Locale locale, Object... context)
    • getResourceBundle

      public static ResourceBundle getResourceBundle(String resource, Locale locale)
      Returns the specified resource/properties file as a ResourceBundle
      Parameters:
      resource - The name of the resource - can be a file, class, or URL
      locale - The locale that the given resource will correspond to
      Returns:
      The ResourceBundle
    • getResourceBundleMap

      public static ResourceBundleMapWrapper getResourceBundleMap(String resource, Locale locale)
      Returns the specified resource/properties file as a Map with the original ResourceBundle in the Map under the key _RESOURCE_BUNDLE_
      Parameters:
      resource - The name of the resource - can be a file, class, or URL
      locale - The locale that the given resource will correspond to
      Returns:
      Map containing all entries in The ResourceBundle
    • getResourceBundleMap

      public static ResourceBundleMapWrapper getResourceBundleMap(String resource, Locale locale, Map<String,Object> context)
      Returns the specified resource/properties file as a Map with the original ResourceBundle in the Map under the key _RESOURCE_BUNDLE_
      Parameters:
      resource - The name of the resource - can be a file, class, or URL
      locale - The locale that the given resource will correspond to
      context - The screen rendering context
      Returns:
      Map containing all entries in The ResourceBundle
    • getProperties

      public static Properties getProperties(String resource, Locale locale)
      Returns the specified resource/properties file.

      Note that this method will return a Properties instance for the specified locale only - if you need I18n properties, then use getResourceBundle(String resource, Locale locale). This method is intended to be used primarily by the UtilProperties class.

      Parameters:
      resource - The name of the resource - can be a file, class, or URL
      locale - The desired locale
      Returns:
      The Properties instance, or null if no matching properties are found
    • getFallbackLocale

      public static Locale getFallbackLocale()
      Returns the configured fallback locale. UtilProperties uses this locale to resolve locale-specific XML properties.

      The fallback locale can be configured using the locale.properties.fallback property in general.properties.

      Returns:
      The configured fallback locale
    • localeToCandidateList

      public static List<Locale> localeToCandidateList(Locale locale)
      Converts a Locale instance to a candidate Locale list. The list is ordered most-specific to least-specific. Example: localeToCandidateList(Locale.US) would return a list containing en_US and en.
      Returns:
      A list of candidate locales.
    • getDefaultCandidateLocales

      public static Set<Locale> getDefaultCandidateLocales()
      Returns the default candidate Locale list. The list is populated with the JVM's default locale, the OFBiz fallback locale, and the LOCALE_ROOT (empty) locale - in that order.
      Returns:
      A list of default candidate locales.
    • getCandidateLocales

      public static List<Locale> getCandidateLocales(Locale locale)
      Returns a list of candidate locales based on a supplied locale. The returned list consists of the supplied locale and the default candidate locales - in that order.
      Parameters:
      locale - The desired locale
      Returns:
      A list of candidate locales
    • createResourceName

      public static String createResourceName(String resource, Locale locale, boolean removeExtension)
      Create a localized resource name based on a resource name and a locale.
      Parameters:
      resource - The desired resource
      locale - The desired locale
      removeExtension - Remove file extension from resource String
      Returns:
      Localized resource name
    • isPropertiesResourceNotFound

      public static boolean isPropertiesResourceNotFound(String resource, Locale locale, boolean removeExtension)
    • resolvePropertiesUrl

      public static URL resolvePropertiesUrl(String resource, Locale locale)
      Resolve a properties file URL.

      This method uses the following strategy:

      • Locate the XML file specified in resource (MyProps.xml)
      • Locate the file that starts with the name specified in resource and ends with the locale's string and .xml (MyProps_en.xml)
      • Locate the file that starts with the name specified in resource and ends with the locale's string and .properties (MyProps_en.properties)
      • Locate the file that starts with the name specified in resource and ends with the locale's string (MyProps_en)
      The component:// protocol is supported in the resource parameter.
      Parameters:
      resource - The resource to resolve
      locale - The desired locale
      Returns:
      A URL instance or null if not found.
    • xmlToProperties

      public static Properties xmlToProperties(InputStream in, Locale locale, Properties properties) throws IOException, InvalidPropertiesFormatException
      Convert XML property file to Properties instance. This method will convert both the Java XML properties file format and the OFBiz custom XML properties file format.

      The format of the custom XML properties file is:

       
       <resource>
           <property key="key">
           <value xml:lang="locale 1">Some value</value>
           <value xml:lang="locale 2">Some value</value>
           ...
           </property>
           ...
       </resource>
       
       
      where "locale 1", "locale 2" are valid xml:lang values..
      Parameters:
      in - XML file InputStream
      locale - The desired locale
      properties - Optional Properties object to populate
      Returns:
      Properties instance or null if not found
      Throws:
      IOException
      InvalidPropertiesFormatException