org.ofbiz.base.util
Class UtilProperties

java.lang.Object
  extended by org.ofbiz.base.util.UtilProperties
All Implemented Interfaces:
java.io.Serializable

public class UtilProperties
extends java.lang.Object
implements java.io.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:
Serialized Form

Nested Class Summary
static class UtilProperties.ExtendedProperties
          Custom Properties class.
static class UtilProperties.UtilResourceBundle
          Custom ResourceBundle class.
 
Field Summary
protected static java.util.Set<java.util.Locale> defaultCandidateLocales
           
protected static java.util.Locale fallbackLocale
           
static java.util.Locale LOCALE_ROOT
           
static java.lang.String module
           
protected static java.util.Set<java.lang.String> propertiesNotFound
           
protected static UtilCache<java.lang.String,java.util.Properties> resourceCache
          An instance of the generic cache for storing the non-locale-specific properties.
protected static java.util.Set<java.lang.String> resourceNotFoundMessagesShown
           
protected static UtilCache<java.lang.String,java.util.Properties> urlCache
          An instance of the generic cache for storing the non-locale-specific properties.
 
Constructor Summary
UtilProperties()
           
 
Method Summary
static java.lang.String createResourceName(java.lang.String resource, java.util.Locale locale, boolean removeExtension)
          Create a localized resource name based on a resource name and a locale.
static java.util.List<java.util.Locale> getCandidateLocales(java.util.Locale locale)
          Returns a list of candidate locales based on a supplied locale.
static java.util.Set<java.util.Locale> getDefaultCandidateLocales()
          Returns the default candidate Locale list.
static java.util.Locale getFallbackLocale()
          Returns the configured fallback locale.
