Package io.microsphere.util
Class PropertyResourceBundleControl
- java.lang.Object
-
- java.util.ResourceBundle.Control
-
- io.microsphere.util.PropertyResourceBundleControl
-
public class PropertyResourceBundleControl extends java.util.ResourceBundle.ControlAResourceBundle.Controlimplementation for loading property-based resource bundles with customizable character encoding.PropertyResourceBundleControlallows specifying the character encoding used when reading properties files, ensuring correct interpretation of non-ASCII characters. By default, it uses the platform's default encoding unless explicitly configured otherwise.Example Usage
// Create a control with UTF-8 encoding ResourceBundle.Control utf8Control = new PropertyResourceBundleControl("UTF-8"); // Load a resource bundle using the custom control ResourceBundle bundle = ResourceBundle.getBundle("my.resources.Messages", Locale.US, utf8Control);This class is thread-safe and can be reused across multiple calls to
ResourceBundle.getBundle(String, ResourceBundle.Control).- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
PropertyResourceBundle,ResourceBundle.Control
-
-
Field Summary
Fields Modifier and Type Field Description static PropertyResourceBundleControlDEFAULT_CONTROLThe singleton instance ofPropertyResourceBundleControlas defaultstatic java.lang.StringSUFFIXThe suffix of the properties file
-
Constructor Summary
Constructors Modifier Constructor Description protectedPropertyResourceBundleControl()Constructs aPropertyResourceBundleControlwith the default encoding.protectedPropertyResourceBundleControl(java.lang.String encoding)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringgetEncoding()Sets the encoding of properties file.java.util.List<java.lang.String>getFormats(java.lang.String baseName)Returns the list of supported formats for the given base name, restricted to properties files only.java.util.ResourceBundlenewBundle(java.lang.String baseName, java.util.Locale locale, java.lang.String format, java.lang.ClassLoader classLoader, boolean reload)Creates a newResourceBundleinstance for the given parameters, reading the properties file with the configured character encoding.static java.util.ResourceBundle.ControlnewControl(java.lang.String encoding)Creates a new instance ofPropertyResourceBundleControlif absent.
-
-
-
Field Detail
-
SUFFIX
public static final java.lang.String SUFFIX
The suffix of the properties file- See Also:
- Constant Field Values
-
DEFAULT_CONTROL
public static final PropertyResourceBundleControl DEFAULT_CONTROL
The singleton instance ofPropertyResourceBundleControlas default
-
-
Constructor Detail
-
PropertyResourceBundleControl
protected PropertyResourceBundleControl() throws java.nio.charset.UnsupportedCharsetExceptionConstructs aPropertyResourceBundleControlwith the default encoding.Example Usage
PropertyResourceBundleControl control = new PropertyResourceBundleControl(); String encoding = control.getEncoding(); System.out.println("Default encoding: " + encoding);- Throws:
java.nio.charset.UnsupportedCharsetException- if the default encoding is not supported- Since:
- 1.0.0
-
PropertyResourceBundleControl
protected PropertyResourceBundleControl(java.lang.String encoding) throws java.nio.charset.UnsupportedCharsetException- Parameters:
encoding- the encoding- Throws:
java.nio.charset.UnsupportedCharsetException- Ifencodingis not supported
-
-
Method Detail
-
getFormats
public final java.util.List<java.lang.String> getFormats(java.lang.String baseName)
Returns the list of supported formats for the given base name, restricted to properties files only.Example Usage
PropertyResourceBundleControl control = PropertyResourceBundleControl.DEFAULT_CONTROL; List<String> formats = control.getFormats("my.resources.Messages"); System.out.println("Supported formats: " + formats);- Overrides:
getFormatsin classjava.util.ResourceBundle.Control- Parameters:
baseName- the base name of the resource bundle- Returns:
- a list containing the properties format
- Throws:
java.lang.NullPointerException- ifbaseNameisnull- Since:
- 1.0.0
-
newBundle
public java.util.ResourceBundle newBundle(java.lang.String baseName, java.util.Locale locale, java.lang.String format, java.lang.ClassLoader classLoader, boolean reload) throws java.io.IOExceptionCreates a newResourceBundleinstance for the given parameters, reading the properties file with the configured character encoding.Example Usage
PropertyResourceBundleControl control = PropertyResourceBundleControl.DEFAULT_CONTROL; ResourceBundle bundle = control.newBundle( "my.resources.Messages", Locale.US, "java.properties", Thread.currentThread().getContextClassLoader(), false ); System.out.println(bundle.getString("greeting"));- Overrides:
newBundlein classjava.util.ResourceBundle.Control- Parameters:
baseName- the base name of the resource bundlelocale- the locale for which the resource bundle should be loadedformat- the resource bundle format to be loadedclassLoader- the class loader to use for loading the resourcereload- whether to bypass the cache and reload the resource- Returns:
- a new
ResourceBundleinstance, ornullif the resource is not found - Throws:
java.io.IOException- if an I/O error occurs while reading the resource- Since:
- 1.0.0
-
getEncoding
public java.lang.String getEncoding()
Sets the encoding of properties file.- Returns:
- the encoding
-
newControl
public static java.util.ResourceBundle.Control newControl(java.lang.String encoding) throws java.nio.charset.UnsupportedCharsetExceptionCreates a new instance ofPropertyResourceBundleControlif absent.- Parameters:
encoding- Encoding- Returns:
- Control
- Throws:
java.nio.charset.UnsupportedCharsetException- Ifencodingis not supported
-
-