Class CharSetMap
- java.lang.Object
-
- org.apache.wicket.extensions.util.encoding.CharSetMap
-
public final class CharSetMap extends Object
This class maintains a set of mappers defining mappings between locales and the corresponding charsets. The mappings are defined as properties between locale and charset names. The definitions can be listed in property files located in user's home directory, Java home directory or the current class jar. In addition, this class maintains static default mappings and constructors support application specific mappings. This source has originally been taken from the jakarta Turbine project.- Author:
- Ilkka Priha
-
-
Field Summary
Fields Modifier and Type Field Description static String
CHARSET_RESOURCE
The name for charset mapper resources.static String
DEFAULT_CHARSET
The default charset when nothing else is applicable.
-
Constructor Summary
Constructors Constructor Description CharSetMap()
Constructs a new charset map with default mappers.CharSetMap(File file)
Constructs a charset map read from a property file.CharSetMap(InputStream input)
Constructs a charset map read from a stream.CharSetMap(String path)
Constructs a charset map read from a property file path.CharSetMap(Properties props)
Constructs a charset map from properties.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description String
getCharSet(String key)
Gets the charset for a specified key.String
getCharSet(String key, String def)
Gets the charset for a specified key.String
getCharSet(Locale locale)
Gets the charset for a locale.String
getCharSet(Locale locale, String variant)
Gets the charset for a locale with a variant.protected static Map<String,String>
loadFile(File file)
Loads mappings from a file.protected static Map<String,String>
loadPath(String path)
Loads mappings from a file path.protected static Map<String,String>
loadResource(String name)
Loads mappings from a resource.protected static Map<String,String>
loadStream(InputStream input)
Loads mappings from a stream.void
setCharSet(String key, String charset)
Sets a locale-charset mapping.protected void
setCommonCharSet(String key, String charset)
Sets a common locale-charset mapping.
-
-
-
Field Detail
-
DEFAULT_CHARSET
public static final String DEFAULT_CHARSET
The default charset when nothing else is applicable.- See Also:
- Constant Field Values
-
CHARSET_RESOURCE
public static final String CHARSET_RESOURCE
The name for charset mapper resources.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
CharSetMap
public CharSetMap()
Constructs a new charset map with default mappers.
-
CharSetMap
public CharSetMap(Properties props)
Constructs a charset map from properties.- Parameters:
props
- charset mapping properties.
-
CharSetMap
public CharSetMap(InputStream input) throws IOException
Constructs a charset map read from a stream.- Parameters:
input
- an input stream.- Throws:
IOException
- for an incorrect stream.
-
CharSetMap
public CharSetMap(File file) throws IOException
Constructs a charset map read from a property file.- Parameters:
file
- a property file.- Throws:
IOException
- for an incorrect property file.
-
CharSetMap
public CharSetMap(String path) throws IOException
Constructs a charset map read from a property file path.- Parameters:
path
- a property file path.- Throws:
IOException
- for an incorrect property file.
-
-
Method Detail
-
loadStream
protected static Map<String,String> loadStream(InputStream input) throws IOException
Loads mappings from a stream.- Parameters:
input
- an input stream.- Returns:
- the mappings.
- Throws:
IOException
- for an incorrect stream.
-
loadFile
protected static Map<String,String> loadFile(File file) throws IOException
Loads mappings from a file.- Parameters:
file
- a file.- Returns:
- the mappings.
- Throws:
IOException
- for an incorrect file.
-
loadPath
protected static Map<String,String> loadPath(String path) throws IOException
Loads mappings from a file path.- Parameters:
path
- a file path.- Returns:
- the mappings.
- Throws:
IOException
- for an incorrect file.
-
loadResource
protected static Map<String,String> loadResource(String name)
Loads mappings from a resource.- Parameters:
name
- a resource name.- Returns:
- the mappings.
-
setCharSet
public final void setCharSet(String key, String charset)
Sets a locale-charset mapping.- Parameters:
key
- the key for the charset.charset
- the corresponding charset.
-
getCharSet
public final String getCharSet(Locale locale)
Gets the charset for a locale. First a locale specific charset is searched for, then a country specific one and lastly a language specific one. If none is found, the default charset is returned.- Parameters:
locale
- the locale.- Returns:
- the charset.
-
getCharSet
public final String getCharSet(Locale locale, String variant)
Gets the charset for a locale with a variant. The search is performed in the following order: "lang"_"country"_"variant"="charset", _"country"_"variant"="charset", "lang"__"variant"="charset", __"variant"="charset", "lang"_"country"="charset", _"country"="charset", "lang"="charset". If nothing of the above is found, the default charset is returned.- Parameters:
locale
- the locale.variant
- a variant field.- Returns:
- the charset.
-
getCharSet
public final String getCharSet(String key)
Gets the charset for a specified key.- Parameters:
key
- the key for the charset.- Returns:
- the found charset or the default one.
-
getCharSet
public final String getCharSet(String key, String def)
Gets the charset for a specified key.- Parameters:
key
- the key for the charset.def
- the default charset if none is found.- Returns:
- the found charset or the given default.
-
setCommonCharSet
protected final void setCommonCharSet(String key, String charset)
Sets a common locale-charset mapping.- Parameters:
key
- the key for the charset.charset
- the corresponding charset.
-
-