static java.lang.String getMessage(java.lang.String resource, java.lang.String name, java.util.List arguments, java.util.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
static java.lang.String getMessage(java.lang.String resource, java.lang.String name, java.util.Locale locale)
          Returns the value of the specified property name from the specified resource/properties file corresponding to the given locale.
static java.lang.String getMessage(java.lang.String resource, java.lang.String name, java.util.Map<java.lang.String,? extends java.lang.Object> context, java.util.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
static java.lang.String getMessage(java.lang.String resource, java.lang.String name, java.lang.Object[] arguments, java.util.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
static java.lang.String getMessageList(java.lang.String resource, java.lang.String name, java.util.Locale locale, java.lang.Object... arguments)
           
static java.lang.String getMessageMap(java.lang.String resource, java.lang.String name, java.util.Locale locale, java.lang.Object... context)
           
static java.util.Properties getProperties(java.lang.String resource)
          Returns the specified resource/properties file
static java.util.Properties getProperties(java.lang.String resource, java.util.Locale locale)
          Returns the specified resource/properties file.
static java.util.Properties getProperties(java.net.URL url)
          Returns the specified resource/properties file
static double getPropertyNumber(java.lang.String resource, java.lang.String name)
           
static double getPropertyNumber(java.net.URL url, java.lang.String name)
           
static java.lang.String getPropertyValue(java.lang.String resource, java.lang.String name)
          Returns the value of the specified property name from the specified resource/properties file
static java.lang.String getPropertyValue(java.lang.String resource, java.lang.String name, java.lang.String defaultValue)
          Returns the value of the specified property name from the specified resource/properties file.
static java.lang.String getPropertyValue(java.net.URL url, java.lang.String name)
          Returns the value of the specified property name from the specified resource/properties file
static java.lang.String getPropertyValue(java.net.URL url, java.lang.String name, java.lang.String defaultValue)
          Returns the value of the specified property name from the specified resource/properties file.
static java.util.ResourceBundle getResourceBundle(java.lang.String resource, java.util.Locale locale)
          Returns the specified resource/properties file as a ResourceBundle
static ResourceBundleMapWrapper getResourceBundleMap(java.lang.String resource, java.util.Locale locale)
          Returns the specified resource/properties file as a Map with the original ResourceBundle in the Map under the key _RESOURCE_BUNDLE_
static ResourceBundleMapWrapper getResourceBundleMap(java.lang.String resource, java.util.Locale locale, java.util.Map<java.lang.String,java.lang.Object> context)
          Returns the specified resource/properties file as a Map with the original ResourceBundle in the Map under the key _RESOURCE_BUNDLE_
static java.lang.String getSplitPropertyValue(java.net.URL url, java.lang.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.
static boolean isPropertiesResourceNotFound(java.lang.String resource, java.util.Locale locale, boolean removeExtension)
           
static java.util.List<java.util.Locale> localeToCandidateList(java.util.Locale locale)
          Converts a Locale instance to a candidate Locale list.
static boolean propertyValueEquals(java.lang.String resource, java.lang.String name, java.lang.String compareString)
          Compares the specified property to the compareString, returns true if they are the same, false otherwise
static boolean propertyValueEquals(java.net.URL url, java.lang.String name, java.lang.String compareString)
          Compares the specified property to the compareString, returns true if they are the same, false otherwise
static boolean propertyValueEqualsIgnoreCase(java.lang.String resource, java.lang.String name, java.lang.String compareString)
          Compares Ignoring Case the specified property to the compareString, returns true if they are the same, false otherwise
static boolean propertyValueEqualsIgnoreCase(java.net.URL url, java.lang.String name, java.lang.String compareString)
          Compares Ignoring Case the specified property to the compareString, returns true if they are the same, false otherwise
static java.net.URL resolvePropertiesUrl(java.lang.String resource, java.util.Locale locale)
          Resolve a properties file URL.
static void setPropertyValue(java.lang.String resource, java.lang.String name, java.lang.String value)
          Sets the specified value of the specified property name to the specified resource/properties file
static java.util.Properties xmlToProperties(java.io.InputStream in, java.util.Locale locale, java.util.Properties properties)
          Convert XML property file to Properties instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

module

public static final java.lang.String module

resourceCache

protected static UtilCache<java.lang.String,java.util.Properties> resourceCache
An instance of the generic cache for storing the non-locale-specific properties. Each Properties instance is keyed by the resource String.


urlCache

protected static UtilCache<java.lang.String,java.util.Properties> urlCache
An instance of the generic cache for storing the non-locale-specific properties. Each Properties instance is keyed by the file's URL.


LOCALE_ROOT

public static final java.util.Locale LOCALE_ROOT

fallbackLocale

protected static java.util.Locale fallbackLocale

defaultCandidateLocales

protected static java.util.Set<java.util.Locale> defaultCandidateLocales

propertiesNotFound

protected static java.util.Set<java.lang.String> propertiesNotFound

resourceNotFoundMessagesShown

protected static java.util.Set<java.lang.String> resourceNotFoundMessagesShown
Constructor Detail

UtilProperties

public UtilProperties()
Method Detail

propertyValueEquals

public static boolean propertyValueEquals(java.lang.String resource,
                                          java.lang.String name,
                                          java.lang.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(java.lang.String resource,
                                                    java.lang.String name,
                                                    java.lang.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 java.lang.String getPropertyValue(java.lang.String resource,
                                                java.lang.String name,
                                                java.lang.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(java.lang.String resource,
                                       java.lang.String name)

getPropertyValue

public static java.lang.String getPropertyValue(java.lang.String resource,
                                                java.lang.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 java.util.Properties getProperties(java.lang.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 java.util.Properties getProperties(java.net.URL url)
Returns the specified resource/properties file

Parameters:
url - The URL to the resource
Returns:
The properties file

propertyValueEquals

public static boolean propertyValueEquals(java.net.URL url,
                                          java.lang.String name,
                                          java.lang.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(java.net.URL url,
                                                    java.lang.String name,
                                                    java.lang.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 java.lang.String getPropertyValue(java.net.URL url,
                                                java.lang.String name,
                                                java.lang.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(java.net.URL url,
                                       java.lang.String name)

getPropertyValue

public static java.lang.String getPropertyValue(java.net.URL url,
                                                java.lang.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 java.lang.String getSplitPropertyValue(java.net.URL url,
                                                     java.lang.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

setPropertyValue

public static void setPropertyValue(java.lang.String resource,
                                    java.lang.String name,
                                    java.lang.String value)
Sets the specified value of the specified property name to the specified resource/properties file

Parameters:
resource - The name of the resource - must be a file
name - The name of the property in the properties file
value - The value of the property in the properties file

getMessage

public static java.lang.String getMessage(java.lang.String resource,
                                          java.lang.String name,
                                          java.util.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 java.lang.String getMessage(java.lang.String resource,
                                          java.lang.String name,
                                          java.lang.Object[] arguments,
                                          java.util.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 java.lang.String getMessage(java.lang.String resource,
                                          java.lang.String name,
                                          java.util.List arguments,
                                          java.util.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

getMessageList

public static java.lang.String getMessageList(java.lang.String resource,
                                              java.lang.String name,
                                              java.util.Locale locale,
                                              java.lang.Object... arguments)

getMessage

public static java.lang.String getMessage(java.lang.String resource,
                                          java.lang.String name,
                                          java.util.Map<java.lang.String,? extends java.lang.Object> context,
                                          java.util.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
locale - The locale that the given resource will correspond to
context - A Map of Objects to insert into the message place holders using the ${} syntax of the FlexibleStringExpander
Returns:
The value of the property in the properties file

getMessageMap

public static java.lang.String getMessageMap(java.lang.String resource,
                                             java.lang.String name,
                                             java.util.Locale locale,
                                             java.lang.Object... context)

getResourceBundle

public static java.util.ResourceBundle getResourceBundle(java.lang.String resource,
                                                         java.util.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(java.lang.String resource,
                                                            java.util.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(java.lang.String resource,
                                                            java.util.Locale locale,
                                                            java.util.Map<java.lang.String,java.lang.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 java.util.Properties getProperties(java.lang.String resource,
                                                 java.util.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 java.util.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 java.util.List<java.util.Locale> localeToCandidateList(java.util.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 java.util.Set<java.util.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 java.util.List<java.util.Locale> getCandidateLocales(java.util.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 java.lang.String createResourceName(java.lang.String resource,
                                                  java.util.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(java.lang.String resource,
                                                   java.util.Locale locale,
                                                   boolean removeExtension)

resolvePropertiesUrl

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

This method uses the following strategy:


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 java.util.Properties xmlToProperties(java.io.InputStream in,
                                                   java.util.Locale locale,
                                                   java.util.Properties properties)
                                            throws java.io.IOException,
                                                   java.util.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 Locale strings.

Parameters:
in - XML file InputStream
locale - The desired locale
properties - Optional Properties object to populate
Returns:
Properties instance or null if not found
Throws:
java.io.IOException
java.util.InvalidPropertiesFormatException