org.norther.tammi.acorn.mime
Class CharSetMap

java.lang.Object
  extended by org.norther.tammi.acorn.mime.CharSetMap

public class CharSetMap
extends Object

CharSetMap 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.

More than one charset can be specified for a locale separated by commas. The first one always the default one.

Author:
Ilkka Priha

Field Summary
static String ANY_KEY
          The wildcard key.
static String CHARSET_RESOURCE
          The name for charset mapper resources.
static String DEFAULT_ENCODING
          The default encoding if UTF-8 cannot be applied.
 
Constructor Summary
CharSetMap()
          Constructs a new charset map with default mappers.
CharSetMap(File file)
          Contructs a charset map read from a property file.
CharSetMap(InputStream input)
          Contructs a charset map read from a stream.
CharSetMap(Properties props)
          Contructs a charset map from properties.
CharSetMap(String path)
          Contructs a charset map read from a property file path.
 
Method Summary
 String getCharSet(Locale locale)
          Gets the charset for a locale.
 String getCharSet(Locale locale, List accepted)
          Gets the accepted charset for a locale.
 String getCharSet(Locale locale, String variant)
          Gets the charset for a locale with an additional variant.
 String getCharSet(Locale locale, String variant, List accepted)
          Gets the accepted charset for a locale with an optional variant.
 String getCharSet(String path)
          Gets the charset for a localized file path.
protected static Map loadFile(File file)
          Loads mappings from a file.
protected static Map loadPath(String path)
          Loads mappings from a file path.
protected static Map loadResource(String name)
          Loads mappings from a resource.
protected static Map loadStream(InputStream input)
          Loads mappings from a stream.
 void setCharSet(String key, String charset)
          Sets a locale-charset mapping.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CHARSET_RESOURCE

public static final String CHARSET_RESOURCE
The name for charset mapper resources.

See Also:
Constant Field Values

DEFAULT_ENCODING

public static final String DEFAULT_ENCODING
The default encoding if UTF-8 cannot be applied.

See Also:
Constant Field Values

ANY_KEY

public static final String ANY_KEY
The wildcard key.

See Also:
Constant Field Values
Constructor Detail

CharSetMap

public CharSetMap()
Constructs a new charset map with default mappers.


CharSetMap

public CharSetMap(Properties props)
Contructs a charset map from properties.

Parameters:
props - charset mapping propeties.

CharSetMap

public CharSetMap(InputStream input)
           throws IOException
Contructs 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
Contructs 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
Contructs 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 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 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 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 loadResource(String name)
Loads mappings from a resource.

Parameters:
name - a resource name.
Returns:
the mappings.

getCharSet

public String getCharSet(String path)
Gets the charset for a localized file path.

Parameters:
path - the localized path.
Returns:
the found charset.

setCharSet

public void setCharSet(String key,
                       String charset)
Sets a locale-charset mapping.

Parameters:
key - the key for the charset.
charset - the corresponding charset.

getCharSet

public 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 a variant is included in the locale, it is applied to the search as well.

Parameters:
locale - the locale.
Returns:
the charset.

getCharSet

public String getCharSet(Locale locale,
                         String variant)
Gets the charset for a locale with an additional variant. If the optional variant is not null and a variant is also included in the locale, the one in the locale is replaced by the given variant.

Parameters:
locale - the locale.
variant - the variant.
Returns:
the charset.

getCharSet

public String getCharSet(Locale locale,
                         List accepted)
Gets the accepted charset for a locale.

Parameters:
locale - the locale.
accepted - a list of accepted charsets.
Returns:
the charset.

getCharSet

public String getCharSet(Locale locale,
                         String variant,
                         List accepted)
Gets the accepted charset for a locale with an optional variant. If the optional variant is not null and a variant is also included in the locale, the one in the locale is replaced by the given variant.

The search is performed in the following order:

The found charsets are compared with the list of accepted ones and the first accepted charset is returned. If none of the above is accepted but UTF-8 is, UTF-8 is returned. Otherwise the first found charset is returned.

Parameters:
locale - the locale.
variant - the variant.
accepted - a list of accepted charsets.
Returns:
the charset.


Copyright © 2004 The Norther Organization. All rights reserved.