Class UtilProperties

java.lang.Object
org.ofbiz.core.util.UtilProperties

public class UtilProperties extends Object
Generic Property Accessor with Cache - Utilities for working with properties files
Version:
1.0
Author:
David E. Jones
  • Field Details

    • resourceCache

      public static UtilCache<Object,FlexibleProperties> resourceCache
      An instance of the generic cache for storing the FlexibleProperties corresponding to each properties file keyed by a String for the resource location. This will be used for both non-locale and locale keyed FexibleProperties instances.
    • urlCache

      public static UtilCache<URL,FlexibleProperties> urlCache
      An instance of the generic cache for storing the FlexibleProperties corresponding to each properties file keyed by a URL object
    • bundleLocaleCache

      public static UtilCache<String,ResourceBundle> bundleLocaleCache
      An instance of the generic cache for storing the ResourceBundle corresponding to each properties file keyed by a String for the resource location and the locale
  • Constructor Details

    • UtilProperties

      public UtilProperties()
  • 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)
    • 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
    • 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 of 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)
    • 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
    • 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.

      Two reasons why we do not use the FlexibleProperties class for this:
      • Doesn't support flexible locale based naming: try fname_locale (5 letter), then fname_locale (2 letter lang only), then fname
      • Does not support parent properties/bundles so that if the fname_locale5 file doesn't have it then fname_locale2 is tried, then the fname bundle
      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
      locale - The locale that the given resource will correspond to
      arguments - An array of Objects to insert into the message argument place holders
      Returns:
      The value of the property in the properties file
    • getMessage

      public static String getMessage(String resource, String name, List<?> 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
      locale - The locale that the given resource will correspond to
      arguments - A list of Objects to insert into the message argument place holders
      Returns:
      The value of the property in the properties file
    • 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
    • getProperties

      public static Properties getProperties(String resource, Locale locale)
      Returns the specified resource/properties file NOTE: This is NOT fully implemented yet to fulfill all of the requirements for i18n messages. Do NOT use. To be used in an i18n context this still needs to be extended quite a bit. The behavior needed is that for each getMessage the most specific locale (with fname_en_US for instance) is searched first, then the next less specific (fname_en for instance), then without the locale if it is still not found (plain fname for example, not that these examples would have .properties appended to them). This would be accomplished by returning the following structure: 1. Get "fname" FlexibleProperties object 2. Get the "fname_en" FlexibleProperties object and if the "fname" one is not null, set it as the default/parent of the "fname_en" object 3. Get the "fname_en_US" FlexibleProperties object and if the "fname_en" one is not null, set it as the default/parent of the "fname_en_US" object; if the "fname_en" one is null, but the "fname" one is not, set the "fname" object as the default/parent of the "fname_en_US" object Then return the fname_en_US object if not null, else the fname_en, else the fname. To make this all more fun, the default locale should be the parent of the "fname" object in this example so that there is an even higher chance of finding something for each request. For efficiency all of these should be cached indendependently so the same instance can be shared, speeding up loading time/efficiency. All of this should work with the setDefaultProperties method of the FlexibleProperties class, but it should be tested and updated as necessary. It's a bit tricky, so chances are it won't work as desired...
      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 Properties class