Class UtilProperties
- All Implemented Interfaces:
Serializable
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:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Custom Properties class.static class
Custom ResourceBundle class. -
Method Summary
Modifier and TypeMethodDescriptionstatic Properties
createProperties
(String fileName) Returns a newProperties
instance created fromfileName
.static String
createResourceName
(String resource, Locale locale, boolean removeExtension) Create a localized resource name based on a resource name and a locale.getCandidateLocales
(Locale locale) Returns a list of candidate locales based on a supplied locale.Returns the default candidate Locale list.static Locale
Returns the configured fallback locale.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 classstatic <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 classstatic 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.static String
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 classstatic String
getMessageList
(String resource, String name, Locale locale, Object... arguments) static String
getMessageMap
(String resource, String name, Locale locale, Object... context) static Properties
getProperties
(String resource) Returns the specified resource/properties filestatic Properties
getProperties
(String resource, Locale locale) Returns the specified resource/properties file.static Properties
getProperties
(URL url) Returns the specified resource/properties filestatic BigDecimal
getPropertyAsBigDecimal
(String resource, String name, BigDecimal defaultNumber) Returns a BigDecimal-Object of the specified property name from the specified resource/properties file.static BigInteger
getPropertyAsBigInteger
(String resource, String name, BigInteger defaultNumber) Returns a BigInteger-Object of the specified property name from the specified resource/properties file.static Boolean
getPropertyAsBoolean
(String resource, String name, boolean defaultValue) Returns a Boolean-Object of the specified property name from the specified resource/properties file.static Double
getPropertyAsDouble
(String resource, String name, double defaultNumber) Returns a Double-Object of the specified property name from the specified resource/properties file.static Float
getPropertyAsFloat
(String resource, String name, float defaultNumber) Returns a Float-Object of the specified property name from the specified resource/properties file.static Integer
getPropertyAsInteger
(String resource, String name, int defaultNumber) Returns an Integer-Object of the specified property name from the specified resource/properties file.static Long
getPropertyAsLong
(String resource, String name, long defaultNumber) Returns a Long-Object of the specified property name from the specified resource/properties file.static double
getPropertyNumber
(String resource, String name) static double
getPropertyNumber
(String resource, String name, double defaultValue) static double
getPropertyNumber
(URL url, String name) static double
getPropertyNumber
(URL url, String name, double defaultValue) static String
getPropertyValue
(String resource, String name) Returns the value of the specified property name from the specified resource/properties filestatic String
getPropertyValue
(String resource, String name, String defaultValue) Returns the value of the specified property name from the specified resource/properties file.static String
getPropertyValue
(URL url, String name) Returns the value of the specified property name from the specified resource/properties filestatic String
getPropertyValue
(URL url, String name, String defaultValue) Returns the value of the specified property name from the specified resource/properties file.static ResourceBundle
getResourceBundle
(String resource, Locale locale) Returns the specified resource/properties file as a ResourceBundlestatic 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_static ResourceBundleMapWrapper
Returns the specified resource/properties file as a Map with the original ResourceBundle in the Map under the key _RESOURCE_BUNDLE_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.static boolean
isPropertiesResourceNotFound
(String resource, Locale locale, boolean removeExtension) localeToCandidateList
(Locale locale) Converts a Locale instance to a candidate Locale list.static boolean
propertyValueEquals
(String resource, String name, String compareString) Compares the specified property to the compareString, returns true if they are the same, false otherwisestatic boolean
propertyValueEquals
(URL url, String name, String compareString) Compares the specified property to the compareString, returns true if they are the same, false otherwisestatic 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 otherwisestatic 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 otherwisestatic URL
resolvePropertiesUrl
(String resource, Locale locale) Resolve a properties file URL.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 itstatic 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 itstatic Properties
xmlToProperties
(InputStream in, Locale locale, Properties properties) Convert XML property file to Properties instance.
-
Method Details
-
propertyValueEquals
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 filecompareString
- 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 filecompareString
- The String to compare the property value to- Returns:
- True if the strings are the same, false otherwise
-
getPropertyValue
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 filedefaultValue
- 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
-
getPropertyNumber
-
getPropertyAsBoolean
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 filedefaultValue
- 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
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 filedefaultNumber
- 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
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 filedefaultNumber
- 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
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 filedefaultNumber
- 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
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 filedefaultNumber
- 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 filedefaultNumber
- 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 filedefaultNumber
- 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
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 URLname
- The name of the property in the properties file- Returns:
- The value of the property in the properties file
-
createProperties
Returns a newProperties
instance created fromfileName
.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 fromfileName
, ornull
if the file was not found - Throws:
IllegalArgumentException
- iffileName
is emptyIllegalStateException
- if there was a problem reading the file
-
getProperties
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
Returns the specified resource/properties file- Parameters:
url
- The URL to the resource- Returns:
- The properties file
-
propertyValueEquals
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 resourcename
- The name of the property in the properties filecompareString
- The String to compare the property value to- Returns:
- True if the strings are the same, false otherwise
-
propertyValueEqualsIgnoreCase
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 resourcename
- The name of the property in the properties filecompareString
- The String to compare the property value to- Returns:
- True if the strings are the same, false otherwise
-
getPropertyValue
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 resourcename
- The name of the property in the properties filedefaultValue
- 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
-
getPropertyNumber
-
getPropertyValue
Returns the value of the specified property name from the specified resource/properties file- Parameters:
url
- URL object specifying the location of the resourcename
- The name of the property in the properties file- Returns:
- The value of the property in the properties file
-
getSplitPropertyValue
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 resourcename
- The name of the split property in the properties file- Returns:
- The value of the split property from the properties file
-
setPropertyValueInMemory
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 resourcename
- The name of the property in the resourcevalue
- 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 testsresource
- The name of the resourcename
- The name of the property in the resourcevalue
- The value of the property to set in memory
-
getMessage
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 URLname
- The name of the property in the properties filelocale
- The locale that the given resource will correspond to- Returns:
- The value of the property in the properties file
-
getMessage
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 URLname
- The name of the property in the properties filearguments
- An array of Objects to insert into the message argument place holderslocale
- The locale that the given resource will correspond to- Returns:
- The value of the property in the properties file
-
getMessage
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 URLname
- The name of the property in the properties filearguments
- A List of Objects to insert into the message argument place holderslocale
- The locale that the given resource will correspond to- Returns:
- The value of the property in the properties file
-
getMessageList
-
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 URLname
- The name of the property in the properties filecontext
- A Map of Objects to insert into the message place holders using the ${} syntax of the FlexibleStringExpanderlocale
- The locale that the given resource will correspond to- Returns:
- The value of the property in the properties file
-
getMessageMap
-
getResourceBundle
Returns the specified resource/properties file as a ResourceBundle- Parameters:
resource
- The name of the resource - can be a file, class, or URLlocale
- The locale that the given resource will correspond to- Returns:
- The ResourceBundle
-
getResourceBundleMap
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 URLlocale
- 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 URLlocale
- The locale that the given resource will correspond tocontext
- The screen rendering context- Returns:
- Map containing all entries in The ResourceBundle
-
getProperties
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 URLlocale
- The desired locale- Returns:
- The Properties instance, or null if no matching properties are found
-
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 ingeneral.properties
.- Returns:
- The configured fallback locale
-
localeToCandidateList
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 containingen_US
anden
.- Returns:
- A list of candidate locales.
-
getDefaultCandidateLocales
Returns the default candidate Locale list. The list is populated with the JVM's default locale, the OFBiz fallback locale, and theLOCALE_ROOT
(empty) locale - in that order.- Returns:
- A list of default candidate locales.
-
getCandidateLocales
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
Create a localized resource name based on a resource name and a locale.- Parameters:
resource
- The desired resourcelocale
- The desired localeremoveExtension
- Remove file extension from resource String- Returns:
- Localized resource name
-
isPropertiesResourceNotFound
-
resolvePropertiesUrl
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)
component://
protocol is supported in theresource
parameter.- Parameters:
resource
- The resource to resolvelocale
- The desired locale- Returns:
- A URL instance or null if not found.
- Locate the XML file specified in
-
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>
- Parameters:
in
- XML file InputStreamlocale
- The desired localeproperties
- Optional Properties object to populate- Returns:
- Properties instance or null if not found
- Throws:
IOException
InvalidPropertiesFormatException
-